You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/07/08 13:28:43 UTC

svn commit: r1500664 - in /cxf/trunk: rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/

Author: sergeyb
Date: Mon Jul  8 11:28:43 2013
New Revision: 1500664

URL: http://svn.apache.org/r1500664
Log:
[CXF-5026] Updating WebClient to handle async exceptions better

Modified:
    cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java
    cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java

Modified: cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java?rev=1500664&r1=1500663&r2=1500664&view=diff
==============================================================================
--- cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java (original)
+++ cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java Mon Jul  8 11:28:43 2013
@@ -21,12 +21,12 @@ package org.apache.cxf.jaxrs.client;
 
 import java.lang.reflect.Type;
 import java.util.Map;
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
-import javax.ws.rs.ProcessingException;
 import javax.ws.rs.client.InvocationCallback;
 
 import org.apache.cxf.endpoint.ClientCallback;
@@ -51,6 +51,15 @@ class JaxrsClientCallback<T> extends Cli
         return responseClass;
     }
     
+    @Override
+    public boolean cancel(boolean mayInterruptIfRunning) {
+        boolean result = super.cancel(mayInterruptIfRunning);
+        if (result && handler != null) {
+            handler.failed(new CancellationException());
+        }
+        return result;
+    }
+    
     public Future<T> createFuture() {
         return new JaxrsResponseCallback<T>(this);
     }
@@ -70,15 +79,34 @@ class JaxrsClientCallback<T> extends Cli
         public boolean cancel(boolean mayInterruptIfRunning) {
             return callback.cancel(mayInterruptIfRunning);
         }
-        @SuppressWarnings("unchecked")
+        
         public T get() throws InterruptedException, ExecutionException {
-            return (T)callback.get()[0];
+            try {
+                return getObject(callback.get()[0]);
+            } catch (InterruptedException ex) {
+                if (callback.handler != null) {
+                    callback.handler.failed((InterruptedException)ex);
+                }
+                throw ex;
+            }
         }
-        @SuppressWarnings("unchecked")
         public T get(long timeout, TimeUnit unit) throws InterruptedException,
             ExecutionException, TimeoutException {
-            return (T)callback.get(timeout, unit)[0];
+            try {
+                return getObject(callback.get(timeout, unit)[0]);
+            } catch (InterruptedException ex) {
+                if (callback.handler != null) {
+                    callback.handler.failed((InterruptedException)ex);
+                }
+                throw ex;
+            }
+        }
+        
+        @SuppressWarnings("unchecked")
+        private T getObject(Object object) {
+            return (T)object;
         }
+        
         public boolean isCancelled() {
             return callback.isCancelled();
         }
@@ -104,13 +132,9 @@ class JaxrsClientCallback<T> extends Cli
     @Override
     public void handleException(Map<String, Object> ctx, final Throwable ex) {
         context = ctx;
-        if (ex instanceof ProcessingException) {
-            exception = ex;
-        } else {
-            exception = new ProcessingException(ex);
-        }
+        exception = ex;
         if (handler != null) {
-            handler.failed((ProcessingException)exception);
+            handler.failed(exception);
         }
         done = true;
         synchronized (this) {

Modified: cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1500664&r1=1500663&r2=1500664&view=diff
==============================================================================
--- cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/trunk/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Mon Jul  8 11:28:43 2013
@@ -931,7 +931,15 @@ public class WebClient extends AbstractC
         
         doRunInterceptorChain(m);
         
-        return cb.createFuture();
+        Future<T> future = cb.createFuture();
+        if (m.getExchange().get(Exception.class) != null) {
+            Throwable ex = m.getExchange().get(Exception.class);
+            if (ex instanceof Fault) {
+                ex = ex.getCause();
+            }
+            cb.handleException(m, ex);
+        }
+        return future;
     }
 
     
@@ -963,22 +971,31 @@ public class WebClient extends AbstractC
                 r = (Response)results[0];
             }
         } catch (Exception ex) {
-            throw ex instanceof WebApplicationException 
+            Throwable t = ex instanceof WebApplicationException 
                 ? (WebApplicationException)ex 
                 : ex instanceof ProcessingException 
-                ? (ProcessingException)ex : new ProcessingException(ex); 
+                ? (ProcessingException)ex : new ProcessingException(ex);
+            cb.handleException(message, t);
+            return;
         }
         if (r == null) {
-            r = handleResponse(message.getExchange().getOutMessage(),
-                                        cb.getResponseClass(),
-                                        cb.getOutGenericType());
+            try {
+                r = handleResponse(message.getExchange().getOutMessage(),
+                                            cb.getResponseClass(),
+                                            cb.getOutGenericType());
+            } catch (Throwable t) {
+                cb.handleException(message, t);
+                return;
+            }
         }
