use of com.hotels.styx.api.ResponseEventListener.State.INITIAL 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());
}
Aggregations