Skip to content

Commit bfa6c2d

Browse files
committed
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
1 parent 9f3b8a2 commit bfa6c2d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,37 @@ public HttpComponentsClientHttpRequestFactory() {
7474
/**
7575
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
7676
* with the given {@link HttpClient} instance.
77+
* <p>
78+
* As of Spring Framework 4 the given client is expected to be of type
79+
* CloseableHttpClient (requiring HttpClient 4.3+).
80+
*
7781
* @param httpClient the HttpClient instance to use for this request factory
7882
*/
79-
public HttpComponentsClientHttpRequestFactory(CloseableHttpClient httpClient) {
83+
public HttpComponentsClientHttpRequestFactory(HttpClient httpClient) {
8084
Assert.notNull(httpClient, "'httpClient' must not be null");
81-
this.httpClient = httpClient;
85+
Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient");
86+
this.httpClient = (CloseableHttpClient) httpClient;
8287
}
8388

8489

8590
/**
8691
* Set the {@code HttpClient} used for
92+
* <p>
93+
* As of Spring Framework 4 the given client is expected to be of type
94+
* CloseableHttpClient (requiring HttpClient 4.3+).
95+
*
8796
* {@linkplain #createRequest(URI, HttpMethod) synchronous execution}.
8897
*/
89-
public void setHttpClient(CloseableHttpClient httpClient) {
90-
this.httpClient = httpClient;
98+
public void setHttpClient(HttpClient httpClient) {
99+
Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient");
100+
this.httpClient = (CloseableHttpClient) httpClient;
91101
}
92102

93103
/**
94104
* Return the {@code HttpClient} used for
95105
* {@linkplain #createRequest(URI, HttpMethod) synchronous execution}.
96106
*/
97-
public CloseableHttpClient getHttpClient() {
107+
public HttpClient getHttpClient() {
98108
return this.httpClient;
99109
}
100110

@@ -130,7 +140,7 @@ public void setBufferRequestBody(boolean bufferRequestBody) {
130140

131141
@Override
132142
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
133-
CloseableHttpClient client = getHttpClient();
143+
CloseableHttpClient client = (CloseableHttpClient) getHttpClient();
134144
Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");
135145
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
136146
postProcessHttpRequest(httpRequest);

0 commit comments

Comments
 (0)