Skip to content

Commit 097bad7

Browse files
author
akankari
committed
Issue : #62
Reviewed by: Sunny and Ashutosh Changes made : Adding Timeout for http connection and http read data. Adding the constants for same which read value from properties file.
1 parent ba9f0df commit 097bad7

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

anet-java-sdk.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ md5.hash.key=MD5_HASH_KEY
2323
# http.ProxyUse=true
2424
# https.proxyHost=HTTPS_PROXY_HOST
2525
# https.proxyPort=HTTPS_PROXY_PORT
26-
# https.proxyUse=true
26+
# https.proxyUse=true
27+
28+
#Settings for HTTP connection timeouts (in milliseconds)
29+
HTTP_CONNECTION_TIME_OUT=30000
30+
HTTP_READ_TIME_OUT=30000

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ public final class Constants {
2525

2626
public static final String PROP_API_LOGINID_APPLEPAY = "api.login.id.applepay";
2727
public static final String PROP_TRANSACTION_KEY_APPLEPAY = "transaction.key.applepay";
28-
public static final String PROP_MD5_HASHKEY_APPLEPAY = "md5.hash.key.applepay";
28+
public static final String PROP_MD5_HASHKEY_APPLEPAY = "md5.hash.key.applepay";
29+
30+
public static final String HTTP_CONNECTION_TIME_OUT = "HTTP_CONNECTION_TIME_OUT";
31+
public static final String HTTP_READ_TIME_OUT = "HTTP_READ_TIME_OUT";
2932
}

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
import org.apache.http.conn.params.ConnRoutePNames;
2525
import org.apache.http.entity.StringEntity;
2626
import org.apache.http.impl.client.DefaultHttpClient;
27+
import org.apache.http.params.BasicHttpParams;
2728
import org.apache.http.params.CoreProtocolPNames;
29+
import org.apache.http.params.HttpConnectionParams;
30+
import org.apache.http.params.HttpParams;
2831
import org.apache.http.protocol.HTTP;
2932

33+
3034
/**
3135
* Transportation object used to facilitate the communication with the respective gateway.
3236
*
@@ -40,7 +44,9 @@ public class HttpClient {
4044
static boolean UseProxy = Environment.getBooleanProperty(Constants.HTTPS_USE_PROXY);
4145
static String ProxyHost = Environment.getProperty(Constants.HTTPS_PROXY_HOST);
4246
static int ProxyPort = Environment.getIntProperty(Constants.HTTPS_PROXY_PORT);
43-
47+
static int httpConnectionTimeout = Environment.getIntProperty(Constants.HTTP_CONNECTION_TIME_OUT);
48+
static int httpReadTimeout = Environment.getIntProperty(Constants.HTTP_READ_TIME_OUT);
49+
4450
static {
4551
LogHelper.info(logger, "Use Proxy: '%s'", UseProxy);
4652
}
@@ -71,17 +77,28 @@ private static HttpPost createHttpPost(Environment env, Transaction transaction)
7177
httpPost = new HttpPost(postUrl);
7278

7379
httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
80+
81+
//set the tcp connection timeout
82+
httpPost.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, httpConnectionTimeout);
83+
//set the time out on read-data request
84+
httpPost.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, httpReadTimeout);
85+
7486
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
75-
7687
httpPost.setEntity(new StringEntity(transaction.toNVPString()));
7788
} else if (transaction instanceof net.authorize.arb.Transaction ||
7889
transaction instanceof net.authorize.cim.Transaction ||
7990
transaction instanceof net.authorize.reporting.Transaction) {
8091

8192
postUrl = new URI(env.getXmlBaseUrl() + "/xml/v1/request.api");
8293
httpPost = new HttpPost(postUrl);
83-
httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
84-
httpPost.setHeader("Content-Type", "text/xml; charset=utf-8");
94+
httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
95+
96+
//set the TCP connection timeout
97+
httpPost.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, httpConnectionTimeout);
98+
//set the time out on read-data request
99+
httpPost.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, httpReadTimeout);
100+
101+
httpPost.setHeader("Content-Type", "text/xml; charset=utf-8");
85102
httpPost.setEntity(new StringEntity(transaction.toXMLString()));
86103
}
87104

0 commit comments

Comments
 (0)