77import java .io .UnsupportedEncodingException ;
88import java .net .URI ;
99import java .net .URLDecoder ;
10+ import java .security .KeyStore ;
1011import java .util .Arrays ;
1112import java .util .HashMap ;
1213import java .util .Map ;
1314
15+ import javax .net .ssl .SSLContext ;
16+ import javax .net .ssl .TrustManagerFactory ;
17+
1418import org .apache .commons .logging .Log ;
1519import org .apache .commons .logging .LogFactory ;
1620import org .apache .http .HttpEntity ;
1721import org .apache .http .HttpHost ;
1822import org .apache .http .HttpResponse ;
23+ import org .apache .http .client .config .RequestConfig ;
1924import org .apache .http .client .methods .HttpPost ;
2025import org .apache .http .conn .params .ConnRoutePNames ;
26+ import org .apache .http .conn .socket .LayeredConnectionSocketFactory ;
27+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
2128import org .apache .http .entity .StringEntity ;
2229import org .apache .http .impl .client .DefaultHttpClient ;
30+ import org .apache .http .impl .client .HttpClients ;
31+ import org .apache .http .impl .client .LaxRedirectStrategy ;
2332import org .apache .http .params .CoreProtocolPNames ;
2433import org .apache .http .params .HttpConnectionParams ;
2534import org .apache .http .protocol .HTTP ;
@@ -144,7 +153,7 @@ public static Map<ResponseField, String> execute(Environment environment, Transa
144153
145154 if (environment != null && transaction != null ) {
146155 try {
147- DefaultHttpClient httpClient = new DefaultHttpClient ();
156+ org . apache . http . client . HttpClient httpClient = getHttpsClient ();
148157
149158 setProxyIfRequested (httpClient );
150159
@@ -234,7 +243,7 @@ public static BasicXmlDocument executeXML(Environment environment, Transaction t
234243
235244 if (environment != null && transaction != null ) {
236245 try {
237- DefaultHttpClient httpClient = new DefaultHttpClient ();
246+ org . apache . http . client . HttpClient httpClient = getHttpsClient ();
238247
239248 setProxyIfRequested (httpClient );
240249
@@ -302,7 +311,7 @@ public static BasicXmlDocument executeXML(Environment environment, Transaction t
302311 * if proxy use is requested, set http-client appropriately
303312 * @param httpClient the client to add proxy values to
304313 */
305- public static void setProxyIfRequested (DefaultHttpClient httpClient ) {
314+ public static void setProxyIfRequested (org . apache . http . client . HttpClient httpClient ) {
306315 if ( UseProxy )
307316 {
308317 if ( !proxySet ) {
@@ -313,4 +322,41 @@ public static void setProxyIfRequested(DefaultHttpClient httpClient) {
313322 httpClient .getParams ().setParameter ( ConnRoutePNames .DEFAULT_PROXY , proxyHttpHost );
314323 }
315324 }
325+
326+ /**
327+ * @return returns an SSL context with TLSv1.2 protocol instance to be used in the call
328+ */
329+ private static SSLContext getSSLContext () {
330+ try {
331+ final SSLContext sc = SSLContext .getInstance ("TLSv1.2" );
332+ final TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
333+ trustManagerFactory .init ((KeyStore ) null );
334+ sc .init (null , trustManagerFactory .getTrustManagers (), new java .security .SecureRandom ());
335+ return sc ;
336+ }
337+ catch (Exception e ) {
338+ e .printStackTrace ();
339+ return null ;
340+ }
341+ }
342+
343+ /**
344+ * Returns a HTTPClient instance which enforce TLSv1.2 protocol for all the calls
345+ * @return org.apache.http.client.HttpClient instance
346+ * @throws Exception
347+ */
348+ static org .apache .http .client .HttpClient getHttpsClient () throws Exception {
349+ SSLContext sslcontext = getSSLContext ();
350+ try {
351+ LayeredConnectionSocketFactory sslSocketFactory = new org .apache .http .conn .ssl .SSLConnectionSocketFactory (sslcontext , SSLConnectionSocketFactory .STRICT_HOSTNAME_VERIFIER );
352+ RequestConfig requestConfig = RequestConfig .custom ().setConnectTimeout (httpConnectionTimeout ).build ();
353+ return HttpClients .custom ()
354+ .setSSLSocketFactory (sslSocketFactory )
355+ .setDefaultRequestConfig (requestConfig )
356+ .setRedirectStrategy (new LaxRedirectStrategy ())
357+ .build ();
358+ } catch (Exception e ) {
359+ return null ;
360+ }
361+ }
316362}
0 commit comments