Search in sources :

Example 1 with NetworkListener

use of org.glassfish.grizzly.http.server.NetworkListener in project jersey by jersey.

the class App method startServer.

/**
     * Starts Grizzly HTTP server exposing static content, JAX-RS resources
     * and web sockets defined in this application.
     *
     * @param webRootPath static content root path.
     * @return Grizzly HTTP server.
     */
public static HttpServer startServer(String webRootPath) {
    final HttpServer server = new HttpServer();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            server.shutdownNow();
        }
    }));
    final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT);
    server.addListener(listener);
    final ServerConfiguration config = server.getServerConfiguration();
    // add handler for serving static content
    config.addHttpHandler(new StaticContentHandler(webRootPath), APP_PATH);
    // add handler for serving JAX-RS resources
    config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), API_PATH);
    try {
        // Start the server.
        server.start();
    } catch (Exception ex) {
        throw new ProcessingException("Exception thrown when trying to start grizzly server", ex);
    }
    return server;
}
Also used : ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) HttpServer(org.glassfish.grizzly.http.server.HttpServer) GrizzlyHttpContainer(org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with NetworkListener

use of org.glassfish.grizzly.http.server.NetworkListener in project graylog2-server by Graylog2.

the class JerseyService method setUp.

private HttpServer setUp(String namePrefix, URI listenUri, SSLEngineConfigurator sslEngineConfigurator, int threadPoolSize, int selectorRunnersCount, int maxInitialLineLength, int maxHeaderSize, boolean enableGzip, boolean enableCors, Set<Resource> additionalResources, String[] controllerPackages) throws GeneralSecurityException, IOException {
    final ResourceConfig resourceConfig = buildResourceConfig(enableCors, additionalResources, controllerPackages);
    final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(listenUri, resourceConfig, sslEngineConfigurator != null, sslEngineConfigurator, false);
    final NetworkListener listener = httpServer.getListener("grizzly");
    listener.setMaxHttpHeaderSize(maxInitialLineLength);
    listener.setMaxRequestHeaders(maxHeaderSize);
    final ExecutorService workerThreadPoolExecutor = instrumentedExecutor(namePrefix + "-worker-executor", namePrefix + "-worker-%d", threadPoolSize);
    listener.getTransport().setWorkerThreadPool(workerThreadPoolExecutor);
    // The Grizzly default value is equal to `Runtime.getRuntime().availableProcessors()` which doesn't make
    // sense for Graylog because we are not mainly a web server.
    // See "Selector runners count" at https://grizzly.java.net/bestpractices.html for details.
    listener.getTransport().setSelectorRunnersCount(selectorRunnersCount);
    listener.setDefaultErrorPageGenerator(errorPageGenerator);
    if (enableGzip) {
        final CompressionConfig compressionConfig = listener.getCompressionConfig();
        compressionConfig.setCompressionMode(CompressionConfig.CompressionMode.ON);
        compressionConfig.setCompressionMinSize(512);
    }
    return httpServer;
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) InstrumentedExecutorService(com.codahale.metrics.InstrumentedExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) CompressionConfig(org.glassfish.grizzly.http.CompressionConfig)

Example 3 with NetworkListener

use of org.glassfish.grizzly.http.server.NetworkListener in project jersey by jersey.

the class GrizzlyHttpServerFactory method createHttpServer.

/**
     * Create new {@link HttpServer} instance.
     *
     * @param uri                   uri on which the {@link ApplicationHandler} will be deployed. Only first path
     *                              segment will be used as context path, the rest will be ignored.
     * @param handler               {@link HttpHandler} instance.
     * @param secure                used for call {@link NetworkListener#setSecure(boolean)}.
     * @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
     * @param start                 if set to false, server will not get started, this allows end users to set
     *                              additional properties on the underlying listener.
     * @return newly created {@code HttpServer}.
     * @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
     * @see GrizzlyHttpContainer
     */
public static HttpServer createHttpServer(final URI uri, final GrizzlyHttpContainer handler, final boolean secure, final SSLEngineConfigurator sslEngineConfigurator, final boolean start) {
    final String host = (uri.getHost() == null) ? NetworkListener.DEFAULT_NETWORK_HOST : uri.getHost();
    final int port = (uri.getPort() == -1) ? (secure ? Container.DEFAULT_HTTPS_PORT : Container.DEFAULT_HTTP_PORT) : uri.getPort();
    final NetworkListener listener = new NetworkListener("grizzly", host, port);
    listener.getTransport().getWorkerThreadPoolConfig().setThreadFactory(new ThreadFactoryBuilder().setNameFormat("grizzly-http-server-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build());
    listener.setSecure(secure);
    if (sslEngineConfigurator != null) {
        listener.setSSLEngineConfig(sslEngineConfigurator);
    }
    final HttpServer server = new HttpServer();
    server.addListener(listener);
    // Map the path to the processor.
    final ServerConfiguration config = server.getServerConfiguration();
    if (handler != null) {
        final String path = uri.getPath().replaceAll("/{2,}", "/");
        final String contextPath = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
        config.addHttpHandler(handler, HttpHandlerRegistration.bulder().contextPath(contextPath).build());
    }
    config.setPassTraceRequest(true);
    config.setDefaultQueryEncoding(Charsets.UTF8_CHARSET);
    if (start) {
        try {
            // Start the server.
            server.start();
        } catch (final IOException ex) {
            server.shutdownNow();
            throw new ProcessingException(LocalizationMessages.FAILED_TO_START_SERVER(ex.getMessage()), ex);
        }
    }
    return server;
}
Also used : JerseyProcessingUncaughtExceptionHandler(org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler) ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ThreadFactoryBuilder(org.glassfish.jersey.internal.guava.ThreadFactoryBuilder) IOException(java.io.IOException) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) ProcessingException(javax.ws.rs.ProcessingException)

Example 4 with NetworkListener

use of org.glassfish.grizzly.http.server.NetworkListener in project dukescript-presenters by dukescript.

the class DynamicHTTP method initServer.

static URI initServer() throws Exception {
    server = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
    final WebSocketAddOn addon = new WebSocketAddOn();
    for (NetworkListener listener : server.getListeners()) {
        listener.registerAddOn(addon);
    }
    resources = new ArrayList<Resource>();
    conf = server.getServerConfiguration();
    final DynamicHTTP dh = new DynamicHTTP();
    conf.addHttpHandler(dh, "/");
    server.start();
    return pageURL("http", server, "/test.html");
}
Also used : PortRange(org.glassfish.grizzly.PortRange) WebSocketAddOn(org.glassfish.grizzly.websockets.WebSocketAddOn) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener)

Example 5 with NetworkListener

use of org.glassfish.grizzly.http.server.NetworkListener in project dukescript-presenters by dukescript.

the class DynamicHTTP method pageURL.

private static URI pageURL(String proto, HttpServer server, final String page) {
    NetworkListener listener = server.getListeners().iterator().next();
    int port = listener.getPort();
    try {
        return new URI(proto + "://localhost:" + port + page);
    } catch (URISyntaxException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener)

Aggregations

NetworkListener (org.glassfish.grizzly.http.server.NetworkListener)20 HttpServer (org.glassfish.grizzly.http.server.HttpServer)11 IOException (java.io.IOException)7 ProcessingException (javax.ws.rs.ProcessingException)5 ServerConfiguration (org.glassfish.grizzly.http.server.ServerConfiguration)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 CompressionConfig (org.glassfish.grizzly.http.CompressionConfig)3 CLStaticHttpHandler (org.glassfish.grizzly.http.server.CLStaticHttpHandler)3 SSLEngineConfigurator (org.glassfish.grizzly.ssl.SSLEngineConfigurator)3 GrizzlyHttpContainer (org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer)3 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)2 BindException (java.net.BindException)2 ExecutorService (java.util.concurrent.ExecutorService)2 PortRange (org.glassfish.grizzly.PortRange)2 TCPNIOTransport (org.glassfish.grizzly.nio.transport.TCPNIOTransport)2 SpdyAddOn (org.glassfish.grizzly.spdy.SpdyAddOn)2 ThreadPoolConfig (org.glassfish.grizzly.threadpool.ThreadPoolConfig)2 WebSocketAddOn (org.glassfish.grizzly.websockets.WebSocketAddOn)2 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)2