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