A SERVICE OF

logo

System-to-System Developer’s Guide v3.0
Confidential Material 18 of 19
APPENDIX B – JAVA CODE SAMPLE
Following is a Java code sample taken from the NetSuite smbXMLPost tool that illustrates how a two-way SSL
connection is established using a keystore.
import javax.net.ssl.*;
import java.net.*;
import java.security.*;
import java.security.cert.*;
// Private key password
String keyPassword = "mykeypass";
// Keystore password
String storePassword = "mystorepass";
// Fully qualified path to keystore file
String keystoreName = "my.keystore";
// Read the keystore file and load the keystore
KeyStore keystore = null;
InputStream keystoreStream = new ByteArrayInputStream( base64Decode(
keystoreName.getBytes() ));
keystore = KeyStore.getInstance("JKS");
keystore.load(keystoreStream, storePassword.toCharArray());
// Initialize the KeyManagerFactory
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keystore, keyPassword.toCharArray());
// Create and initialize SSLContext and generate random number
SSLContext ssl = SSLContext.getInstance("SSL");
SecureRandom sr = new SecureRandom();
sr.nextInt();
ssl.init(keyManagerFactory.getKeyManagers(), null, sr);
// Open HTTP connection
URL url = new URL(m_sUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Set connection to include SSL context
SSLSocketFactory sf = ssl.getSocketFactory();
((javax.net.ssl.HttpsURLConnection)conn).setSSLSocketFactory (sf);
// Set other connection parameters and load request data
...
// Connect and push the request into the connection socket
conn.connect();
// This method used above decodes the given byte[] using the
// base64-encoding specified in RFC-2045 (Section 6.8).
public final static byte[] base64Decode(byte[] data)
{
...
}