Search in sources :

Example 1 with STREAMING

use of com.hotels.styx.api.ResponseEventListener.State.STREAMING in project styx by ExpediaGroup.

the class ResponseEventListener method apply.

public Flux<LiveHttpResponse> apply() {
    EventProcessor eventProcessor = new QueueDrainingEventProcessor(event -> {
        switch(state) {
            case INITIAL:
                if (event instanceof MessageHeaders) {
                    onHeaders.run();
                    state = STREAMING;
                } else if (event instanceof MessageCancelled) {
                    cancelAction.run();
                    whenFinishedAction.run();
                    state = TERMINATED;
                } else if (event instanceof MessageCompleted) {
                    // TODO: Add custom exception type?
                    responseErrorAction.accept(new RuntimeException("Response Observable completed without message headers."));
                    whenFinishedAction.run();
                    state = TERMINATED;
                } else if (event instanceof MessageError) {
                    responseErrorAction.accept(((MessageError) event).cause());
                    whenFinishedAction.run();
                    state = TERMINATED;
                }
                break;
            case STREAMING:
                if (event instanceof ContentEnd) {
                    onCompletedAction.accept(((ContentEnd) event).response);
                    whenFinishedAction.run();
                    state = COMPLETED;
                } else if (event instanceof ContentError) {
                    contentErrorAction.accept(((ContentError) event).cause());
                    whenFinishedAction.run();
                    state = TERMINATED;
                } else if (event instanceof ContentCancelled) {
                    cancelAction.run();
                    whenFinishedAction.run();
                    state = TERMINATED;
                }
                break;
        }
    });
    return publisher.doOnNext(headers -> eventProcessor.submit(new MessageHeaders())).doOnComplete(() -> eventProcessor.submit(new MessageCompleted())).doOnError(cause -> eventProcessor.submit(new MessageError(cause))).doOnCancel(() -> eventProcessor.submit(new MessageCancelled())).map(response -> response.newBuilder().body(it -> it.doOnEnd(ifError(cause -> eventProcessor.submit(new ContentError(cause))))).body(it -> it.doOnEnd(ifSuccessful(() -> eventProcessor.submit(new ContentEnd(response))))).body(it -> it.doOnCancel(() -> eventProcessor.submit(new ContentCancelled()))).build());
}
Also used : Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) COMPLETED(com.hotels.styx.api.ResponseEventListener.State.COMPLETED) INITIAL(com.hotels.styx.api.ResponseEventListener.State.INITIAL) TERMINATED(com.hotels.styx.api.ResponseEventListener.State.TERMINATED) EventProcessor(com.hotels.styx.common.EventProcessor) Publisher(org.reactivestreams.Publisher) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) QueueDrainingEventProcessor(com.hotels.styx.common.QueueDrainingEventProcessor) STREAMING(com.hotels.styx.api.ResponseEventListener.State.STREAMING) QueueDrainingEventProcessor(com.hotels.styx.common.QueueDrainingEventProcessor) EventProcessor(com.hotels.styx.common.EventProcessor) QueueDrainingEventProcessor(com.hotels.styx.common.QueueDrainingEventProcessor)

Aggregations

COMPLETED (com.hotels.styx.api.ResponseEventListener.State.COMPLETED)1 INITIAL (com.hotels.styx.api.ResponseEventListener.State.INITIAL)1 STREAMING (com.hotels.styx.api.ResponseEventListener.State.STREAMING)1 TERMINATED (com.hotels.styx.api.ResponseEventListener.State.TERMINATED)1 EventProcessor (com.hotels.styx.common.EventProcessor)1 QueueDrainingEventProcessor (com.hotels.styx.common.QueueDrainingEventProcessor)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Publisher (org.reactivestreams.Publisher)1 Flux (reactor.core.publisher.Flux)1