}
private String initServer() throws Exception
{
String httpsURL = null;
SSLContext sslCtx = null;
try
{
sslCtx = SSLContext.getInstance("TLS");
ClassLoader loader = getClass().getClassLoader();
URL keyStoreURL = loader.getResource("tst.keystore");
if( keyStoreURL == null )
throw new IOException("Failed to find resource tst.keystore");
log.debug("Opening KeyStore: "+keyStoreURL);
KeyStore keyStore = KeyStore.getInstance("JKS");
InputStream is = keyStoreURL.openStream();
keyStore.load(is, KEYSTORE_PASSWORD.toCharArray());
String algorithm = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory keyMgr = KeyManagerFactory.getInstance(algorithm);
keyMgr.init(keyStore, KEYSTORE_PASSWORD.toCharArray());
algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory trustMgr = TrustManagerFactory.getInstance(algorithm);
trustMgr.init(keyStore);
TrustManager[] trustMgrs = trustMgr.getTrustManagers();
sslCtx.init(keyMgr.getKeyManagers(), trustMgrs, null);
}
catch(Exception e)
{
log.error("Failed to init SSLContext", e);
throw new IOException("Failed to get SSLContext for TLS algorithm");
}
ServerSocketFactory factory = sslCtx.getServerSocketFactory();
ServerSocket serverSocket = factory.createServerSocket(0);
getLog().debug("Created serverSocket: "+serverSocket);
int port = serverSocket.getLocalPort();
InetAddress addr = serverSocket.getInetAddress();
httpsURL = "https://localhost:" + port + '/';