Add support for Apache HttpClient if available
This commit is contained in:
parent
7a285cf65d
commit
4c14f6f685
|
|
@ -48,6 +48,11 @@
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.tomcat.embed</groupId>
|
<groupId>org.apache.tomcat.embed</groupId>
|
||||||
<artifactId>tomcat-embed-core</artifactId>
|
<artifactId>tomcat-embed-core</artifactId>
|
||||||
|
|
@ -119,11 +124,6 @@
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Test -->
|
<!-- Test -->
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpclient</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpasyncclient</artifactId>
|
<artifactId>httpasyncclient</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -17,22 +17,33 @@
|
||||||
package org.springframework.boot.test;
|
package org.springframework.boot.test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.HttpRequest;
|
||||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||||
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.InterceptingClientHttpRequestFactory;
|
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
|
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenient static factory for {@link RestTemplate} instances that are suitable for
|
* Convenient static factory for {@link RestTemplate} instances that are suitable for
|
||||||
* integration tests. They are fault tolerant, ignore cookies, and optionally can carry
|
* integration tests. They are fault tolerant, and optionally can carry Basic
|
||||||
* Basic authentication headers.
|
* 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
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
|
|
@ -80,6 +91,9 @@ public class RestTemplates {
|
||||||
RestTemplate restTemplate = new RestTemplate(
|
RestTemplate restTemplate = new RestTemplate(
|
||||||
new InterceptingClientHttpRequestFactory(
|
new InterceptingClientHttpRequestFactory(
|
||||||
new SimpleClientHttpRequestFactory(), interceptors));
|
new SimpleClientHttpRequestFactory(), interceptors));
|
||||||
|
if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) {
|
||||||
|
new HttpComponentsCustomizer().customize(restTemplate);
|
||||||
|
}
|
||||||
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
|
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void handleError(ClientHttpResponse response) throws IOException {
|
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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue