Search in sources :

Example 1 with InboundSseEvent

use of javax.ws.rs.sse.InboundSseEvent in project cxf by apache.

the class SsePublisher method start.

private void start() {
    if (isStarted.compareAndSet(false, true)) {
        executor.execute(() -> {
            try (BufferedReader br2 = br) {
                SseEventBuilder builder = new SseEventBuilder(providers);
                String line = br.readLine();
                while (line != null && !subscriptions.isEmpty()) {
                    if (line.startsWith("data:")) {
                        builder.data(removeSpace(line.substring(5)));
                    } else if (line.startsWith("id:")) {
                        builder.id(removeSpace(line.substring(3)));
                    } else if (line.startsWith("event:")) {
                        builder.name(removeSpace(line.substring(6)));
                    } else if (line.startsWith(":")) {
                        builder.comment(removeSpace(line.substring(1)));
                    } else if ("".equals(line)) {
                        InboundSseEvent event = builder.build();
                        for (SseSubscription subscription : subscriptions) {
                            subscription.fireEvent(event);
                        }
                        builder = new SseEventBuilder(providers);
                    }
                    line = br.readLine();
                }
                for (SseSubscription subscription : subscriptions) {
                    subscription.complete();
                }
            } catch (IOException ex) {
                for (SseSubscription subscription : subscriptions) {
                    subscription.fireError(ex);
                }
            }
        });
    }
}
Also used : BufferedReader(java.io.BufferedReader) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) IOException(java.io.IOException)

Example 2 with InboundSseEvent

use of javax.ws.rs.sse.InboundSseEvent in project cxf by apache.

the class InboundSseEventProcessor method process.

private Callable<?> process(Response response, InputStream is, ClientProviderFactory factory, Message message) {
    return () -> {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line = reader.readLine();
            InboundSseEventImpl.Builder builder = null;
            while (line != null && !Thread.interrupted() && !closed) {
                if (StringUtils.isEmpty(line) && builder != null) {
                    /* empty new line */
                    final InboundSseEvent event = builder.build(factory, message);
                    builder = null;
                    /* reset the builder for next event */
                    listener.onNext(event);
                } else {
                    // https://www.w3.org/TR/eventsource/#parsing-an-event-stream
                    if (line.startsWith(EVENT)) {
                        int beginIndex = findFirstNonSpacePosition(line, EVENT);
                        builder = getOrCreate(builder).name(line.substring(beginIndex));
                    } else if (line.startsWith(ID)) {
                        int beginIndex = findFirstNonSpacePosition(line, ID);
                        builder = getOrCreate(builder).id(line.substring(beginIndex));
                    } else if (line.startsWith(COMMENT)) {
                        int beginIndex = findFirstNonSpacePosition(line, COMMENT);
                        builder = getOrCreate(builder).comment(line.substring(beginIndex));
                    } else if (line.startsWith(RETRY)) {
                        int beginIndex = findFirstNonSpacePosition(line, RETRY);
                        builder = getOrCreate(builder).reconnectDelay(line.substring(beginIndex));
                    } else if (line.startsWith(DATA)) {
                        int beginIndex = findFirstNonSpacePosition(line, DATA);
                        builder = getOrCreate(builder).appendData(line.substring(beginIndex));
                    }
                }
                line = reader.readLine();
            }
            if (builder != null) {
                // 
                if (discardIncomplete) /* default */
                {
                    LOG.fine("Discarding incomplete SSE event");
                } else {
                    listener.onNext(builder.build(factory, message));
                }
            }
            // complete the stream
            listener.onComplete();
        } catch (final Exception ex) {
            listener.onError(ex);
        }
        if (response != null) {
            LOG.fine("Closing the response");
            response.close();
        }
        return null;
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) Builder(org.apache.cxf.jaxrs.sse.client.InboundSseEventImpl.Builder) BufferedReader(java.io.BufferedReader) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 3 with InboundSseEvent

use of javax.ws.rs.sse.InboundSseEvent in project jersey by jersey.

the class SseEventSinkToEventSourceTest method testWithSimpleSubscriber.

@Test
public void testWithSimpleSubscriber() {
    transmitLatch = new CountDownLatch(MSG_COUNT);
    final WebTarget endpoint = target().path("events");
    final List<InboundSseEvent> results = new ArrayList<>();
    try (final SseEventSource eventSource = SseEventSource.target(endpoint).build()) {
        final CountDownLatch receivedLatch = new CountDownLatch(3 * MSG_COUNT);
        eventSource.subscribe((event) -> {
            results.add(event);
            receivedLatch.countDown();
        });
        eventSource.open();
        final boolean allTransmitted = transmitLatch.await(5000, TimeUnit.MILLISECONDS);
        final boolean allReceived = receivedLatch.await(5000, TimeUnit.MILLISECONDS);
        Assert.assertTrue(allTransmitted);
        Assert.assertTrue(allReceived);
        Assert.assertEquals(30, results.size());
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) WebTarget(javax.ws.rs.client.WebTarget) CountDownLatch(java.util.concurrent.CountDownLatch) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 4 with InboundSseEvent

use of javax.ws.rs.sse.InboundSseEvent in project cxf by apache.

the class SseSubscription method request.

@Override
public void request(long n) {
    if (canceled.get()) {
        return;
    }
    if (n < 1) {
        fireError(new IllegalArgumentException("Only positive values may be requested - passed-in " + n));
        return;
    }
    requested.addAndGet(n);
    synchronized (buffer) {
        synchronized (delivered) {
            InboundSseEvent bufferedEvent;
            while (delivered.get() < requested.get() && (bufferedEvent = buffer.pollFirst()) != null) {
                subscriber.onNext(bufferedEvent);
                delivered.incrementAndGet();
            }
        }
    }
}
Also used : InboundSseEvent(javax.ws.rs.sse.InboundSseEvent)

Aggregations

InboundSseEvent (javax.ws.rs.sse.InboundSseEvent)4 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 WebTarget (javax.ws.rs.client.WebTarget)1 SseEventSource (javax.ws.rs.sse.SseEventSource)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 Builder (org.apache.cxf.jaxrs.sse.client.InboundSseEventImpl.Builder)1 JerseyTest (org.glassfish.jersey.test.JerseyTest)1 Test (org.junit.Test)1