Skip to content

Commit 3c2c610

Browse files
committed
readme changes and added staleconnecttion check
1 parent 6b2899d commit 3c2c610

7 files changed

Lines changed: 116 additions & 19 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,24 @@ Version Cybersource-sdk-java 6.1.0 (Feb 24,2016)
296296
_______________________________
297297
1) SHA256 changes which are required to signed the request with SHA256.
298298

299+
## Troubleshooting
300+
- If you get an exception **java.lang.SecurityException: JCE cannot authenticate the provider BC**. This could be because of
301+
many reasons. bcprov*.jar is a signed jar if java fails to validate the signature, it throws this exception. Make sure
302+
you run below java command to verify this signature.
303+
jarsigner -verify bcprov-jdk15on-1.61.jar
304+
when above command fails it says "jar is unsigned. (signatures missing or not parsable)", this could be because of many
305+
reasons. e.g
306+
1) When we unpack it and include in our own jar file. Including bcprov*.jar separately in the CLASSPATH should solve this issue.
307+
2) May be changes in Oracle jar signer. If using Java SDK 1.6 or 1.7 with cybersource-sdk-java:6.2.7 and higher
308+
(ships with org.bouncycastle:bcprov-jdk15on:1.61). Upgrading version to bcprov-jdk15to18-1.63.jar should solve this issue.
309+
3) If you are using some old version of JBOSS and have copied bcprov*.jar under $JBOSS_HOME/server/default/lib/.
310+
copying bcprov*.jar in $JBOSS_HOME/server/default/lib/ instead of $JBOSS_HOME/server/servername/lib/ should solve this issue.
311+
312+
- If you get an exception **exception decrypting data - java.security.InvalidKeyException: Illegal key size**.
313+
It is recommended to download Unlimited Strength Jurisdiction Policy files from Oracle (US_export_policy.jar and local_policy.jar)
314+
for appropriate JAVA version. I meant if merchant are using java 6 then download these policy file only for java6.
315+
You need to copy security jars (US_export_policy.jar, local_policy.jar) in the $JAVA_HOME/jre/lib/security directory not in $JAVA_HOME/jre/lib/ext/).
316+
299317
## Documentation
300318
- For more information about CyberSource services, see <http://www.cybersource.com/developers/documentation>.
301319
- For all other support needs, see <http://www.cybersource.com/support>.

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public class MerchantConfig {
7878
private boolean customHttpClassEnabled;
7979
private boolean certificateCacheEnabled;
8080
private boolean merchantConfigCacheEnabled;
81+
private boolean staleConnectionCheckEnabled;
8182
private boolean shutdownHookEnabled;
82-
8383
public String getcustomHttpClass() {
8484
return customHttpClass;
8585
}
@@ -381,6 +381,14 @@ public boolean isMerchantConfigCacheEnabled() {
381381
return merchantConfigCacheEnabled;
382382
}
383383

384+
/**
385+
* Getter method for staleConnectionCheckEnabled
386+
* @return boolean
387+
*/
388+
public boolean isStaleConnectionCheckEnabled() {
389+
return staleConnectionCheckEnabled;
390+
}
391+
384392
/**
385393
* Getter method for shutdownHookEnabled
386394
* @return boolean
@@ -534,52 +542,77 @@ public MerchantConfig(Properties _props, String _merchantID)
534542
throw new ConfigException("maxConnections property is empty");
535543
} else {
536544
maxConnections = getIntegerProperty(merchantID, "maxConnections");
545+
if(maxConnections <= 0){
546+
throw new ConfigException("maxConnections property can't be 0 or negative");
547+
}
537548
}
538549

539550
if(StringUtils.isEmpty(getProperty(merchantID, "defaultMaxConnectionsPerRoute"))) {
540551
throw new ConfigException("defaultMaxConnectionsPerRoute property is empty");
541552
} else {
542553
defaultMaxConnectionsPerRoute = getIntegerProperty(merchantID, "defaultMaxConnectionsPerRoute");
554+
if(defaultMaxConnectionsPerRoute <= 0){
555+
throw new ConfigException("defaultMaxConnectionsPerRoute property can't be 0 or negative");
556+
}
543557
}
544558

545559
if(StringUtils.isEmpty(getProperty(merchantID, "maxConnectionsPerRoute"))) {
546560
throw new ConfigException("maxConnectionsPerRoute property is empty");
547561
} else {
548562
maxConnectionsPerRoute = getIntegerProperty(merchantID, "maxConnectionsPerRoute");
563+
if(maxConnectionsPerRoute <= 0){
564+
throw new ConfigException("maxConnectionsPerRoute property can't be 0 or negative");
565+
}
549566
}
550567

551568
if(StringUtils.isEmpty(getProperty(merchantID, "connectionRequestTimeoutMs"))) {
552569
throw new ConfigException("connectionRequestTimeoutMs property is empty");
553570
} else {
554571
connectionRequestTimeoutMs = getIntegerProperty(merchantID, "connectionRequestTimeoutMs");
572+
if(connectionRequestTimeoutMs <= 0){
573+
throw new ConfigException("connectionRequestTimeoutMs property can't be 0 or negative");
574+
}
555575
}
556576

557577
if(StringUtils.isEmpty(getProperty(merchantID, "connectionTimeoutMs"))) {
558578
throw new ConfigException("connectionTimeoutMs property is empty");
559579
} else {
560580
connectionTimeoutMs = getIntegerProperty(merchantID, "connectionTimeoutMs");
581+
if(connectionTimeoutMs <= 0){
582+
throw new ConfigException("connectionTimeoutMs property can't be 0 or negative");
583+
}
561584
}
562585

563586
if(StringUtils.isEmpty(getProperty(merchantID, "socketTimeoutMs"))) {
564587
throw new ConfigException("socketTimeoutMs property is empty");
565588
} else {
566589
socketTimeoutMs = getIntegerProperty(merchantID, "socketTimeoutMs");
590+
if(socketTimeoutMs <= 0){
591+
throw new ConfigException("socketTimeoutMs property can't be 0 or negative");
592+
}
567593
}
568594

569595
if(StringUtils.isEmpty(getProperty(merchantID, "evictThreadSleepTimeMs"))) {
570596
throw new ConfigException("evictThreadSleepTimeMs property is empty");
571597
} else {
572598
evictThreadSleepTimeMs = getIntegerProperty(merchantID, "evictThreadSleepTimeMs");
599+
if(evictThreadSleepTimeMs <= 0){
600+
throw new ConfigException("evictThreadSleepTimeMs property can't be 0 or negative");
601+
}
573602
}
574603

575604
if(StringUtils.isEmpty(getProperty(merchantID, "maxKeepAliveTimeMs"))) {
576605
throw new ConfigException("maxKeepAliveTimeMs property is empty");
577606
} else {
578607
maxKeepAliveTimeMs = getIntegerProperty(merchantID, "maxKeepAliveTimeMs");
608+
if(maxKeepAliveTimeMs <= 0){
609+
throw new ConfigException("maxKeepAliveTimeMs property can't be 0 or negative");
610+
}
579611
}
580612

581-
validateAfterInactivityMs = getIntegerProperty(merchantID, "validateAfterInactivityMs", 2000);
582-
shutdownHookEnabled =getBooleanProperty(merchantID, "enabledShutdownHook", true);
613+
validateAfterInactivityMs = getIntegerProperty(merchantID, "validateAfterInactivityMs", 0);
614+
staleConnectionCheckEnabled = getBooleanProperty(merchantID, "staleConnectionCheckEnabled", true);
615+
shutdownHookEnabled = getBooleanProperty(merchantID, "enabledShutdownHook", true);
583616
}
584617

585618
allowRetry = getBooleanProperty(merchantID, "allowRetry", true);
@@ -772,6 +805,7 @@ public String getLogString() {
772805
appendPair(sb, "evictThreadSleepTimeMs", evictThreadSleepTimeMs);
773806
appendPair(sb, "maxKeepAliveTimeMs", maxKeepAliveTimeMs);
774807
appendPair(sb, "validateAfterInactivityMs", validateAfterInactivityMs);
808+
appendPair(sb, "staleConnectionCheckEnabled", staleConnectionCheckEnabled);
775809
appendPair(sb, "enabledShutdownHook", shutdownHookEnabled);
776810
}
777811
appendPair(sb, "timeout", timeout);

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Arrays;
3333
import java.util.Collections;
3434
import java.util.List;
35+
import java.util.concurrent.TimeUnit;
3536

3637
import static com.cybersource.ws.client.Utility.*;
3738

@@ -44,6 +45,7 @@ public class PoolingHttpClientConnection extends Connection {
4445
private HttpPost httpPost = null;
4546
private HttpClientContext httpContext = null;
4647
private CloseableHttpResponse httpResponse = null;
48+
4749
private static CloseableHttpClient httpClient = null;
4850
private static IdleConnectionMonitorThread staleMonitorThread;
4951
private final static String STALE_CONNECTION_MONITOR_THREAD_NAME = "http-stale-connection-cleaner-thread";
@@ -107,6 +109,8 @@ protected void initHttpClient(MerchantConfig merchantConfig, PoolingHttpClientCo
107109
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
108110
.setSocketTimeout(merchantConfig.getSocketTimeoutMs())
109111
.setConnectionRequestTimeout(merchantConfig.getConnectionRequestTimeoutMs())
112+
//This we added to check every connection before leasing.
113+
.setStaleConnectionCheckEnabled(merchantConfig.isStaleConnectionCheckEnabled())
110114
.setConnectTimeout(merchantConfig.getConnectionTimeoutMs());
111115

112116
HttpClientBuilder httpClientBuilder = HttpClients.custom()
@@ -209,6 +213,12 @@ public static void onShutdown() throws IOException {
209213
if (staleMonitorThread != null && staleMonitorThread.isAlive()) {
210214
staleMonitorThread.shutdown();
211215
}
216+
//wait before shutdown.
217+
try {
218+
Thread.sleep(1000);
219+
} catch (InterruptedException var4) {
220+
Thread.currentThread().interrupt();
221+
}
212222
}
213223

214224
/**
@@ -323,22 +333,6 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
323333
return false;
324334
}
325335

326-
if (exception instanceof NoHttpResponseException) {
327-
System.out.println("not retrying as it is NoHttpResponseException and request is sent");
328-
return false;
329-
}
330-
331-
// if (exception instanceof NoHttpResponseException) {
332-
// System.out.println("retrying as it NoHttpResponseException and request is not sent");
333-
// return true;
334-
// }
335-
//
336-
// if (exception instanceof java.net.SocketException && (exception.getMessage().equalsIgnoreCase("Connection reset") ||
337-
// exception.getLocalizedMessage().equalsIgnoreCase("Connection reset"))) {
338-
//
339-
// System.out.println("retrying for connection reset");
340-
// return true;
341-
// }
342336
HttpClientContext httpClientContext = HttpClientContext.adapt(httpContext);
343337
if (!httpClientContext.isRequestSent()) {
344338
try {
@@ -349,6 +343,12 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
349343
}
350344
return true;
351345
}
346+
347+
if (exception instanceof NoHttpResponseException) {
348+
System.out.println("not retrying as it is NoHttpResponseException and request is sent");
349+
return false;
350+
}
351+
352352
return false;
353353
}
354354
}

samples/nvp/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
</dependencies>
6868
<build>
6969
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-compiler-plugin</artifactId>
73+
<version>3.1</version>
74+
<configuration>
75+
<source>1.6</source>
76+
<target>1.6</target>
77+
</configuration>
78+
</plugin>
7079
<plugin>
7180
<groupId>org.codehaus.mojo</groupId>
7281
<artifactId>exec-maven-plugin</artifactId>

samples/xml/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
</dependencies>
6868
<build>
6969
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-compiler-plugin</artifactId>
73+
<version>3.1</version>
74+
<configuration>
75+
<source>1.6</source>
76+
<target>1.6</target>
77+
</configuration>
78+
</plugin>
7079
<plugin>
7180
<groupId>org.codehaus.mojo</groupId>
7281
<artifactId>exec-maven-plugin</artifactId>

zip/README

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ Version Cybersource-sdk-java 6.1.0 (Feb 24,2016)
315315
_______________________________
316316
1) SHA256 changes which are required to signed the request with SHA256.
317317

318+
##Troubleshooting
319+
- If you get an exception **java.lang.SecurityException: JCE cannot authenticate the provider BC**. This could be because of
320+
many reasons. bcprov*.jar is a signed jar if java fails to validate the signature, it throws this exception. Make sure
321+
you run below java command to verify this signature.
322+
jarsigner -verify bcprov-jdk15on-1.61.jar
323+
when above command fails it says "jar is unsigned. (signatures missing or not parsable)", this could be because of many
324+
reasons. e.g
325+
1) When we unpack it and include in our own jar file. Including bcprov*.jar separately in the CLASSPATH should solve this issue.
326+
2) May be changes in Oracle jar signer. If using Java SDK 1.6 or 1.7 with cybersource-sdk-java:6.2.7 and higher
327+
(ships with org.bouncycastle:bcprov-jdk15on:1.61). Upgrading version to bcprov-jdk15to18-1.63.jar should solve this issue.
328+
3) If you are using some old version of JBOSS and have copied bcprov*.jar under $JBOSS_HOME/server/default/lib/.
329+
copying bcprov*.jar in $JBOSS_HOME/server/default/lib/ instead of $JBOSS_HOME/server/servername/lib/ should solve this issue.
330+
331+
- If you get an exception **exception decrypting data - java.security.InvalidKeyException: Illegal key size**.
332+
It is recommended to download Unlimited Strength Jurisdiction Policy files from Oracle (US_export_policy.jar and local_policy.jar)
333+
for appropriate JAVA version. I meant if merchant are using java 6 then download these policy file only for java6.
334+
You need to copy security jars (US_export_policy.jar, local_policy.jar) in the $JAVA_HOME/jre/lib/security directory not in $JAVA_HOME/jre/lib/ext/).
335+
318336
##Documentation
319337

320338
For more information about CyberSource services, see <http://www.cybersource.com/developers/documentation>

zip/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515

1616
<build>
1717
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>3.1</version>
22+
<configuration>
23+
<source>1.6</source>
24+
<target>1.6</target>
25+
</configuration>
26+
</plugin>
1827
<plugin>
1928
<artifactId>maven-assembly-plugin</artifactId>
2029
<version>2.3</version>

0 commit comments

Comments
 (0)