Search in sources :

Example 1 with HttpsServer

use of com.sun.net.httpserver.HttpsServer in project ribbon by Netflix.

the class MockHttpServer method before.

public void before(final Description description) throws Exception {
    this.service = Executors.newFixedThreadPool(threadCount, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TestHttpServer-%d").build());
    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 0);
    if (hasSsl) {
        byte[] sampleTruststore1 = Base64.decode(TEST_TS1);
        byte[] sampleKeystore1 = Base64.decode(TEST_KS1);
        keystore = File.createTempFile("SecureAcceptAllGetTest", ".keystore");
        truststore = File.createTempFile("SecureAcceptAllGetTest", ".truststore");
        FileOutputStream keystoreFileOut = new FileOutputStream(keystore);
        try {
            keystoreFileOut.write(sampleKeystore1);
        } finally {
            keystoreFileOut.close();
        }
        FileOutputStream truststoreFileOut = new FileOutputStream(truststore);
        try {
            truststoreFileOut.write(sampleTruststore1);
        } finally {
            truststoreFileOut.close();
        }
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(new FileInputStream(keystore), PASSWORD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, PASSWORD.toCharArray());
        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(new FileInputStream(truststore), PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ts);
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        HttpsServer secureServer = HttpsServer.create(inetSocketAddress, 0);
        secureServer.setHttpsConfigurator(new HttpsConfigurator(sc) {

            public void configure(HttpsParameters params) {
                SSLContext c = getSSLContext();
                SSLParameters sslparams = c.getDefaultSSLParameters();
                params.setSSLParameters(sslparams);
            }
        });
        server = secureServer;
    } else {
        server = HttpServer.create(inetSocketAddress, 0);
    }
    server.setExecutor(service);
    for (Entry<String, HttpHandler> handler : handlers.entrySet()) {
        server.createContext(handler.getKey(), handler.getValue());
    }
    server.start();
    localHttpServerPort = server.getAddress().getPort();
    System.out.println(description.getClassName() + " TestServer is started: " + getServerUrl());
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) SSLParameters(javax.net.ssl.SSLParameters) FileOutputStream(java.io.FileOutputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpsServer(com.sun.net.httpserver.HttpsServer)

Example 2 with HttpsServer

use of com.sun.net.httpserver.HttpsServer in project jersey by jersey.

the class JdkHttpsServerTest method testHttpsServerNoSslContextDelayedStart.

/**
     * Test, that {@link HttpsServer} can be manually started even with (empty) SSLContext, but will throw an exception
     * on request.
     * @throws Exception
     */
@Test(expected = IOException.class)
public void testHttpsServerNoSslContextDelayedStart() throws Throwable {
    server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, false);
    assertThat(server, instanceOf(HttpsServer.class));
    server.start();
    final Client client = ClientBuilder.newBuilder().newClient();
    try {
        client.target(httpsUri).path("testHttps").request().get(String.class);
    } catch (final ProcessingException e) {
        throw e.getCause();
    }
}
Also used : Client(javax.ws.rs.client.Client) HttpsServer(com.sun.net.httpserver.HttpsServer) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 3 with HttpsServer

use of com.sun.net.httpserver.HttpsServer in project ignite by apache.

the class GridEmbeddedHttpServer method createAndStart.

/**
 * Internal method which creates and starts the server.
 *
 * @param httpsMode True if the server to be started is HTTPS, false otherwise.
 * @return Started server.
 */
private static GridEmbeddedHttpServer createAndStart(boolean httpsMode) throws Exception {
    HttpServer httpSrv;
    InetSocketAddress addrToBind = new InetSocketAddress(HOSTNAME_TO_BIND_SRV, getAvailablePort());
    if (httpsMode) {
        HttpsServer httpsSrv = HttpsServer.create(addrToBind, 0);
        httpsSrv.setHttpsConfigurator(new HttpsConfigurator(GridTestUtils.sslContext()));
        httpSrv = httpsSrv;
    } else
        httpSrv = HttpServer.create(addrToBind, 0);
    GridEmbeddedHttpServer embeddedHttpSrv = new GridEmbeddedHttpServer();
    embeddedHttpSrv.proto = httpsMode ? "https" : "http";
    embeddedHttpSrv.httpSrv = httpSrv;
    embeddedHttpSrv.httpSrv.start();
    return embeddedHttpSrv;
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpsServer(com.sun.net.httpserver.HttpsServer)

Example 4 with HttpsServer

use of com.sun.net.httpserver.HttpsServer in project cosmic by MissionCriticalCloud.

the class ConsoleProxySecureServerFactoryImpl method createHttpServerInstance.

@Override
public HttpServer createHttpServerInstance(final int port) throws IOException {
    try {
        final HttpsServer server = HttpsServer.create(new InetSocketAddress(port), 5);
        server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {

            @Override
            public void configure(final HttpsParameters params) {
                final SSLContext c = getSSLContext();
                // get the default parameters
                final SSLParameters sslparams = c.getDefaultSSLParameters();
                params.setSSLParameters(sslparams);
            // statement above could throw IAE if any params invalid.
            // eg. if app has a UI and parameters supplied by a user.
            }
        });
        s_logger.info("create HTTPS server instance on port: " + port);
        return server;
    } catch (final Exception ioe) {
        s_logger.error(ioe.toString(), ioe);
    }
    return null;
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) SSLParameters(javax.net.ssl.SSLParameters) InetSocketAddress(java.net.InetSocketAddress) HttpsParameters(com.sun.net.httpserver.HttpsParameters) SSLContext(javax.net.ssl.SSLContext) HttpsServer(com.sun.net.httpserver.HttpsServer) IOException(java.io.IOException)

