Search in sources :

Example 11 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("Jersey Entity Data Filtering Example.");
        final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new EntityFilteringApplication(), false);
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            @Override
            public void run() {
                server.shutdownNow();
            }
        }));
        server.start();
        System.out.println("Application started.\nTry out one of these URIs:");
        for (final String path : new String[] { "projects", "projects/detailed", "users", "users?detailed=true", "tasks", "tasks/detailed" }) {
            System.out.println(BASE_URI + path);
        }
        System.out.println("Stop the application using CTRL+C");
        Thread.currentThread().join();
    } catch (IOException | InterruptedException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, "I/O error occurred during reading from an system input stream.", ex);
    }
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) IOException(java.io.IOException)

Example 12 with HttpServer

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

the class Server method start.

/**
     * Start SSL-secured HTTP test server.
     *
     * @throws IOException in case there is an error while reading server key store or trust store.
     * @return an instance of the started SSL-secured HTTP test server.
     */
public static Server start(String keystore) throws IOException {
    final InputStream trustStore = Server.class.getResourceAsStream(SERVER_TRUST_STORE);
    final InputStream keyStore = Server.class.getResourceAsStream(keystore);
    // Grizzly ssl configuration
    SSLContextConfigurator sslContext = new SSLContextConfigurator();
    // set up security context
    // contains server key pair
    sslContext.setKeyStoreBytes(ByteStreams.toByteArray(keyStore));
    sslContext.setKeyStorePass("asdfgh");
    // contains client certificate
    sslContext.setTrustStoreBytes(ByteStreams.toByteArray(trustStore));
    sslContext.setTrustStorePass("asdfgh");
    ResourceConfig rc = new ResourceConfig();
    rc.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY));
    rc.registerClasses(RootResource.class, SecurityFilter.class, AuthenticationExceptionMapper.class);
    final HttpServer grizzlyServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI(), rc, true, new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(true));
    // start Grizzly embedded server //
    LOGGER.info("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");
    grizzlyServer.start();
    return new Server(grizzlyServer);
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) InputStream(java.io.InputStream) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) SSLEngineConfigurator(org.glassfish.grizzly.ssl.SSLEngineConfigurator) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) SSLContextConfigurator(org.glassfish.grizzly.ssl.SSLContextConfigurator)

Example 13 with HttpServer

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

the class Main method main.

/**
     * Main method.
     * @param args
     * @throws IOException
     */
public static void main(String[] args) throws IOException {
    final HttpServer server = startServer();
    System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
    System.in.read();
    server.stop();
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer)

Example 14 with HttpServer

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

the class JsonProcessingTest method testJsonObject.

@Test
public void testJsonObject() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(Resource.class);
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    final JsonObject jsonObject = Json.createObjectBuilder().add("foo", "bar").build();
    final Client client = ClientBuilder.newClient();
    final JsonObject entity = client.target(baseUri).request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(jsonObject), JsonObject.class);
    System.out.println("RESULT = " + entity);
    assertEquals(jsonObject, entity);
    server.shutdownNow();
}
Also used : HttpServer(org.glassfish.grizzly.http.server.HttpServer) JsonObject(javax.json.JsonObject) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 15 with HttpServer

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

the class MultiPartTest method testMultiPartResource.

@Test
public void testMultiPartResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(MultiPartResource.class).register(new MultiPartFeature());
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    Client c = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class));
    final Response response = c.target(baseUri).path("/multipart-simple").request().buildGet().invoke();
    MultiPart result = response.readEntity(MultiPart.class);
    System.out.println("RESULT = " + result);
    checkEntity("This is the only segment", (BodyPartEntity) result.getBodyParts().get(0).getEntity());
    server.shutdownNow();
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MultiPartFeature(org.glassfish.jersey.media.multipart.MultiPartFeature) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

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