3030import org .apache .http .client .methods .HttpGet ;
3131import org .apache .http .client .methods .HttpPost ;
3232import org .apache .http .client .methods .HttpPut ;
33+ import org .apache .http .client .methods .HttpUriRequest ;
3334import org .apache .http .client .protocol .HttpClientContext ;
3435import org .apache .http .client .utils .URIBuilder ;
3536import org .apache .http .entity .StringEntity ;
@@ -85,7 +86,15 @@ public class HttpClientRestClient implements RestClient {
8586 */
8687 private CloseableHttpClient httpClient ;
8788
88- private HttpClientContext httpClientContext ;
89+ /**
90+ * The AuthCache used when creating the HttpClientContext.
91+ */
92+ private AuthCache authCache ;
93+
94+ /**
95+ * The CredentialsProvider used when creating the HttpClientContext.
96+ */
97+ private CredentialsProvider credsProvider ;
8998
9099 /**
91100 * Constructor.
@@ -121,13 +130,10 @@ public void init(final Configuration configuration) {
121130 requestConfigBuilder .setConnectTimeout (configuration .getRequestTimeoutInSeconds () * 1_000 );
122131
123132 // Define our Credentials Provider
124- final CredentialsProvider credsProvider = new BasicCredentialsProvider ();
125-
126- // Define our context
127- httpClientContext = HttpClientContext .create ();
133+ credsProvider = new BasicCredentialsProvider ();
128134
129135 // Define our auth cache
130- final AuthCache authCache = new BasicAuthCache ();
136+ authCache = new BasicAuthCache ();
131137
132138 // If we have a configured proxy host
133139 if (configuration .getProxyHost () != null ) {
@@ -180,10 +186,6 @@ public void init(final Configuration configuration) {
180186 }
181187 }
182188
183- // Configure context.
184- httpClientContext .setAuthCache (authCache );
185- httpClientContext .setCredentialsProvider (credsProvider );
186-
187189 // Attach Credentials provider to client builder.
188190 clientBuilder .setDefaultCredentialsProvider (credsProvider );
189191
@@ -272,7 +274,7 @@ private <T> T submitGetRequest(final String url, final Map<String, String> getPa
272274 logger .debug ("Executing request {}" , get .getRequestLine ());
273275
274276 // Execute and return
275- return httpClient . execute (get , responseHandler , httpClientContext );
277+ return execute (get , responseHandler );
276278 } catch (final ClientProtocolException | SocketException | URISyntaxException | SSLHandshakeException connectionException ) {
277279 // Typically this is a connection or certificate issue.
278280 throw new ConnectionException (connectionException .getMessage (), connectionException );
@@ -305,7 +307,7 @@ private <T> T submitPostRequest(final String url, final Object requestBody, fina
305307 logger .debug ("Executing request {} with {}" , post .getRequestLine (), jsonPayloadStr );
306308
307309 // Execute and return
308- return httpClient . execute (post , responseHandler , httpClientContext );
310+ return execute (post , responseHandler );
309311 } catch (final ClientProtocolException | SocketException | SSLHandshakeException connectionException ) {
310312 // Typically this is a connection issue.
311313 throw new ConnectionException (connectionException .getMessage (), connectionException );
@@ -337,7 +339,7 @@ private <T> T submitPutRequest(final String url, final Object requestBody, final
337339 logger .debug ("Executing request {} with {}" , put .getRequestLine (), jsonPayloadStr );
338340
339341 // Execute and return
340- return httpClient . execute (put , responseHandler , httpClientContext );
342+ return execute (put , responseHandler );
341343 } catch (final ClientProtocolException | SocketException | SSLHandshakeException connectionException ) {
342344 // Typically this is a connection issue.
343345 throw new ConnectionException (connectionException .getMessage (), connectionException );
@@ -368,7 +370,7 @@ private <T> T submitDeleteRequest(final String url, final Object requestBody, fi
368370 logger .debug ("Executing request {} with {}" , delete .getRequestLine (), jsonPayloadStr );
369371
370372 // Execute and return
371- return httpClient . execute (delete , responseHandler , httpClientContext );
373+ return execute (delete , responseHandler );
372374 } catch (final ClientProtocolException | SocketException | SSLHandshakeException connectionException ) {
373375 // Typically this is a connection issue.
374376 throw new ConnectionException (connectionException .getMessage (), connectionException );
@@ -378,6 +380,18 @@ private <T> T submitDeleteRequest(final String url, final Object requestBody, fi
378380 }
379381 }
380382
383+ /**
384+ * Creates an HttpClientContext and executes the HTTP request.
385+ *
386+ * @param request The request to execute
387+ * @param responseHandler The response Handler to use to parse the response
388+ * @param <T> The type that ResponseHandler returns.
389+ * @return Parsed response.
390+ */
391+ private <T > T execute (HttpUriRequest request , ResponseHandler <T > responseHandler ) throws IOException {
392+ return httpClient .execute (request , responseHandler , createHttpClientContext ());
393+ }
394+
381395 /**
382396 * Internal helper method for generating URLs w/ the appropriate API host and API version.
383397 * @param endPoint The end point you want to hit.
@@ -386,4 +400,18 @@ private <T> T submitDeleteRequest(final String url, final Object requestBody, fi
386400 private String constructApiUrl (final String endPoint ) {
387401 return configuration .getApiHost () + endPoint ;
388402 }
403+
404+ /**
405+ * Creates a new HttpClientContext with the authCache and credsProvider.
406+ * @return the created HttpClientContext.
407+ */
408+ private HttpClientContext createHttpClientContext () {
409+ // Define our context
410+ HttpClientContext httpClientContext = HttpClientContext .create ();
411+ // Configure context.
412+ httpClientContext .setAuthCache (authCache );
413+ httpClientContext .setCredentialsProvider (credsProvider );
414+
415+ return httpClientContext ;
416+ }
389417}
0 commit comments