19
19
import java .io .Closeable ;
20
20
import java .io .IOException ;
21
21
import java .net .URI ;
22
+ import java .util .concurrent .TimeUnit ;
22
23
import java .util .function .BiFunction ;
23
24
24
- import org .apache .http .client .HttpClient ;
25
- import org .apache .http .client .config .RequestConfig ;
26
- import org .apache .http .client .methods .Configurable ;
27
- import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
28
- import org .apache .http .client .methods .HttpGet ;
29
- import org .apache .http .client .methods .HttpHead ;
30
- import org .apache .http .client .methods .HttpOptions ;
31
- import org .apache .http .client .methods .HttpPatch ;
32
- import org .apache .http .client .methods .HttpPost ;
33
- import org .apache .http .client .methods .HttpPut ;
34
- import org .apache .http .client .methods .HttpTrace ;
35
- import org .apache .http .client .methods .HttpUriRequest ;
36
- import org .apache .http .client .protocol .HttpClientContext ;
37
- import org .apache .http .impl .client .HttpClients ;
38
- import org .apache .http .protocol .HttpContext ;
25
+ import org .apache .commons .logging .Log ;
26
+ import org .apache .commons .logging .LogFactory ;
27
+ import org .apache .hc .client5 .http .classic .HttpClient ;
28
+ import org .apache .hc .client5 .http .classic .methods .HttpDelete ;
29
+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
30
+ import org .apache .hc .client5 .http .classic .methods .HttpHead ;
31
+ import org .apache .hc .client5 .http .classic .methods .HttpOptions ;
32
+ import org .apache .hc .client5 .http .classic .methods .HttpPatch ;
33
+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
34
+ import org .apache .hc .client5 .http .classic .methods .HttpPut ;
35
+ import org .apache .hc .client5 .http .classic .methods .HttpTrace ;
36
+ import org .apache .hc .client5 .http .config .Configurable ;
37
+ import org .apache .hc .client5 .http .config .RequestConfig ;
38
+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
39
+ import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
40
+ import org .apache .hc .client5 .http .protocol .HttpClientContext ;
41
+ import org .apache .hc .core5 .http .ClassicHttpRequest ;
42
+ import org .apache .hc .core5 .http .io .SocketConfig ;
43
+ import org .apache .hc .core5 .http .protocol .HttpContext ;
44
+ import org .apache .hc .core5 .util .Timeout ;
39
45
40
46
import org .springframework .beans .factory .DisposableBean ;
41
47
import org .springframework .http .HttpMethod ;
60
66
*/
61
67
public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory , DisposableBean {
62
68
63
- private HttpClient httpClient ;
69
+ private static final Log logger = LogFactory . getLog ( HttpComponentsClientHttpRequestFactory . class ) ;
64
70
65
- @ Nullable
66
- private RequestConfig requestConfig ;
71
+
72
+ private HttpClient httpClient ;
67
73
68
74
private boolean bufferRequestBody = true ;
69
75
70
76
@ Nullable
71
77
private BiFunction <HttpMethod , URI , HttpContext > httpContextFactory ;
72
78
79
+ private int connectTimeout = -1 ;
80
+
81
+ private int connectionRequestTimeout = -1 ;
73
82
74
83
/**
75
84
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
@@ -113,15 +122,15 @@ public HttpClient getHttpClient() {
113
122
* {@link RequestConfig} instance on a custom {@link HttpClient}.
114
123
* <p>This options does not affect connection timeouts for SSL
115
124
* handshakes or CONNECT requests; for that, it is required to
116
- * use the {@link org.apache.http.config. SocketConfig} on the
125
+ * use the {@link SocketConfig} on the
117
126
* {@link HttpClient} itself.
118
- * @param timeout the timeout value in milliseconds
127
+ * @param connectTimeout the timeout value in milliseconds
119
128
* @see RequestConfig#getConnectTimeout()
120
- * @see org.apache.http.config. SocketConfig#getSoTimeout
129
+ * @see SocketConfig#getSoTimeout
121
130
*/
122
- public void setConnectTimeout (int timeout ) {
123
- Assert .isTrue (timeout >= 0 , "Timeout must be a non-negative value" );
124
- this .requestConfig = requestConfigBuilder (). setConnectTimeout ( timeout ). build () ;
131
+ public void setConnectTimeout (int connectTimeout ) {
132
+ Assert .isTrue (connectTimeout >= 0 , "Timeout must be a non-negative value" );
133
+ this .connectTimeout = connectTimeout ;
125
134
}
126
135
127
136
/**
@@ -134,21 +143,24 @@ public void setConnectTimeout(int timeout) {
134
143
* @see RequestConfig#getConnectionRequestTimeout()
135
144
*/
136
145
public void setConnectionRequestTimeout (int connectionRequestTimeout ) {
137
- this . requestConfig = requestConfigBuilder ()
138
- . setConnectionRequestTimeout ( connectionRequestTimeout ). build () ;
146
+ Assert . isTrue ( connectionRequestTimeout >= 0 , "Timeout must be a non-negative value" );
147
+ this . connectionRequestTimeout = connectionRequestTimeout ;
139
148
}
140
149
141
150
/**
142
- * Set the socket read timeout for the underlying {@link RequestConfig}.
143
- * A timeout value of 0 specifies an infinite timeout.
144
- * <p>Additional properties can be configured by specifying a
145
- * {@link RequestConfig} instance on a custom {@link HttpClient}.
146
- * @param timeout the timeout value in milliseconds
147
- * @see RequestConfig#getSocketTimeout()
151
+ * As of version 6.0, setting this property has no effect.
152
+ *
153
+ * <p/>To change the socket read timeout, use {@link SocketConfig.Builder#setSoTimeout(Timeout)},
154
+ * supply the resulting {@link SocketConfig} to
155
+ * {@link org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder#setDefaultSocketConfig(SocketConfig)},
156
+ * use the resulting connection manager for
157
+ * {@link org.apache.hc.client5.http.impl.classic.HttpClientBuilder#setConnectionManager(HttpClientConnectionManager)},
158
+ * and supply the built {@link HttpClient} to {@link #HttpComponentsClientHttpRequestFactory(HttpClient)}.
159
+ * @deprecated as of 6.0, in favor of {@link SocketConfig.Builder#setSoTimeout(Timeout)}, see above.
148
160
*/
161
+ @ Deprecated (since = "6.0" , forRemoval = true )
149
162
public void setReadTimeout (int timeout ) {
150
- Assert .isTrue (timeout >= 0 , "Timeout must be a non-negative value" );
151
- this .requestConfig = requestConfigBuilder ().setSocketTimeout (timeout ).build ();
163
+ logger .warn ("HttpComponentsClientHttpRequestFactory.setReadTimeout has no effect" );
152
164
}
153
165
154
166
/**
@@ -179,7 +191,7 @@ public void setHttpContextFactory(BiFunction<HttpMethod, URI, HttpContext> httpC
179
191
public ClientHttpRequest createRequest (URI uri , HttpMethod httpMethod ) throws IOException {
180
192
HttpClient client = getHttpClient ();
181
193
182
- HttpUriRequest httpRequest = createHttpUriRequest (httpMethod , uri );
194
+ ClassicHttpRequest httpRequest = createHttpUriRequest (httpMethod , uri );
183
195
postProcessHttpRequest (httpRequest );
184
196
HttpContext context = createHttpContext (httpMethod , uri );
185
197
if (context == null ) {
@@ -210,14 +222,6 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
210
222
}
211
223
212
224
213
- /**
214
- * Return a builder for modifying the factory-level {@link RequestConfig}.
215
- * @since 4.2
216
- */
217
- private RequestConfig .Builder requestConfigBuilder () {
218
- return (this .requestConfig != null ? RequestConfig .copy (this .requestConfig ) : RequestConfig .custom ());
219
- }
220
-
221
225
/**
222
226
* Create a default {@link RequestConfig} to use with the given client.
223
227
* Can return {@code null} to indicate that no custom request config should
@@ -231,37 +235,31 @@ private RequestConfig.Builder requestConfigBuilder() {
231
235
*/
232
236
@ Nullable
233
237
protected RequestConfig createRequestConfig (Object client ) {
234
- if (client instanceof Configurable ) {
235
- RequestConfig clientRequestConfig = (( Configurable ) client ) .getConfig ();
238
+ if (client instanceof Configurable configurableClient ) {
239
+ RequestConfig clientRequestConfig = configurableClient .getConfig ();
236
240
return mergeRequestConfig (clientRequestConfig );
237
241
}
238
- return this . requestConfig ;
242
+ return mergeRequestConfig ( RequestConfig . DEFAULT ) ;
239
243
}
240
244
241
245
/**
242
246
* Merge the given {@link HttpClient}-level {@link RequestConfig} with
243
- * the factory-level {@link RequestConfig} , if necessary.
247
+ * the factory-level configuration , if necessary.
244
248
* @param clientConfig the config held by the current
245
249
* @return the merged request config
246
250
* @since 4.2
247
251
*/
248
252
protected RequestConfig mergeRequestConfig (RequestConfig clientConfig ) {
249
- if (this .requestConfig == null ) { // nothing to merge
253
+ if (this .connectTimeout == - 1 && this . connectionRequestTimeout == - 1 ) { // nothing to merge
250
254
return clientConfig ;
251
255
}
252
256
253
257
RequestConfig .Builder builder = RequestConfig .copy (clientConfig );
254
- int connectTimeout = this .requestConfig .getConnectTimeout ();
255
- if (connectTimeout >= 0 ) {
256
- builder .setConnectTimeout (connectTimeout );
257
- }
258
- int connectionRequestTimeout = this .requestConfig .getConnectionRequestTimeout ();
259
- if (connectionRequestTimeout >= 0 ) {
260
- builder .setConnectionRequestTimeout (connectionRequestTimeout );
258
+ if (this .connectTimeout >= 0 ) {
259
+ builder .setConnectTimeout (this .connectTimeout , TimeUnit .MILLISECONDS );
261
260
}
262
- int socketTimeout = this .requestConfig .getSocketTimeout ();
263
- if (socketTimeout >= 0 ) {
264
- builder .setSocketTimeout (socketTimeout );
261
+ if (this .connectionRequestTimeout >= 0 ) {
262
+ builder .setConnectionRequestTimeout (this .connectionRequestTimeout , TimeUnit .MILLISECONDS );
265
263
}
266
264
return builder .build ();
267
265
}
@@ -272,7 +270,7 @@ protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
272
270
* @param uri the URI
273
271
* @return the Commons HttpMethodBase object
274
272
*/
275
- protected HttpUriRequest createHttpUriRequest (HttpMethod httpMethod , URI uri ) {
273
+ protected ClassicHttpRequest createHttpUriRequest (HttpMethod httpMethod , URI uri ) {
276
274
if (HttpMethod .GET .equals (httpMethod )) {
277
275
return new HttpGet (uri );
278
276
}
@@ -301,12 +299,12 @@ else if (HttpMethod.TRACE.equals(httpMethod)) {
301
299
}
302
300
303
301
/**
304
- * Template method that allows for manipulating the {@link HttpUriRequest } before it is
302
+ * Template method that allows for manipulating the {@link ClassicHttpRequest } before it is
305
303
* returned as part of a {@link HttpComponentsClientHttpRequest}.
306
304
* <p>The default implementation is empty.
307
305
* @param request the request to process
308
306
*/
309
- protected void postProcessHttpRequest (HttpUriRequest request ) {
307
+ protected void postProcessHttpRequest (ClassicHttpRequest request ) {
310
308
}
311
309
312
310
/**
@@ -324,7 +322,7 @@ protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
324
322
325
323
/**
326
324
* Shutdown hook that closes the underlying
327
- * {@link org.apache.http.conn. HttpClientConnectionManager ClientConnectionManager}'s
325
+ * {@link HttpClientConnectionManager ClientConnectionManager}'s
328
326
* connection pool, if any.
329
327
*/
330
328
@ Override
@@ -340,25 +338,4 @@ public void close() throws IOException {
340
338
}
341
339
}
342
340
343
- /**
344
- * An alternative to {@link org.apache.http.client.methods.HttpDelete} that
345
- * extends {@link org.apache.http.client.methods.HttpEntityEnclosingRequestBase}
346
- * rather than {@link org.apache.http.client.methods.HttpRequestBase} and
347
- * hence allows HTTP delete with a request body. For use with the RestTemplate
348
- * exchange methods which allow the combination of HTTP DELETE with an entity.
349
- * @since 4.1.2
350
- */
351
- private static class HttpDelete extends HttpEntityEnclosingRequestBase {
352
-
353
- public HttpDelete (URI uri ) {
354
- super ();
355
- setURI (uri );
356
- }
357
-
358
- @ Override
359
- public String getMethod () {
360
- return "DELETE" ;
361
- }
362
- }
363
-
364
341
}
0 commit comments