Package com.sun.net.httpserver

Examples of com.sun.net.httpserver.HttpHandler


        final int port = 29484;
        InetSocketAddress address = new InetSocketAddress(port);
        HttpServer httpServer = HttpServer.create(address, 0);

        final Element expectedResponse = new Element("resource").addContent(new Element("id").setText("test"));
        HttpHandler finalHandler = new HttpHandler() {

            @Override
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = Xml.getString(expectedResponse).getBytes();
                exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK,
                        response.length);
                exchange.getResponseBody().write(response);
                exchange.close();
            }
        };
        final String finalUrlPath = "/final.xml";
        httpServer.createContext(finalUrlPath, finalHandler);

        HttpHandler permRedirectHandler = new HttpHandler() {

            @Override
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = finalUrlPath.getBytes();
                exchange.getResponseHeaders().add("location", finalUrlPath);
                exchange.sendResponseHeaders(HttpURLConnection.HTTP_MOVED_PERM,
                        response.length);
                exchange.getResponseBody().write(response);
                exchange.close();
            }
        };
        final String permUrlPath = "/permRedirect.xml";
        httpServer.createContext(permUrlPath, permRedirectHandler);

        HttpHandler tempRedirectHandler = new HttpHandler() {

            @Override
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = finalUrlPath.getBytes();
                exchange.getResponseHeaders().add("location", finalUrlPath);
View Full Code Here


    /**
     * 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;
    }
View Full Code Here

    this.server = HttpServer.create(addr, 0);

    Receiver receiver = new ReceiverImpl(uberClassLoader, createContext());
    final RemoteControlHttpHandler handler = new RemoteControlHttpHandler(receiver);

    server.createContext("/", new HttpHandler()
    {
      @Override
      public void handle(final HttpExchange exchange) throws IOException {
        int id = requests.incrementAndGet();
        log.debug("Handling remote request: [{}] {}", id, exchange);
View Full Code Here

        targetServer.stop(0);
    }

    @Test
    public void testGetHeaders() throws Exception {
        targetServer.createContext("/request", new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                final Headers responseHeaders = httpExchange.getResponseHeaders();
                responseHeaders.add("Content-Type", "application/json; charset=utf8");
                httpExchange.sendResponseHeaders(200, 0);
View Full Code Here

        final HttpProxy httpProxy = new HttpProxy();
        httpProxy.setHost(LOCALHOST);
        httpProxy.setPort(PROXY_PORT);

        final String path = "/request";
        proxyServer.createContext(path, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                respond(httpExchange, MESSAGE_FROM_PROXY, 200);
            }
        });
View Full Code Here

        httpProxy.setHost(LOCALHOST);
        httpProxy.setPort(HTTPS_PROXY_PORT);
        httpProxy.setUsername("username");

        final String path = "/username";
        httpsServer.createContext(path, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                final String authorization = httpExchange.getRequestHeaders().getFirst("Authorization");
                if (authorization == null) {
                    httpExchange.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"Test Site\"");
View Full Code Here

        httpProxy.setPort(HTTPS_PROXY_PORT);
        httpProxy.setUsername("username");
        httpProxy.setPassword("password");

        final String path = "/usernameAndPassword";
        httpsServer.createContext(path, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                final String authorization = httpExchange.getRequestHeaders().getFirst("Authorization");
                if (authorization == null) {
                    httpExchange.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"Test Site\"");
View Full Code Here

        dnsHostMatcher.setHost("google.com");
        httpProxy.setMatchers(Lists.newArrayList(dnsHostMatcher));
        final String message = "Target was reached without proxy";

        final String path = "/nomatch";
        targetServer.createContext(path, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                respond(httpExchange, message, 200);
            }
        });
        proxyServer.createContext(path, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                String msg = "Proxy was reached but in this test the proxy should not have been used.";
                respond(httpExchange, msg, 500);
            }
View Full Code Here

    @Test
    public void testToHttpsBehaviour() throws Exception {
        final String message = "Message from server";

        final String path = "/username";
        httpsServer.createContext(path, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                final String authorization = httpExchange.getRequestHeaders().getFirst("Authorization");
                if (authorization == null) {
                    httpExchange.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"Test Site\"");
View Full Code Here

        httpServer = HttpServer.create(new InetSocketAddress(port), 0);
        httpServer.setExecutor(null);
    }

    public void mapping(final String context, final String data) {
        httpServer.createContext(context, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                httpExchange.sendResponseHeaders(200, data.length());
                BufferedOutputStream os = new BufferedOutputStream(httpExchange.getResponseBody());
                os.write(data.getBytes());
View Full Code Here

TOP

Related Classes of com.sun.net.httpserver.HttpHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.