andymc12 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_r363455700
 
 

 ##########
 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:
   The spec is very vague about this, and I don't remember if there was a TCK 
test that tests this or not...
   
   The notion of invoking both callbacks was discussed in JAX-RS spec issue 
[606](https://github.com/eclipse-ee4j/jaxrs-api/issues/606).  I have coded up a 
clarification to resolve that issue (still waiting on one more committer to 
review it) - and calling both callbacks would satisfy my write-up (and the 
issue's original request).
   
   I think the main reason for calling both callbacks is to notify the client 
of two different states:
   1) an error occurred.
   2) this event source will not receive any more events.

----------------------------------------------------------------
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

Reply via email to