use of org.simpleframework.http.core.ContainerServer in project lobcder by skoulouzis.
the class SslSimpletonServer method initHttps.
/**
* Setting up certificates
* EG C:\Program Files\Java\jdk1.6.0_10\bin>keytool -genkey -keystore certs -keyalg rsa -alias jamie -storepass serverkspw -keypass serverpw
* note that 'first name last name' should be machine name
*
* @param connection
* @param port
*/
protected SocketConnection initHttps(int port) {
SSLServerSocketFactory fac = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
log.info("initHttps: port: " + port + " sslProtocol: " + sslProtocol + " keystoreAlgorithm:" + keystoreAlgorithm);
try {
KeyStore keystore = KeyStore.getInstance(keystoreType);
keystore.load(new FileInputStream(keystoreFile), keystorePassword.toCharArray());
log.info("listing aliases defined in keystore");
Enumeration<String> aliases = keystore.aliases();
while (aliases.hasMoreElements()) {
String a = aliases.nextElement();
log.info(" - alias: " + a);
Certificate cert = keystore.getCertificate(a);
log.info(" - cert type: " + cert.getType());
log.info(" - algorithm: " + cert.getPublicKey().getAlgorithm());
log.info(" - format: " + cert.getPublicKey().getFormat());
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(keystoreAlgorithm);
kmf.init(keystore, keystorePassword.toCharArray());
X509TrustManager trustManager = new AnonymousTrustManager();
X509TrustManager[] trustManagers = new X509TrustManager[] { trustManager };
// An SSLContext is an environment for implementing JSSE. It is used to create a ServerSocketFactory
SSLContext sslc = SSLContext.getInstance(sslProtocol);
sslc.init(kmf.getKeyManagers(), trustManagers, null);
ContainerServer processor = new ContainerServer(this, 25);
org.simpleframework.transport.Server secure = new SecureProcessor(processor, sslc);
SocketConnection ssl = new SocketConnection(secure);
InetSocketAddress address = new InetSocketAddress(port);
ssl.connect(address, sslc);
log.debug("server running on: " + address);
return ssl;
} catch (java.net.BindException ex) {
throw new RuntimeException("Couldnt bind to port: " + port);
} catch (KeyManagementException ex) {
throw new RuntimeException(ex);
} catch (UnrecoverableKeyException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
} catch (CertificateException ex) {
throw new RuntimeException(ex);
} catch (KeyStoreException ex) {
throw new RuntimeException(ex);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of org.simpleframework.http.core.ContainerServer in project gradle by gradle.
the class SimpleHttpFileServerFactory method start.
public HttpFileServer start(File contentRoot, int port) {
Container container = new SimpleFileServerContainer(new FileContext(contentRoot));
try {
final Server server = new ContainerServer(container);
Connection connection = new SocketConnection(server);
InetSocketAddress address = new InetSocketAddress(port);
InetSocketAddress usedAddress = (InetSocketAddress) connection.connect(address);
return new SimpleHttpFileServer(contentRoot, usedAddress.getPort(), new Stoppable() {
public void stop() {
try {
server.stop();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
});
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Aggregations