Search in sources :

Example 6 with HttpContext

use of com.sun.net.httpserver.HttpContext in project spring-framework by spring-projects.

the class SimpleHttpServerJaxWsServiceExporter method buildHttpContext.

/**
	 * Build the HttpContext for the given endpoint.
	 * @param endpoint the JAX-WS Provider Endpoint object
	 * @param serviceName the given service name
	 * @return the fully populated HttpContext
	 */
protected HttpContext buildHttpContext(Endpoint endpoint, String serviceName) {
    String fullPath = calculateEndpointPath(endpoint, serviceName);
    HttpContext httpContext = this.server.createContext(fullPath);
    if (this.filters != null) {
        httpContext.getFilters().addAll(this.filters);
    }
    if (this.authenticator != null) {
        httpContext.setAuthenticator(this.authenticator);
    }
    return httpContext;
}
Also used : HttpContext(com.sun.net.httpserver.HttpContext)

Example 7 with HttpContext

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

the class HeadTest method server.

static void server() throws Exception {
    InetSocketAddress inetAddress = new InetSocketAddress(0);
    HttpServer server = HttpServer.create(inetAddress, 5);
    try {
        server.setExecutor(Executors.newFixedThreadPool(5));
        HttpContext chunkedContext = server.createContext("/chunked");
        chunkedContext.setHandler(new HttpHandler() {

            @Override
            public void handle(HttpExchange msg) {
                try {
                    try {
                        if (msg.getRequestMethod().equals("HEAD")) {
                            msg.getRequestBody().close();
                            msg.getResponseHeaders().add("Transfer-encoding", "chunked");
                            msg.sendResponseHeaders(200, -1);
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                } finally {
                    msg.close();
                }
            }
        });
        HttpContext clContext = server.createContext("/content");
        clContext.setHandler(new HttpHandler() {

            @Override
            public void handle(HttpExchange msg) {
                try {
                    try {
                        if (msg.getRequestMethod().equals("HEAD")) {
                            msg.getRequestBody().close();
                            msg.getResponseHeaders().add("Content-length", "1024");
                            msg.sendResponseHeaders(200, -1);
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                } finally {
                    msg.close();
                }
            }
        });
        server.start();
        String urlStr = "http://localhost:" + server.getAddress().getPort() + "/";
        System.out.println("Server is at " + urlStr);
        // Run the chunked client
        for (int i = 0; i < 10; i++) {
            runClient(urlStr + "chunked/");
        }
        // Run the content length client
        for (int i = 0; i < 10; i++) {
            runClient(urlStr + "content/");
        }
    } finally {
        // Stop the server
        ((ExecutorService) server.getExecutor()).shutdown();
        server.stop(0);
    }
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpContext(com.sun.net.httpserver.HttpContext) HttpExchange(com.sun.net.httpserver.HttpExchange) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException)

Example 8 with HttpContext

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

the class Deadlock method main.

public static void main(String[] args) throws Exception {
    Handler handler = new Handler();
    InetSocketAddress addr = new InetSocketAddress(0);
    HttpServer server = HttpServer.create(addr, 0);
    HttpContext ctx = server.createContext("/test", handler);
    BasicAuthenticator a = new BasicAuthenticator("foobar@test.realm") {

        @Override
        public boolean checkCredentials(String username, String pw) {
            return "fred".equals(username) && pw.charAt(0) == 'x';
        }
    };
    ctx.setAuthenticator(a);
    ExecutorService executor = Executors.newCachedThreadPool();
    server.setExecutor(executor);
    server.start();
    java.net.Authenticator.setDefault(new MyAuthenticator());
    System.out.print("Deadlock: ");
    for (int i = 0; i < 2; i++) {
        Runner t = new Runner(server, i);
        t.start();
        t.join();
    }
    server.stop(2);
    executor.shutdown();
    if (error) {
        throw new RuntimeException("test failed error");
    }
    if (count != 2) {
        throw new RuntimeException("test failed count = " + count);
    }
    System.out.println("OK");
}
Also used : BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpContext(com.sun.net.httpserver.HttpContext) ExecutorService(java.util.concurrent.ExecutorService) HttpHandler(com.sun.net.httpserver.HttpHandler)

Aggregations

HttpContext (com.sun.net.httpserver.HttpContext)8 InetSocketAddress (java.net.InetSocketAddress)7 HttpServer (com.sun.net.httpserver.HttpServer)6 HttpHandler (com.sun.net.httpserver.HttpHandler)4 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)3 IOException (java.io.IOException)3 HttpExchange (com.sun.net.httpserver.HttpExchange)2 URL (java.net.URL)2 ExecutorService (java.util.concurrent.ExecutorService)2 Headers (com.sun.net.httpserver.Headers)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Enumeration (java.util.Enumeration)1