Search in sources :

Example 1 with HttpsConfigurator

use of com.sun.net.httpserver.HttpsConfigurator in project elasticsearch by elastic.

the class GceDiscoverTests method startHttpd.

@BeforeClass
public static void startHttpd() throws Exception {
    logDir = createTempDir();
    SSLContext sslContext = getSSLContext();
    httpsServer = MockHttpServer.createHttps(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
    httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
    httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
    httpServer.createContext("/computeMetadata/v1/instance/service-accounts/default/token", (s) -> {
        String response = GceMockUtils.readGoogleInternalJsonResponse("http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token");
        byte[] responseAsBytes = response.getBytes(StandardCharsets.UTF_8);
        s.sendResponseHeaders(200, responseAsBytes.length);
        OutputStream responseBody = s.getResponseBody();
        responseBody.write(responseAsBytes);
        responseBody.close();
    });
    httpsServer.createContext("/compute/v1/projects/testproject/zones/primaryzone/instances", (s) -> {
        Headers headers = s.getResponseHeaders();
        headers.add("Content-Type", "application/json; charset=UTF-8");
        Logger logger = Loggers.getLogger(GceDiscoverTests.class);
        try {
            Path[] files = FileSystemUtils.files(logDir);
            StringBuilder builder = new StringBuilder("{\"id\": \"dummy\",\"items\":[");
            int foundFiles = 0;
            for (int i = 0; i < files.length; i++) {
                Path resolve = files[i].resolve("transport.ports");
                if (Files.exists(resolve)) {
                    if (foundFiles++ > 0) {
                        builder.append(",");
                    }
                    List<String> addressses = Files.readAllLines(resolve);
                    Collections.shuffle(addressses, random());
                    logger.debug("addresses for node: [{}] published addresses [{}]", files[i].getFileName(), addressses);
                    builder.append("{\"description\": \"ES Node ").append(files[i].getFileName()).append("\",\"networkInterfaces\": [ {");
                    builder.append("\"networkIP\": \"").append(addressses.get(0)).append("\"}],");
                    builder.append("\"status\" : \"RUNNING\"}");
                }
            }
            builder.append("]}");
            String responseString = builder.toString();
            final byte[] responseAsBytes = responseString.getBytes(StandardCharsets.UTF_8);
            s.sendResponseHeaders(200, responseAsBytes.length);
            OutputStream responseBody = s.getResponseBody();
            responseBody.write(responseAsBytes);
            responseBody.close();
        } catch (Exception e) {
            //
            byte[] responseAsBytes = ("{ \"error\" : {\"message\" : \"" + e.toString() + "\" } }").getBytes(StandardCharsets.UTF_8);
            s.sendResponseHeaders(500, responseAsBytes.length);
            OutputStream responseBody = s.getResponseBody();
            responseBody.write(responseAsBytes);
            responseBody.close();
        }
    });
    httpsServer.start();
    httpServer.start();
}
Also used : Path(java.nio.file.Path) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) Logger(org.apache.logging.log4j.Logger) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) BeforeClass(org.junit.BeforeClass)

Example 2 with HttpsConfigurator

use of com.sun.net.httpserver.HttpsConfigurator 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 3 with HttpsConfigurator

use of com.sun.net.httpserver.HttpsConfigurator in project jdk8u_jdk by JetBrains.

the class HttpsSocketFacTest method startHttpsServer.

/**
     * Https Server
     */
public void startHttpsServer() throws IOException, NoSuchAlgorithmException {
    httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0);
    httpsServer.createContext("/test6614957/", new MyHandler());
    httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));
    httpsServer.start();
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress)

Example 4 with HttpsConfigurator

use of com.sun.net.httpserver.HttpsConfigurator in project jdk8u_jdk by JetBrains.

the class HttpsCreateSockTest method startHttpsServer.

/**
     * Https Server
     */
public void startHttpsServer() throws IOException, NoSuchAlgorithmException {
    httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0);
    httpsServer.createContext("/", new MyHandler());
    httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));
    httpsServer.start();
}
Also used : HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) InetSocketAddress(java.net.InetSocketAddress)

Example 5 with HttpsConfigurator

use of com.sun.net.httpserver.HttpsConfigurator 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)

Aggregations

HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)11 InetSocketAddress (java.net.InetSocketAddress)9 SSLContext (javax.net.ssl.SSLContext)6 HttpsServer (com.sun.net.httpserver.HttpsServer)5 HttpsParameters (com.sun.net.httpserver.HttpsParameters)4 IOException (java.io.IOException)4 SSLParameters (javax.net.ssl.SSLParameters)4 Headers (com.sun.net.httpserver.Headers)2 HttpServer (com.sun.net.httpserver.HttpServer)2 OutputStream (java.io.OutputStream)2 Path (java.nio.file.Path)2 BeforeClass (org.junit.BeforeClass)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 HttpHandler (com.sun.net.httpserver.HttpHandler)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 StringWriter (java.io.StringWriter)1 BindException (java.net.BindException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1