Skip to content

Commit 370e3a5

Browse files
committed
HttpComponentsClientHttpRequestFactory supports plain HttpClient (i.e. non-CloseableHttpClient implementations) as well
Issue: SPR-12826
1 parent 8ee0e98 commit 370e3a5

File tree

5 files changed

+45
-42
lines changed

5 files changed

+45
-42
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,8 +43,7 @@
4343
* @since 4.0
4444
* @see HttpAsyncClient
4545
*/
46-
public class HttpComponentsAsyncClientHttpRequestFactory
47-
extends HttpComponentsClientHttpRequestFactory
46+
public class HttpComponentsAsyncClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory
4847
implements AsyncClientHttpRequestFactory, InitializingBean {
4948

5049
private CloseableHttpAsyncClient httpAsyncClient;
@@ -86,7 +85,7 @@ public HttpComponentsAsyncClientHttpRequestFactory(
8685

8786
/**
8887
* Set the {@code HttpClient} used for
89-
* {@linkplain #createAsyncRequest(java.net.URI, org.springframework.http.HttpMethod) asynchronous execution}.
88+
* {@linkplain #createAsyncRequest(URI, HttpMethod) asynchronous execution}.
9089
*/
9190
public void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) {
9291
this.httpAsyncClient = httpAsyncClient;
@@ -97,9 +96,10 @@ public void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) {
9796
* {@linkplain #createAsyncRequest(URI, HttpMethod) asynchronous execution}.
9897
*/
9998
public CloseableHttpAsyncClient getHttpAsyncClient() {
100-
return httpAsyncClient;
99+
return this.httpAsyncClient;
101100
}
102101

102+
103103
@Override
104104
public void afterPropertiesSet() {
105105
startAsyncClient();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,10 +23,10 @@
2323

2424
import org.apache.http.HttpEntity;
2525
import org.apache.http.HttpEntityEnclosingRequest;
26-
import org.apache.http.client.methods.CloseableHttpResponse;
26+
import org.apache.http.HttpResponse;
27+
import org.apache.http.client.HttpClient;
2728
import org.apache.http.client.methods.HttpUriRequest;
2829
import org.apache.http.entity.ByteArrayEntity;
29-
import org.apache.http.impl.client.CloseableHttpClient;
3030
import org.apache.http.protocol.HTTP;
3131
import org.apache.http.protocol.HttpContext;
3232

@@ -50,14 +50,14 @@
5050
*/
5151
final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpRequest {
5252

53-
private final CloseableHttpClient httpClient;
53+
private final HttpClient httpClient;
5454

5555
private final HttpUriRequest httpRequest;
5656

5757
private final HttpContext httpContext;
5858

5959

60-
HttpComponentsClientHttpRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
60+
HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
6161
this.httpClient = httpClient;
6262
this.httpRequest = httpRequest;
6363
this.httpContext = httpContext;
@@ -88,7 +88,7 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] buffere
8888
HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
8989
entityEnclosingRequest.setEntity(requestEntity);
9090
}
91-
CloseableHttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
91+
HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
9292
return new HttpComponentsClientHttpResponse(httpResponse);
9393
}
9494

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.http.client;
1818

19+
import java.io.Closeable;
1920
import java.io.IOException;
2021
import java.net.URI;
2122

@@ -32,7 +33,6 @@
3233
import org.apache.http.client.methods.HttpTrace;
3334
import org.apache.http.client.methods.HttpUriRequest;
3435
import org.apache.http.client.protocol.HttpClientContext;
35-
import org.apache.http.impl.client.CloseableHttpClient;
3636
import org.apache.http.impl.client.HttpClients;
3737
import org.apache.http.protocol.HttpContext;
3838

@@ -57,7 +57,7 @@
5757
*/
5858
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean {
5959

60-
private CloseableHttpClient httpClient;
60+
private HttpClient httpClient;
6161

6262
private RequestConfig requestConfig;
6363

@@ -75,25 +75,20 @@ public HttpComponentsClientHttpRequestFactory() {
7575
/**
7676
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
7777
* with the given {@link HttpClient} instance.
78-
* <p>As of Spring Framework 4.0, the given client is expected to be of type
79-
* {@link CloseableHttpClient} (requiring HttpClient 4.3+).
8078
* @param httpClient the HttpClient instance to use for this request factory
8179
*/
8280
public HttpComponentsClientHttpRequestFactory(HttpClient httpClient) {
83-
Assert.notNull(httpClient, "'httpClient' must not be null");
84-
Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient");
85-
this.httpClient = (CloseableHttpClient) httpClient;
81+
Assert.notNull(httpClient, "HttpClient must not be null");
82+
this.httpClient = httpClient;
8683
}
8784

8885

8986
/**
9087
* Set the {@code HttpClient} used for
91-
* <p>As of Spring Framework 4.0, the given client is expected to be of type
92-
* {@link CloseableHttpClient} (requiring HttpClient 4.3+).
88+
* {@linkplain #createRequest(URI, HttpMethod) synchronous execution}.
9389
*/
9490
public void setHttpClient(HttpClient httpClient) {
95-
Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient");
96-
this.httpClient = (CloseableHttpClient) httpClient;
91+
this.httpClient = httpClient;
9792
}
9893

9994
/**
@@ -200,8 +195,9 @@ public void setBufferRequestBody(boolean bufferRequestBody) {
200195

201196

202197
@Override
198+
@SuppressWarnings("deprecation")
203199
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
204-
CloseableHttpClient client = (CloseableHttpClient) getHttpClient();
200+
HttpClient client = getHttpClient();
205201
Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");
206202
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
207203
postProcessHttpRequest(httpRequest);
@@ -232,15 +228,16 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
232228
}
233229

234230
/**
235-
* Create a default {@link RequestConfig} to use with the given client. Can
236-
* return {@code null} to indicate that no custom request config should be set
237-
* and the defaults of the {@link HttpClient} should be used.
238-
* <p>The default implementation tries to merge the defaults of the client with the
239-
* local customizations of this instance, if any.
231+
* Create a default {@link RequestConfig} to use with the given client.
232+
* Can return {@code null} to indicate that no custom request config should
233+
* be set and the defaults of the {@link HttpClient} should be used.
234+
* <p>The default implementation tries to merge the defaults of the client
235+
* with the local customizations of this instance, if any.
240236
* @param client the client
241237
* @return the RequestConfig to use
238+
* @since 4.2
242239
*/
243-
protected RequestConfig createRequestConfig(CloseableHttpClient client) {
240+
protected RequestConfig createRequestConfig(HttpClient client) {
244241
if (client instanceof Configurable) {
245242
RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
246243
return mergeRequestConfig(clientRequestConfig);
@@ -325,7 +322,9 @@ protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
325322
*/
326323
@Override
327324
public void destroy() throws Exception {
328-
this.httpClient.close();
325+
if (this.httpClient instanceof Closeable) {
326+
((Closeable) this.httpClient).close();
327+
}
329328
}
330329

331330

@@ -349,4 +348,5 @@ public String getMethod() {
349348
return "DELETE";
350349
}
351350
}
351+
352352
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,13 @@
1616

1717
package org.springframework.http.client;
1818

19+
import java.io.Closeable;
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122

2223
import org.apache.http.Header;
2324
import org.apache.http.HttpEntity;
24-
import org.apache.http.client.methods.CloseableHttpResponse;
25+
import org.apache.http.HttpResponse;
2526
import org.apache.http.util.EntityUtils;
2627

2728
import org.springframework.http.HttpHeaders;
@@ -41,12 +42,12 @@
4142
*/
4243
final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse {
4344

44-
private final CloseableHttpResponse httpResponse;
45+
private final HttpResponse httpResponse;
4546

4647
private HttpHeaders headers;
4748

4849

49-
HttpComponentsClientHttpResponse(CloseableHttpResponse httpResponse) {
50+
HttpComponentsClientHttpResponse(HttpResponse httpResponse) {
5051
this.httpResponse = httpResponse;
5152
}
5253

@@ -87,7 +88,9 @@ public void close() {
8788
EntityUtils.consume(this.httpResponse.getEntity());
8889
}
8990
finally {
90-
this.httpResponse.close();
91+
if (this.httpResponse instanceof Closeable) {
92+
((Closeable) this.httpResponse).close();
93+
}
9194
}
9295
}
9396
catch (IOException ex) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,9 +24,9 @@
2424
import org.apache.http.Header;
2525
import org.apache.http.HttpEntity;
2626
import org.apache.http.HttpEntityEnclosingRequest;
27-
import org.apache.http.client.methods.CloseableHttpResponse;
27+
import org.apache.http.HttpResponse;
28+
import org.apache.http.client.HttpClient;
2829
import org.apache.http.client.methods.HttpUriRequest;
29-
import org.apache.http.impl.client.CloseableHttpClient;
3030
import org.apache.http.message.BasicHeader;
3131
import org.apache.http.protocol.HttpContext;
3232

@@ -47,7 +47,7 @@
4747
*/
4848
final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpRequest implements StreamingHttpOutputMessage {
4949

50-
private final CloseableHttpClient httpClient;
50+
private final HttpClient httpClient;
5151

5252
private final HttpUriRequest httpRequest;
5353

@@ -56,7 +56,7 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
5656
private Body body;
5757

5858

59-
HttpComponentsStreamingClientHttpRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
59+
HttpComponentsStreamingClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
6060
this.httpClient = httpClient;
6161
this.httpRequest = httpRequest;
6262
this.httpContext = httpContext;
@@ -94,7 +94,7 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOExcep
9494
entityEnclosingRequest.setEntity(requestEntity);
9595
}
9696

97-
CloseableHttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
97+
HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
9898
return new HttpComponentsClientHttpResponse(httpResponse);
9999
}
100100

0 commit comments

Comments
 (0)