Search in sources :

Example 11 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 12 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)

Example 13 with HttpContext

use of com.sun.net.httpserver.HttpContext in project pentaho-kettle by pentaho.

the class GetHandler method startHTTPServer.

private static void startHTTPServer() throws IOException {
    httpServer = HttpServer.create(new InetSocketAddress(JobEntryHTTP_PDI208_Test.HTTP_HOST, JobEntryHTTP_PDI208_Test.HTTP_PORT), 10);
    HttpContext uploadFileContext = httpServer.createContext("/uploadFile", new JobEntryHTTP_PDI_18044_Test_Handler());
    // HttpContext getContext = httpServer.createContext( "/get", new GetHandler() );
    uploadFileContext.setAuthenticator(new BasicAuthenticator("uploadFile") {

        @Override
        public boolean checkCredentials(String username, String password) {
            return username.equals("admin") && password.equals("password");
        }
    });
    httpServer.start();
}
Also used : BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) InetSocketAddress(java.net.InetSocketAddress) HttpContext(com.sun.net.httpserver.HttpContext) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString)

Aggregations

HttpContext (com.sun.net.httpserver.HttpContext)13 InetSocketAddress (java.net.InetSocketAddress)10 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)7 HttpServer (com.sun.net.httpserver.HttpServer)7 HttpHandler (com.sun.net.httpserver.HttpHandler)5 IOException (java.io.IOException)4 HttpExchange (com.sun.net.httpserver.HttpExchange)2 URL (java.net.URL)2 ExecutorService (java.util.concurrent.ExecutorService)2 FailureException (com.github.noraui.exception.FailureException)1 TechnicalException (com.github.noraui.exception.TechnicalException)1 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