Example 5 with HttpsServer

use of com.sun.net.httpserver.HttpsServer in project jersey by jersey.

the class JdkHttpServerFactory method createHttpServer.

private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerContainer handler, final SSLContext sslContext, final boolean start) {
    if (uri == null) {
        throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_NULL());
    }
    final String scheme = uri.getScheme();
    final boolean isHttp = "http".equalsIgnoreCase(scheme);
    final boolean isHttps = "https".equalsIgnoreCase(scheme);
    final HttpsConfigurator httpsConfigurator = sslContext != null ? new HttpsConfigurator(sslContext) : null;
    if (isHttp) {
        if (httpsConfigurator != null) {
            // attempt to use https with http scheme
            LOG.warning(LocalizationMessages.WARNING_CONTAINER_URI_SCHEME_SECURED());
        }
    } else if (isHttps) {
        if (httpsConfigurator == null) {
            if (start) {
                // Starting https server w/o SSL is invalid, it will lead to error anyway.
                throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_HTTPS_NO_SSL());
            } else {
                // Creating the https server w/o SSL context, but not starting it is valid.
                // However, server.setHttpsConfigurator() must be called before the start.
                LOG.info(LocalizationMessages.INFO_CONTAINER_HTTPS_NO_SSL());
            }
        }
    } else {
        throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_SCHEME_UNKNOWN(uri));
    }
    final String path = uri.getPath();
    if (path == null) {
        throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_NULL(uri));
    } else if (path.isEmpty()) {
        throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_EMPTY(uri));
    } else if (path.charAt(0) != '/') {
        throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_START(uri));
    }
    final int port = (uri.getPort() == -1) ? (isHttp ? Container.DEFAULT_HTTP_PORT : Container.DEFAULT_HTTPS_PORT) : uri.getPort();
    final HttpServer server;
    try {
        server = isHttp ? HttpServer.create(new InetSocketAddress(port), 0) : HttpsServer.create(new InetSocketAddress(port), 0);
    } catch (final IOException ioe) {
        throw new ProcessingException(LocalizationMessages.ERROR_CONTAINER_EXCEPTION_IO(), ioe);
    }
    if (isHttps && httpsConfigurator != null) {
        ((HttpsServer) server).setHttpsConfigurator(httpsConfigurator);
    }
    server.setExecutor(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("jdk-http-server-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build()));
    server.createContext(path, handler);
    final HttpServer wrapper = isHttp ? createHttpServerWrapper(server, handler) : createHttpsServerWrapper((HttpsServer) server, handler);
    if (start) {
        wrapper.start();
    }
    return wrapper;
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) JerseyProcessingUncaughtExceptionHandler(org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) ThreadFactoryBuilder(org.glassfish.jersey.internal.guava.ThreadFactoryBuilder) IOException(java.io.IOException) HttpsServer(com.sun.net.httpserver.HttpsServer) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

HttpsServer (com.sun.net.httpserver.HttpsServer)9 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)8 InetSocketAddress (java.net.InetSocketAddress)7 HttpsParameters (com.sun.net.httpserver.HttpsParameters)4 SSLContext (javax.net.ssl.SSLContext)4 SSLParameters (javax.net.ssl.SSLParameters)4 IOException (java.io.IOException)3 Test (org.junit.Test)3 HttpServer (com.sun.net.httpserver.HttpServer)2 ProcessingException (javax.ws.rs.ProcessingException)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Headers (com.sun.net.httpserver.Headers)1 HttpHandler (com.sun.net.httpserver.HttpHandler)1 JdkSslContext (io.netty.handler.ssl.JdkSslContext)1 SslContext (io.netty.handler.ssl.SslContext)1 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)1 SslProvider (io.netty.handler.ssl.SslProvider)1 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 BufferedReader (java.io.BufferedReader)1