|
7 | 7 | import java.io.UnsupportedEncodingException;
|
8 | 8 | import java.net.URI;
|
9 | 9 | import java.net.URLDecoder;
|
| 10 | +import java.security.KeyStore; |
10 | 11 | import java.util.Arrays;
|
11 | 12 | import java.util.HashMap;
|
12 | 13 | import java.util.Map;
|
13 | 14 |
|
| 15 | +import javax.net.ssl.SSLContext; |
| 16 | +import javax.net.ssl.TrustManagerFactory; |
| 17 | + |
14 | 18 | import org.apache.commons.logging.Log;
|
15 | 19 | import org.apache.commons.logging.LogFactory;
|
16 | 20 | import org.apache.http.HttpEntity;
|
17 | 21 | import org.apache.http.HttpHost;
|
18 | 22 | import org.apache.http.HttpResponse;
|
| 23 | +import org.apache.http.client.config.RequestConfig; |
19 | 24 | import org.apache.http.client.methods.HttpPost;
|
20 | 25 | import org.apache.http.conn.params.ConnRoutePNames;
|
| 26 | +import org.apache.http.conn.socket.LayeredConnectionSocketFactory; |
| 27 | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
21 | 28 | import org.apache.http.entity.StringEntity;
|
22 | 29 | import org.apache.http.impl.client.DefaultHttpClient;
|
| 30 | +import org.apache.http.impl.client.HttpClients; |
| 31 | +import org.apache.http.impl.client.LaxRedirectStrategy; |
23 | 32 | import org.apache.http.params.CoreProtocolPNames;
|
24 | 33 | import org.apache.http.params.HttpConnectionParams;
|
25 | 34 | import org.apache.http.protocol.HTTP;
|
@@ -313,4 +322,41 @@ public static void setProxyIfRequested(DefaultHttpClient httpClient) {
|
313 | 322 | httpClient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost);
|
314 | 323 | }
|
315 | 324 | }
|
| 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 | + } |
316 | 362 | }
|
0 commit comments