reta commented on a change in pull request #621: CXF-7996: TCK Fixes for SSE
reconnect state tests
URL: https://github.com/apache/cxf/pull/621#discussion_r364500050
##########
File path:
rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImpl.java
##########
@@ -206,36 +222,86 @@ private void connect(String lastEventId) {
throw ExceptionUtils.toWebApplicationException(response);
}
+ // A client can be told to trigger a reconnect delay via a HTTP
503 Service Unavailable response code.
+ if (status == 503) {
+ LOG.fine("SSE endpoint " + target.getUri() + " returns 503");
+ MultivaluedMap<String, Object> headerMap =
response.getHeaders();
+ // There should only be one header entry
+ Object retryAfter =
headerMap.getFirst(HttpHeaders.RETRY_AFTER);
+ if (retryAfter != null) {
+ long retryAfterDelay = handleRetry((String) retryAfter);
+ delay = retryAfterDelay;
+ if (retryAfterDelay > -1) {
+ scheduleReconnect(retryAfterDelay, lastEventId);
+ response.close();
+ return;
+ }
+
+ }
+ }
+
+ String contentType =
response.getHeaderString(HttpHeaders.CONTENT_TYPE);
+ if (status != 200 ||
!MediaType.SERVER_SENT_EVENTS.equals(contentType)) {
+ if (LOG.isLoggable(Level.FINEST)) {
+ LOG.log(Level.FINEST, "Received " + status + "
Content-Type=" + contentType);
+ }
+ final Response fResponse = response;
+ Throwable t;
+ if (!MediaType.SERVER_SENT_EVENTS.equals(contentType)) {
+ t = new WebApplicationException("Unexpected Content-Type
in response", response);
+ } else {
+ t =
AccessController.doPrivileged((PrivilegedExceptionAction<Throwable>) () -> {
+ @SuppressWarnings("unchecked")
+ Class<? extends Throwable> throwableClass = (Class<?
extends Throwable>) ExceptionUtils
+ .getWebApplicationExceptionClass(fResponse,
WebApplicationException.class);
+ Constructor<? extends Throwable> ctor;
+ try {
+ ctor =
throwableClass.getConstructor(Response.class);
+ } catch (NoSuchMethodException ex) {
+ ctor = null;
+ }
+ return ctor == null ? throwableClass.newInstance() :
ctor.newInstance(fResponse);
+ });
+ }
+
+ delegate.onError(t);
+ delegate.onComplete();
Review comment:
Sorry for the delay @andymc12 , just went over the discussion threads,
indeed the JAX-RS spec is not very clear regarding the callback. As far as I
remember, the initial SSE spec draft was based on Reactive Streams (basically
by copy/pasting Java 9's Flow & family). The Reactive Streams specs is very
clear:
- onError: unrecoverable error & no more events (failed terminal state)
- onComplete: normal completion & no more events (successful terminal state)
Projecting it to SSE:
- onError: consumer is invoked invoked upon a unrecoverable error
encountered (failed terminal state)
- onComplete callback is invoked when there are no further events to be
received (successful terminal state)
I have checked Jersey, the reference implementation, and it strictly follows
this semantics, so does the CXF's current implementation. I would strongly
advocate for sticking to the same semantics: it is widely accepted and
understood.
But you are very right by raising
https://github.com/eclipse-ee4j/jaxrs-api/issues/606 and
https://github.com/eclipse-ee4j/jaxrs-api/pull/670, the spec is not clear and
leaves the space for different interpretations.
For me as a developer, the expectation would be to receive either `onError`
or `onComplete`, but not both.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services