From bfa6c2d5a27e4f8095f1047853ff3db5d07656af Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 31 Oct 2013 11:10:35 -0400 Subject: [PATCH] Tweak method signatures in HttpComponentsClientHttpRF This change replaces CloseableHttpClient with HttpClient in the methods signatures of HttpComponentsClientHttpRequestFactory. This allows 3rd party libraries such as Spring OAth, which configure an instance of HttpComponentsClientHttpRequestFactory on the RestTemplate to remain compatible with both Spring Framework 3/4 Issue: SPR-11053 --- ...ttpComponentsClientHttpRequestFactory.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java index 52471d2bdb..cf3f61946c 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java @@ -74,27 +74,37 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest /** * Create a new instance of the {@code HttpComponentsClientHttpRequestFactory} * with the given {@link HttpClient} instance. + *

+ * As of Spring Framework 4 the given client is expected to be of type + * CloseableHttpClient (requiring HttpClient 4.3+). + * * @param httpClient the HttpClient instance to use for this request factory */ - public HttpComponentsClientHttpRequestFactory(CloseableHttpClient httpClient) { + public HttpComponentsClientHttpRequestFactory(HttpClient httpClient) { Assert.notNull(httpClient, "'httpClient' must not be null"); - this.httpClient = httpClient; + Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient"); + this.httpClient = (CloseableHttpClient) httpClient; } /** * Set the {@code HttpClient} used for + *

+ * As of Spring Framework 4 the given client is expected to be of type + * CloseableHttpClient (requiring HttpClient 4.3+). + * * {@linkplain #createRequest(URI, HttpMethod) synchronous execution}. */ - public void setHttpClient(CloseableHttpClient httpClient) { - this.httpClient = httpClient; + public void setHttpClient(HttpClient httpClient) { + Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient"); + this.httpClient = (CloseableHttpClient) httpClient; } /** * Return the {@code HttpClient} used for * {@linkplain #createRequest(URI, HttpMethod) synchronous execution}. */ - public CloseableHttpClient getHttpClient() { + public HttpClient getHttpClient() { return this.httpClient; } @@ -130,7 +140,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest @Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { - CloseableHttpClient client = getHttpClient(); + CloseableHttpClient client = (CloseableHttpClient) getHttpClient(); Assert.state(client != null, "Synchronous execution requires an HttpClient to be set"); HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri); postProcessHttpRequest(httpRequest);