Search in sources :

Example 1 with ServerConfiguration

use of org.glassfish.grizzly.http.server.ServerConfiguration 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 ServerConfiguration

use of org.glassfish.grizzly.http.server.ServerConfiguration 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 3 with ServerConfiguration

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

the class KoBrowserTest method compatibilityTests.

@Factory
public static Object[] compatibilityTests() throws Exception {
    Browser.LOG.setLevel(Level.FINE);
    Browser.LOG.addHandler(new ConsoleHandler());
    final BrowserBuilder bb = BrowserBuilder.newBrowser(new Browser("KoBrowserTest")).loadClass(KoBrowserTest.class).loadPage("empty.html").invoke("initialized");
    Executors.newSingleThreadExecutor().submit(new Runnable() {

        @Override
        public void run() {
            bb.showAndWait();
        }
    });
    List<Object> res = new ArrayList<Object>();
    Class<? extends Annotation> test = loadClass().getClassLoader().loadClass(KOTest.class.getName()).asSubclass(Annotation.class);
    Class[] arr = (Class[]) loadClass().getDeclaredMethod("tests").invoke(null);
    final HttpServer s = Browser.findServer(browserPresenter);
    ServerConfiguration conf = s.getServerConfiguration();
    conf.addHttpHandler(new DynamicHTTP(s), "/dynamic");
    for (Class c : arr) {
        for (Method m : c.getMethods()) {
            if (m.getAnnotation(test) != null) {
                res.add(new KOScript(browserPresenter, m));
            }
        }
    }
    return res.toArray();
}
Also used : ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ConsoleHandler(java.util.logging.ConsoleHandler) BrowserBuilder(net.java.html.boot.BrowserBuilder) HttpServer(org.glassfish.grizzly.http.server.HttpServer) Factory(org.testng.annotations.Factory)

Example 4 with ServerConfiguration

use of org.glassfish.grizzly.http.server.ServerConfiguration 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 5 with ServerConfiguration

use of org.glassfish.grizzly.http.server.ServerConfiguration 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)

Aggregations

ServerConfiguration (org.glassfish.grizzly.http.server.ServerConfiguration)8 HttpServer (org.glassfish.grizzly.http.server.HttpServer)7 IOException (java.io.IOException)5 ProcessingException (javax.ws.rs.ProcessingException)5 NetworkListener (org.glassfish.grizzly.http.server.NetworkListener)5 CLStaticHttpHandler (org.glassfish.grizzly.http.server.CLStaticHttpHandler)3 GrizzlyHttpContainer (org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer)3 NulsResourceConfig (io.nuls.rpc.resources.impl.NulsResourceConfig)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConsoleHandler (java.util.logging.ConsoleHandler)1 BrowserBuilder (net.java.html.boot.BrowserBuilder)1 PortRange (org.glassfish.grizzly.PortRange)1 TCPNIOTransport (org.glassfish.grizzly.nio.transport.TCPNIOTransport)1 SpdyAddOn (org.glassfish.grizzly.spdy.SpdyAddOn)1 ThreadFactoryBuilder (org.glassfish.jersey.internal.guava.ThreadFactoryBuilder)1 JerseyProcessingUncaughtExceptionHandler (org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler)1 Factory (org.testng.annotations.Factory)1