From 4c14f6f68550c3c7b0b3760ab8599f58559c6752 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 11 Mar 2014 14:23:33 +0000 Subject: [PATCH] Add support for Apache HttpClient if available --- spring-boot/pom.xml | 10 +++--- .../boot/test/RestTemplates.java | 36 +++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 2cd397b1c2..14c69d5784 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -48,6 +48,11 @@ log4j true + + org.apache.httpcomponents + httpclient + true + org.apache.tomcat.embed tomcat-embed-core @@ -119,11 +124,6 @@ true - - org.apache.httpcomponents - httpclient - test - org.apache.httpcomponents httpasyncclient diff --git a/spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java b/spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java index 6a4db40d28..02bc9d483f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java @@ -17,22 +17,33 @@ package org.springframework.boot.test; import java.io.IOException; +import java.net.URI; import java.util.ArrayList; import java.util.List; +import org.apache.http.client.config.CookieSpecs; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.config.RequestConfig.Builder; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.protocol.HttpContext; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.InterceptingClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.util.ClassUtils; import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.RestTemplate; /** * Convenient static factory for {@link RestTemplate} instances that are suitable for - * integration tests. They are fault tolerant, ignore cookies, and optionally can carry - * Basic authentication headers. + * integration tests. They are fault tolerant, and optionally can carry Basic + * authentication headers. If Apache Http Client 4.3.2 or better is available + * (recommended) it will be used as the client, and configured to ignore cookies and + * redirects. * * @author Dave Syer */ @@ -80,6 +91,9 @@ public class RestTemplates { RestTemplate restTemplate = new RestTemplate( new InterceptingClientHttpRequestFactory( new SimpleClientHttpRequestFactory(), interceptors)); + if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) { + new HttpComponentsCustomizer().customize(restTemplate); + } restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { @Override public void handleError(ClientHttpResponse response) throws IOException { @@ -89,4 +103,22 @@ public class RestTemplates { } + private static class HttpComponentsCustomizer { + + public void customize(RestTemplate restTemplate) { + restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() { + @Override + protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { + HttpClientContext context = HttpClientContext.create(); + Builder builder = RequestConfig.custom() + .setCookieSpec(CookieSpecs.IGNORE_COOKIES) + .setAuthenticationEnabled(false).setRedirectsEnabled(false); + context.setRequestConfig(builder.build()); + return context; + } + }); + } + + } + }