Search in sources :

Example 1 with CLStaticHttpHandler

use of org.glassfish.grizzly.http.server.CLStaticHttpHandler in project OpenTripPlanner by opentripplanner.

the class GrizzlyServer method run.

/**
 * This function goes through roughly the same steps as Jersey's GrizzlyServerFactory, but we instead construct
 * an HttpServer and NetworkListener manually so we can set the number of threads and other details.
 */
public void run() {
    LOG.info("Starting OTP Grizzly server on ports {} (HTTP) and {} (HTTPS) of interface {}", params.port, params.securePort, params.bindAddress);
    LOG.info("OTP server base path is {}", params.basePath);
    HttpServer httpServer = new HttpServer();
    /* Configure SSL */
    SSLContextConfigurator sslConfig = new SSLContextConfigurator();
    sslConfig.setKeyStoreFile(new File(params.basePath, "keystore").getAbsolutePath());
    sslConfig.setKeyStorePass("opentrip");
    /* Set up a pool of threads to handle incoming HTTP requests. */
    // TODO we should probably use Grizzly async processing rather than tying up the HTTP handler threads.
    ThreadPoolConfig threadPoolConfig = ThreadPoolConfig.defaultConfig().setCorePoolSize(MIN_THREADS).setMaxPoolSize(getMaxThreads());
    /* HTTP (non-encrypted) listener */
    NetworkListener httpListener = new NetworkListener("otp_insecure", params.bindAddress, params.port);
    httpListener.setSecure(false);
    /* HTTPS listener */
    NetworkListener httpsListener = new NetworkListener("otp_secure", params.bindAddress, params.securePort);
    // Ideally we'd share the threads between HTTP and HTTPS.
    httpsListener.setSecure(true);
    httpsListener.setSSLEngineConfig(new SSLEngineConfigurator(sslConfig).setClientMode(false).setNeedClientAuth(false));
    // For both HTTP and HTTPS listeners: enable gzip compression, set thread pool, add listener to httpServer.
    for (NetworkListener listener : new NetworkListener[] { httpListener, httpsListener }) {
        CompressionConfig cc = listener.getCompressionConfig();
        cc.setCompressionMode(CompressionConfig.CompressionMode.ON);
        // the min number of bytes to compress
        cc.setCompressionMinSize(50000);
        // the mime types to compress
        cc.setCompressableMimeTypes("application/json", "text/json");
        listener.getTransport().setWorkerThreadPoolConfig(threadPoolConfig);
        httpServer.addListener(listener);
    }
    /* Add a few handlers (~= servlets) to the Grizzly server. */
    /* 1. A Grizzly wrapper around the Jersey Application. */
    Application app = new OTPApplication(server, !params.insecure);
    HttpHandler dynamicHandler = ContainerFactory.createContainer(HttpHandler.class, app);
    httpServer.getServerConfiguration().addHttpHandler(dynamicHandler, "/otp/");
    /* 2. A static content handler to serve the client JS apps etc. from the classpath. */
    CLStaticHttpHandler staticHandler = new CLStaticHttpHandler(GrizzlyServer.class.getClassLoader(), "/client/");
    if (params.disableFileCache) {
        LOG.info("Disabling HTTP server static file cache.");
        staticHandler.setFileCacheEnabled(false);
    }
    httpServer.getServerConfiguration().addHttpHandler(staticHandler, "/");
    /* 3. A static content handler to serve local files from the filesystem, under the "local" path. */
    if (params.clientDirectory != null) {
        StaticHttpHandler localHandler = new StaticHttpHandler(params.clientDirectory.getAbsolutePath());
        localHandler.setFileCacheEnabled(false);
        httpServer.getServerConfiguration().addHttpHandler(localHandler, "/local");
    }
    /* 3. Test alternate HTTP handling without Jersey. */
    // As in servlets, * is needed in base path to identify the "rest" of the path.
    // GraphService gs = (GraphService) iocFactory.getComponentProvider(GraphService.class).getInstance();
    // Graph graph = gs.getGraph();
    // httpServer.getServerConfiguration().addHttpHandler(new OTPHttpHandler(graph), "/test/*");
    // Add shutdown hook to gracefully shut down Grizzly.
    // Signal handling (sun.misc.Signal) is potentially not available on all JVMs.
    Thread shutdownThread = new Thread(httpServer::shutdown);
    Runtime.getRuntime().addShutdownHook(shutdownThread);
    /* RELINQUISH CONTROL TO THE SERVER THREAD */
    try {
        httpServer.start();
        LOG.info("Grizzly server running.");
        Thread.currentThread().join();
    } catch (BindException be) {
        LOG.error("Cannot bind to port {}. Is it already in use?", params.port);
    } catch (IOException ioe) {
        LOG.error("IO exception while starting server.");
    } catch (InterruptedException ie) {
        LOG.info("Interrupted, shutting down.");
    }
    // Clean up graceful shutdown hook before shutting down Grizzly.
    Runtime.getRuntime().removeShutdownHook(shutdownThread);
    httpServer.shutdown();
}
Also used : StaticHttpHandler(org.glassfish.grizzly.http.server.StaticHttpHandler) HttpHandler(org.glassfish.grizzly.http.server.HttpHandler) CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) BindException(java.net.BindException) ThreadPoolConfig(org.glassfish.grizzly.threadpool.ThreadPoolConfig) IOException(java.io.IOException) StaticHttpHandler(org.glassfish.grizzly.http.server.StaticHttpHandler) CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) SSLEngineConfigurator(org.glassfish.grizzly.ssl.SSLEngineConfigurator) HttpServer(org.glassfish.grizzly.http.server.HttpServer) File(java.io.File) Application(javax.ws.rs.core.Application) SSLContextConfigurator(org.glassfish.grizzly.ssl.SSLContextConfigurator) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) CompressionConfig(org.glassfish.grizzly.http.CompressionConfig)

