cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1400835 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Mon, 22 Oct 2012 10:31:36 GMT
Author: sergeyb
Date: Mon Oct 22 10:31:36 2012
New Revision: 1400835

URL: http://svn.apache.org/viewvc?rev=1400835&view=rev
Log:
Initial support for AsyncResponse ResumeCallback

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=1400835&r1=1400834&r2=1400835&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java Mon Oct
22 10:31:36 2012
@@ -88,8 +88,6 @@ public class JAXRSInvoker extends Abstra
                     if (asyncObj instanceof Throwable) {
                         return handleFault(new Fault((Throwable)asyncObj), 
                                            exchange.getInMessage(), null, null);    
-                    } else if (!(asyncObj instanceof Response)) {
-                        response = Response.ok().entity(asyncObj).build();
                     } else {
                         response = (Response)asyncObj;
                     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java?rev=1400835&r1=1400834&r2=1400835&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
Mon Oct 22 10:31:36 2012
@@ -21,8 +21,10 @@ package org.apache.cxf.jaxrs.impl;
 import java.util.Date;
 import java.util.concurrent.TimeUnit;
 
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.container.AsyncResponse;
 import javax.ws.rs.container.CompletionCallback;
+import javax.ws.rs.container.ResumeCallback;
 import javax.ws.rs.container.TimeoutHandler;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
@@ -31,6 +33,7 @@ import javax.ws.rs.core.Response.Respons
 import org.apache.cxf.continuations.Continuation;
 import org.apache.cxf.continuations.ContinuationCallback;
 import org.apache.cxf.continuations.ContinuationProvider;
+import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.message.Message;
 
@@ -41,12 +44,13 @@ public class AsyncResponseImpl implement
     private long timeout = AsyncResponse.NO_TIMEOUT;
     private Message inMessage;
     private boolean cancelled;
-    private boolean done;
+    private volatile boolean done;
     private boolean newTimeoutRequested;
     private boolean resumedByApplication;
     private TimeoutHandler timeoutHandler;
     
     private CompletionCallback completionCallback;
+    private ResumeCallback resumeCallback;
     
     public AsyncResponseImpl(Message inMessage) {
         inMessage.put(AsyncResponse.class, this);
@@ -113,7 +117,7 @@ public class AsyncResponseImpl implement
     }
 
     @Override
-    public synchronized boolean isDone() {
+    public boolean isDone() {
         return done;
     }
 
@@ -147,9 +151,10 @@ public class AsyncResponseImpl implement
         
     }
 
+    //TODO: API bug, boolean[] needs to be returned...
     @Override
     public boolean register(Object callback) throws NullPointerException {
-        return register(callback, CompletionCallback.class)[0];
+        return register(callback, CompletionCallback.class, ResumeCallback.class)[0];
     }
 
     //TODO: API bug, has to be Class<?>...
@@ -163,9 +168,12 @@ public class AsyncResponseImpl implement
                 throw new NullPointerException();
             }
             Class<?> cls = (Class<?>)interf;
-            if (cls == CompletionCallback.class) {
+            if (cls == CompletionCallback.class && callback instanceof CompletionCallback)
{
                 completionCallback = (CompletionCallback)callback;
                 result[i] = true;
+            } else if (cls == ResumeCallback.class && callback instanceof ResumeCallback)
{
+                resumeCallback = (ResumeCallback)callback;
+                result[i] = true;
             } else {
                 result[i] = false;
             }
@@ -192,9 +200,18 @@ public class AsyncResponseImpl implement
     }
     
     public synchronized Object getResponseObject() {
-        // it may have to be set to true only after a continuation-specific onComplete event
-        done = true;
-        return cont.getObject();
+        Object obj = cont.getObject();
+        if (!(obj instanceof Response) && !(obj instanceof Throwable)) {
+            obj = Response.ok().entity(obj).build();    
+        }
+        if (resumeCallback != null) {    
+            if (obj instanceof Response) {
+                resumeCallback.onResume(this, (Response)obj);
+            } else {
+                resumeCallback.onResume(this, (Throwable)obj);
+            }
+        }
+        return obj;
     }
     
     public synchronized boolean isResumedByApplication() {
@@ -211,16 +228,19 @@ public class AsyncResponseImpl implement
                 suspend();
                 timeoutHandler.handleTimeout(this);
                 return true;
-            } else {
-                done = true;
+            } else if (resumeCallback != null) {
+                resumeCallback.onResume(this, new WebApplicationException(503));    
             }
         }
         return false;
         
     }
 
+    
+    
     @Override
     public void onComplete() {
+        done = true;
         if (completionCallback != null) {
             completionCallback.onComplete();
         }
@@ -229,7 +249,8 @@ public class AsyncResponseImpl implement
     @Override
     public void onError(Throwable error) {
         if (completionCallback != null) {
-            completionCallback.onError(error);
+            Throwable actualError = error instanceof Fault ? ((Fault)error).getCause() :
error;
+            completionCallback.onError(actualError);
         }
         
     }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java?rev=1400835&r1=1400834&r2=1400835&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
Mon Oct 22 10:31:36 2012
@@ -32,7 +32,9 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.container.AsyncResponse;
 import javax.ws.rs.container.CompletionCallback;
+import javax.ws.rs.container.ResumeCallback;
 import javax.ws.rs.container.TimeoutHandler;
+import javax.ws.rs.core.Response;
 
 @Path("/bookstore")
 public class BookContinuationStore {
@@ -48,7 +50,7 @@ public class BookContinuationStore {
     @GET
     @Path("/books/defaulttimeout")
     public void getBookDescriptionWithHandler(AsyncResponse async) {
-        async.register(new CompletionCallbackImpl());
+        async.register(new CallbackImpl());
         async.setTimeout(2000, TimeUnit.MILLISECONDS);
     }
     
@@ -118,18 +120,27 @@ public class BookContinuationStore {
         
     }
     
-    private class CompletionCallbackImpl implements CompletionCallback {
+    private class CallbackImpl implements CompletionCallback, ResumeCallback {
 
         @Override
         public void onComplete() {
-            System.out.println("CompletionCallbackImpl: onComplete");
+            System.out.println("CompletionCallback: onComplete");
             
         }
 
         @Override
         public void onError(Throwable throwable) {
-            // TODO Auto-generated method stub
-            
+            System.out.println("CompletionCallback: onError");
+        }
+
+        @Override
+        public void onResume(AsyncResponse resuming, Response response) {
+            System.out.println("ResumeCallback: onResume");
+        }
+
+        @Override
+        public void onResume(AsyncResponse resuming, Throwable error) {
+            System.out.println("ResumeCallback: onResumeError");
         }
         
     }



Mime
View raw message