1717
1818package org .sourcelab .hkp .rest ;
1919
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 ;
4446import org .slf4j .Logger ;
4547import org .slf4j .LoggerFactory ;
4648import org .sourcelab .hkp .ConnectionFailedException ;
5557import javax .net .ssl .TrustManager ;
5658import javax .net .ssl .TrustManagerFactory ;
5759import java .io .IOException ;
58- import java .net .ConnectException ;
5960import java .net .SocketException ;
6061import java .net .URISyntaxException ;
6162import java .nio .charset .StandardCharsets ;
6465import java .security .KeyStoreException ;
6566import java .security .NoSuchAlgorithmException ;
6667import java .security .SecureRandom ;
67- import java .util .ArrayList ;
68- import java .util .HashMap ;
69- import java .util .List ;
7068import java .util .Map ;
71- import java .util .concurrent .TimeUnit ;
7269
7370/**
7471 * RestClient implementation using HTTPClient.
7572 */
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 );
7875
7976 /**
8077 * Save a copy of the configuration.
@@ -90,7 +87,7 @@ public class HttpClientRestClient implements RestClient {
9087 /**
9188 * Constructor.
9289 */
93- public HttpClientRestClient () {
90+ public HttpClient5RestClient () {
9491 }
9592
9693 /**
@@ -123,23 +120,22 @@ public void init(final Configuration configuration) {
123120 hostnameVerifier = NoopHostnameVerifier .INSTANCE ;
124121 } else {
125122 // Use default implementation
126- hostnameVerifier = SSLConnectionSocketFactory . getDefaultHostnameVerifier ();
123+ hostnameVerifier = new DefaultHostnameVerifier ();
127124 }
128125
129126 // 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 ();
136135
137136 // 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 );
143139
144140 // Define our RequestConfigBuilder
145141 final RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ();
@@ -154,32 +150,32 @@ public void init(final Configuration configuration) {
154150 if (configuration .hasProxyConfigured ()) {
155151 // Define proxy host
156152 final HttpHost proxyHost = new HttpHost (
153+ configuration .getProxyConfiguration ().getScheme (),
157154 configuration .getProxyConfiguration ().getHost (),
158- configuration .getProxyConfiguration ().getPort (),
159- configuration .getProxyConfiguration ().getScheme ()
155+ configuration .getProxyConfiguration ().getPort ()
160156 );
161157
162158 // If we have proxy auth enabled
163159 if (configuration .getProxyConfiguration ().isAuthenticationRequired ()) {
164160 // Create credential provider
165- final CredentialsProvider credsProvider = new BasicCredentialsProvider ();
161+ final CredentialsStore credsProvider = new BasicCredentialsProvider ();
166162 credsProvider .setCredentials (
167163 new AuthScope (
168164 configuration .getProxyConfiguration ().getHost (),
169165 configuration .getProxyConfiguration ().getPort ()
170166 ),
171167 new UsernamePasswordCredentials (
172168 configuration .getProxyConfiguration ().getUsername (),
173- configuration .getProxyConfiguration ().getPassword ()
169+ configuration .getProxyConfiguration ().getPassword (). toCharArray ()
174170 )
175171 );
176172
177173 // Preemptive load context with authentication.
178174 authCache .put (
179175 new HttpHost (
176+ configuration .getProxyConfiguration ().getScheme (),
180177 configuration .getProxyConfiguration ().getHost (),
181- configuration .getProxyConfiguration ().getPort (),
182- configuration .getProxyConfiguration ().getScheme ()
178+ configuration .getProxyConfiguration ().getPort ()
183179 ),
184180 new BasicScheme ()
185181 );
@@ -190,7 +186,10 @@ public void init(final Configuration configuration) {
190186 }
191187
192188 // 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 );
194193
195194 // Configure context.
196195 httpClientContext .setAuthCache (authCache );
@@ -233,8 +232,8 @@ public void close() {
233232 if (httpClient != null ) {
234233 try {
235234 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 );
238237 }
239238 }
240239 httpClient = null ;
@@ -250,7 +249,7 @@ public void close() {
250249 public RestResponse submitRequest (final Request request ) throws RestException {
251250 try {
252251 return submitRequest (request , new RestResponseHandler ());
253- } catch (IOException exception ) {
252+ } catch (final IOException exception ) {
254253 throw new RestException (exception .getMessage (), exception );
255254 }
256255 }
@@ -262,7 +261,7 @@ public RestResponse submitRequest(final Request request) throws RestException {
262261 * @param <T> The return type.
263262 * @return The parsed API response.
264263 */
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 {
266265 final String url = constructApiUrl (request );
267266 return submitRequest (url , request .getRequestParameters (), responseHandler );
268267 }
@@ -276,7 +275,7 @@ private <T> T submitRequest(final Request request, final ResponseHandler<T> resp
276275 * @return Parsed response.
277276 * @throws ConnectionFailedException if remote server does not accept connection.
278277 */
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 ) {
280279 try {
281280 // Construct URI including our request parameters.
282281 final URIBuilder uriBuilder = new URIBuilder (url )
@@ -288,13 +287,13 @@ private <T> T submitRequest(final String url, final Map<String, String> getParam
288287 }
289288
290289 // Build Get Request
291- final HttpGet get = new HttpGet (uriBuilder .build ());
290+ final ClassicHttpRequest get = new HttpGet (uriBuilder .build ());
292291
293292 // Debug logging
294- logger .info ("Executing request {}" , get .getRequestLine ());
293+ logger .info ("Executing request {}" , get .getRequestUri ());
295294
296295 // Execute and return
297- return httpClient .execute (get , responseHandler , httpClientContext );
296+ return httpClient .execute (get , httpClientContext , responseHandler );
298297 } catch (final ClientProtocolException | SocketException | URISyntaxException | SSLHandshakeException connectionException ) {
299298 // Signals that an error occurred while attempting to connect a
300299 // socket to a remote address and port. Typically, the connection
0 commit comments