21
21
import java .io .IOException ;
22
22
import java .net .ProxySelector ;
23
23
import java .net .URI ;
24
- import java .sql .Time ;
25
24
import java .util .concurrent .TimeUnit ;
26
25
import org .apache .hc .client5 .http .classic .HttpClient ;
27
26
import org .apache .hc .client5 .http .classic .methods .HttpDelete ;
31
30
import org .apache .hc .client5 .http .classic .methods .HttpPatch ;
32
31
import org .apache .hc .client5 .http .classic .methods .HttpPost ;
33
32
import org .apache .hc .client5 .http .classic .methods .HttpPut ;
34
- import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
35
33
import org .apache .hc .client5 .http .classic .methods .HttpTrace ;
34
+ import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
36
35
import org .apache .hc .client5 .http .config .ConnectionConfig ;
37
- import org .apache .hc .client5 .http .impl .classic .HttpClients ;
38
- import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
39
- import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactory ;
40
36
import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
41
37
import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
38
+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
42
39
import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
40
+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
43
41
import org .apache .hc .client5 .http .impl .routing .SystemDefaultRoutePlanner ;
44
- import org .apache .hc .core5 .http .io .SocketConfig ;
42
+ import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactory ;
43
+ import org .apache .hc .core5 .io .CloseMode ;
45
44
46
45
/**
47
46
* Thread-safe HTTP transport based on the Apache HTTP Client library.
51
50
* applications should use a single globally-shared instance of the HTTP transport.
52
51
*
53
52
* <p>Default settings are specified in {@link #newDefaultHttpClient()}. Use the {@link
54
- * #ApacheHttpTransport(CloseableHttpClient)} constructor to override the Apache HTTP Client used. Please
55
- * read the <a
56
- * href="https://hc. apache.org /httpcomponents-client-4.5.x/current/tutorial/pdf/httpclient-tutorial.pdf ">
57
- * Apache HTTP Client connection management tutorial </a> for more complex configuration options.
53
+ * #ApacheHttpTransport(CloseableHttpClient)} constructor to override the Apache HTTP Client used.
54
+ * Please read the <a
55
+ * href="https://github.com/ apache/httpcomponents-client/blob/f898f1aca38f77f62a007856a674629cae5a02e6/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientConfiguration.java ">
56
+ * Apache HTTP Client 5.x configuration example </a> for more complex configuration options.
58
57
*
59
- * @since 1.30
58
+ * @since 1.44
60
59
*/
61
60
public final class ApacheHttpTransport extends HttpTransport {
62
61
@@ -69,22 +68,20 @@ public final class ApacheHttpTransport extends HttpTransport {
69
68
/**
70
69
* Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client.
71
70
*
72
- * @since 1.30
71
+ * @since 1.44
73
72
*/
74
73
public ApacheHttpTransport () {
75
74
this (newDefaultHttpClient (), false );
76
75
}
77
76
78
77
/**
78
+ * {@link Beta} <br>
79
79
* Constructor that allows an alternative Apache HTTP client to be used.
80
80
*
81
- * <p>Note that in the previous version, we overrode several settings. However, we are no longer
82
- * able to do so.
83
- *
84
81
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
85
82
*
86
83
* <ul>
87
- * <li>Redirects are disabled (google-http-client handles redirects) .
84
+ * <li>HTTP version is set to 1.1 .
88
85
* <li>Retries are disabled (google-http-client handles retries).
89
86
* </ul>
90
87
*
@@ -99,13 +96,10 @@ public ApacheHttpTransport(CloseableHttpClient httpClient) {
99
96
* {@link Beta} <br>
100
97
* Constructor that allows an alternative CLoseable Apache HTTP client to be used.
101
98
*
102
- * <p>Note that in the previous version, we overrode several settings. However, we are no longer
103
- * able to do so.
104
- *
105
99
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
106
100
*
107
101
* <ul>
108
- * <li>Redirects are disabled (google-http-client handles redirects) .
102
+ * <li>HTTP version is set to 1.1 .
109
103
* <li>Retries are disabled (google-http-client handles retries).
110
104
* </ul>
111
105
*
@@ -119,6 +113,7 @@ public ApacheHttpTransport(CloseableHttpClient httpClient, boolean isMtls) {
119
113
}
120
114
121
115
/**
116
+ * {@link Beta} <br>
122
117
* Creates a new instance of the Apache HTTP client that is used by the {@link
123
118
* #ApacheHttpTransport()} constructor.
124
119
*
@@ -130,12 +125,12 @@ public ApacheHttpTransport(CloseableHttpClient httpClient, boolean isMtls) {
130
125
* HttpClientBuilder#disableRedirectHandling}.
131
126
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
132
127
* ProxySelector#getDefault()}, which uses the proxy settings from <a
133
- * href="https://docs.oracle.com/javase/7 /docs/api/java/net/doc-files/net-properties.html">system
128
+ * href="https://docs.oracle.com/javase/8 /docs/api/java/net/doc-files/net-properties.html">system
134
129
* properties</a>.
135
130
* </ul>
136
131
*
137
132
* @return new instance of the Apache HTTP client
138
- * @since 1.30
133
+ * @since 1.44
139
134
*/
140
135
public static CloseableHttpClient newDefaultHttpClient () {
141
136
return newDefaultCloseableHttpClientBuilder ().build ();
@@ -149,26 +144,27 @@ public static CloseableHttpClient newDefaultHttpClient() {
149
144
*
150
145
* <ul>
151
146
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
152
- * <li>< The retry mechanism is turned off using {@link
147
+ * <li>The retry mechanism is turned off using {@link
153
148
* HttpClientBuilder#disableRedirectHandling}.
149
+ * <li>Redirects are turned off using {@link HttpClientBuilder#disableAutomaticRetries()}.
154
150
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
155
151
* ProxySelector#getDefault()}, which uses the proxy settings from <a
156
- * href="http://docs.oracle.com/javase/7 /docs/api/java/net/doc-files/net-properties.html">system
152
+ * href="http://docs.oracle.com/javase/8 /docs/api/java/net/doc-files/net-properties.html">system
157
153
* properties</a>.
158
154
* </ul>
159
155
*
160
156
* @return new instance of the Apache HTTP client
161
- * @since 1.31
157
+ * @since 1.44
162
158
*/
163
159
public static HttpClientBuilder newDefaultCloseableHttpClientBuilder () {
164
- PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder . create ()
165
- . setSSLSocketFactory ( SSLConnectionSocketFactory . getSocketFactory () )
166
- . setMaxConnTotal ( 200 )
167
- . setMaxConnPerRoute ( 20 )
168
- . setDefaultConnectionConfig ( ConnectionConfig . custom ( )
169
- .setTimeToLive (- 1 , TimeUnit . MILLISECONDS )
170
- .build ())
171
- .build ();
160
+ PoolingHttpClientConnectionManager connectionManager =
161
+ PoolingHttpClientConnectionManagerBuilder . create ( )
162
+ . setSSLSocketFactory ( SSLConnectionSocketFactory . getSocketFactory () )
163
+ . setMaxConnTotal ( 200 )
164
+ . setMaxConnPerRoute ( 20 )
165
+ .setDefaultConnectionConfig (
166
+ ConnectionConfig . custom (). setTimeToLive (- 1 , TimeUnit . MILLISECONDS ) .build ())
167
+ .build ();
172
168
173
169
return HttpClients .custom ()
174
170
.useSystemProperties ()
@@ -209,20 +205,20 @@ protected ApacheHttpRequest buildRequest(String method, String url) {
209
205
}
210
206
211
207
/**
212
- * Shuts down the connection manager and releases allocated resources. This closes all
208
+ * Gracefully shuts down the connection manager and releases allocated resources. This closes all
213
209
* connections, whether they are currently used or not.
214
210
*
215
- * @since 1.30
211
+ * @since 1.44
216
212
*/
217
213
@ Override
218
214
public void shutdown () throws IOException {
219
- httpClient .close ();
215
+ httpClient .close (CloseMode . GRACEFUL );
220
216
}
221
217
222
218
/**
223
219
* Returns the Apache HTTP client.
224
220
*
225
- * @since 1.30
221
+ * @since 1.44
226
222
*/
227
223
public HttpClient getHttpClient () {
228
224
return httpClient ;
0 commit comments