Skip to content

Commit 1ca59d1

Browse files
author
Sunny Raj Rathod
authored
Merge pull request AuthorizeNet#127 from Sapbasu15/master
Force HttpClient to implement TLSv1.2 protocol
2 parents 278b618 + 0bf6c64 commit 1ca59d1

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/main/java/net/authorize/util/HttpCallTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public ANetApiResponse call() throws Exception {
6363
ANetApiResponse response = null;
6464
StringBuilder buffer = new StringBuilder();
6565

66-
DefaultHttpClient httpCaller = null;
66+
org.apache.http.client.HttpClient httpCaller = null;
6767

6868
try {
6969
HttpPost httppost = HttpUtility.createPostRequest(this.env, this.request);
70-
httpCaller = new DefaultHttpClient();
70+
httpCaller = HttpClient.getHttpsClient();
7171
HttpClient.setProxyIfRequested(httpCaller);
7272
HttpResponse httpResponse = httpCaller.execute(httppost);
7373

src/main/java/net/authorize/util/HttpClient.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@
77
import java.io.UnsupportedEncodingException;
88
import java.net.URI;
99
import java.net.URLDecoder;
10+
import java.security.KeyStore;
1011
import java.util.Arrays;
1112
import java.util.HashMap;
1213
import java.util.Map;
1314

15+
import javax.net.ssl.SSLContext;
16+
import javax.net.ssl.TrustManagerFactory;
17+
1418
import org.apache.commons.logging.Log;
1519
import org.apache.commons.logging.LogFactory;
1620
import org.apache.http.HttpEntity;
1721
import org.apache.http.HttpHost;
1822
import org.apache.http.HttpResponse;
23+
import org.apache.http.client.config.RequestConfig;
1924
import org.apache.http.client.methods.HttpPost;
2025
import org.apache.http.conn.params.ConnRoutePNames;
26+
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
27+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2128
import org.apache.http.entity.StringEntity;
2229
import org.apache.http.impl.client.DefaultHttpClient;
30+
import org.apache.http.impl.client.HttpClients;
31+
import org.apache.http.impl.client.LaxRedirectStrategy;
2332
import org.apache.http.params.CoreProtocolPNames;
2433
import org.apache.http.params.HttpConnectionParams;
2534
import 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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy