Search in sources :

Example 26 with HttpServer

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

the class RestClientMultipleHostsIntegTests method createHttpServer.

private static HttpServer createHttpServer() throws Exception {
    HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
    httpServer.start();
    //returns a different status code depending on the path
    for (int statusCode : getAllStatusCodes()) {
        httpServer.createContext(pathPrefix + "/" + statusCode, new ResponseHandler(statusCode));
    }
    return httpServer;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) MockHttpServer(org.elasticsearch.mocksocket.MockHttpServer)

Example 27 with HttpServer

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

the class RestClientMultipleHostsIntegTests method stopHttpServers.

@AfterClass
public static void stopHttpServers() throws IOException {
    restClient.close();
    restClient = null;
    for (HttpServer httpServer : httpServers) {
        httpServer.stop(0);
    }
    httpServers = null;
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) MockHttpServer(org.elasticsearch.mocksocket.MockHttpServer) AfterClass(org.junit.AfterClass)

Example 28 with HttpServer

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

the class RuntimeDelegateTest method testFetch.

@Test
public void testFetch() throws Exception {
    final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    final HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new Application() {

        @Override
        public Set<Class<?>> getClasses() {
            return Collections.<Class<?>>singleton(Resource.class);
        }
    }, HttpHandler.class);
    try {
        server.createContext("/", handler);
        server.start();
        final Response response = ClientBuilder.newClient().target(UriBuilder.fromUri("http://localhost/").port(server.getAddress().getPort()).build()).request().get();
        assertThat(response.readEntity(String.class), is("get"));
    } finally {
        server.stop(0);
    }
}
Also used : Response(javax.ws.rs.core.Response) HttpHandler(com.sun.net.httpserver.HttpHandler) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) Application(javax.ws.rs.core.Application) Test(org.junit.Test)

Example 29 with HttpServer

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

Example 30 with HttpServer

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

the class HelloWorldTest method testHelloWorld.

@Test
public void testHelloWorld() throws Exception {
    HttpServer server = App.startServer();
    WebTarget target = ClientBuilder.newClient().target(App.getBaseURI() + "helloworld");
    assertEquals(HelloWorldResource.CLICHED_MESSAGE, target.request(MediaType.TEXT_PLAIN).get(String.class));
    server.stop(0);
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Aggregations

HttpServer (com.sun.net.httpserver.HttpServer)48 InetSocketAddress (java.net.InetSocketAddress)28 HttpHandler (com.sun.net.httpserver.HttpHandler)12 IOException (java.io.IOException)10 File (java.io.File)7 Test (org.junit.Test)7 HttpContext (com.sun.net.httpserver.HttpContext)6 HttpExchange (com.sun.net.httpserver.HttpExchange)6 JarFile (java.util.jar.JarFile)6 MockHttpServer (org.elasticsearch.mocksocket.MockHttpServer)6 URL (java.net.URL)5 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)3 HttpsServer (com.sun.net.httpserver.HttpsServer)3 InputStream (java.io.InputStream)3 ExecutorService (java.util.concurrent.ExecutorService)3 CompilerError (com.redhat.ceylon.compiler.java.test.CompilerError)2 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2