Search in sources :

Example 1 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jetty.project by eclipse.

the class JettyHttpServerProvider method createHttpServer.

@Override
public HttpServer createHttpServer(InetSocketAddress addr, int backlog) throws IOException {
    Server server = _server;
    boolean shared = true;
    if (server == null) {
        ThreadPool threadPool = new DelegatingThreadPool(new QueuedThreadPool());
        server = new Server(threadPool);
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { new ContextHandlerCollection(), new DefaultHandler() });
        server.setHandler(handlerCollection);
        shared = false;
    }
    JettyHttpServer jettyHttpServer = new JettyHttpServer(server, shared);
    jettyHttpServer.bind(addr, backlog);
    return jettyHttpServer;
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) HttpsServer(com.sun.net.httpserver.HttpsServer) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 2 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jetty.project by eclipse.

the class TestSPIServer method main.

public static void main(String[] args) throws Exception {
    String host = "localhost";
    int port = 8080;
    HttpServer server = new JettyHttpServerProvider().createHttpServer(new InetSocketAddress(host, port), 10);
    server.start();
    final HttpContext httpContext = server.createContext("/", new HttpHandler() {

        public void handle(HttpExchange exchange) throws IOException {
            Headers responseHeaders = exchange.getResponseHeaders();
            responseHeaders.set("Content-Type", "text/plain");
            exchange.sendResponseHeaders(200, 0);
            OutputStream responseBody = exchange.getResponseBody();
            Headers requestHeaders = exchange.getRequestHeaders();
            Set<String> keySet = requestHeaders.keySet();
            Iterator<String> iter = keySet.iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                List values = requestHeaders.get(key);
                String s = key + " = " + values.toString() + "\n";
                responseBody.write(s.getBytes());
            }
            responseBody.close();
        }
    });
    httpContext.setAuthenticator(new BasicAuthenticator("Test") {

        @Override
        public boolean checkCredentials(String username, String password) {
            if ("username".equals(username) && password.equals("password"))
                return true;
            return false;
        }
    });
    Thread.sleep(10000000);
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) HttpContext(com.sun.net.httpserver.HttpContext) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) HttpServer(com.sun.net.httpserver.HttpServer) Iterator(java.util.Iterator) List(java.util.List)

Example 3 with HttpServer

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

the class RestClientMultipleHostsIntegTests method startHttpServer.

@BeforeClass
public static void startHttpServer() throws Exception {
    String pathPrefixWithoutLeadingSlash;
    if (randomBoolean()) {
        pathPrefixWithoutLeadingSlash = "testPathPrefix/" + randomAsciiOfLengthBetween(1, 5);
        pathPrefix = "/" + pathPrefixWithoutLeadingSlash;
    } else {
        pathPrefix = pathPrefixWithoutLeadingSlash = "";
    }
    int numHttpServers = randomIntBetween(2, 4);
    httpServers = new HttpServer[numHttpServers];
    HttpHost[] httpHosts = new HttpHost[numHttpServers];
    for (int i = 0; i < numHttpServers; i++) {
        HttpServer httpServer = createHttpServer();
        httpServers[i] = httpServer;
        httpHosts[i] = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
    }
    RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
    if (pathPrefix.length() > 0) {
        restClientBuilder.setPathPrefix((randomBoolean() ? "/" : "") + pathPrefixWithoutLeadingSlash);
    }
    restClient = restClientBuilder.build();
}
Also used : HttpHost(org.apache.http.HttpHost) HttpServer(com.sun.net.httpserver.HttpServer) MockHttpServer(org.elasticsearch.mocksocket.MockHttpServer) BeforeClass(org.junit.BeforeClass)

Example 4 with HttpServer

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

the class RestClientMultipleHostsIntegTests method stopRandomHost.

@Before
public void stopRandomHost() {
    //verify that shutting down some hosts doesn't matter as long as one working host is left behind
    if (httpServers.length > 1 && randomBoolean()) {
        List<HttpServer> updatedHttpServers = new ArrayList<>(httpServers.length - 1);
        int nodeIndex = randomInt(httpServers.length - 1);
        for (int i = 0; i < httpServers.length; i++) {
            HttpServer httpServer = httpServers[i];
            if (i == nodeIndex) {
                httpServer.stop(0);
            } else {
                updatedHttpServers.add(httpServer);
            }
        }
        httpServers = updatedHttpServers.toArray(new HttpServer[updatedHttpServers.size()]);
    }
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpServer(com.sun.net.httpserver.HttpServer) MockHttpServer(org.elasticsearch.mocksocket.MockHttpServer) Before(org.junit.Before)

Example 5 with HttpServer

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

the class RestClientSingleHostIntegTests 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)

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