Package com.sun.net.httpserver

Examples of com.sun.net.httpserver.HttpServer


  }

  /** Tests that refresh gets a new cookie value. */
  public void testRefresh() throws RepositoryException, IOException {
    CookieHandler handler = new CookieHandler();
    HttpServer server = createServer(handler);
    server.start();

    out.getInputStream(0, 0, 0, 0).close();
    String cookie = handler.getCookie();
    assertTrue(cookie, cookie.startsWith("LLCookie="));

View Full Code Here


    out.setUrlBase("http://" + host + ":" + port);
    out.setUrlPath(path);
    out.initialize(connector, client);

    HttpServer server = HttpServer.create(new InetSocketAddress(host, port), 0);
    server.createContext(path, handler);
    return server;
  }
View Full Code Here

            default:
                System.err.println("--net must be one of main, test or regtest");
                return;
        }

        HttpServer server = createServer(portFlag, keystoreFlag, options);

        // Where will we store our projects and received pledges?
        if (options.has(dirFlag))
            AppDirectory.overrideAppDir(Paths.get(options.valueOf(dirFlag)));
        Path appDir = AppDirectory.initAppDir("lighthouse-server");   // Create dir if necessary.

        WalletAppKit kit = new WalletAppKit(params, appDir.toFile(), "lighthouse-server") {
            {
                walletFactory = PledgingWallet::new;
            }
        };
        if (kit.isChainFileLocked()) {
            log.error("App is already running. Please terminate the other instance or use a different directory (--dir=...)");
            return;
        }
        int minPeersSupportingGetUTXO = 2;   // Increase when the feature eventually rolls out to the network.
        if (options.has("local-node") || params == RegTestParams.get()) {
            kit.connectToLocalHost();
            minPeersSupportingGetUTXO = 1// Only local matters.
        }
        // Eventually take this out when getutxo is merged, released and people have upgraded.
        kit.setBlockingStartup(true)
           .setAutoSave(true)
           .setAutoStop(true)
           .setUserAgent("Lighthouse Server", "1.0")
           .startAsync()
           .awaitRunning();
        log.info("bitcoinj initialised");

        // Don't start up fully until we're properly set up. Eventually this can go away.
        // TODO: Make this also check for NODE_GETUTXOS flag.
        log.info("Waiting to find a peer that supports getutxo");
        kit.peerGroup().waitForPeersOfVersion(minPeersSupportingGetUTXO, GetUTXOsMessage.MIN_PROTOCOL_VERSION).get();
        log.info("Found ... starting web server on port {}", portFlag.value(options));

        // This app is mostly single threaded. It handles all requests and state changes on a single thread.
        // Speed should ideally not be an issue, as the backend blocks only rarely. If it's a problem then
        // we'll have to split the backend thread from the http server thread.
        AffinityExecutor.ServiceAffinityExecutor executor = new AffinityExecutor.ServiceAffinityExecutor("server");
        server.setExecutor(executor);
        LighthouseBackend backend = new LighthouseBackend(SERVER, kit.peerGroup(), kit.chain(), (PledgingWallet) kit.wallet(), executor);
        backend.setMinPeersForUTXOQuery(minPeersSupportingGetUTXO);
        server.createContext(LHUtils.HTTP_PATH_PREFIX, new ProjectHandler(backend));
        server.createContext("/", exchange -> {
            log.warn("404 Not Found: {}", exchange.getRequestURI());
            exchange.sendResponseHeaders(404, -1);
        });
        server.start();
    }
