2020import org .apache .http .HttpEntity ;
2121import org .apache .http .HttpHost ;
2222import org .apache .http .HttpResponse ;
23+ import org .apache .http .auth .AuthScope ;
24+ import org .apache .http .auth .Credentials ;
25+ import org .apache .http .auth .UsernamePasswordCredentials ;
26+ import org .apache .http .client .CredentialsProvider ;
2327import org .apache .http .client .config .RequestConfig ;
2428import org .apache .http .client .methods .HttpPost ;
2529import org .apache .http .conn .params .ConnRoutePNames ;
2630import org .apache .http .conn .socket .LayeredConnectionSocketFactory ;
2731import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
2832import org .apache .http .entity .StringEntity ;
33+ import org .apache .http .impl .client .BasicCredentialsProvider ;
34+ import org .apache .http .impl .client .CloseableHttpClient ;
2935import org .apache .http .impl .client .DefaultHttpClient ;
36+ import org .apache .http .impl .client .HttpClientBuilder ;
3037import org .apache .http .impl .client .HttpClients ;
3138import org .apache .http .impl .client .LaxRedirectStrategy ;
39+ import org .apache .http .impl .conn .DefaultProxyRoutePlanner ;
3240import org .apache .http .params .CoreProtocolPNames ;
3341import org .apache .http .params .HttpConnectionParams ;
3442import org .apache .http .protocol .HTTP ;
@@ -51,6 +59,9 @@ public class HttpClient {
5159 static boolean UseProxy = Environment .getBooleanProperty (Constants .HTTPS_USE_PROXY );
5260 static String ProxyHost = Environment .getProperty (Constants .HTTPS_PROXY_HOST );
5361 static int ProxyPort = Environment .getIntProperty (Constants .HTTPS_PROXY_PORT );
62+ static String proxyUsername = Environment .getProperty (Constants .HTTPS_PROXY_USERNAME );
63+ static String proxyPassword = Environment .getProperty (Constants .HTTPS_PROXY_PASSWORD );
64+
5465 static int httpConnectionTimeout = Environment .getIntProperty (Constants .HTTP_CONNECTION_TIME_OUT );
5566 static int httpReadTimeout = Environment .getIntProperty (Constants .HTTP_READ_TIME_OUT );
5667
@@ -153,9 +164,7 @@ public static Map<ResponseField, String> execute(Environment environment, Transa
153164
154165 if (environment != null && transaction != null ) {
155166 try {
156- org .apache .http .client .HttpClient httpClient = getHttpsClient ();
157-
158- setProxyIfRequested (httpClient );
167+ CloseableHttpClient httpClient = getHttpsClient ();
159168
160169 // create the HTTP POST object
161170 HttpPost httpPost = createHttpPost (environment , transaction );
@@ -243,10 +252,8 @@ public static BasicXmlDocument executeXML(Environment environment, Transaction t
243252
244253 if (environment != null && transaction != null ) {
245254 try {
246- org . apache . http . client . HttpClient httpClient = getHttpsClient ();
255+ CloseableHttpClient httpClient = getHttpsClient ();
247256
248- setProxyIfRequested (httpClient );
249-
250257 // create the HTTP POST object
251258 HttpPost httpPost = createHttpPost (environment , transaction );
252259
@@ -307,22 +314,6 @@ public static BasicXmlDocument executeXML(Environment environment, Transaction t
307314 return response ;
308315 }
309316
310- /**
311- * if proxy use is requested, set http-client appropriately
312- * @param httpClient the client to add proxy values to
313- */
314- public static void setProxyIfRequested (org .apache .http .client .HttpClient httpClient ) {
315- if ( UseProxy )
316- {
317- if ( !proxySet ) {
318- LogHelper .info (logger , "Setting up proxy to URL: '%s://%s:%d'" , Constants .PROXY_PROTOCOL , ProxyHost , ProxyPort );
319- proxySet = true ;
320- }
321- HttpHost proxyHttpHost = new HttpHost (ProxyHost , ProxyPort , Constants .PROXY_PROTOCOL );
322- httpClient .getParams ().setParameter ( ConnRoutePNames .DEFAULT_PROXY , proxyHttpHost );
323- }
324- }
325-
326317 /**
327318 * @return returns an SSL context with TLSv1.2 protocol instance to be used in the call
328319 */
@@ -342,19 +333,52 @@ private static SSLContext getSSLContext() {
342333
343334 /**
344335 * Returns a HTTPClient instance which enforce TLSv1.2 protocol for all the calls
345- * @return org.apache.http.client.HttpClient instance
336+ * @return CloseableHttpClient instance
346337 * @throws Exception
347338 */
348- static org . apache . http . client . HttpClient getHttpsClient () throws Exception {
339+ static CloseableHttpClient getHttpsClient () throws Exception {
349340 SSLContext sslcontext = getSSLContext ();
350341 try {
351342 LayeredConnectionSocketFactory sslSocketFactory = new org .apache .http .conn .ssl .SSLConnectionSocketFactory (sslcontext , SSLConnectionSocketFactory .STRICT_HOSTNAME_VERIFIER );
352343 RequestConfig requestConfig = RequestConfig .custom ().setConnectTimeout (httpConnectionTimeout ).build ();
353- return HttpClients .custom ()
354- .setSSLSocketFactory (sslSocketFactory )
355- .setDefaultRequestConfig (requestConfig )
356- .setRedirectStrategy (new LaxRedirectStrategy ())
357- .build ();
344+
345+ CloseableHttpClient httpClient ;
346+
347+ if ( !UseProxy && ( ProxyHost == null || proxyUsername == null || proxyPassword == null ) ) {
348+ LogHelper .warn (logger , "Defaulting to non-proxy environment" );
349+
350+ httpClient = HttpClients .custom ()
351+ .setSSLSocketFactory (sslSocketFactory )
352+ .setDefaultRequestConfig (requestConfig )
353+ .setRedirectStrategy (new LaxRedirectStrategy ())
354+ .build ();
355+ }
356+
357+ else {
358+
359+ LogHelper .info (logger , "Setting up proxy to URL: '%s://%s:%d'" , Constants .PROXY_PROTOCOL , ProxyHost , ProxyPort );
360+ CredentialsProvider credsProvider = new BasicCredentialsProvider ();
361+
362+ AuthScope proxyScope = new AuthScope (ProxyHost , ProxyPort );
363+ Credentials proxyCreds = new UsernamePasswordCredentials (proxyUsername , proxyPassword );
364+ credsProvider .setCredentials (proxyScope , proxyCreds );
365+
366+ HttpClientBuilder hcBuilder = HttpClients .custom ()
367+ .setSSLSocketFactory (sslSocketFactory )
368+ .setDefaultRequestConfig (requestConfig )
369+ .setRedirectStrategy (new LaxRedirectStrategy ())
370+ .setDefaultCredentialsProvider (credsProvider );
371+
372+ HttpHost httpProxy = new HttpHost (ProxyHost , ProxyPort , Constants .PROXY_PROTOCOL );
373+ DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner (httpProxy );
374+ hcBuilder .setRoutePlanner (routePlanner );
375+
376+ httpClient = hcBuilder .build ();
377+
378+ proxySet = true ;
379+ }
380+
381+ return httpClient ;
358382 } catch (Exception e ) {
359383 return null ;
360384 }
0 commit comments