1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .http .client ;
18
18
19
+ import java .io .Closeable ;
19
20
import java .io .IOException ;
20
21
import java .net .URI ;
21
22
32
33
import org .apache .http .client .methods .HttpTrace ;
33
34
import org .apache .http .client .methods .HttpUriRequest ;
34
35
import org .apache .http .client .protocol .HttpClientContext ;
35
- import org .apache .http .impl .client .CloseableHttpClient ;
36
36
import org .apache .http .impl .client .HttpClients ;
37
37
import org .apache .http .protocol .HttpContext ;
38
38
57
57
*/
58
58
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory , DisposableBean {
59
59
60
- private CloseableHttpClient httpClient ;
60
+ private HttpClient httpClient ;
61
61
62
62
private RequestConfig requestConfig ;
63
63
@@ -75,25 +75,20 @@ public HttpComponentsClientHttpRequestFactory() {
75
75
/**
76
76
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
77
77
* 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+).
80
78
* @param httpClient the HttpClient instance to use for this request factory
81
79
*/
82
80
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 ;
86
83
}
87
84
88
85
89
86
/**
90
87
* 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}.
93
89
*/
94
90
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 ;
97
92
}
98
93
99
94
/**
@@ -200,8 +195,9 @@ public void setBufferRequestBody(boolean bufferRequestBody) {
200
195
201
196
202
197
@ Override
198
+ @ SuppressWarnings ("deprecation" )
203
199
public ClientHttpRequest createRequest (URI uri , HttpMethod httpMethod ) throws IOException {
204
- CloseableHttpClient client = ( CloseableHttpClient ) getHttpClient ();
200
+ HttpClient client = getHttpClient ();
205
201
Assert .state (client != null , "Synchronous execution requires an HttpClient to be set" );
206
202
HttpUriRequest httpRequest = createHttpUriRequest (httpMethod , uri );
207
203
postProcessHttpRequest (httpRequest );
@@ -232,15 +228,16 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
232
228
}
233
229
234
230
/**
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.
240
236
* @param client the client
241
237
* @return the RequestConfig to use
238
+ * @since 4.2
242
239
*/
243
- protected RequestConfig createRequestConfig (CloseableHttpClient client ) {
240
+ protected RequestConfig createRequestConfig (HttpClient client ) {
244
241
if (client instanceof Configurable ) {
245
242
RequestConfig clientRequestConfig = ((Configurable ) client ).getConfig ();
246
243
return mergeRequestConfig (clientRequestConfig );
@@ -325,7 +322,9 @@ protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
325
322
*/
326
323
@ Override
327
324
public void destroy () throws Exception {
328
- this .httpClient .close ();
325
+ if (this .httpClient instanceof Closeable ) {
326
+ ((Closeable ) this .httpClient ).close ();
327
+ }
329
328
}
330
329
331
330
@@ -349,4 +348,5 @@ public String getMethod() {
349
348
return "DELETE" ;
350
349
}
351
350
}
351
+
352
352
}
0 commit comments