Example usage for javax.net.ssl SSLServerSocketFactory createServerSocket

List of usage examples for javax.net.ssl SSLServerSocketFactory createServerSocket

Introduction

In this page you can find the example usage for javax.net.ssl SSLServerSocketFactory createServerSocket.

Prototype

@Override
    public ServerSocket createServerSocket() throws IOException 

Source Link

Usage

From source file:org.apache.camel.itest.http.HttpTestServer.java

/**
 * Starts this test server.// w w  w  .  ja  v  a 2  s .  c  o m
 */
public void start() throws Exception {
    if (servicedSocket != null) {
        throw new IllegalStateException(this.toString() + " already running");
    }
    ServerSocket ssock;
    if (sslcontext != null) {
        SSLServerSocketFactory sf = sslcontext.getServerSocketFactory();
        ssock = sf.createServerSocket();
    } else {
        ssock = new ServerSocket();
    }

    ssock.setReuseAddress(true); // probably pointless for port '0'
    ssock.bind(TEST_SERVER_ADDR);
    servicedSocket = ssock;

    listenerThread = new ListenerThread();
    listenerThread.setDaemon(false);
    listenerThread.start();
}

From source file:org.apache.http.localserver.LocalTestServer.java

/**
 * Starts this test server./*from  w  ww  . ja  v a2s.  co m*/
 * Use {@link #getServicePort getServicePort}
 * to obtain the port number afterwards.
 */
public void start() throws Exception {
    if (servicedSocket != null)
        throw new IllegalStateException(this.toString() + " already running");

    ServerSocket ssock;
    if (sslcontext != null) {
        SSLServerSocketFactory sf = sslcontext.getServerSocketFactory();
        ssock = sf.createServerSocket();
    } else {
        ssock = new ServerSocket();
    }

    ssock.setReuseAddress(true); // probably pointless for port '0'
    ssock.bind(TEST_SERVER_ADDR);
    servicedSocket = ssock;

    listenerThread = new Thread(new RequestListener());
    listenerThread.setDaemon(false);
    listenerThread.start();
}

From source file:org.nectarframework.base.service.nanohttp.NanoHttpService.java

/**
 * Creates an SSLSocketFactory for HTTPS. Pass a KeyStore resource with your
 * certificate and passphrase//from w ww  .ja  v  a  2  s .  c  o  m
 */
public ServerSocket makeSSLServerSocket(String keyAndTrustStoreClasspathPath, char[] passphrase)
        throws IOException {
    try {
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        InputStream keystoreStream = new FileInputStream(new File(keyAndTrustStoreClasspathPath));

        keystore.load(keystoreStream, passphrase);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, passphrase);

        SSLServerSocketFactory res = null;
        try {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keystore);
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
            res = ctx.getServerSocketFactory();

        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }

        SSLServerSocket ss = null;
        ss = (SSLServerSocket) res.createServerSocket();
        ss.setEnabledProtocols(ss.getSupportedProtocols());
        ss.setUseClientMode(false);
        ss.setWantClientAuth(false);
        ss.setNeedClientAuth(false);

        return ss;

    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:org.reficio.ws.it.util.SslTunnel.java

public void start() {
    try {/*  ww  w.  j a  va 2  s  .c  om*/
        sslContext = SSLContext.getInstance("SSLv3");
        KeyManager[] keyManagers = null;
        TrustManager[] trustManagers = null;

        if (keyStore != null) {
            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
            X509KeyManager defaultKeyManager = (X509KeyManager) keyManagerFactory.getKeyManagers()[0];
            keyManagers = new KeyManager[] { defaultKeyManager };

        }
        if (trustStore != null) {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(trustStore);
            X509TrustManager defaultTrustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
            trustManagers = new TrustManager[] { defaultTrustManager };
        }

        sslContext.init(keyManagers, trustManagers, new SecureRandom());

        SSLServerSocketFactory socketFactory = sslContext.getServerSocketFactory();
        socket = socketFactory.createServerSocket();
        socket.setReuseAddress(true);
        socket.bind(new InetSocketAddress(sourcePort));
        new ServerThread(socket, run).start();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}