Skip to content

Commit be0792f

Browse files
committed
Fixed performance issue in case of multiple merchantIDs, p12 was getting loaded for every request. now p12 will be loaded once per merchant
1 parent bbe61dd commit be0792f

21 files changed

Lines changed: 79 additions & 54 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To install the cybersource-sdk-java from central repository,add dependency to yo
1111
<dependency>
1212
<groupId>com.cybersource</groupId>
1313
<artifactId>cybersource-sdk-java</artifactId>
14-
<version>6.2.2</version>
14+
<version>6.2.3</version>
1515
</dependency>
1616
````
1717
Run mvn install, to install dependency
@@ -20,7 +20,7 @@ To install the cybersource-sdk-java from central repository,add dependency to yo
2020
Add the dependency to your build.gradle
2121
````
2222
dependencies {
23-
compile 'com.cybersource:cybersource-sdk-java:6.2.2'
23+
compile 'com.cybersource:cybersource-sdk-java:6.2.3'
2424
}
2525
````
2626
##Requirements

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,24 @@
1414
*/
1515
public class BaseMessageHandler {
1616

17-
MessageHandlerKeyStore localKeyStoreHandler = null;
17+
static MessageHandlerKeyStore localKeyStoreHandler = null;
1818
Logger logger = null;
19+
static KeyStore keyStore;
20+
21+
static {
22+
try {
23+
initKeystore();
24+
} catch (Exception e) {
25+
localKeyStoreHandler=null;
26+
}
27+
}
28+
1929
BaseMessageHandler(Logger logger) throws SignEncryptException {
2030
if (logger != null) this.logger = logger;
21-
2231
try {
23-
localKeyStoreHandler = new MessageHandlerKeyStore(logger);
24-
KeyStore keyStore = KeyStore.getInstance("jks");
25-
keyStore.load(null, null);
26-
localKeyStoreHandler.setKeyStore(keyStore);
32+
if(localKeyStoreHandler==null){
33+
initKeystore();
34+
}
2735
} catch (CredentialException e) {
2836
throw new SignEncryptException("BaseMessageHandler, " +
2937
"cannot instantiate class with keystore error.", e);
@@ -42,5 +50,13 @@ public class BaseMessageHandler {
4250
}
4351
}
4452

45-
public void addIdentityToKeyStore( Identity id ) throws SignEncryptException { localKeyStoreHandler.addIdentityToKeyStore(id);}
53+
private static void initKeystore() throws KeyStoreException, CredentialException, IOException, NoSuchAlgorithmException, CertificateException{
54+
keyStore = KeyStore.getInstance("jks");
55+
keyStore.load(null, null);
56+
localKeyStoreHandler = new MessageHandlerKeyStore();
57+
localKeyStoreHandler.setKeyStore(keyStore);
58+
59+
}
60+
61+
public void addIdentityToKeyStore(Identity id , Logger logger) throws SignEncryptException { localKeyStoreHandler.addIdentityToKeyStore(id,logger);}
4662
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,14 @@ private static Document soapWrapAndSign(
193193
sr.close();
194194

195195
Document resultDocument = null;
196+
long timeNow = System.currentTimeMillis();
197+
logger.log(Logger.LT_INFO, "Client, Start of getInstance Call, Timer Start in ms " + timeNow + "For merchant " + mc.getMerchantID());
198+
196199
SignedAndEncryptedMessageHandler handler = SignedAndEncryptedMessageHandler.getInstance(mc,logger);
197200

201+
long endTime = System.currentTimeMillis();
202+
logger.log(Logger.LT_INFO, "Client, End of getInstance Call, time taken in ms " + (endTime-timeNow) + "For merchant " + mc.getMerchantID());
203+
198204
// 3/7/2016 change to support encrypted messages as well as signed - jeaton
199205
if ( !mc.getUseSignAndEncrypted() ) {
200206
// sign Document object

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
public class Identity {
2323

2424
//Our p12 files do not contain an alias as a normal name, its the common name and serial number
25-
public String name = null;
25+
private String name = null;
2626

2727
// we need to create alias for our keystores, it looks like "serialNumber=4032987129910179089277,CN=jasoneatoncorp"
28-
protected String keyAlias = null;
28+
private String keyAlias = null;
2929

3030
// for an unknown reason the serial number of the certificate is set incorrectly, we must parse it from DN
31-
protected String serialNumber = null;
31+
private String serialNumber = null;
3232

33-
protected X509Certificate x509Cert =null;
33+
private X509Certificate x509Cert =null;
3434

35-
protected PrivateKey privateKey = null;
35+
private PrivateKey privateKey = null;
3636

37-
protected MerchantConfig merchantConfig = null;
37+
private MerchantConfig merchantConfig = null;
3838

3939
private static final String SERVER_ALIAS = "CyberSource_SJC_US";
4040

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@
1313
*/
1414
public class MessageHandlerKeyStore extends Merlin {
1515

16-
Logger logger = null;
17-
18-
public MessageHandlerKeyStore(Logger logger) throws CredentialException, IOException {
16+
public MessageHandlerKeyStore() throws CredentialException, IOException {
1917
super(null);
2018
properties = new Properties();
21-
this.logger = logger;
2219
}
2320

24-
25-
26-
public void addIdentityToKeyStore(Identity id) throws SignEncryptException {
21+
public void addIdentityToKeyStore(Identity id, Logger logger) throws SignEncryptException {
2722
if (id == null)
2823
return;
2924
X509Certificate certificate = id.getX509Cert();

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.util.ArrayList;
2222
import java.util.Collections;
2323
import java.util.Enumeration;
24+
import java.util.HashSet;
2425
import java.util.List;
26+
import java.util.Set;
2527

2628

2729
/**
@@ -31,9 +33,9 @@ public class SignedAndEncryptedMessageHandler extends BaseMessageHandler {
3133

3234
private static final String KEY_FILE_TYPE = "PKCS12";
3335

34-
public static List<Identity> identities = new ArrayList<Identity>();
36+
private List<Identity> identities = new ArrayList<Identity>();
3537

36-
private static String currentMerchantId = null;
38+
private static Set<String> currentMerchantId = new HashSet<String>();
3739

3840
private static final String SERVER_ALIAS = "CyberSource_SJC_US";
3941

@@ -52,7 +54,7 @@ private SignedAndEncryptedMessageHandler(MerchantConfig merchantConfig, Logger l
5254
// load keystore from disk p12 file (not keystore)
5355
loadMerchantP12File(merchantConfig, logger);
5456
for(int pos=0;pos<identities.size();pos++) {
55-
localKeyStoreHandler.addIdentityToKeyStore(identities.get(pos));
57+
localKeyStoreHandler.addIdentityToKeyStore(identities.get(pos),logger);
5658
}
5759
}
5860

@@ -69,10 +71,10 @@ static SignedAndEncryptedMessageHandler getInstance(MerchantConfig merchantConfi
6971
* @param logger - logger instance
7072
* @throws SignException - Signature exception
7173
*/
72-
private static void loadMerchantP12File(MerchantConfig merchantConfig, Logger logger) throws SignException {
74+
private void loadMerchantP12File(MerchantConfig merchantConfig, Logger logger) throws SignException {
7375
// Load the KeyStore and get the signing key and certificate do this once only
7476
// This change is made based on the assumptions that at point of time , a merchant will have only one P12 Key
75-
if( !merchantConfig.getMerchantID().equals(currentMerchantId)){
77+
if(!currentMerchantId.contains(merchantConfig.getMerchantID())){
7678
readAndStoreCertificateAndPrivateKey( merchantConfig, logger);
7779
}
7880
}
@@ -84,7 +86,7 @@ private static void loadMerchantP12File(MerchantConfig merchantConfig, Logger lo
8486
* @throws SignException
8587
*/
8688

87-
private static void readAndStoreCertificateAndPrivateKey(
89+
private void readAndStoreCertificateAndPrivateKey(
8890
MerchantConfig merchantConfig, Logger logger) throws SignException {
8991
KeyStore merchantKeyStore;
9092
try {
@@ -142,7 +144,7 @@ private static void readAndStoreCertificateAndPrivateKey(
142144
logger.log(Logger.LT_EXCEPTION, "No valid entries found in the KeyStore, check alias, '" + merchantConfig.getKeyAlias() + "'");
143145
throw new SignException("No valid entries found in the KeyStore, check alias, '" + merchantConfig.getKeyAlias() + "'");
144146
}
145-
currentMerchantId = merchantConfig.getMerchantID();
147+
currentMerchantId.add(merchantConfig.getMerchantID());
146148
} catch (KeyStoreException e) {
147149
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
148150
throw new SignException(e);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private Utility() {
4848
/**
4949
* Version number of this release.
5050
*/
51-
public static final String VERSION = "6.2.2";
51+
public static final String VERSION = "6.2.3";
5252

5353
/**
5454
* If in the Request map, a key called "_has_escapes" is present and is set

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,14 @@ private static Document soapWrapAndSign(
362362
}
363363

364364
Document resultDocument = null;
365+
long timeNow = System.currentTimeMillis();
366+
logger.log(Logger.LT_INFO, "Client, Start of getInstance Call, Timer Start in ms " + timeNow + "For merchant " + mc.getMerchantID());
367+
365368
SignedAndEncryptedMessageHandler handler = SignedAndEncryptedMessageHandler.getInstance(mc,logger);
366369

370+
long endTime = System.currentTimeMillis();
371+
logger.log(Logger.LT_INFO, "Client, End of getInstance Call, time taken in ms " + (endTime-timeNow) + "For merchant " + mc.getMerchantID());
372+
367373
// 3/7/2016 change to support encrypted messages as well as signed - jeaton
368374
if ( !mc.getUseSignAndEncrypted() ) {
369375
// sign wrapped Document object

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public void testServerIdentityToKeyStore() throws Exception{
2525
Mockito.when(identity.getX509Cert()).thenReturn(x509Cert);
2626
Mockito.when(identity.getName()).thenReturn("MahenCertTest");
2727

28-
MessageHandlerKeyStore mhKeyStore= new MessageHandlerKeyStore(logger);
28+
MessageHandlerKeyStore mhKeyStore= new MessageHandlerKeyStore();
2929

3030
MessageHandlerKeyStore spyMhKeyStore = Mockito.spy(mhKeyStore);
3131
Mockito.when(spyMhKeyStore.getKeyStore()).thenReturn(myKeystore);
32-
spyMhKeyStore.addIdentityToKeyStore(identity);
32+
spyMhKeyStore.addIdentityToKeyStore(identity,logger);
3333
Mockito.verify(spyMhKeyStore).getKeyStore();
3434
}
3535

@@ -48,10 +48,10 @@ public void testMerchantIdentityToKeyStore() throws Exception{
4848
Mockito.when(identity.getX509Cert()).thenReturn(x509Cert);
4949
Mockito.when(identity.getName()).thenReturn("MahenCertTest");
5050

51-
MessageHandlerKeyStore mhKeyStore= new MessageHandlerKeyStore(logger);
51+
MessageHandlerKeyStore mhKeyStore= new MessageHandlerKeyStore();
5252
MessageHandlerKeyStore spyMhKeyStore = Mockito.spy(mhKeyStore);
5353
Mockito.when(spyMhKeyStore.getKeyStore()).thenReturn(myKeystore);
54-
spyMhKeyStore.addIdentityToKeyStore(identity);
54+
spyMhKeyStore.addIdentityToKeyStore(identity,logger);
5555
}
5656

5757
private static PrivateKey instPrivateKey(PrivateKey pkey) throws Exception{

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<groupId>com.cybersource</groupId>
2424
<artifactId>cybersource-sdk-master</artifactId>
25-
<version>6.2.2</version>
25+
<version>6.2.3</version>
2626

2727

2828
</project>

0 commit comments

Comments
 (0)