Commit 4ec059ea authored by Dave Syer's avatar Dave Syer

Ensure authentication is added to customized request factory

Fixes gh-692
parent 7401f7c6
...@@ -29,12 +29,10 @@ import org.apache.http.protocol.HttpContext; ...@@ -29,12 +29,10 @@ import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest; import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory; import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -63,10 +61,10 @@ public class TestRestTemplate extends RestTemplate { ...@@ -63,10 +61,10 @@ public class TestRestTemplate extends RestTemplate {
* @param password the password (or {@code null}) * @param password the password (or {@code null})
*/ */
public TestRestTemplate(String username, String password) { public TestRestTemplate(String username, String password) {
super(getRequestFactory(username, password));
if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) { if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) {
new HttpComponentsCustomizer().customize(this); new HttpComponentsCustomizer().customize(this);
} }
addAuthentication(username, password);
setErrorHandler(new DefaultResponseErrorHandler() { setErrorHandler(new DefaultResponseErrorHandler() {
@Override @Override
public void handleError(ClientHttpResponse response) throws IOException { public void handleError(ClientHttpResponse response) throws IOException {
...@@ -75,16 +73,15 @@ public class TestRestTemplate extends RestTemplate { ...@@ -75,16 +73,15 @@ public class TestRestTemplate extends RestTemplate {
} }
private static ClientHttpRequestFactory getRequestFactory(String username, private void addAuthentication(String username, String password) {
String password) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
if (username == null) { if (username == null) {
return factory; return;
} }
List<ClientHttpRequestInterceptor> interceptors = Collections List<ClientHttpRequestInterceptor> interceptors = Collections
.<ClientHttpRequestInterceptor> singletonList(new BasicAuthorizationInterceptor( .<ClientHttpRequestInterceptor> singletonList(new BasicAuthorizationInterceptor(
username, password)); username, password));
return new InterceptingClientHttpRequestFactory(factory, interceptors); setRequestFactory(new InterceptingClientHttpRequestFactory(getRequestFactory(),
interceptors));
} }
private static class BasicAuthorizationInterceptor implements private static class BasicAuthorizationInterceptor implements
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment