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();
}
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;
}
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;
}
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/");
}
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());
}
Aggregations