Skip to content

Commit 00d6e9d

Browse files
authored
Merge pull request #133 from gnongsie/master
Adding proxy authentication support
2 parents c37e1ca + 36afd75 commit 00d6e9d

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<groupId>net.authorize</groupId>
1010
<artifactId>anet-java-sdk</artifactId>
1111
<packaging>jar</packaging>
12-
<version>1.9.5-SNAPSHOT</version>
12+
<version>1.9.6-SNAPSHOT</version>
1313
<name>Authorize.Net Java SDK</name>
1414
<description>Authorize.Net SDK includes standard payments, recurring billing, and customer profiles.</description>
1515
<url>http://developer.authorize.net</url>
@@ -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.proxyUser";
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.4";
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: 53 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,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

Comments
 (0)