cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [cxf] branch master updated: [CXF-7668] Start pulling the servlet 3.1 specific code into stuff that is only used/accessed if Servlet 3.1 is detected
Date Tue, 06 Mar 2018 16:39:21 GMT
This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new a0b83f3  [CXF-7668] Start pulling the servlet 3.1 specific code into stuff that is
only used/accessed if Servlet 3.1 is detected
a0b83f3 is described below

commit a0b83f3c9d8259709e2f22cbf15641e3207d4af3
Author: Daniel Kulp <dkulp@apache.org>
AuthorDate: Tue Mar 6 11:38:42 2018 -0500

    [CXF-7668] Start pulling the servlet 3.1 specific code into stuff that is only used/accessed
if Servlet 3.1 is detected
---
 .../apache/cxf/jaxrs/nio/NioMessageBodyWriter.java | 14 +++++-
 .../http/Servlet3ContinuationProvider.java         | 53 ++++++++++++++++------
 2 files changed, 52 insertions(+), 15 deletions(-)

diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/nio/NioMessageBodyWriter.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/nio/NioMessageBodyWriter.java
index b80dbb8..c9848ee 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/nio/NioMessageBodyWriter.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/nio/NioMessageBodyWriter.java
@@ -24,12 +24,14 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
 import javax.servlet.WriteListener;
+import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.continuations.Continuation;
 import org.apache.cxf.continuations.ContinuationProvider;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
@@ -37,13 +39,21 @@ import org.apache.cxf.message.Message;
 
 @Provider
 public class NioMessageBodyWriter implements MessageBodyWriter<NioWriteEntity> {
-
+    boolean is31;
+    
     public NioMessageBodyWriter() {
+        try {
+            ClassLoaderUtils.loadClass("javax.servlet.WriteListener", HttpServletRequest.class);
+            is31 = true;
+        } catch (Throwable t) {
+            is31 = false;
+        }
+
     }
 
     @Override
     public boolean isWriteable(Class<?> cls, Type type, Annotation[] anns, MediaType
mt) {
-        return NioWriteEntity.class.isAssignableFrom(cls) && getContinuationProvider()
!= null;
+        return is31 && NioWriteEntity.class.isAssignableFrom(cls) && getContinuationProvider()
!= null;
     }
 
     @Override
diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
index 5266009..364364b 100644
--- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
@@ -29,6 +29,7 @@ import javax.servlet.WriteListener;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.util.PropertyUtils;
 import org.apache.cxf.continuations.Continuation;
 import org.apache.cxf.continuations.ContinuationCallback;
@@ -44,6 +45,7 @@ public class Servlet3ContinuationProvider implements ContinuationProvider
{
     HttpServletResponse resp;
     Message inMessage;
     Servlet3Continuation continuation;
+    boolean is31;
 
     public Servlet3ContinuationProvider(HttpServletRequest req,
                                         HttpServletResponse resp,
@@ -51,6 +53,14 @@ public class Servlet3ContinuationProvider implements ContinuationProvider
{
         this.inMessage = inMessage;
         this.req = req;
         this.resp = resp;
+        
+        
+        try {
+            ClassLoaderUtils.loadClass("javax.servlet.WriteListener", HttpServletRequest.class);
+            is31 = true;
+        } catch (Throwable t) {
+            is31 = false;
+        }
     }
 
     public void complete() {
@@ -68,7 +78,7 @@ public class Servlet3ContinuationProvider implements ContinuationProvider
{
         }
 
         if (continuation == null) {
-            continuation = new Servlet3Continuation();
+            continuation = is31 ? new Servlet31Continuation() : new Servlet3Continuation();
         } else {
             continuation.startAsyncAgain();
         }
@@ -86,6 +96,7 @@ public class Servlet3ContinuationProvider implements ContinuationProvider
{
         volatile Object obj;
         private ContinuationCallback callback;
         private boolean blockRestart;
+        
         public Servlet3Continuation() {
             req.setAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE,
                              inMessage.getExchange().getInMessage());
@@ -121,18 +132,12 @@ public class Servlet3ContinuationProvider implements ContinuationProvider
{
 
             context.setTimeout(timeout);
 
-            Message currentMessage = PhaseInterceptorChain.getCurrentMessage();
-            if (currentMessage.get(WriteListener.class) != null) {
-                // CXF Continuation WriteListener will likely need to be introduced
-                // for NIO supported with non-Servlet specific mechanisms
-                getOutputStream().setWriteListener(currentMessage.get(WriteListener.class));
-                currentMessage.getInterceptorChain().suspend();
-            } else {
-                inMessage.getExchange().getInMessage().getInterceptorChain().suspend();
-            }
-
+            updateMessageForSuspend();
             return true;
         }
+        protected void updateMessageForSuspend() {
+            inMessage.getExchange().getInMessage().getInterceptorChain().suspend();
+        }
         public void redispatch() {
             if (!isComplete) {
                 context.dispatch();
@@ -232,10 +237,10 @@ public class Servlet3ContinuationProvider implements ContinuationProvider
{
 
         @Override
         public boolean isReadyForWrite() {
-            return getOutputStream().isReady();
+            return true;
         }
 
-        private ServletOutputStream getOutputStream() {
+        protected ServletOutputStream getOutputStream() {
             try {
                 return resp.getOutputStream();
             } catch (IOException ex) {
@@ -248,4 +253,26 @@ public class Servlet3ContinuationProvider implements ContinuationProvider
{
             return isTimeout;
         }
     }
+    public class Servlet31Continuation extends Servlet3Continuation {
+        public Servlet31Continuation() {
+        }
+
+        @Override
+        protected void updateMessageForSuspend() {
+            Message currentMessage = PhaseInterceptorChain.getCurrentMessage();
+            if (currentMessage.get(WriteListener.class) != null) {
+                // CXF Continuation WriteListener will likely need to be introduced
+                // for NIO supported with non-Servlet specific mechanisms
+                getOutputStream().setWriteListener(currentMessage.get(WriteListener.class));
+                currentMessage.getInterceptorChain().suspend();
+            } else {
+                inMessage.getExchange().getInMessage().getInterceptorChain().suspend();
+            }
+        }
+        
+        @Override
+        public boolean isReadyForWrite() {
+            return getOutputStream().isReady();
+        }
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
dkulp@apache.org.

Mime
View raw message