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 14:17:19 UTC
svn commit: r1500686 - in /cxf/branches/2.7.x-fixes: ./
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/
systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Author: sergeyb
Date: Mon Jul 8 12:17:18 2013
New Revision: 1500686
URL: http://svn.apache.org/r1500686
Log:
Merged revisions 1500664 via svnmerge from
https://svn.apache.org/repos/asf/cxf/trunk
........
r1500664 | sergeyb | 2013-07-08 12:28:43 +0100 (Mon, 08 Jul 2013) | 1 line
[CXF-5026] Updating WebClient to handle async exceptions better
........
Modified:
cxf/branches/2.7.x-fixes/ (props changed)
cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java
cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java
Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
Merged /cxf/trunk:r1500664
Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java?rev=1500686&r1=1500685&r2=1500686&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java (original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java Mon Jul 8 12:17:18 2013
@@ -21,6 +21,7 @@ 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;
@@ -51,6 +52,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 ClientException(new CancellationException()));
+ }
+ return result;
+ }
+
public Future<T> createFuture() {
return new JaxrsResponseCallback<T>(this);
}
@@ -70,15 +80,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(new ClientException((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(new ClientException((InterruptedException)ex));
+ }
+ throw ex;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private T getObject(Object object) {
+ return (T)object;
}
+
public boolean isCancelled() {
return callback.isCancelled();
}
@@ -104,13 +133,9 @@ class JaxrsClientCallback<T> extends Cli
@Override
public void handleException(Map<String, Object> ctx, final Throwable ex) {
context = ctx;
- if (ex instanceof ClientException) {
- exception = ex;
- } else {
- exception = new ClientException(ex);
- }
+ exception = ex;
if (handler != null) {
- handler.failed((ClientException)exception);
+ handler.failed(new ClientException(exception));
}
done = true;
synchronized (this) {
Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1500686&r1=1500685&r2=1500686&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Mon Jul 8 12:17:18 2013
@@ -932,7 +932,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;
}
@@ -964,19 +972,28 @@ 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 ClientException
- ? (ClientException)ex : new ClientException(ex);
+ ? (ClientException)ex : new ClientException(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()});
}
@@ -1136,7 +1153,7 @@ public class WebClient extends AbstractC
writeBody(body, outMessage,
requestClass == null || !isAssignable ? body.getClass() : requestClass,
requestType == null || !isAssignable ? body.getClass() : requestType,
- new Annotation[]{}, os);
+ anns, os);
} catch (Exception ex) {
throw new Fault(ex);
}
Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java?rev=1500686&r1=1500685&r2=1500686&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java (original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java Mon Jul 8 12:17:18 2013
@@ -19,13 +19,26 @@
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.WebApplicationException;
import javax.ws.rs.client.ClientException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
+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;
@@ -33,7 +46,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 {
@@ -68,27 +80,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 ClientException);
+ }
+ }
+
+ @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 ClientException);
+ }
}
@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);
@@ -107,4 +165,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();
+ }
+
+
+ }
}