From commits-return-52411-archive-asf-public=cust-asf.ponee.io@cxf.apache.org Fri Sep 27 00:27:51 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id D787A180660 for ; Fri, 27 Sep 2019 02:27:50 +0200 (CEST) Received: (qmail 73431 invoked by uid 500); 27 Sep 2019 00:27:50 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 73406 invoked by uid 99); 27 Sep 2019 00:27:50 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 27 Sep 2019 00:27:50 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 8821A81E74; Fri, 27 Sep 2019 00:27:49 +0000 (UTC) Date: Fri, 27 Sep 2019 00:27:50 +0000 To: "commits@cxf.apache.org" Subject: [cxf] 01/03: CXF-8120: JAX-RS 2.1 SSE client: forbidden response without WebApplicationException MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: reta@apache.org In-Reply-To: <156954406904.7378.3795570015963144183@gitbox.apache.org> References: <156954406904.7378.3795570015963144183@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: cxf X-Git-Refname: refs/heads/3.2.x-fixes X-Git-Reftype: branch X-Git-Rev: aa81997eb1403418b6a174b1c8410370512e3ac1 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20190927002749.8821A81E74@gitbox.apache.org> 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 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 readBody(Response r, Message outMessage, Class 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 -> {