Skip to content

Commit 104cdc6

Browse files
authored
Merge pull request #135 from AuthorizeNet/future
RC 1.9.6
2 parents 2243130 + dd18fc8 commit 104cdc6

File tree

5 files changed

+67
-32
lines changed

5 files changed

+67
-32
lines changed

.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>anet_java_sdk.1.4.x</name>
3+
<name>anet_java_sdk</name>
44
<comment></comment>
55
<projects>
66
</projects>

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151
<includes>
152152
<include>*</include>
153153
</includes>
154+
<excludes>
155+
<exclude>log4j.properties</exclude>
156+
</excludes>
154157
</resource>
155158
</resources>
156159
</build>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public final class Constants {
66
public static final String HTTPS_USE_PROXY = "https.proxyUse";
77
public static final String HTTPS_PROXY_HOST = "https.proxyHost";
88
public static final String HTTPS_PROXY_PORT = "https.proxyPort";
9+
public static final String HTTPS_PROXY_USERNAME = "https.proxyUsername";
10+
public static final String HTTPS_PROXY_PASSWORD = "https.proxyPassword";
911

1012
public static final String HTTP_USE_PROXY = "http.proxyUse";
1113
public static final String HTTP_PROXY_HOST = "http.proxyHost";
@@ -33,5 +35,5 @@ public final class Constants {
3335
public static final String HTTP_READ_TIME_OUT = "http.ReadTimeout";
3436
public static final int HTTP_READ_TIME_OUT_DEFAULT_VALUE = 30000;
3537

36-
public static final String CLIENT_ID = "sdk-java-1.9.5";
38+
public static final String CLIENT_ID = "sdk-java-1.9.6";
3739
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public ANetApiResponse call() throws Exception {
6868
try {
6969
HttpPost httppost = HttpUtility.createPostRequest(this.env, this.request);
7070
httpCaller = HttpClient.getHttpsClient();
71-
HttpClient.setProxyIfRequested(httpCaller);
71+
7272
HttpResponse httpResponse = httpCaller.execute(httppost);
7373

7474
if ( null != httpResponse) {

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

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@
2020
import org.apache.http.HttpEntity;
2121
import org.apache.http.HttpHost;
2222
import 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;
2327
import org.apache.http.client.config.RequestConfig;
2428
import org.apache.http.client.methods.HttpPost;
2529
import org.apache.http.conn.params.ConnRoutePNames;
2630
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
2731
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2832
import org.apache.http.entity.StringEntity;
33+
import org.apache.http.impl.client.BasicCredentialsProvider;
34+
import org.apache.http.impl.client.CloseableHttpClient;
2935
import org.apache.http.impl.client.DefaultHttpClient;
36+
import org.apache.http.impl.client.HttpClientBuilder;
3037
import org.apache.http.impl.client.HttpClients;
3138
import org.apache.http.impl.client.LaxRedirectStrategy;
39+
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
3240
import org.apache.http.params.CoreProtocolPNames;
3341
import org.apache.http.params.HttpConnectionParams;
3442
import 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,58 @@ 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) {
348+
349+
HttpClientBuilder hcBuilder;
350+
if (proxyUsername != null && proxyPassword != null) {
351+
LogHelper.info(logger, "Setting up proxy to URL with Authentication: '%s://%s@%s:%d'", Constants.PROXY_PROTOCOL, proxyUsername, ProxyHost, ProxyPort);
352+
CredentialsProvider credsProvider = new BasicCredentialsProvider();
353+
AuthScope proxyScope = new AuthScope(ProxyHost, ProxyPort);
354+
Credentials proxyCreds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
355+
credsProvider.setCredentials(proxyScope, proxyCreds);
356+
hcBuilder = HttpClients.custom()
357+
.setSSLSocketFactory(sslSocketFactory)
358+
.setDefaultRequestConfig(requestConfig)
359+
.setRedirectStrategy(new LaxRedirectStrategy())
360+
.setDefaultCredentialsProvider(credsProvider);
361+
}
362+
else {
363+
LogHelper.info(logger, "Setting up proxy to URL: '%s://%s:%d'", Constants.PROXY_PROTOCOL, ProxyHost, ProxyPort);
364+
hcBuilder = HttpClients.custom()
365+
.setSSLSocketFactory(sslSocketFactory)
366+
.setDefaultRequestConfig(requestConfig)
367+
.setRedirectStrategy(new LaxRedirectStrategy());
368+
}
369+
370+
HttpHost httpProxy = new HttpHost(ProxyHost, ProxyPort, Constants.PROXY_PROTOCOL);
371+
hcBuilder.setProxy(httpProxy);
372+
373+
httpClient = hcBuilder.build();
374+
375+
proxySet = true;
376+
}
377+
else {
378+
LogHelper.warn(logger, "Defaulting to non-proxy environment");
379+
380+
httpClient = HttpClients.custom()
381+
.setSSLSocketFactory(sslSocketFactory)
382+
.setDefaultRequestConfig(requestConfig)
383+
.setRedirectStrategy(new LaxRedirectStrategy())
384+
.build();
385+
}
386+
387+
return httpClient;
358388
} catch (Exception e) {
359389
return null;
360390
}

0 commit comments

Comments
 (0)