if (ssl.getProvider() == null) {
context = SSLContext.getInstance(ssl.getProtocol());
} else {
context = SSLContext.getInstance(ssl.getProtocol(), ssl.getProvider());
}
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(ssl.getKeyManagerFactoryAlgorithm());
String keyStore = ssl.getKeyStore();
if (keyStore == null) {
keyStore = System.getProperty("javax.net.ssl.keyStore");
if (keyStore == null) {
throw new IllegalArgumentException("keyStore or system property javax.net.ssl.keyStore must be set");
}
}
if (keyStore.startsWith("classpath:")) {
try {
String res = keyStore.substring(10);
URL url = new ClassPathResource(res).getURL();
keyStore = url.toString();
} catch (IOException e) {
throw new JBIException("Unable to find keyStore " + keyStore, e);
}
}
String keyStorePassword = ssl.getKeyStorePassword();
if (keyStorePassword == null) {
keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
if (keyStorePassword == null) {
throw new IllegalArgumentException(
"keyStorePassword or system property javax.net.ssl.keyStorePassword must be set");
}
}
String trustStore = ssl.getTrustStore();
String trustStorePassword = null;
if (trustStore == null) {
trustStore = System.getProperty("javax.net.ssl.trustStore");
}
if (trustStore != null) {
if (trustStore.startsWith("classpath:")) {
try {
String res = trustStore.substring(10);
URL url = new ClassPathResource(res).getURL();
trustStore = url.toString();
} catch (IOException e) {
throw new JBIException("Unable to find trustStore " + trustStore, e);
}
}
trustStorePassword = ssl.getTrustStorePassword();
if (trustStorePassword == null) {
trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
if (trustStorePassword == null) {
throw new IllegalArgumentException(
"trustStorePassword or system property javax.net.ssl.trustStorePassword must be set");
}
}
}
KeyStore ks = KeyStore.getInstance(ssl.getKeyStoreType());
ks.load(Resource.newResource(keyStore).getInputStream(), keyStorePassword.toCharArray());
keyManagerFactory.init(ks, ssl.getKeyPassword() != null ? ssl.getKeyPassword().toCharArray() : keyStorePassword
.toCharArray());
if (trustStore != null) {
KeyStore ts = KeyStore.getInstance(ssl.getTrustStoreType());
ts.load(Resource.newResource(trustStore).getInputStream(), trustStorePassword.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(ssl
.getTrustManagerFactoryAlgorithm());
trustManagerFactory.init(ts);
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(),
new java.security.SecureRandom());
} else {
context.init(keyManagerFactory.getKeyManagers(), null, new java.security.SecureRandom());
}
factory = context.getSocketFactory();
}