cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1449019 - in /cxf/trunk: rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Date Fri, 22 Feb 2013 12:48:42 GMT
Author: sergeyb
Date: Fri Feb 22 12:48:41 2013
New Revision: 1449019

URL: http://svn.apache.org/r1449019
Log:
[CXF-4848] Getting GenericEntity supported during async calls too

Modified:
    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/JAXRS20ClientServerBookTest.java

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=1449019&r1=1449018&r2=1449019&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 Fri Feb
22 12:48:41 2013
@@ -388,7 +388,7 @@ public class WebClient extends AbstractC
      * @return the future
      */
     public <T> Future<T> post(Object body, InvocationCallback<T> callback)
{
-        return doInvokeAsyncCallback("POST", body, body.getClass(), body.getClass(), callback);
+        return doInvokeAsyncCallback("POST", body, body.getClass(), null, callback);
     }
     
     /**
@@ -409,7 +409,7 @@ public class WebClient extends AbstractC
      * @return the future
      */
     public <T> Future<T> put(Object body, InvocationCallback<T> callback)
{
-        return doInvokeAsyncCallback("PUT", body, body.getClass(), body.getClass(), callback);
+        return doInvokeAsyncCallback("PUT", body, body.getClass(), null, callback);
     }
     
     /**
@@ -832,6 +832,13 @@ public class WebClient extends AbstractC
                                           Type outType,
                                           InvocationCallback<T> callback) {
         
+        if (body instanceof GenericEntity) {
+            GenericEntity<?> genericEntity = (GenericEntity<?>)body;
+            body = genericEntity.getEntity();
+            requestClass = genericEntity.getRawType();
+            inType = getGenericEntityType(genericEntity, inType);
+        }
+        
         MultivaluedMap<String, String> headers = prepareHeaders(respClass, body);
         resetResponse();
 
@@ -869,9 +876,23 @@ public class WebClient extends AbstractC
     
     private void handleAsyncResponse(Message message) {
         JaxrsClientCallback<?> cb = message.getExchange().get(JaxrsClientCallback.class);
-        Response r = handleResponse(message.getExchange().getOutMessage(),
-                                    cb.getResponseClass(),
-                                    cb.getOutGenericType());
+        Response r = null;
+        try {
+            Object[] results = preProcessResult(message);
+            if (results != null && results.length == 1) {
+                r = (Response)results[0];
+            }
+        } catch (Exception ex) {
+            throw ex instanceof WebApplicationException 
+                ? (WebApplicationException)ex 
+                : ex instanceof ProcessingException 
+                ? (ProcessingException)ex : new ProcessingException(ex); 
+        }
+        if (r == null) {
+            r = handleResponse(message.getExchange().getOutMessage(),
+                                        cb.getResponseClass(),
+                                        cb.getOutGenericType());
+        }
         
         if (cb.getResponseClass() == null || Response.class.equals(cb.getResponseClass()))
{
             cb.handleResponse(message, new Object[] {r});
@@ -1258,22 +1279,33 @@ public class WebClient extends AbstractC
 
         @Override
         public <T> Future<T> method(String name, Entity<?> entity, Class<T>
responseType) {
-            return doInvokeAsync(name, entity.getEntity(), entity.getClass(), entity.getClass(),

+            setEntityHeaders(entity);
+            return doInvokeAsync(name, entity.getEntity(), entity.getEntity().getClass(),
null, 
                                  responseType, responseType, null);
         }
 
         @Override
         public <T> Future<T> method(String name, Entity<?> entity, GenericType<T>
responseType) {
-            return doInvokeAsync(name, entity.getEntity(), entity.getClass(), entity.getClass(),

+            setEntityHeaders(entity);
+            return doInvokeAsync(name, entity.getEntity(), entity.getEntity().getClass(),
null, 
                                  responseType.getRawType(), responseType.getType(), null);
         }
 
         @Override
         public <T> Future<T> method(String name, Entity<?> entity, InvocationCallback<T>
callback) {
-            return doInvokeAsync(name, entity.getEntity(), entity.getClass(), entity.getClass(),

-                                 Response.class, Response.class, callback);
+            setEntityHeaders(entity);
+            return doInvokeAsyncCallback(name, entity.getEntity(), entity.getEntity().getClass(),
null, 
+                callback);
+        }
+        private void setEntityHeaders(Entity<?> entity) {
+            WebClient.this.type(entity.getMediaType());
+            if (entity.getLanguage() != null) {
+                WebClient.this.language(entity.getLanguage().toString());
+            }
+            if (entity.getEncoding() != null) {
+                WebClient.this.encoding(entity.getEncoding());
+            }
         }
-        
     }
     
 }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java?rev=1449019&r1=1449018&r2=1449019&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Fri Feb 22 12:48:41 2013
@@ -32,6 +32,7 @@ import javax.ws.rs.client.ClientResponse
 import javax.ws.rs.client.ClientResponseFilter;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.InvocationCallback;
+import javax.ws.rs.core.GenericEntity;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -101,6 +102,68 @@ public class JAXRS20ClientServerBookTest
         doTestGetBookAsync(address, false);
     }
     
+    @Test
+    public void testPostCollectionGenericEntity() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/collections3"; 
+        WebClient wc = WebClient.create(endpointAddress);
+        wc.accept("application/xml").type("application/xml");
+        
+        GenericEntity<List<Book>> collectionEntity = createGenericEntity();
+        final Holder<Book> holder = new Holder<Book>();
+        InvocationCallback<Book> callback = createCallback(holder);        
+            
+        Future<Book> future = wc.post(collectionEntity, callback);
+        Book book = future.get();
+        assertEquals(200, wc.getResponse().getStatus());
+        assertSame(book, holder.value);
+        assertNotSame(collectionEntity.getEntity().get(0), book);
+        assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName());
+    }
+    
+    @Test
+    public void testPostCollectionGenericEntityAsEntity() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/collections3"; 
+        WebClient wc = WebClient.create(endpointAddress);
+        wc.accept("application/xml");
+        
+        GenericEntity<List<Book>> collectionEntity = createGenericEntity();
+        
+        final Holder<Book> holder = new Holder<Book>();
+        InvocationCallback<Book> callback = createCallback(holder);        
+            
+        Future<Book> future = wc.async().post(Entity.entity(collectionEntity, "application/xml"),
+                                              callback);
+        Book book = future.get();
+        assertEquals(200, wc.getResponse().getStatus());
+        assertSame(book, holder.value);
+        assertNotSame(collectionEntity.getEntity().get(0), book);
+        assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName());
+    }
+    
+    private GenericEntity<List<Book>> createGenericEntity() {
+        Book b1 = new Book("CXF in Action", 123L);
+        Book b2 = new Book("CXF Rocks", 124L);
+        List<Book> books = new ArrayList<Book>();
+        books.add(b1);
+        books.add(b2);
+        return new GenericEntity<List<Book>>(books) {
+            };
+    }
+    
+    private InvocationCallback<Book> createCallback(final Holder<Book> holder)
{
+        return new InvocationCallback<Book>() {
+            public void completed(Book response) {
+                holder.value = response;
+            }
+            public void failed(Throwable error) {
+            }
+        };
+    }
+    
     private void doTestGetBook(String address) {
         WebClient wc = createWebClient(address);
         Book book = wc.get(Book.class);
@@ -134,13 +197,7 @@ public class JAXRS20ClientServerBookTest
         WebClient wc = createWebClient(address);
         
         final Holder<Book> holder = new Holder<Book>();
-        final InvocationCallback<Book> callback = new InvocationCallback<Book>()
{
-            public void completed(Book response) {
-                holder.value = response;
-            }
-            public void failed(Throwable error) {
-            }
-        };
+        InvocationCallback<Book> callback = createCallback(holder);
         
         Future<Book> future = asyncInvoker ? wc.async().get(callback) : wc.get(callback);
         Book book = future.get();



Mime
View raw message