cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1449112 - 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/
Date Fri, 22 Feb 2013 16:43:27 GMT
Author: sergeyb
Date: Fri Feb 22 16:43:26 2013
New Revision: 1449112

URL: http://svn.apache.org/r1449112
Log:
Merged revisions 1449019,1449079 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1449019 | sergeyb | 2013-02-22 12:48:41 +0000 (Fri, 22 Feb 2013) | 1 line
  
  [CXF-4848] Getting GenericEntity supported during async calls too
........
  r1449079 | sergeyb | 2013-02-22 15:44:51 +0000 (Fri, 22 Feb 2013) | 1 line
  
  [CXF-4849] Support for JAX-RS 2.0 GenericType
........

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/WebClient.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1449019-1449079

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/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=1449112&r1=1449111&r2=1449112&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
Fri Feb 22 16:43:26 2013
@@ -59,6 +59,7 @@ import org.apache.cxf.jaxrs.model.Parame
 import org.apache.cxf.jaxrs.model.URITemplate;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
 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;
@@ -348,6 +349,22 @@ 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()
@@ -383,13 +400,25 @@ 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 
      * @return the future
      */
     public <T> Future<T> post(Object body, InvocationCallback<T> callback)
{
-        return doInvokeAsyncCallback("POST", body, body.getClass(), getClass(), callback);
+        return doInvokeAsyncCallback("POST", body, body.getClass(), null, callback);
     }
     
     /**
@@ -404,13 +433,24 @@ public class WebClient extends AbstractC
     }
     
     /**
+     * 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)
      * @param callback invocation callback 
      * @return the future
      */
     public <T> Future<T> put(Object body, InvocationCallback<T> callback)
{
-        return doInvokeAsyncCallback("PUT", body, body.getClass(), getClass(), callback);
+        return doInvokeAsyncCallback("PUT", body, body.getClass(), null, callback);
     }
     
     /**
@@ -513,6 +553,16 @@ public class WebClient extends AbstractC
     }
     
     /**
+     * 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)
      * @param callback invocation callback 
@@ -821,7 +871,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);
     }
@@ -834,6 +893,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();
 
@@ -871,9 +937,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 ClientException 
+                ? (ClientException)ex : new ClientException(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});
@@ -1260,22 +1340,34 @@ 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/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java?rev=1449112&r1=1449111&r2=1449112&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Fri Feb 22 16:43:26 2013
@@ -33,6 +33,8 @@ 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.GenericType;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -102,6 +104,147 @@ 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());
+    }
+    
+    @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);
+        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(ClientException error) {
+            }
+        };
+    }
+    
+    
+    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(ClientException arg0) {
+            // TODO Auto-generated method stub
+            
+        }
+        
+    }
+    
     private void doTestGetBook(String address) {
         WebClient wc = createWebClient(address);
         Book book = wc.get(Book.class);
@@ -135,13 +278,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(ClientException error) {
-            }
-        };
+        InvocationCallback<Book> callback = createCallback(holder);
         
         Future<Book> future = asyncInvoker ? wc.async().get(callback) : wc.get(callback);
         Book book = future.get();

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1449112&r1=1449111&r2=1449112&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Fri Feb 22 16:43:26 2013
@@ -36,6 +36,7 @@ import javax.ws.rs.ServerErrorException;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.client.ClientException;
 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;
@@ -341,6 +342,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