@@ -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}
0 commit comments