Skip to content

Commit 7844541

Browse files
author
adkumar3
committed
changes for JAVA certificate to be read from JAVA keystore
1 parent e82d340 commit 7844541

9 files changed

Lines changed: 891 additions & 100 deletions

File tree

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#CyberSource Simple Order API for Java
22

3-
[![Build Status](https://travis-ci.org/CyberSource/cybersource-sdk-java.png?branch=master)]
3+
[![Build Status](https://travis-ci.org/CyberSource/cybersource-sdk-java.png?branch=future)]
44
(https://travis-ci.org/CyberSource/cybersource-sdk-java)
55

66
##Package Managers
@@ -85,8 +85,13 @@ You do not need to download and build the source to use the SDK but if you want
8585
By default, it is set to true.
8686
8787
h. "serverURL" config parameter will take precedence over sendToProduction and sendToAkamai config parameters. By default the "serverURL" configuration is commented out.
88+
89+
i. "enablejdkcert" parameter if set to true then it will read the JKS file specified at keysDirectory location. The file should be of the same name as specified in keyFilename.
90+
91+
j. cacerts this property will be considered only if "enablejdkcert" is set to true. If it is set to true then it means the JKS file is under cacerts folder of the JDK and
92+
it will read the JKS file from cacerts.
8893
89-
i. Please refer to the accompanying documentation for the other optional properties that you may wish to specify.
94+
k. Please refer to the accompanying documentation for the other optional properties that you may wish to specify.
9095

9196

9297
4. Build this project using Maven.
@@ -107,38 +112,47 @@ You do not need to download and build the source to use the SDK but if you want
107112

108113
We have two ways to test, One is using maven tool and other is to download the zip and use scripts to test.
109114

115+
110116
1.) Unzip the downloaded zip file into a directory of your choice. It will create a directory called
111117
cybersource-sdk-java-master.
118+
* If in the Request, a key called "_has_escapes" is present and is set
119+
* to "1", we will not escape the special characters. Basically, the
120+
* merchant is saying that they have escaped the characters themselves.
121+
* This might prove useful for more advanced users of the Basic client.
122+
123+
Note: The Script will take Service_name as program argument. Service Name can
124+
be auth, auth_reversal, capture, sale, emv_auth, credit. If no argument is passed the script
125+
will terminate the program.
112126

113127
a. TESTING THE NAME-VALUE PAIR SAMPLE
114128
. Go to the cybersource-sdk-java-master/sample/nvp directory.
115129
. Use compileSample scripts to create classes directory.As it is not included in SDK.
116130
. Then at a command prompt, type this line:
117-
Windows runSample.bat
118-
Unix or Linux runSample.sh
131+
Windows runSample.bat <service_name>
132+
Unix or Linux runSample.sh <service_name>
119133

120134
If JAVA_HOME is defined, the script uses <JAVA_HOME>/bin/java. Otherwise, it uses
121135
whatever java is in the path.
122136
If the client is installed correctly, the requests and replies for a credit card authorization
123137
and a follow-on capture appear.
124138

125-
. If you make any changes to the AuthCaptureSample.java sample, you
139+
. If you make any changes to the RunSample.java sample, you
126140
must rebuild the sample before using it. Use the compileSample batch file or
127141
shell script provided in the sample directory.
128142
129143
b. TESTING THE XML SAMPLE
130144
. Go to the cybersource-sdk-java-master/sample/xml directory.
131145
. Use compileSample scripts to create classes directory.As it is not included in SDK.
132146
. At a command prompt, type this line:
133-
Windows runSample.bat
134-
Unix or Linux runSample.sh
147+
Windows runSample.bat <Service_name>
148+
Unix or Linux runSample.sh <service_name>
135149
136150
If JAVA_HOME is defined, the script uses <JAVA_HOME>/bin/java. Otherwise, it uses
137151
whatever java is in the path.
138152
If the client is installed correctly, the requests and replies for a credit card authorization
139153
and a follow-on capture appear.
140154

141-
. If you make any changes to the AuthSample.java sample, you
155+
. If you make any changes to the RunSample.java sample, you
142156
must rebuild the sample before using it. Use the compileSample batch file or
143157
shell script provided in the sample directory.
144158

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

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@
2626
import org.xml.sax.InputSource;
2727
import org.xml.sax.SAXException;
2828

29+
import javax.security.cert.X509Certificate;
2930
import javax.xml.parsers.DocumentBuilder;
3031
import javax.xml.parsers.ParserConfigurationException;
3132

33+
import java.io.File;
34+
import java.io.FileInputStream;
3235
import java.io.IOException;
3336
import java.io.StringReader;
37+
import java.security.KeyStore;
38+
import java.security.cert.PKIXParameters;
39+
import java.security.cert.TrustAnchor;
3440
import java.text.MessageFormat;
3541
import java.util.HashMap;
42+
import java.util.Iterator;
3643
import java.util.Map;
3744
import java.util.Properties;
3845

@@ -118,8 +125,10 @@ public static Map runTransaction(
118125

119126
con = Connection.getInstance(mc, builder, logger);
120127
Document wrappedReply = con.post(signedDoc);
121-
122-
return (soapUnwrap(wrappedReply, mc, logger));
128+
Map<String, String> replyMap = soapUnwrap(wrappedReply, mc, logger);
129+
logger.log(Logger.LT_INFO, "Client, End of runTransaction Call ",false);
130+
131+
return replyMap;
123132
} catch (IOException e) {
124133
throw new ClientException(
125134
e, con != null && con.isRequestSent(), logger);
@@ -178,51 +187,54 @@ private static Document soapWrapAndSign(
178187
boolean logSignedData = mc.getLogSignedData();
179188
if (!logSignedData) {
180189
logger.log(
181-
Logger.LT_REQUEST,
182-
mapToString(request, true, PCI.REQUEST));
190+
Logger.LT_REQUEST,
191+
"UUID > "+(mc.getUniqueKey()).toString() + "\n" +
192+
"Input request is" + "\n" +
193+
"======================================= \n"
194+
+ mapToString(request, true, PCI.REQUEST));
195+
}
196+
197+
Document wrappedDoc = soapWrap(request, mc, builder,logger);
198+
logger.log(Logger.LT_INFO, "Client, End of soapWrap ",true);
199+
200+
Document resultDocument = null;
201+
if(mc.getEnablejdkcert()){
202+
SecurityUtil.readJdkCert(mc,logger);
203+
}
204+
else{
205+
SecurityUtil.loadMerchantP12File(mc,logger);
206+
logger.log(Logger.LT_INFO, "Client, End of loadMerchantP12File ", true);
183207
}
208+
// sign Document object
209+
resultDocument = SecurityUtil.createSignedDoc(wrappedDoc, mc.getMerchantID(), mc.getKeyPassword(), logger);
210+
logger.log(Logger.LT_INFO, "Client, End of createSignedDoc ", true);
211+
212+
if ( mc.getUseSignAndEncrypted() ) {
213+
// Encrypt signed Document
214+
resultDocument = SecurityUtil.handleMessageCreation(resultDocument, mc.getMerchantID(), logger);
215+
logger.log(Logger.LT_INFO, "Client, End of handleMessageCreation ", true);
216+
}
217+
if (logSignedData) {
218+
logger.log(Logger.LT_REQUEST,Utility.nodeToString(resultDocument, PCI.REQUEST));
219+
//logger.log(Logger.LT_REQUEST,XMLUtils.PrettyDocumentToString(resultDocument));
220+
}
221+
222+
return resultDocument ;
223+
}
184224

185-
// wrap in SOAP envelope
225+
private static Document soapWrap(Map request, MerchantConfig mc, DocumentBuilder builder, LoggerWrapper logger) throws SAXException, IOException{
226+
// wrap in SOAP envelope
186227
Object[] arguments
187228
= {mc.getEffectiveNamespaceURI(),
188229
mapToString(request, false, PCI.REQUEST)};
189230
String xmlString = MessageFormat.format(SOAP_ENVELOPE1, arguments);
190-
// load XML string into a Document object
231+
// load XML string into a Document object
191232
StringReader sr = new StringReader( xmlString );
192233
Document wrappedDoc = builder.parse( new InputSource( sr ) );
193-
sr.close();
194-
195-
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-
199-
SignedAndEncryptedMessageHandler handler = SignedAndEncryptedMessageHandler.getInstance(mc,logger);
200-
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-
204-
// 3/7/2016 change to support encrypted messages as well as signed - jeaton
205-
if ( !mc.getUseSignAndEncrypted() ) {
206-
// sign Document object
207-
logger.log(Logger.LT_INFO, "Signing request...");
208-
resultDocument = handler.createSignedDoc(wrappedDoc,mc.getMerchantID(),mc.getKeyPassword(),null);
209-
if (logSignedData) {
210-
logger.log(Logger.LT_REQUEST,
211-
Utility.nodeToString(resultDocument, PCI.REQUEST));
212-
}
213-
} else {
214-
logger.log(Logger.LT_INFO, "Signing and encrypting request...");
215-
resultDocument = handler.handleMessageCreation(wrappedDoc,mc.getMerchantID(),mc.getKeyPassword());
216-
if (logSignedData) {
217-
logger.log(Logger.LT_REQUEST,XMLUtils.PrettyDocumentToString(resultDocument));
218-
}
219-
}
220-
221-
//System.out.println(XMLUtils.PrettyDocumentToString(resultDocument));
222-
return resultDocument ;
234+
sr.close();
235+
return wrappedDoc;
223236
}
224-
225-
237+
226238
/**
227239
* Extracts the content of the SOAP body from the given Document object
228240
* inside a SOAP envelope.

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.cybersource.ws.client;
2020

21+
import org.apache.commons.lang3.time.StopWatch;
22+
2123
/**
2224
* An internal class used by the clients to encapsulate the logger, primarily
2325
* to avoid having to check if the Logger object is null before logging. It
@@ -26,7 +28,8 @@
2628
*/
2729
public class LoggerWrapper implements Logger {
2830
private Logger logger = null;
29-
31+
private MyStopWatch stopWatch = new MyStopWatch();
32+
3033
/**
3134
* Constructor.
3235
*
@@ -53,10 +56,12 @@ public LoggerWrapper(
5356
logger = _logger;
5457
prepare = _prepare;
5558
logTranStart = _logTranStart;
59+
stopWatch.start();
5660
} else if (mc.getEnableLog()) {
5761
logger = new LoggerImpl(mc);
5862
prepare = true;
5963
logTranStart = true;
64+
stopWatch.start();
6065
}
6166

6267
if (prepare) {
@@ -66,6 +71,7 @@ public LoggerWrapper(
6671
if (logTranStart) {
6772
logTransactionStart();
6873
}
74+
6975
}
7076

7177
/**
@@ -100,6 +106,23 @@ public void log(String type, String text) {
100106
logger.log(type, text);
101107
}
102108
}
109+
110+
/**
111+
* Calls the encapsulated Logger object's log() method.
112+
*
113+
* @param type the log entry type.
114+
* @param text the actual text to be logged.
115+
* @param splitTimer to log split time.
116+
*/
117+
public void log(String type, String text, boolean splitTimer) {
118+
if (logger != null) {
119+
if (splitTimer) {
120+
logger.log(type, text + stopWatch.getMethodElapsedTime() + "ms");
121+
} else {
122+
logger.log(type, text + stopWatch.getElapsedTimeForTransaction() + "ms");
123+
}
124+
}
125+
}
103126

104127
/**
105128
* Returns the encapsulated Logger object.
@@ -109,4 +132,13 @@ public void log(String type, String text) {
109132
public Logger getLogger() {
110133
return logger;
111134
}
135+
136+
/**
137+
* Returns the encapsulated StopWatch object.
138+
*
139+
* @return the encapsulated StopWatch object.
140+
*/
141+
public MyStopWatch getStopWatch() {
142+
return stopWatch;
143+
}
112144
}

0 commit comments

Comments
 (0)