Skip to content

Commit 6167659

Browse files
authored
Merge pull request #130 from mahendya1002/future
added sysout for debugging
2 parents 266a4ac + 1841809 commit 6167659

11 files changed

Lines changed: 79 additions & 115 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ openssl pkcs12 -export -certfile CyberSourceCertAuth.crt -in <Merchant_ID>.crt -
124124

125125
- Create JKS from p12 using keytool
126126
```
127-
keytool -importkeystore -destkeystore <Your_keystore_name> -deststorepass <your_password> -srckeystore identity.p12 -srcstoretype PKCS12 -srcstorepass <Merchant_ID>
127+
keytool -importkeystore -destkeystore <Your_keystore_name>.jks -deststorepass <your_password> -srckeystore identity.p12 -srcstoretype PKCS12 -srcstorepass <Merchant_ID>
128128
```
129129
- Now import the CyberSource_SJC_US.crt to your keystore
130130
```
@@ -183,10 +183,12 @@ Retry Pattern allows to retry sending a failed request and it will only work wit
183183

184184
## Changes
185185

186-
Version Cybersource-sdk-java 6.2.8 (SEPT,2019)
186+
Version Cybersource-sdk-java 6.2.8 (OCT,2019)
187187
_______________________________
188188

189189
1)Added request header and logged request and response headers
190+
191+
2)Caching of certificate is done using keyAlias earlier it was done using merchant_id
190192

191193
Version Cybersource-sdk-java 6.2.7 (MAR,2019)
192194
_______________________________

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,19 @@
1919
package com.cybersource.ws.client;
2020

2121

22-
import org.apache.ws.security.util.XMLUtils;
2322
import org.w3c.dom.Document;
2423
import org.w3c.dom.Node;
2524
import org.w3c.dom.Text;
2625
import org.xml.sax.InputSource;
2726
import org.xml.sax.SAXException;
2827

29-
import javax.security.cert.X509Certificate;
3028
import javax.xml.parsers.DocumentBuilder;
3129
import javax.xml.parsers.ParserConfigurationException;
32-
33-
import java.io.File;
34-
import java.io.FileInputStream;
3530
import java.io.IOException;
3631
import java.io.StringReader;
3732
import java.lang.reflect.InvocationTargetException;
38-
import java.security.KeyStore;
39-
import java.security.cert.PKIXParameters;
40-
import java.security.cert.TrustAnchor;
4133
import java.text.MessageFormat;
4234
import java.util.HashMap;
43-
import java.util.Iterator;
4435
import java.util.Map;
4536
import java.util.Properties;
4637

@@ -49,8 +40,6 @@
4940
* form of a Map object.
5041
*/
5142
public class Client {
52-
private static final String HEADER_FORMAT = "{0}={1}";
53-
5443
private static final String SOAP_ENVELOPE1 = "<soap:Envelope xmlns:soap=\"" +
5544
"http://schemas.xmlsoap.org/soap/envelope/\">\n<soap:Body id=\"body1\">\n" +
5645
"<nvpRequest xmlns=\"{0}\">\n{1}</nvpRequest>" +
@@ -99,8 +88,6 @@ public static Map runTransaction(
9988
MerchantConfig mc;
10089
LoggerWrapper logger = null;
10190
Connection con = null;
102-
103-
10491
try {
10592
setVersionInformation(request);
10693

@@ -229,16 +216,16 @@ private static Document soapWrapAndSign(
229216
+ mapToString(request, true, PCI.REQUEST));
230217
}
231218

232-
Document wrappedDoc = soapWrap(request, mc, builder,logger);
219+
Document wrappedDoc = soapWrap(request, mc, builder);
233220
logger.log(Logger.LT_INFO, "Client, End of soapWrap ",true);
234221

235-
Document resultDocument = null;
222+
Document resultDocument;
236223

237224
SecurityUtil.loadMerchantP12File(mc,logger);
238225
logger.log(Logger.LT_INFO, "Client, End of loading Merchant Certificates ", true);
239226

240227
// sign Document object
241-
resultDocument = SecurityUtil.createSignedDoc(wrappedDoc, mc.getMerchantID(), mc.getKeyPassword(), logger);
228+
resultDocument = SecurityUtil.createSignedDoc(wrappedDoc, mc.getKeyAlias(), mc.getKeyPassword(), logger);
242229
logger.log(Logger.LT_INFO, "Client, End of createSignedDoc ", true);
243230

244231
if ( mc.getUseSignAndEncrypted() ) {
@@ -254,7 +241,7 @@ private static Document soapWrapAndSign(
254241
return resultDocument ;
255242
}
256243

257-
private static Document soapWrap(Map request, MerchantConfig mc, DocumentBuilder builder, LoggerWrapper logger) throws SAXException, IOException{
244+
private static Document soapWrap(Map request, MerchantConfig mc, DocumentBuilder builder) throws SAXException, IOException{
258245
// wrap in SOAP envelope
259246
Object[] arguments
260247
= {mc.getEffectiveNamespaceURI(),

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.HttpURLConnection;
3636
import java.net.MalformedURLException;
3737
import java.net.ProtocolException;
38+
import java.util.concurrent.TimeUnit;
3839

3940

4041
/**
@@ -123,7 +124,7 @@ public Document post(Document request)
123124
private void checkForFault()
124125
throws FaultException, ClientException {
125126
try {
126-
logger.log(Logger.LT_INFO, "Reading response...");
127+
logger.log(Logger.LT_INFO, "waiting for response...");
127128
int responseCode = getHttpResponseCode();
128129
logResponseHeaders();
129130
// if successful, there's nothing left to do here.
@@ -192,7 +193,10 @@ private void checkForFault()
192193
private Document parseReceivedDocument()
193194
throws IOException, SAXException {
194195
logger.log(Logger.LT_INFO, "Parsing response...");
195-
return builder.parse(getResponseStream());
196+
//long startTime = System.nanoTime();
197+
Document document = builder.parse(getResponseStream());
198+
//System.out.println("Connection.parseReceivedDocument time taken to parse the response is " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) + " ms");
199+
return document;
196200
}
197201

198202
/**

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,16 @@
2626
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
2727
import org.apache.commons.httpclient.params.HttpMethodParams;
2828
import org.w3c.dom.Document;
29+
2930
import javax.xml.parsers.DocumentBuilder;
3031
import javax.xml.transform.TransformerConfigurationException;
3132
import javax.xml.transform.TransformerException;
3233
import java.io.ByteArrayOutputStream;
3334
import java.io.IOException;
3435
import java.io.InputStream;
35-
import java.net.MalformedURLException;
36-
import java.net.ProtocolException;
37-
import java.util.ArrayList;
38-
import java.util.List;
39-
import java.util.List;
40-
import org.apache.commons.httpclient.Header;
4136
import java.util.ArrayList;
4237
import java.util.Arrays;
38+
import java.util.List;
4339
/**
4440
* Class helps in posting the Request document for the Transaction using HttpClient.
4541
* Converts the document to String format and also helps in setting up the Proxy connections.
@@ -59,9 +55,7 @@ class HttpClientConnection extends Connection {
5955
* @see com.cybersource.ws.client.Connection#postDocument(org.w3c.dom.Document)
6056
*/
6157
void postDocument(Document request)
62-
throws IOException, TransformerConfigurationException,
63-
TransformerException, MalformedURLException,
64-
ProtocolException {
58+
throws IOException, TransformerException {
6559

6660
/*
6761
* SimpleHttpConnectionManager(boolean alwaysClose) :
@@ -108,8 +102,7 @@ public void release() {
108102
/* (non-Javadoc)
109103
* @see com.cybersource.ws.client.Connection#getHttpResponseCode()
110104
*/
111-
int getHttpResponseCode()
112-
throws IOException {
105+
int getHttpResponseCode(){
113106
return postMethod != null ? postMethod.getStatusCode() : -1;
114107
}
115108

@@ -225,7 +218,6 @@ public boolean retryMethod(
225218
Thread.sleep(retryWaitInterval);
226219
logger.log( Logger.LT_INFO+" Retrying Request -- ",mc.getUniqueKey().toString()+ " Retry Count -- "+executionCount);
227220
} catch (InterruptedException e) {
228-
// TODO Auto-generated catch block
229221
e.printStackTrace();
230222
}
231223
return true;

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class Identity {
4343

4444
private char[] pswd;
4545

46-
private Logger logger = null;
47-
4846
/**
4947
* Creates an Identity instance.this type of the instance can
5048
* only be used to store server certificate identity.
@@ -56,18 +54,15 @@ public class Identity {
5654
public Identity(MerchantConfig merchantConfig,X509Certificate x509Certificate,Logger logger) throws SignException {
5755
this.merchantConfig = merchantConfig;
5856
this.x509Cert=x509Certificate;
59-
if(this.logger == null){
60-
this.logger=logger;
61-
}
6257
if(merchantConfig.isJdkCertEnabled() || merchantConfig.isCacertEnabled()){
6358
setupJdkServerCerts();
6459
}
6560
else{
6661
setUpServer();
6762
}
6863
}
64+
6965
private void setupJdkServerCerts() throws SignException {
70-
7166
if (x509Cert != null) {
7267
String subjectDN = x509Cert.getSubjectDN().getName();
7368
if (subjectDN != null) {
@@ -82,7 +77,6 @@ else if (subjectDNrray.length == 2 && subjectDNrray[1].contains(SERVER_ALIAS)) {
8277
}else{
8378
throw new SignException("Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
8479
}
85-
8680
} else {
8781
throw new SignException("Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
8882
}
@@ -99,17 +93,13 @@ else if (subjectDNrray.length == 2 && subjectDNrray[1].contains(SERVER_ALIAS)) {
9993
* @param privateKey
10094
* @throws SignException
10195
*/
102-
public Identity(MerchantConfig merchantConfig,X509Certificate x509Certificate, PrivateKey privateKey,Logger logger) throws SignException {
96+
public Identity(MerchantConfig merchantConfig,X509Certificate x509Certificate, PrivateKey privateKey, Logger logger) throws SignException {
10397
this.merchantConfig = merchantConfig;
10498
this.x509Cert = x509Certificate;
10599
this.privateKey = privateKey;
106-
if(this.logger == null){
107-
this.logger=logger;
108-
}
109100
try {
110101
this.lastModifiedDate=merchantConfig.getKeyFile().lastModified();
111102
} catch (ConfigException e) {
112-
113103
logger.log(Logger.LT_EXCEPTION,
114104
"Identity object ,cannot instantiate with key file lastModifiedDate. "
115105
+ e.getMessage());
@@ -123,7 +113,7 @@ public Identity(MerchantConfig merchantConfig,X509Certificate x509Certificate, P
123113
* else isValid method will return true and certificate reload will not occur.
124114
*/
125115

126-
public boolean isValid(File keyFile) {
116+
public boolean isValid(File keyFile, Logger logger) {
127117
boolean changeKeyFileStatus=(this.lastModifiedDate == keyFile.lastModified());
128118
if (!changeKeyFileStatus) {
129119
logger.log(Logger.LT_INFO, "Key file changed");
@@ -136,14 +126,14 @@ private void setUpMerchant() throws SignException {
136126
if (serialNumber == null && x509Cert != null) {
137127
String subjectDN = x509Cert.getSubjectDN().getName();
138128
if (subjectDN != null) {
139-
String subjectDNrray[] = subjectDN.split("SERIALNUMBER=");
129+
String[] subjectDNrray = subjectDN.split("SERIALNUMBER=");
140130
if (subjectDNrray.length != 2) {
141131
throw new SignException("Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
142132
}
143133
name = merchantConfig.getMerchantID();
144134
pswd = merchantConfig.getKeyPassword().toCharArray();
145135
serialNumber = subjectDNrray[1];
146-
keyAlias = "serialNumber=" + serialNumber + ",CN=" + name;
136+
keyAlias = merchantConfig.getKeyAlias();
147137
} else {
148138
throw new SignException("Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
149139
}
@@ -155,7 +145,7 @@ private void setUpServer() throws SignException {
155145
if (serialNumber == null && x509Cert != null) {
156146
String subjectDN = x509Cert.getSubjectDN().getName();
157147
if (subjectDN != null) {
158-
String subjectDNrray[] = subjectDN.split("SERIALNUMBER=");
148+
String[] subjectDNrray = subjectDN.split("SERIALNUMBER=");
159149
if (subjectDNrray.length == 1 && subjectDNrray[0].contains("CyberSourceCertAuth")){
160150
name = keyAlias = "CyberSourceCertAuth";
161151
}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@
2828
import java.io.InputStream;
2929
import java.io.OutputStream;
3030
import java.net.HttpURLConnection;
31-
import java.net.MalformedURLException;
32-
import java.net.ProtocolException;
3331
import java.net.URL;
34-
import java.util.HashMap;
35-
import java.util.List;
36-
import java.util.Map;
37-
import java.util.Map.Entry;
32+
import java.util.concurrent.TimeUnit;
3833

3934

4035
/**
@@ -54,26 +49,25 @@ class JDKHttpURLConnection extends Connection {
5449
}
5550

5651
void postDocument(Document request)
57-
throws IOException, TransformerConfigurationException,
58-
TransformerException, MalformedURLException,
59-
ProtocolException {
52+
throws IOException,
53+
TransformerException{
54+
//long startTime = System.nanoTime();
6055
String serverURL = mc.getEffectiveServerURL();
6156
URL url = new URL(serverURL);
6257

6358
con = ConnectionHelper.openConnection(url, mc);
6459
con.setRequestProperty(Utility.ORIGIN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
65-
logRequestHeaders();
6660
con.setRequestMethod("POST");
6761
con.setDoOutput(true);
6862
ConnectionHelper.setTimeout(con, mc.getTimeout());
69-
63+
logRequestHeaders();
7064
OutputStream out = con.getOutputStream();
71-
byte[] requestBytes = documentToByteArray(request);
65+
byte[] requestBytes = documentToByteArray(request);
7266
logger.log(Logger.LT_INFO,
7367
"Sending " + requestBytes.length + " bytes to " + serverURL);
7468
out.write(requestBytes);
7569
out.close();
76-
70+
//System.out.println("JDKHttpURLConnection.postDocument time taken is " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) + " ms");
7771
_isRequestSent = true;
7872
}
7973

@@ -110,8 +104,7 @@ InputStream getResponseStream()
110104
/* (non-Javadoc)
111105
* @see com.cybersource.ws.client.Connection#getResponseErrorStream()
112106
*/
113-
InputStream getResponseErrorStream()
114-
throws IOException {
107+
InputStream getResponseErrorStream() {
115108
return con != null ? con.getErrorStream() : null;
116109
}
117110

0 commit comments

Comments
 (0)