cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [cxf] 01/03: CXF-8120: JAX-RS 2.1 SSE client: forbidden response without WebApplicationException
Date Fri, 27 Sep 2019 00:27:50 GMT
This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit aa81997eb1403418b6a174b1c8410370512e3ac1
Author: reta <drreta@gmail.com>
AuthorDate: Sun Sep 22 12:26:32 2019 -0400

    CXF-8120: JAX-RS 2.1 SSE client: forbidden response without WebApplicationException
    
    (cherry picked from commit 88e3eb24de7ee6498240aac557ccf3d75a1be9af)
    (cherry picked from commit 1a4505c09fa403262a54e0fa745edf78a8c3ba0c)
    
    # Conflicts:
    #	rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java
---
 .../org/apache/cxf/jaxrs/utils/ExceptionUtils.java | 12 ++++++++
 .../apache/cxf/jaxrs/client/AbstractClient.java    | 10 +------
 .../cxf/jaxrs/sse/client/SseEventSourceImpl.java   | 10 ++++++-
 .../jaxrs/sse/client/SseEventSourceImplTest.java   | 32 ++++++++++++++++++++++
 4 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ExceptionUtils.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ExceptionUtils.java
index 5e8b014..a8c607a 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ExceptionUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ExceptionUtils.java
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxrs.utils;
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.lang.reflect.Constructor;
 
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
@@ -169,4 +170,15 @@ public final class ExceptionUtils {
             return toWebApplicationException(ex, response);
         }
     }
+    
+    public static WebApplicationException toWebApplicationException(Response response) {
+        try {
+            final Class<?> exceptionClass = ExceptionUtils.getWebApplicationExceptionClass(response,

+                WebApplicationException.class);
+            final Constructor<?> ctr = exceptionClass.getConstructor(Response.class);
+            return (WebApplicationException)ctr.newInstance(response);
+        } catch (Throwable ex) {
+            return new WebApplicationException(response);
+        }
+    }
 }
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
index 01d448c..4090293 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
@@ -23,7 +23,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
@@ -537,14 +536,7 @@ public abstract class AbstractClient implements Client {
     }
 
     protected WebApplicationException convertToWebApplicationException(Response r) {
-        try {
-            Class<?> exceptionClass = ExceptionUtils.getWebApplicationExceptionClass(r,
-                                       WebApplicationException.class);
-            Constructor<?> ctr = exceptionClass.getConstructor(Response.class);
-            return (WebApplicationException)ctr.newInstance(r);
-        } catch (Throwable ex2) {
-            return new WebApplicationException(r);
-        }
+        return ExceptionUtils.toWebApplicationException(r);
     }
 
     protected <T> T readBody(Response r, Message outMessage, Class<T> cls,
diff --git a/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImpl.java
b/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImpl.java
index 3a7f78e..f0c2acf 100644
--- a/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImpl.java
+++ b/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImpl.java
@@ -41,6 +41,7 @@ import javax.ws.rs.sse.SseEventSource;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.utils.ExceptionUtils;
 
 /**
  * SSE Event Source implementation 
@@ -191,13 +192,20 @@ public class SseEventSourceImpl implements SseEventSource {
 
             // A client can be told to stop reconnecting using the HTTP 204 No Content 
             // response code. In this case, we should give up.
-            if (response.getStatus() == 204) {
+            final int status = response.getStatus();
+            if (status == 204) {
                 LOG.fine("SSE endpoint " + target.getUri() + " returns no data, disconnecting");
                 state.set(SseSourceState.CLOSED);
                 response.close();
                 return;
             }
 
+            // Convert unsuccessful responses to instances of WebApplicationException
+            if (status != 304 && status >= 300) {
+                LOG.fine("SSE connection to " + target.getUri() + " returns " + status);
+                throw ExceptionUtils.toWebApplicationException(response);
+            }
+
             // Should not happen but if close() was called from another thread, we could
             // end up there.
             if (state.get() == SseSourceState.CLOSED) {
diff --git a/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java
b/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java
index 176dc14..635f432 100644
--- a/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java
+++ b/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java
@@ -389,6 +389,38 @@ public class SseEventSourceImplTest extends Assert {
     }
     
     @Test
+    public void testReconnectAndNotAuthorized() throws InterruptedException, IOException
{
+        when(response.getStatus()).thenReturn(401);
+        
+        try (SseEventSource eventSource = withReconnect()) {
+            eventSource.open();
+            assertThat(eventSource.isOpen(), equalTo(false));
+            verify(response, times(1)).getStatus();
+
+            // Allow the event processor to pull for events (150ms)
+            Thread.sleep(150L);
+        }
+        
+        verify(response, times(2)).getStatus();
+    }
+
+    @Test
+    public void testNoReconnectAndNotAuthorized() throws InterruptedException, IOException
{
+        when(response.getStatus()).thenReturn(401);
+        
+        try (SseEventSource eventSource = withNoReconnect()) {
+            eventSource.open();
+            assertThat(eventSource.isOpen(), equalTo(false));
+            verify(response, times(1)).getStatus();
+
+            // Allow the event processor to pull for events (150ms)
+            Thread.sleep(150L);
+        }
+        
+        verify(response, times(1)).getStatus();
+    }
+
+    @Test
     public void testNoReconnectAndCloseTheStreamWhileEventIsBeingReceived() throws InterruptedException,
IOException {
         when(response.getStatus()).thenReturn(200);
         when(response.readEntity(InputStream.class)).then(invocation -> {


Mime
View raw message