Search in sources :

Example 46 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class SseTest method testSse.

@Test
public void testSse() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(SseResource.class, SseFeature.class);
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    Client c = ClientBuilder.newClient();
    c.register(SseFeature.class);
    final List<String> data = new LinkedList<String>();
    final CountDownLatch latch = new CountDownLatch(2);
    final EventSource eventSource = new EventSource(c.target(baseUri).path("/sse")) {

        @Override
        public void onEvent(InboundEvent event) {
            try {
                data.add(event.readData());
                latch.countDown();
            } catch (ProcessingException e) {
            // ignore
            }
        }
    };
    assertTrue(latch.await(2, TimeUnit.SECONDS));
    eventSource.close();
    assertEquals(2, data.size());
    server.shutdownNow();
}
Also used : EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 47 with ProcessingException

use of javax.ws.rs.ProcessingException 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 48 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class ServerSentEventsTest method testEventSource.

/**
     * Test consuming a single SSE event via event source.
     *
     * @throws Exception in case of a failure during the test execution.
     */
@Test
public void testEventSource() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> message = new AtomicReference<String>();
    final EventSource eventSource = new EventSource(target().path(App.ROOT_PATH)) {

        @Override
        public void onEvent(InboundEvent inboundEvent) {
            try {
                final String value = inboundEvent.readData();
                message.set(value);
                latch.countDown();
            } catch (ProcessingException e) {
                e.printStackTrace();
            }
        }
    };
    target().path(App.ROOT_PATH).request().post(Entity.text("message"));
    try {
        assertTrue("Waiting for message to be delivered has timed out.", latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
    } finally {
        eventSource.close();
    }
    assertThat("Unexpected SSE event data value.", message.get(), equalTo("message"));
}
Also used : EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 49 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class EventSourceWithNamedEventsTest method testWithEventSource.

@Test
public void testWithEventSource() throws IOException, NoSuchAlgorithmException, InterruptedException {
    final WebTarget endpoint = target().register(SseFeature.class).path("events");
    EventSource eventSource = EventSource.target(endpoint).build();
    final CountDownLatch count = new CountDownLatch(MSG_COUNT);
    final EventListener listener = new EventListener() {

        @Override
        public void onEvent(InboundEvent inboundEvent) {
            try {
                final Integer data = inboundEvent.readData(Integer.class);
                System.out.println(inboundEvent.getName() + "; " + data);
                Assert.assertEquals(SSE_NAME, inboundEvent.getName());
                Assert.assertEquals(MSG_COUNT - count.getCount(), data.intValue());
                count.countDown();
            } catch (ProcessingException ex) {
                throw new RuntimeException("Error when deserializing of data.", ex);
            }
        }
    };
    eventSource.register(listener, "message-to-client");
    eventSource.open();
    final boolean sent = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were sent.", sent);
    final boolean handled = count.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by the listener.", handled);
}
Also used : EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch) SseFeature(org.glassfish.jersey.media.sse.SseFeature) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 50 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class SseEventSinkToEventSourceTest method testWithJerseyApi.

@Test
public void testWithJerseyApi() throws InterruptedException {
    final WebTarget endpoint = target().path("events");
    final EventSource eventSource = EventSource.target(endpoint).build();
    transmitLatch = new CountDownLatch(MSG_COUNT);
    final CountDownLatch receiveLatch = new CountDownLatch(MSG_COUNT);
    final List<Integer> results = new ArrayList<>();
    final EventListener listener = inboundEvent -> {
        try {
            results.add(inboundEvent.readData(Integer.class));
            receiveLatch.countDown();
            Assert.assertEquals(INTEGER_SSE_NAME, inboundEvent.getName());
        } catch (ProcessingException ex) {
            throw new RuntimeException("Error when deserializing of data.", ex);
        }
    };
    eventSource.register(listener, INTEGER_SSE_NAME);
    eventSource.open();
    Assert.assertTrue(transmitLatch.await(5000, TimeUnit.MILLISECONDS));
    Assert.assertTrue(receiveLatch.await(5000, TimeUnit.MILLISECONDS));
    Assert.assertEquals(10, results.size());
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) SseEventSink(javax.ws.rs.sse.SseEventSink) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Application(javax.ws.rs.core.Application) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) JerseyTest(org.glassfish.jersey.test.JerseyTest) Sse(javax.ws.rs.sse.Sse) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ExecutorService(java.util.concurrent.ExecutorService) Context(javax.ws.rs.core.Context) EventListener(org.glassfish.jersey.media.sse.EventListener) EventSource(org.glassfish.jersey.media.sse.EventSource) Test(org.junit.Test) Logger(java.util.logging.Logger) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) ProcessingException(javax.ws.rs.ProcessingException) WebTarget(javax.ws.rs.client.WebTarget) Assert(org.junit.Assert) SseEventSource(javax.ws.rs.sse.SseEventSource) EventSource(org.glassfish.jersey.media.sse.EventSource) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

ProcessingException (javax.ws.rs.ProcessingException)91 Test (org.junit.Test)32 IOException (java.io.IOException)26 Response (javax.ws.rs.core.Response)20 WebTarget (javax.ws.rs.client.WebTarget)19 JerseyTest (org.glassfish.jersey.test.JerseyTest)16 WebApplicationException (javax.ws.rs.WebApplicationException)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 Client (javax.ws.rs.client.Client)9 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ClientRequest (org.glassfish.jersey.client.ClientRequest)8 ClientResponse (org.glassfish.jersey.client.ClientResponse)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 URI (java.net.URI)6 NoContentException (javax.ws.rs.core.NoContentException)6 OutputStream (java.io.OutputStream)5 Map (java.util.Map)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EventSource (org.glassfish.jersey.media.sse.EventSource)5