17
17
18
18
package org .sourcelab .hkp .rest ;
19
19
20
- import org .apache .http .HttpHost ;
21
- import org .apache .http .NameValuePair ;
22
- import org .apache .http .auth .AuthScope ;
23
- import org .apache .http .auth .UsernamePasswordCredentials ;
24
- import org .apache .http .client .AuthCache ;
25
- import org .apache .http .client .ClientProtocolException ;
26
- import org .apache .http .client .CredentialsProvider ;
27
- import org .apache .http .client .ResponseHandler ;
28
- import org .apache .http .client .config .RequestConfig ;
29
- import org .apache .http .client .entity .UrlEncodedFormEntity ;
30
- import org .apache .http .client .methods .HttpGet ;
31
- import org .apache .http .client .methods .HttpPost ;
32
- import org .apache .http .client .protocol .HttpClientContext ;
33
- import org .apache .http .client .utils .URIBuilder ;
34
- import org .apache .http .conn .socket .LayeredConnectionSocketFactory ;
35
- import org .apache .http .conn .ssl .NoopHostnameVerifier ;
36
- import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
37
- import org .apache .http .impl .auth .BasicScheme ;
38
- import org .apache .http .impl .client .BasicAuthCache ;
39
- import org .apache .http .impl .client .BasicCredentialsProvider ;
40
- import org .apache .http .impl .client .CloseableHttpClient ;
41
- import org .apache .http .impl .client .HttpClientBuilder ;
42
- import org .apache .http .message .BasicNameValuePair ;
43
- import org .apache .http .ssl .SSLContexts ;
20
+ import org .apache .hc .client5 .http .ClientProtocolException ;
21
+ import org .apache .hc .client5 .http .auth .AuthCache ;
22
+ import org .apache .hc .client5 .http .auth .AuthScope ;
23
+ import org .apache .hc .client5 .http .auth .CredentialsStore ;
24
+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
25
+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
26
+ import org .apache .hc .client5 .http .config .RequestConfig ;
27
+ import org .apache .hc .client5 .http .impl .auth .BasicAuthCache ;
28
+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
29
+ import org .apache .hc .client5 .http .impl .auth .BasicScheme ;
30
+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
31
+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
32
+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
33
+ import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
34
+ import org .apache .hc .client5 .http .protocol .HttpClientContext ;
35
+ import org .apache .hc .client5 .http .ssl .DefaultHostnameVerifier ;
36
+ import org .apache .hc .client5 .http .ssl .NoopHostnameVerifier ;
37
+ import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactory ;
38
+ import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactoryBuilder ;
39
+ import org .apache .hc .core5 .http .ClassicHttpRequest ;
40
+ import org .apache .hc .core5 .http .HttpHost ;
41
+ import org .apache .hc .core5 .http .io .HttpClientResponseHandler ;
42
+ import org .apache .hc .core5 .http .ssl .TLS ;
43
+ import org .apache .hc .core5 .net .URIBuilder ;
44
+ import org .apache .hc .core5 .ssl .SSLContexts ;
45
+ import org .apache .hc .core5 .util .Timeout ;
44
46
import org .slf4j .Logger ;
45
47
import org .slf4j .LoggerFactory ;
46
48
import org .sourcelab .hkp .ConnectionFailedException ;
55
57
import javax .net .ssl .TrustManager ;
56
58
import javax .net .ssl .TrustManagerFactory ;
57
59
import java .io .IOException ;
58
- import java .net .ConnectException ;
59
60
import java .net .SocketException ;
60
61
import java .net .URISyntaxException ;
61
62
import java .nio .charset .StandardCharsets ;
64
65
import java .security .KeyStoreException ;
65
66
import java .security .NoSuchAlgorithmException ;
66
67
import java .security .SecureRandom ;
67
- import java .util .ArrayList ;
68
- import java .util .HashMap ;
69
- import java .util .List ;
70
68
import java .util .Map ;
71
- import java .util .concurrent .TimeUnit ;
72
69
73
70
/**
74
71
* RestClient implementation using HTTPClient.
75
72
*/
76
- public class HttpClientRestClient implements RestClient {
77
- private static final Logger logger = LoggerFactory .getLogger (HttpClientRestClient .class );
73
+ public class HttpClient5RestClient implements RestClient {
74
+ private static final Logger logger = LoggerFactory .getLogger (HttpClient5RestClient .class );
78
75
79
76
/**
80
77
* Save a copy of the configuration.
@@ -90,7 +87,7 @@ public class HttpClientRestClient implements RestClient {
90
87
/**
91
88
* Constructor.
92
89
*/
93
- public HttpClientRestClient () {
90
+ public HttpClient5RestClient () {
94
91
}
95
92
96
93
/**
@@ -123,23 +120,22 @@ public void init(final Configuration configuration) {
123
120
hostnameVerifier = NoopHostnameVerifier .INSTANCE ;
124
121
} else {
125
122
// Use default implementation
126
- hostnameVerifier = SSLConnectionSocketFactory . getDefaultHostnameVerifier ();
123
+ hostnameVerifier = new DefaultHostnameVerifier ();
127
124
}
128
125
129
126
// Allow TLSv1_1 and TLSv1_2 protocols
130
- final LayeredConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (
131
- sslcontext ,
132
- new String [] { "TLSv1.1" , "TLSv1.2" },
133
- null ,
134
- hostnameVerifier
135
- );
127
+ final SSLConnectionSocketFactory sslsf = SSLConnectionSocketFactoryBuilder .create ()
128
+ .setSslContext (sslcontext )
129
+ .setTlsVersions (TLS .V_1_1 , TLS .V_1_2 )
130
+ .setHostnameVerifier (hostnameVerifier )
131
+ .build ();
132
+ final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder .create ()
133
+ .setSSLSocketFactory (sslsf )
134
+ .build ();
136
135
137
136
// Setup client builder
138
- final HttpClientBuilder clientBuilder = HttpClientBuilder .create ();
139
- clientBuilder
140
- // Disconnect requests after 120 seconds.
141
- .setConnectionTimeToLive (configuration .getRequestTimeoutSecs (), TimeUnit .SECONDS )
142
- .setSSLSocketFactory (sslsf );
137
+ final HttpClientBuilder clientBuilder = HttpClientBuilder .create ()
138
+ .setConnectionManager (cm );
143
139
144
140
// Define our RequestConfigBuilder
145
141
final RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ();
@@ -154,32 +150,32 @@ public void init(final Configuration configuration) {
154
150
if (configuration .hasProxyConfigured ()) {
155
151
// Define proxy host
156
152
final HttpHost proxyHost = new HttpHost (
153
+ configuration .getProxyConfiguration ().getScheme (),
157
154
configuration .getProxyConfiguration ().getHost (),
158
- configuration .getProxyConfiguration ().getPort (),
159
- configuration .getProxyConfiguration ().getScheme ()
155
+ configuration .getProxyConfiguration ().getPort ()
160
156
);
161
157
162
158
// If we have proxy auth enabled
163
159
if (configuration .getProxyConfiguration ().isAuthenticationRequired ()) {
164
160
// Create credential provider
165
- final CredentialsProvider credsProvider = new BasicCredentialsProvider ();
161
+ final CredentialsStore credsProvider = new BasicCredentialsProvider ();
166
162
credsProvider .setCredentials (
167
163
new AuthScope (
168
164
configuration .getProxyConfiguration ().getHost (),
169
165
configuration .getProxyConfiguration ().getPort ()
170
166
),
171
167
new UsernamePasswordCredentials (
172
168
configuration .getProxyConfiguration ().getUsername (),
173
- configuration .getProxyConfiguration ().getPassword ()
169
+ configuration .getProxyConfiguration ().getPassword (). toCharArray ()
174
170
)
175
171
);
176
172
177
173
// Preemptive load context with authentication.
178
174
authCache .put (
179
175
new HttpHost (
176
+ configuration .getProxyConfiguration ().getScheme (),
180
177
configuration .getProxyConfiguration ().getHost (),
181
- configuration .getProxyConfiguration ().getPort (),
182
- configuration .getProxyConfiguration ().getScheme ()
178
+ configuration .getProxyConfiguration ().getPort ()
183
179
),
184
180
new BasicScheme ()
185
181
);
@@ -190,7 +186,10 @@ public void init(final Configuration configuration) {
190
186
}
191
187
192
188
// Attach Proxy to request config builder
193
- requestConfigBuilder .setProxy (proxyHost );
189
+ requestConfigBuilder
190
+ .setConnectionRequestTimeout (Timeout .ofSeconds (configuration .getRequestTimeoutSecs ()))
191
+ .setConnectTimeout (Timeout .ofSeconds (configuration .getRequestTimeoutSecs ()))
192
+ .setProxy (proxyHost );
194
193
195
194
// Configure context.
196
195
httpClientContext .setAuthCache (authCache );
@@ -233,8 +232,8 @@ public void close() {
233
232
if (httpClient != null ) {
234
233
try {
235
234
httpClient .close ();
236
- } catch (IOException e ) {
237
- logger .error ("Error closing: {}" , e .getMessage (), e );
235
+ } catch (final IOException exception ) {
236
+ logger .error ("Error closing: {}" , exception .getMessage (), exception );
238
237
}
239
238
}
240
239
httpClient = null ;
@@ -250,7 +249,7 @@ public void close() {
250
249
public RestResponse submitRequest (final Request request ) throws RestException {
251
250
try {
252
251
return submitRequest (request , new RestResponseHandler ());
253
- } catch (IOException exception ) {
252
+ } catch (final IOException exception ) {
254
253
throw new RestException (exception .getMessage (), exception );
255
254
}
256
255
}
@@ -262,7 +261,7 @@ public RestResponse submitRequest(final Request request) throws RestException {
262
261
* @param <T> The return type.
263
262
* @return The parsed API response.
264
263
*/
265
- private <T > T submitRequest (final Request request , final ResponseHandler <T > responseHandler ) throws IOException {
264
+ private <T > T submitRequest (final Request request , final HttpClientResponseHandler <T > responseHandler ) throws IOException {
266
265
final String url = constructApiUrl (request );
267
266
return submitRequest (url , request .getRequestParameters (), responseHandler );
268
267
}
@@ -276,7 +275,7 @@ private <T> T submitRequest(final Request request, final ResponseHandler<T> resp
276
275
* @return Parsed response.
277
276
* @throws ConnectionFailedException if remote server does not accept connection.
278
277
*/
279
- private <T > T submitRequest (final String url , final Map <String , String > getParams , final ResponseHandler <T > responseHandler ) throws IOException {
278
+ private <T > T submitRequest (final String url , final Map <String , String > getParams , final HttpClientResponseHandler <T > responseHandler ) {
280
279
try {
281
280
// Construct URI including our request parameters.
282
281
final URIBuilder uriBuilder = new URIBuilder (url )
@@ -288,13 +287,13 @@ private <T> T submitRequest(final String url, final Map<String, String> getParam
288
287
}
289
288
290
289
// Build Get Request
291
- final HttpGet get = new HttpGet (uriBuilder .build ());
290
+ final ClassicHttpRequest get = new HttpGet (uriBuilder .build ());
292
291
293
292
// Debug logging
294
- logger .info ("Executing request {}" , get .getRequestLine ());
293
+ logger .info ("Executing request {}" , get .getRequestUri ());
295
294
296
295
// Execute and return
297
- return httpClient .execute (get , responseHandler , httpClientContext );
296
+ return httpClient .execute (get , httpClientContext , responseHandler );
298
297
} catch (final ClientProtocolException | SocketException | URISyntaxException | SSLHandshakeException connectionException ) {
299
298
// Signals that an error occurred while attempting to connect a
300
299
// socket to a remote address and port. Typically, the connection
0 commit comments