Search in sources :

Example 1 with ContainerServer

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);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) ContainerServer(org.simpleframework.http.core.ContainerServer) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SocketConnection(org.simpleframework.transport.connect.SocketConnection) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) X509TrustManager(javax.net.ssl.X509TrustManager) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 2 with ContainerServer

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);
    }
}
Also used : SimpleFileServerContainer(org.gradle.plugins.javascript.envjs.http.simple.internal.SimpleFileServerContainer) ContainerServer(org.simpleframework.http.core.ContainerServer) Container(org.simpleframework.http.core.Container) SimpleFileServerContainer(org.gradle.plugins.javascript.envjs.http.simple.internal.SimpleFileServerContainer) Server(org.simpleframework.transport.Server) HttpFileServer(org.gradle.plugins.javascript.envjs.http.HttpFileServer) ContainerServer(org.simpleframework.http.core.ContainerServer) SocketConnection(org.simpleframework.transport.connect.SocketConnection) InetSocketAddress(java.net.InetSocketAddress) Connection(org.simpleframework.transport.connect.Connection) SocketConnection(org.simpleframework.transport.connect.SocketConnection) Stoppable(org.gradle.internal.concurrent.Stoppable) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) FileContext(org.simpleframework.http.resource.FileContext)

Aggregations

IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 ContainerServer (org.simpleframework.http.core.ContainerServer)2 SocketConnection (org.simpleframework.transport.connect.SocketConnection)2 FileInputStream (java.io.FileInputStream)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 Certificate (java.security.cert.Certificate)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1 SSLContext (javax.net.ssl.SSLContext)1 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)1 X509TrustManager (javax.net.ssl.X509TrustManager)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 Stoppable (org.gradle.internal.concurrent.Stoppable)1 HttpFileServer (org.gradle.plugins.javascript.envjs.http.HttpFileServer)1