Search in sources :

Example 1 with Builder

use of org.apache.cxf.jaxrs.sse.client.InboundSseEventImpl.Builder 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)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 InboundSseEvent (javax.ws.rs.sse.InboundSseEvent)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 Builder (org.apache.cxf.jaxrs.sse.client.InboundSseEventImpl.Builder)1