cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1449079 - in /cxf/trunk: rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Fri, 22 Feb 2013 15:44:51 GMT
Author: sergeyb
Date: Fri Feb 22 15:44:51 2013
New Revision: 1449079

URL: http://svn.apache.org/r1449079
Log:
[CXF-4849] Support for JAX-RS 2.0 GenericType

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
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.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=1449079&r1=1449078&r2=1449079&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 15:44:51 2013
@@ -58,6 +58,7 @@ import org.apache.cxf.jaxrs.impl.Respons
 import org.apache.cxf.jaxrs.model.ParameterType;
 import org.apache.cxf.jaxrs.model.URITemplate;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
+import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.jaxrs.utils.ParameterizedCollectionType;
 import org.apache.cxf.message.Exchange;
@@ -347,6 +348,21 @@ public class WebClient extends AbstractC
      * Does HTTP invocation and returns types response object 
      * @param httpMethod HTTP method 
      * @param body request body, can be null
+     * @param responseType generic response type
+     * @return typed object, can be null. Response status code and headers 
+     *         can be obtained too, see Client.getResponse()
+     */
+    public <T> T invoke(String httpMethod, Object body, GenericType<T> responseType)
{
+        @SuppressWarnings("unchecked")
+        Class<T> responseClass = (Class<T>)responseType.getRawType();
+        Response r = doInvoke(httpMethod, body, null, responseClass, responseType.getType());
+        return responseClass.cast(responseClass == Response.class ? r : r.getEntity());
+    }
+    
+    /**
+     * Does HTTP invocation and returns types response object 
+     * @param httpMethod HTTP method 
+     * @param body request body, can be null
      * @param responseClass expected type of response object
      * @return typed object, can be null. Response status code and headers 
      *         can be obtained too, see Client.getResponse()
@@ -382,6 +398,17 @@ public class WebClient extends AbstractC
     }
     
     /**
+     * Does HTTP POST invocation and returns typed response object
+     * @param body request body, can be null
+     * @param responseType generic response type
+     * @return typed object, can be null. Response status code and headers 
+     *         can be obtained too, see Client.getResponse()
+     */
+    public <T> T post(Object body, GenericType<T> responseType) {
+        return invoke("POST", body, responseType);
+    }
+    
+    /**
      * Does HTTP Async POST invocation and returns Future.
      * Shortcut for async().post(Entity, InvocationCallback)
      * @param callback invocation callback 
@@ -402,6 +429,18 @@ public class WebClient extends AbstractC
         return invoke("PUT", body, responseClass);
     }
     
+
+    /**
+     * Does HTTP PUT invocation and returns typed response object
+     * @param body request body, can be null
+     * @param responseType generic response type
+     * @return typed object, can be null. Response status code and headers 
+     *         can be obtained too, see Client.getResponse()
+     */
+    public <T> T put(Object body, GenericType<T> responseType) {
+        return invoke("PUT", body, responseType);
+    }
+    
     /**
      * Does HTTP Async PUT invocation and returns Future.
      * Shortcut for async().put(Entity, InvocationCallback)
@@ -511,6 +550,17 @@ public class WebClient extends AbstractC
         return invoke("GET", null, responseClass);
     }
     
+
+    /**
+     * Does HTTP GET invocation and returns typed response object
+     * @param responseType generic response type
+     * @return typed object, can be null. Response status code and headers 
+     *         can be obtained too, see Client.getResponse()
+     */
+    public <T> T get(GenericType<T> responseType) {
+        return invoke("GET", null, responseType);
+    }
+    
     /**
      * Does HTTP Async GET invocation and returns Future.
      * Shortcut for async().get(InvocationCallback)
@@ -805,8 +855,8 @@ public class WebClient extends AbstractC
     }
     private Type getCallbackType(InvocationCallback<?> callback) {
         Class<?> cls = callback.getClass();
-        ParameterizedType t = findCallbackType(cls);
-        for (Type tp : t.getActualTypeArguments()) {
+        ParameterizedType pt = findCallbackType(cls);
+        for (Type tp : pt.getActualTypeArguments()) {
             return tp;
         }
         return null;
@@ -819,7 +869,16 @@ public class WebClient extends AbstractC
                                                   InvocationCallback<T> callback) {
         
         Type outType = getCallbackType(callback);
-        Class<?> respClass = outType instanceof Class ? (Class<?>) outType :
null;
+        Class<?> respClass = null;
+        if (outType instanceof Class) {
+            respClass = (Class<?>)outType;
+        } else if (outType instanceof ParameterizedType) { 
+            ParameterizedType pt = (ParameterizedType)outType;
+            if (pt.getRawType() instanceof Class) {
+                respClass = (Class<?>)pt.getRawType();
+                outType = InjectionUtils.getActualType(pt);
+            }
+        } 
         
         return doInvokeAsync(httpMethod, body, requestClass, inType, respClass, outType,
callback);
     }

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=1449079&r1=1449078&r2=1449079&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 15:44:51 2013
@@ -33,6 +33,7 @@ import javax.ws.rs.client.ClientResponse
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.InvocationCallback;
 import javax.ws.rs.core.GenericEntity;
+import javax.ws.rs.core.GenericType;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -144,6 +145,64 @@ public class JAXRS20ClientServerBookTest
         assertEquals(collectionEntity.getEntity().get(0).getName(), book.getName());
     }
     
+    @Test
+    public void testPostGetCollectionGenericEntityAndType() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/collections"; 
+        WebClient wc = WebClient.create(endpointAddress);
+        wc.accept("application/xml").type("application/xml");
+        GenericEntity<List<Book>> collectionEntity = createGenericEntity();
+        final Holder<List<Book>> holder = new Holder<List<Book>>();
+        InvocationCallback<List<Book>> callback = new CustomInvocationCallback(holder);
+            
+        Future<List<Book>> future = wc.async().post(Entity.entity(collectionEntity,
"application/xml"),
+                                                    callback);    
+            
+        List<Book> books2 = future.get();
+        assertNotNull(books2);
+        
+        List<Book> books = collectionEntity.getEntity();
+        assertNotSame(books, books2);
+        assertEquals(2, books2.size());
+        Book b11 = books.get(0);
+        assertEquals(123L, b11.getId());
+        assertEquals("CXF in Action", b11.getName());
+        Book b22 = books.get(1);
+        assertEquals(124L, b22.getId());
+        assertEquals("CXF Rocks", b22.getName());
+        assertEquals(200, wc.getResponse().getStatus());
+    }
+    
+    @Test
+    public void testPostGetCollectionGenericEntityAndType2() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/collections"; 
+        WebClient wc = WebClient.create(endpointAddress);
+        wc.accept("application/xml").type("application/xml");
+        GenericEntity<List<Book>> collectionEntity = createGenericEntity();
+        GenericType<List<Book>> genericResponseType = new GenericType<List<Book>>()
{        
+        };
+            
+        Future<List<Book>> future = wc.async().post(Entity.entity(collectionEntity,
"application/xml"),
+                                                    genericResponseType);    
+            
+        List<Book> books2 = future.get();
+        assertNotNull(books2);
+        
+        List<Book> books = collectionEntity.getEntity();
+        assertNotSame(books, books2);
+        assertEquals(2, books2.size());
+        Book b11 = books.get(0);
+        assertEquals(123L, b11.getId());
+        assertEquals("CXF in Action", b11.getName());
+        Book b22 = books.get(1);
+        assertEquals(124L, b22.getId());
+        assertEquals("CXF Rocks", b22.getName());
+        assertEquals(200, wc.getResponse().getStatus());
+    }
+    
     private GenericEntity<List<Book>> createGenericEntity() {
         Book b1 = new Book("CXF in Action", 123L);
         Book b2 = new Book("CXF Rocks", 124L);
@@ -164,6 +223,27 @@ public class JAXRS20ClientServerBookTest
         };
     }
     
+    
+    private static class CustomInvocationCallback implements InvocationCallback<List<Book>>
{
+        private Holder<List<Book>> holder;
+        public CustomInvocationCallback(Holder<List<Book>> holder) {
+            this.holder = holder;
+        }
+        
+        @Override
+        public void completed(List<Book> books) {
+            holder.value = books;
+            
+        }
+
+        @Override
+        public void failed(Throwable arg0) {
+            // TODO Auto-generated method stub
+            
+        }
+        
+    }
+    
     private void doTestGetBook(String address) {
         WebClient wc = createWebClient(address);
         Book book = wc.get(Book.class);

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1449079&r1=1449078&r2=1449079&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Fri Feb 22 15:44:51 2013
@@ -37,6 +37,7 @@ import javax.ws.rs.ServerErrorException;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.client.ResponseProcessingException;
 import javax.ws.rs.core.GenericEntity;
+import javax.ws.rs.core.GenericType;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
@@ -342,6 +343,39 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testPostGetCollectionGenericEntityAndType() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/collections"; 
+        WebClient wc = WebClient.create(endpointAddress);
+        wc.accept("application/xml").type("application/xml");
+        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);
+        
+        GenericEntity<List<Book>> genericCollectionEntity = 
+            new GenericEntity<List<Book>>(books) {
+            };
+        GenericType<List<Book>> genericResponseType =
+            new GenericType<List<Book>>() {
+            };
+        
+        List<Book> books2 = wc.post(genericCollectionEntity, genericResponseType);
+        assertNotNull(books2);
+        assertNotSame(books, books2);
+        assertEquals(2, books2.size());
+        Book b11 = books.get(0);
+        assertEquals(123L, b11.getId());
+        assertEquals("CXF in Action", b11.getName());
+        Book b22 = books.get(1);
+        assertEquals(124L, b22.getId());
+        assertEquals("CXF Rocks", b22.getName());
+        assertEquals(200, wc.getResponse().getStatus());
+    }
+    
+    @Test
     public void testPostCollectionOfBooksWebClient() throws Exception {
         
         String endpointAddress =



Mime
View raw message