-        
         if (cb.getResponseClass() == null || Response.class.equals(cb.getResponseClass())) {
             cb.handleResponse(message, new Object[] {r});
+        } else if (r.getStatus() >= 300) {
+            cb.handleException(message, convertToWebApplicationException(r));
         } else {
             cb.handleResponse(message, new Object[] {r.getEntity()});
         }
+        
     }
     private void handleAsyncFault(Message message) {
     }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java?rev=1500664&r1=1500663&r2=1500664&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java Mon Jul  8 11:28:43 2013
@@ -19,12 +19,27 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
+import javax.ws.rs.NotFoundException;
+import javax.ws.rs.ProcessingException;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.InvocationCallback;
+import javax.ws.rs.client.ResponseProcessingException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
 import javax.xml.ws.Holder;
 
 import org.apache.cxf.jaxrs.client.WebClient;
@@ -32,7 +47,6 @@ import org.apache.cxf.jaxrs.model.Abstra
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 
 import org.junit.BeforeClass;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class JAXRSAsyncClientTest extends AbstractBusClientServerTestBase {
@@ -67,27 +81,73 @@ public class JAXRSAsyncClientTest extend
     }
     
     @Test
-    @Ignore
+    public void testGetBookAsyncResponse404() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/bookheaders/404";
+        WebClient wc = createWebClient(address);
+        Future<Response> future = wc.async().get(Response.class);
+        assertEquals(404, future.get().getStatus());
+    }
+    
+    @Test
     public void testGetBookAsync404() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/404";
         WebClient wc = createWebClient(address);
         Future<Book> future = wc.async().get(Book.class);
-        Book book = future.get();
-        assertEquals(124L, book.getId());
+        try {
+            future.get();
+            fail("Exception expected");
+        } catch (ExecutionException ex) {
+            assertTrue(ex.getCause() instanceof NotFoundException);
+        }
+    }
+    
+    @Test
+    public void testPostBookProcessingException() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/";
+        List<Object> providers = new ArrayList<Object>();
+        providers.add(new FaultyBookWriter());
+        WebClient wc = WebClient.create(address, providers);
+        
+        Future<Book> future = wc.async().post(Entity.xml(new Book()), Book.class);
+        try {
+            future.get();
+            fail("Exception expected");
+        } catch (ExecutionException ex) {
+            assertTrue(ex.getCause() instanceof ProcessingException);
+        }
+    }
+    
+    @Test
+    public void testGetBookResponseProcessingException() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/books/123";
+        List<Object> providers = new ArrayList<Object>();
+        providers.add(new FaultyBookReader());
+        WebClient wc = WebClient.create(address, providers);
+        
+        Future<Book> future = wc.async().get(Book.class);
+        try {
+            future.get();
+            fail("Exception expected");
+        } catch (ExecutionException ex) {
+            assertTrue(ex.getCause() instanceof ResponseProcessingException);
+        }
     }
     
     @Test
-    @Ignore
     public void testGetBookAsync404Callback() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/404";
         WebClient wc = createWebClient(address);
         final Holder<Book> holder = new Holder<Book>();
         InvocationCallback<Book> callback = createCallback(holder);
-        Future<Book> future = wc.async().get(callback);
-        Book book = future.get();
-        assertEquals(124L, book.getId());
+        try {
+            wc.async().get(callback).get();
+            fail("Exception expected");
+        } catch (ExecutionException ex) {
+            assertTrue(ex.getCause() instanceof NotFoundException);
+        }
     }
     
+    
     private WebClient createWebClient(String address) {
         List<Object> providers = new ArrayList<Object>();
         WebClient wc = WebClient.create(address, providers);
@@ -106,4 +166,43 @@ public class JAXRSAsyncClientTest extend
         };
     }
     
+    private static class FaultyBookWriter implements MessageBodyWriter<Book> {
+
+        @Override
+        public long getSize(Book arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
+            // TODO Auto-generated method stub
+            return 0;
+        }
+
+        @Override
+        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return true;
+        }
+
+        @Override
+        public void writeTo(Book arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
+                            MultivaluedMap<String, Object> arg5, OutputStream arg6) throws IOException,
+            WebApplicationException {
+            throw new RuntimeException();
+            
+        }
+        
+    }
+    
+    private static class FaultyBookReader implements MessageBodyReader<Book> {
+
+        @Override
+        public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return true;
+        }
+
+        @Override
+        public Book readFrom(Class<Book> arg0, Type arg1, Annotation[] arg2, MediaType arg3,
+                             MultivaluedMap<String, String> arg4, InputStream arg5) throws IOException,
+            WebApplicationException {
+            throw new RuntimeException();
+        }
+
+                
+    }
 }