Skip to content

Commit 43dbd4f

Browse files
committed
minor refactoring
1 parent 0956db7 commit 43dbd4f

7 files changed

Lines changed: 68 additions & 61 deletions

File tree

java/src/main/java/com/cybersource/ws/client/Client.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
import java.util.Properties;
3838
import java.util.concurrent.ConcurrentHashMap;
3939

40-
import static com.cybersource.ws.client.Utility.setMTIFieldIfNotExist;
41-
import static com.cybersource.ws.client.Utility.setVersionInformation;
40+
import static com.cybersource.ws.client.Utility.*;
4241

4342
/**
4443
* Class containing runTransaction() methods that accept the requests in the
@@ -181,6 +180,17 @@ public static Map<String, String> runTransaction(
181180
}
182181
}
183182

183+
/**
184+
* Sets the version information in the request.
185+
*
186+
* @param request request to set the version information in.
187+
*/
188+
private static void setVersionInformation(Map<String, String> request) {
189+
request.put(ELEM_CLIENT_LIBRARY, Utility.NVP_LIBRARY);
190+
request.put(ELEM_CLIENT_LIBRARY_VERSION, Utility.VERSION);
191+
request.put(ELEM_CLIENT_ENVIRONMENT, Utility.ENVIRONMENT);
192+
}
193+
184194
/**
185195
* Wraps the given Map object in SOAP envelope and signs it.
186196
*

java/src/main/java/com/cybersource/ws/client/HttpClientConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void logResponseHeaders() {
287287
if (responseTimeHeader != null && StringUtils.isNotBlank(responseTimeHeader.getValue())) {
288288
long resIAT = getResponseIssuedAtTimeInSecs(responseTimeHeader.getValue());
289289
if (resIAT > 0) {
290-
logger.log(Logger.LT_INFO, "responseTransitTimeSec : " + getResponseTransitTimeSeconds(resIAT));
290+
logger.log(Logger.LT_INFO, "responseTransitTimeSec : " + getResponseTransitTime(resIAT));
291291
}
292292
}
293293
List<Header> respheaders = Arrays.asList(postMethod.getResponseHeaders());

java/src/main/java/com/cybersource/ws/client/JDKHttpURLConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void logResponseHeaders() {
178178
if (StringUtils.isNotBlank(responseTime)) {
179179
long resIAT = getResponseIssuedAtTimeInSecs(responseTime);
180180
if (resIAT > 0) {
181-
logger.log(Logger.LT_INFO, "responseTransitTimeSec : " + getResponseTransitTimeSeconds(resIAT));
181+
logger.log(Logger.LT_INFO, "responseTransitTimeSec : " + getResponseTransitTime(resIAT));
182182
}
183183
}
184184
logger.log(Logger.LT_INFO, "Response headers : " + con.getHeaderFields());

java/src/main/java/com/cybersource/ws/client/PoolingHttpClientConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void logResponseHeaders() {
293293
if (responseTimeHeader != null && StringUtils.isNotBlank(responseTimeHeader.getValue())) {
294294
long resIAT = getResponseIssuedAtTimeInSecs(responseTimeHeader.getValue());
295295
if (resIAT > 0) {
296-
logger.log(Logger.LT_INFO, "responseTransitTimeSec : " + getResponseTransitTimeSeconds(resIAT));
296+
logger.log(Logger.LT_INFO, "responseTransitTimeSec : " + getResponseTransitTime(resIAT));
297297
}
298298
}
299299
List<Header> respHeaders = Arrays.asList(httpResponse.getAllHeaders());

java/src/main/java/com/cybersource/ws/client/Utility.java

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ private Utility() {
6060
public static final String ELEM_CLIENT_ENVIRONMENT = "clientEnvironment";
6161
private static long lastTick = System.currentTimeMillis();
6262
private static final Object lastTickLock = new Object();
63+
private static InetAddress addr;
64+
65+
static {
66+
try {
67+
addr = InetAddress.getLocalHost();
68+
validateIPAddress(addr);
69+
} catch (UnknownHostException e) {
70+
e.printStackTrace();
71+
}
72+
73+
}
74+
6375

6476
/**
6577
* If in the Request map, a key called "_has_escapes" is present and is set
@@ -553,16 +565,27 @@ public static long getResponseIssuedAtTimeInSecs(String responseTime) {
553565
/**
554566
* get response transit time in seconds
555567
*
556-
* @param resIssuedAtTimeSeconds
568+
* @param resIssuedAtTime
557569
* @return long
558570
*/
559-
public static long getResponseTransitTimeSeconds(long resIssuedAtTimeSeconds) {
560-
if (resIssuedAtTimeSeconds > 0) {
561-
return (System.currentTimeMillis() / 1000) - resIssuedAtTimeSeconds;
571+
public static long getResponseTransitTime(long resIssuedAtTime) {
572+
if (resIssuedAtTime > 0) {
573+
if(checkIfEpochTimeInSecs(resIssuedAtTime)) {
574+
return (System.currentTimeMillis() / 1000) - resIssuedAtTime;
575+
}else{
576+
return System.currentTimeMillis() - resIssuedAtTime;
577+
}
562578
}
563579
return 0;
564580
}
565581

582+
private static boolean checkIfEpochTimeInSecs(long resIssuedAtTime) {
583+
if(String.valueOf(resIssuedAtTime).length() == 10) {
584+
return true;
585+
}
586+
return false;
587+
}
588+
566589
private static long parseLong(String val, long defaultValue) {
567590
long result = defaultValue;
568591
if (val != null) {
@@ -575,17 +598,6 @@ private static long parseLong(String val, long defaultValue) {
575598
return result;
576599
}
577600

578-
/**
579-
* Sets the version information in the request.
580-
*
581-
* @param request request to set the version information in.
582-
*/
583-
public static void setVersionInformation(Map<String, String> request) {
584-
request.put(ELEM_CLIENT_LIBRARY, Utility.NVP_LIBRARY);
585-
request.put(ELEM_CLIENT_LIBRARY_VERSION, Utility.VERSION);
586-
request.put(ELEM_CLIENT_ENVIRONMENT, Utility.ENVIRONMENT);
587-
}
588-
589601
/**
590602
* Use this to pre-set a merchantTransactionIdentifier before sending the request.
591603
* This is a unique value for each ICSRequest. The format for the
@@ -595,7 +607,6 @@ public static void setVersionInformation(Map<String, String> request) {
595607
* number of milliseconds since the epoch (Jan 1, 1970, 00:00 UTC).
596608
* The next 10 digits is the ip address of the hostname, represented
597609
* as a 32 bit integer in decimal format.
598-
*
599610
*/
600611
public static void setMTIFieldIfNotExist(Map<String, String> request) {
601612
String mti = request.get(MERCHANT_TRANSACTION_IDENTIFIER);
@@ -605,42 +616,34 @@ public static void setMTIFieldIfNotExist(Map<String, String> request) {
605616
}
606617

607618
public static String generateMTI() {
608-
InetAddress addr;
609-
try {
610-
addr = InetAddress.getLocalHost();
611-
612-
validateIPAddress(addr);
613-
614-
BigInteger ip = new BigInteger(1, addr.getAddress());
615-
616-
// pad the ip address string to 10 characters
617-
String ipString = ip.toString();
618-
if (ipString.length() < 10) {
619-
ipString = "0000000000".substring(ipString.length()) + ipString;
620-
}
619+
if(addr == null){
620+
return null;
621+
}
622+
BigInteger ip = new BigInteger(1, addr.getAddress());
621623

622-
// trim leading characters in case it's > 10 digits long
623-
if (ipString.length() > 10) {
624-
ipString = ipString.substring(ipString.length() - 10);
625-
}
624+
// pad the ip address string to 10 characters
625+
String ipString = ip.toString();
626+
if (ipString.length() < 10) {
627+
ipString = "0000000000".substring(ipString.length()) + ipString;
628+
}
626629

627-
// pad the time string to 12 characters
628-
String timeString;
629-
timeString = String.valueOf(newTick());
630-
if (timeString.length() < 12) {
631-
timeString = "000000000000".substring(timeString.length()) + timeString;
632-
}
630+
// trim leading characters in case it's > 10 digits long
631+
if (ipString.length() > 10) {
632+
ipString = ipString.substring(ipString.length() - 10);
633+
}
633634

634-
// tim leading characters if time > 12 digits.
635-
if (timeString.length() > 12) {
636-
timeString = timeString.substring(timeString.length() - 12);
637-
}
638-
return (timeString + ipString);
639-
} catch (UnknownHostException e) {
640-
e.printStackTrace();
635+
// pad the time string to 12 characters
636+
String timeString;
637+
timeString = String.valueOf(newTick());
638+
if (timeString.length() < 12) {
639+
timeString = "000000000000".substring(timeString.length()) + timeString;
641640
}
642-
return null;
643641

642+
// tim leading characters if time > 12 digits.
643+
if (timeString.length() > 12) {
644+
timeString = timeString.substring(timeString.length() - 12);
645+
}
646+
return (timeString + ipString);
644647
}
645648

646649
private static long newTick() {
@@ -665,7 +668,7 @@ private static void validateIPAddress(InetAddress addr)
665668
throws UnknownHostException {
666669
if (addr.equals(InetAddress.getByName("127.0.0.1"))) {
667670
throw new UnknownHostException(
668-
"127.0.0.1 is not allowed. Use a different IP address or set host_id.");
671+
"127.0.0.1 is not allowed. Use a different IP address.");
669672
}
670673
}
671674
}

java/src/test/java/com/cybersource/ws/client/ClientTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@
88
import static org.junit.Assert.assertEquals;
99

1010
public class ClientTest extends BaseTest {
11-
String sampleXML = "<results>\n" +
12-
"<result>\n" +
13-
"success\n" +
14-
"</result>\n" +
15-
"</results>\n";
16-
1711
@Test
1812
public void testSetVersionInformation() throws InvocationTargetException {
1913
Class[] argClasses = {Map.class};
2014
Map<String,String> request = getSampleRequest();
2115
Object[] argObjects = {request};
2216
invokePrivateStaticMethod(Client.class, "setVersionInformation", argClasses, argObjects);
23-
assertEquals("Java Basic", request.get("clientLibrary"));
17+
assertEquals(Utility.NVP_LIBRARY, request.get("clientLibrary"));
2418
}
2519
}

java/src/test/java/com/cybersource/ws/client/SecurityUtilIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void testMerchantIdentityToKeyStore() throws Exception{
180180
}
181181

182182
@Test
183-
public void testcertificateCacheEnabled() throws Exception{
183+
public void testCertificateCacheEnabled() throws Exception{
184184
merchantProperties.setProperty("keyFilename", merchantProperties.getProperty("keyAlias") + ".p12");
185185
merchantProperties.setProperty("certificateCacheEnabled", "true");
186186
// caching enabled (default)

0 commit comments

Comments
 (0)