package at.create.android.ffc.http;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author Philipp Ullmann
 * Authenticate with username and password.
 */
public final class FormBasedAuthentication extends HttpBase {
    private static final String PATH = "/authentication";
    private final String username;
    private final String password;
    
    /**
     * Stores the given parameters into instance variables.
     * @param username Username
     * @param password Password
     * @param baseUri Base uri to Fat Free CRM web application
     */
    public FormBasedAuthentication(final String username, final String password, final String baseUri) {
        super(baseUri);
        this.username = username;
        this.password = password;
    }
    
    @Override
    protected String getUrl() {
        return baseUri + PATH;
    }
    
    /**
     * Authentication via username and password.
     * @return True if the authentication succeeded, otherwise false is returned.
     */
    public boolean authenticate() {
        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
        formData.add("authentication[username]", username);
        formData.add("authentication[password]", password);
        formData.add("authentication[remember_me]", "1");
        
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
        ResponseEntity<String> response                         = restTemplate.exchange(getUrl(),
                                                                                        HttpMethod.POST,
                                                                                        requestEntity,
                                                                                        String.class);
        return !response.getHeaders().getLocation().getPath().equals("/login");
    }
}

@@ -41,135 +41,89 @@
 ort 
-org.springframework.http.HttpEntity;%0Aimport org.springframework.http.HttpMethod;%0Aimport org.springframework.http.ResponseEntity
+java.net.URI;%0A%0Aimport org.springframework.http.converter.FormHttpMessageConverter
 ;%0Aim
@@ -1207,22 +1207,22 @@
 String, 
-Object
+String
 %3E formDa
@@ -1258,22 +1258,22 @@
 String, 
-Object
+String
 %3E();%0A   
@@ -1468,599 +1468,177 @@
     
-HttpEntity%3CMultiValueMap%3CString, Object%3E%3E requestEntity = new HttpEntity%3CMultiValueMap%3CString, Object%3E%3E(formData, requestHeaders);%0A        ResponseEntity%3CString%3E response                         = restTemplate.exchange(getUrl(),%0A                                                                                        HttpMethod.POST,%0A                                                                                        requestEntity,%0A                                                                                        String.class);%0A        return !response.getHeaders().getLocation()
+restTemplate.getMessageConverters().add(new FormHttpMessageConverter());%0A        URI uri = restTemplate.postForLocation(getUrl(), formData);%0A        %0A        return !uri
 .get

Use "FormHttpMessageConverter" for form based requests