Search in sources :

Example 21 with HttpServer

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

the class JaxRsRiBundleTest method testSimpleResource.

@Test
public void testSimpleResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(SimpleResource.class);
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    final Client client = ClientBuilder.newClient();
    final String response = client.target(baseUri).path("/simple").request().get(String.class);
    System.out.println("RESULT = " + response);
    assertEquals("OK", response);
    server.shutdownNow();
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 22 with HttpServer

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

the class App method main.

public static void main(final String[] args) throws Exception {
    try {
        LOGGER.info("Resource Config Reload Jersey Example App");
        for (String s : args) {
            if (s.startsWith("-cp=")) {
                Compiler.classpath = s.substring(4);
            }
        }
        final ResourceConfig resourceConfig = createResourceConfig(new File(CONFIG_FILENAME));
        registerReloader(resourceConfig);
        final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, true);
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            @Override
            public void run() {
                server.shutdownNow();
            }
        }));
        server.start();
        System.out.println(String.format("Application started.\nTry out %s%s\nStop the application using CTRL+C", BASE_URI, ROOT_PATH));
        Thread.currentThread().join();
    } catch (IOException | InterruptedException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) IOException(java.io.IOException) JavaFile(org.glassfish.jersey.examples.reload.compiler.JavaFile) File(java.io.File)

Example 23 with HttpServer

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

the class App method main.

public static void main(String[] args) {
    try {
        System.out.println("\"Custom Executor Managed Async Resources\" Jersey Example App");
        final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, create(), false);
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            @Override
            public void run() {
                server.shutdownNow();
            }
        }));
        server.start();
        System.out.println(String.format("Application started.\n" + "To test long-running asynchronous operation resource, try %s%s\n" + "To test async chat resource, try %s%s\n" + "Stop the application using CTRL+C", BASE_URI, ASYNC_LONG_RUNNING_MANAGED_OP_PATH, BASE_URI, "chat"));
        Thread.currentThread().join();
    } catch (IOException | InterruptedException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) IOException(java.io.IOException)

Example 24 with HttpServer

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

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

the class App method main.

public static void main(String[] args) {
    try {
        System.out.println("\"Server-Sent Events\" Jersey Example App");
        final ResourceConfig resourceConfig = new ResourceConfig(ServerSentEventsResource.class, SseFeature.class);
        final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false);
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            @Override
            public void run() {
                server.shutdownNow();
            }
        }));
        server.start();
        System.out.println(String.format("Application started.\nTry out %s%s\nStop the application using CTRL+C", BASE_URI, ROOT_PATH));
        Thread.currentThread().join();
    } catch (IOException | InterruptedException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) IOException(java.io.IOException)

Aggregations

HttpServer (org.glassfish.grizzly.http.server.HttpServer)57 IOException (java.io.IOException)33 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)21 Test (org.junit.Test)11 Client (javax.ws.rs.client.Client)10 ServerConfiguration (org.glassfish.grizzly.http.server.ServerConfiguration)6 ProcessingException (javax.ws.rs.ProcessingException)5 Response (javax.ws.rs.core.Response)5 NetworkListener (org.glassfish.grizzly.http.server.NetworkListener)5 URI (java.net.URI)4 HashMap (java.util.HashMap)3 GrizzlyHttpContainer (org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer)3 CLStaticHttpHandler (org.glassfish.grizzly.http.server.CLStaticHttpHandler)2 SSLContextConfigurator (org.glassfish.grizzly.ssl.SSLContextConfigurator)2 SSLEngineConfigurator (org.glassfish.grizzly.ssl.SSLEngineConfigurator)2 ClientConfig (org.glassfish.jersey.client.ClientConfig)2 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)2 Bundle (org.osgi.framework.Bundle)2 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)1 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1