Search in sources :

Example 6 with HttpHandler

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

the class ChunkedEncodingTest method startHttpServer.

/**
     * Http Server
     */
static HttpServer startHttpServer() throws IOException {
    HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    HttpHandler httpHandler = new SimpleHandler();
    httpServer.createContext("/chunked/", httpHandler);
    httpServer.start();
    return httpServer;
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) HttpServer(com.sun.net.httpserver.HttpServer)

Example 7 with HttpHandler

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

the class ClassLoad method main.

public static void main(String[] args) throws Exception {
    boolean error = true;
    // Start a dummy server to return 404
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    HttpHandler handler = new HttpHandler() {

        public void handle(HttpExchange t) throws IOException {
            InputStream is = t.getRequestBody();
            while (is.read() != -1) ;
            t.sendResponseHeaders(404, -1);
            t.close();
        }
    };
    server.createContext("/", handler);
    server.start();
    // Client request
    try {
        URL url = new URL("http://localhost:" + server.getAddress().getPort());
        String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
        ClassLoader loader = new URLClassLoader(new URL[] { url });
        System.out.println(url);
        Class c = loader.loadClass(name);
        System.out.println("Loaded class \"" + c.getName() + "\".");
    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
        error = false;
    } finally {
        server.stop(0);
    }
    if (error)
        throw new RuntimeException("No ClassNotFoundException generated");
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) HttpServer(com.sun.net.httpserver.HttpServer) URLClassLoader(java.net.URLClassLoader)

Example 8 with HttpHandler

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

use of com.sun.net.httpserver.HttpHandler in project pinot by linkedin.

the class ServerTableSizeReaderTest method createHandler.

private HttpHandler createHandler(final int status, final TableSizeInfo tableSize, final int sleepTimeMs) {
    return new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            if (sleepTimeMs > 0) {
                try {
                    Thread.sleep(sleepTimeMs);
                } catch (InterruptedException e) {
                    LOGGER.info("Handler interrupted during sleep");
                }
            }
            String json = new ObjectMapper().writeValueAsString(tableSize);
            httpExchange.sendResponseHeaders(status, json.length());
            OutputStream responseBody = httpExchange.getResponseBody();
            responseBody.write(json.getBytes());
            responseBody.close();
        }
    };
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with HttpHandler

use of com.sun.net.httpserver.HttpHandler in project pinot by linkedin.

the class TableSizeReaderTest method createHandler.

private HttpHandler createHandler(final int status, final List<SegmentSizeInfo> segmentSizes, final int sleepTimeMs) {
    return new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            if (sleepTimeMs > 0) {
                try {
                    Thread.sleep(sleepTimeMs);
                } catch (InterruptedException e) {
                    LOGGER.info("Handler interrupted during sleep");
                }
            }
            TableSizeInfo tableInfo = new TableSizeInfo("myTable", 0);
            tableInfo.segments = segmentSizes;
            for (SegmentSizeInfo segmentSize : segmentSizes) {
                tableInfo.diskSizeInBytes += segmentSize.diskSizeInBytes;
            }
            String json = new ObjectMapper().writeValueAsString(tableInfo);
            httpExchange.sendResponseHeaders(status, json.length());
            OutputStream responseBody = httpExchange.getResponseBody();
            responseBody.write(json.getBytes());
            responseBody.close();
        }
    };
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) SegmentSizeInfo(com.linkedin.pinot.common.restlet.resources.SegmentSizeInfo) TableSizeInfo(com.linkedin.pinot.common.restlet.resources.TableSizeInfo) Matchers.anyString(org.mockito.Matchers.anyString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

HttpHandler (com.sun.net.httpserver.HttpHandler)16 HttpExchange (com.sun.net.httpserver.HttpExchange)12 InetSocketAddress (java.net.InetSocketAddress)12 HttpServer (com.sun.net.httpserver.HttpServer)9 IOException (java.io.IOException)8 OutputStream (java.io.OutputStream)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpContext (com.sun.net.httpserver.HttpContext)2 Set (java.util.Set)2 ExecutorService (java.util.concurrent.ExecutorService)2 Test (org.junit.Test)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 SegmentSizeInfo (com.linkedin.pinot.common.restlet.resources.SegmentSizeInfo)1 TableSizeInfo (com.linkedin.pinot.common.restlet.resources.TableSizeInfo)1 HttpFutureCallback (com.microsoft.azure.hdinsight.common.HttpFutureCallback)1 MultiHttpFutureCallback (com.microsoft.azure.hdinsight.common.MultiHttpFutureCallback)1 IClusterDetail (com.microsoft.azure.hdinsight.sdk.cluster.IClusterDetail)1 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)1 Headers (com.sun.net.httpserver.Headers)1 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)1