View Full Code Here

     * it uses that server to create a context. Otherwise, it creates a new
     * HTTP server. This sever is added to servers Map.
     */
    /*package*/ HttpContext createContext(String address) {
        try {
            HttpServer server;
            ServerState state;
            URL url = new URL(address);
            int port = url.getPort();
            if (port == -1) {
                port = url.getDefaultPort();
            }
            InetSocketAddress inetAddress = new InetSocketAddress(url.getHost(),
                port);
            synchronized(servers) {
                state = servers.get(inetAddress);
                if (state == null) {
                    logger.fine("Creating new HTTP Server at "+inetAddress);
                    // Creates server with default socket backlog
                    server = HttpServer.create(inetAddress, 0);
                    server.setExecutor(Executors.newCachedThreadPool());
                    String path = url.toURI().getPath();
                    logger.fine("Creating HTTP Context at = "+path);
                    HttpContext context = server.createContext(path);
                    server.start();

                    // we have to get actual inetAddress from server, which can differ from the original in some cases.
                    // e.g. A port number of zero will let the system pick up an ephemeral port in a bind operation,
                    // or IP: 0.0.0.0 - which is used to monitor network traffic from any valid IP address
                    inetAddress = server.getAddress();

                    logger.fine("HTTP server started = "+inetAddress);
                    state = new ServerState(server, path);
                    servers.put(inetAddress, state);
                    return context;
                }
            }
            server = state.getServer();
           
            if (state.getPaths().contains(url.getPath())) {
              String err = "Context with URL path "+url.getPath()+ " already exists on the server "+server.getAddress();
              logger.fine(err);
              throw new IllegalArgumentException(err)
            }
           
            logger.fine("Creating HTTP Context at = "+url.getPath());
            HttpContext context = server.createContext(url.getPath());
            state.oneMoreContext(url.getPath());
            return context;
        } catch(Exception e) {
            throw new ServerRtException("server.rt.err",e );
        }
View Full Code Here

    }

    static class EndpointStopper {

        EndpointStopper(final int port, final Endpoint endpoint) throws IOException {
            final HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
            final ExecutorService threads  = Executors.newFixedThreadPool(2);
            server.setExecutor(threads);
            server.start();

            HttpContext context = server.createContext("/stop");
            context.setHandler(new HttpHandler() {
                public void handle(HttpExchange msg) throws IOException {
                    System.out.println("Shutting down the Endpoint");
                    endpoint.stop();
                    System.out.println("Endpoint is down");
                    msg.sendResponseHeaders(200, 0);
                    msg.close();
                    server.stop(1);
                    threads.shutdown();
                }
            });
        }
View Full Code Here

import java.util.concurrent.Executors;

public class EndpointStopper {

    EndpointStopper(final int port, final Endpoint endpoint) throws IOException {
        final HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
        final ExecutorService threads  = Executors.newFixedThreadPool(2);
        server.setExecutor(threads);
        server.start();

        HttpContext context = server.createContext("/stop");
        context.setHandler(new HttpHandler() {
        public void handle(HttpExchange msg) throws IOException {
        System.out.println("Shutting down the Endpoint");
        endpoint.stop();
        System.out.println("Endpoint is down");
                msg.sendResponseHeaders(200, 0);
                msg.close();
            server.stop(1);
            threads.shutdown();
      }
    });
    }
View Full Code Here

    }

    static class EndpointStopper {

        EndpointStopper(final int port, final Endpoint endpoint) throws IOException {
            final HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
            final ExecutorService threads  = Executors.newFixedThreadPool(2);
            server.setExecutor(threads);
            server.start();

            HttpContext context = server.createContext("/stop");
            context.setHandler(new HttpHandler() {
                public void handle(HttpExchange msg) throws IOException {
                    System.out.println("Shutting down the Endpoint");
                    endpoint.stop();
                    System.out.println("Endpoint is down");
                    msg.sendResponseHeaders(200, 0);
                    msg.close();
                    server.stop(1);
                    threads.shutdown();
                }
            });
        }
View Full Code Here

import java.util.concurrent.Executors;

public class EndpointStopper {

    EndpointStopper(final int port, final Endpoint endpoint) throws IOException {
        final HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
        final ExecutorService threads  = Executors.newFixedThreadPool(2);
        server.setExecutor(threads);
        server.start();

        HttpContext context = server.createContext("/stop");
        context.setHandler(new HttpHandler() {
        public void handle(HttpExchange msg) throws IOException {
        System.out.println("Shutting down the Endpoint");
        endpoint.stop();
        System.out.println("Endpoint is down");
                msg.sendResponseHeaders(200, 0);
                msg.close();
            server.stop(1);
            threads.shutdown();
      }
    });
    }
View Full Code Here

TOP

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

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.