Example 2 with CLStaticHttpHandler

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

the class Main method startServer.

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 CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH);
    // add handler for serving JAX-RS resources
    config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_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 : CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) HttpServer(org.glassfish.grizzly.http.server.HttpServer) GrizzlyHttpContainer(org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer) ProcessingException(javax.ws.rs.ProcessingException) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) ProcessingException(javax.ws.rs.ProcessingException)

Example 3 with CLStaticHttpHandler

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

the class Main method startServer.

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 CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH);
    // add handler for serving JAX-RS resources
    config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_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 : CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) HttpServer(org.glassfish.grizzly.http.server.HttpServer) GrizzlyHttpContainer(org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer) ProcessingException(javax.ws.rs.ProcessingException) IOException(java.io.IOException) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) ProcessingException(javax.ws.rs.ProcessingException)

Example 4 with CLStaticHttpHandler

use of org.glassfish.grizzly.http.server.CLStaticHttpHandler in project pinot by linkedin.

the class AdminApiApplication method setupSwagger.

private void setupSwagger(HttpServer httpServer) {
    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setTitle("Pinot Server API");
    beanConfig.setDescription("APIs for accessing Pinot server information");
    beanConfig.setContact("https://github.com/linkedin/pinot");
    beanConfig.setVersion("1.0");
    beanConfig.setSchemes(new String[] { "http" });
    beanConfig.setBasePath(baseUri.getPath());
    beanConfig.setResourcePackage(RESOURCE_PACKAGE);
    beanConfig.setScan(true);
    CLStaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(AdminApiApplication.class.getClassLoader(), "/api/");
    // map both /api and /help to swagger docs. /api because it looks nice. /help for backward compatibility
    httpServer.getServerConfiguration().addHttpHandler(staticHttpHandler, "/api");
    httpServer.getServerConfiguration().addHttpHandler(staticHttpHandler, "/help");
    URL swaggerDistLocation = AdminApiApplication.class.getClassLoader().getResource("META-INF/resources/webjars/swagger-ui/2.2.2/");
    CLStaticHttpHandler swaggerDist = new CLStaticHttpHandler(new URLClassLoader(new URL[] { swaggerDistLocation }));
    httpServer.getServerConfiguration().addHttpHandler(swaggerDist, "/swaggerui-dist/");
}
Also used : BeanConfig(io.swagger.jaxrs.config.BeanConfig) CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 5 with CLStaticHttpHandler

use of org.glassfish.grizzly.http.server.CLStaticHttpHandler in project nuls by nuls-io.

the class RpcServerServiceImpl method startServer.

@Override
public void startServer(String ip, int port) {
    URI serverURI = UriBuilder.fromUri("http://" + ip).port(port).build();
    final Map<String, Object> initParams = new HashMap<>();
    initParams.put("jersey.config.server.provider.packages", RpcConstant.PACKAGES);
    initParams.put("load-on-startup", "1");
    NulsResourceConfig rc = new NulsResourceConfig();
    rc.addProperties(initParams);
    httpServer = GrizzlyHttpServerFactory.createHttpServer(serverURI, rc);
    try {
        httpServer.start();
        ClassLoader loader = this.getClass().getClassLoader();
        CLStaticHttpHandler docsHandler = new CLStaticHttpHandler(loader, "swagger-ui/");
        docsHandler.setFileCacheEnabled(false);
        ServerConfiguration cfg = httpServer.getServerConfiguration();
        cfg.addHttpHandler(docsHandler, "/docs/");
    } catch (IOException e) {
        Log.error(e);
    }
    Log.info("http restFul server is started!url is " + serverURI.toString());
}
Also used : CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) NulsResourceConfig(io.nuls.rpc.resources.impl.NulsResourceConfig) HashMap(java.util.HashMap) ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) IOException(java.io.IOException) URI(java.net.URI)

Aggregations

CLStaticHttpHandler (org.glassfish.grizzly.http.server.CLStaticHttpHandler)5 IOException (java.io.IOException)3 HttpServer (org.glassfish.grizzly.http.server.HttpServer)3 NetworkListener (org.glassfish.grizzly.http.server.NetworkListener)3 ServerConfiguration (org.glassfish.grizzly.http.server.ServerConfiguration)3 ProcessingException (javax.ws.rs.ProcessingException)2 GrizzlyHttpContainer (org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer)2 NulsResourceConfig (io.nuls.rpc.resources.impl.NulsResourceConfig)1 BeanConfig (io.swagger.jaxrs.config.BeanConfig)1 File (java.io.File)1 BindException (java.net.BindException)1 URI (java.net.URI)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 Application (javax.ws.rs.core.Application)1 CompressionConfig (org.glassfish.grizzly.http.CompressionConfig)1 HttpHandler (org.glassfish.grizzly.http.server.HttpHandler)1 StaticHttpHandler (org.glassfish.grizzly.http.server.StaticHttpHandler)1 SSLContextConfigurator (org.glassfish.grizzly.ssl.SSLContextConfigurator)1