cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1373920 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Thu, 16 Aug 2012 16:56:38 GMT
Author: sergeyb
Date: Thu Aug 16 16:56:38 2012
New Revision: 1373920

URL: http://svn.apache.org/viewvc?rev=1373920&view=rev
Log:
[CXF-4455] Support for 2.0 Response readEntity methods

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=1373920&r1=1373919&r2=1373920&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Thu Aug 16 16:56:38 2012
@@ -54,6 +54,7 @@ import org.apache.cxf.interceptor.Interc
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
 import org.apache.cxf.jaxrs.ext.multipart.Multipart;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.impl.ResponseImpl;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
 import org.apache.cxf.jaxrs.model.Parameter;
@@ -567,6 +568,8 @@ public class ClientProxyImpl extends Abs
         throws Throwable {
         try {
             Response r = setResponseBuilder(outMessage, outMessage.getExchange()).build();
+            ((ResponseImpl)r).setMessage(outMessage);
+            
             Method method = outMessage.getExchange().get(Method.class);
             checkResponse(method, r, outMessage);
             if (method.getReturnType() == Void.class) { 

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1373920&r1=1373919&r2=1373920&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Thu
Aug 16 16:56:38 2012
@@ -47,6 +47,7 @@ import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
 import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.jaxrs.ext.form.Form;
+import org.apache.cxf.jaxrs.impl.ResponseImpl;
 import org.apache.cxf.jaxrs.model.ParameterType;
 import org.apache.cxf.jaxrs.model.URITemplate;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
@@ -841,7 +842,9 @@ public class WebClient extends AbstractC
             rb.entity(entity instanceof Response 
                       ? ((Response)entity).getEntity() : entity);
             
-            return rb.build();
+            Response r = rb.build();
+            ((ResponseImpl)r).setMessage(outMessage);
+            return r;
         } catch (Throwable ex) {
             throw (ex instanceof ClientWebApplicationException) ? (ClientWebApplicationException)ex
                                                               : new ClientWebApplicationException(ex);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java?rev=1373920&r1=1373919&r2=1373920&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
Thu Aug 16 16:56:38 2012
@@ -19,10 +19,10 @@
 
 package org.apache.cxf.jaxrs.impl;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -45,15 +45,21 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.NewCookie;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status.Family;
+import javax.ws.rs.ext.MessageBodyReader;
 
 import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
+import org.apache.cxf.message.Message;
 
 public final class ResponseImpl extends Response {
     private final int status;
     private Object entity;
     private MultivaluedMap<String, Object> metadata;
+    
+    private Message responseMessage;
     private boolean entityClosed;    
+    private boolean entityBufferred;
     
     ResponseImpl(int s) {
         this.status = s;
@@ -63,11 +69,19 @@ public final class ResponseImpl extends 
         this.status = s;
         this.entity = e;
     }
-
+    
     void addMetadata(MultivaluedMap<String, Object> meta) { 
         this.metadata = meta;
     }
 
+    //TODO: This method is needed because on the client side the
+    // Response processing is done after the chain completes, thus
+    // PhaseInterceptorChain.getCurrentMessage() returns null.
+    // The refactoring will be required
+    public void setMessage(Message message) {
+        this.responseMessage = message;
+    }
+    
     public int getStatus() {
         return status;
     }
@@ -224,42 +238,74 @@ public final class ResponseImpl extends 
     }
 
     public <T> T readEntity(Class<T> cls) throws MessageProcessingException,
IllegalStateException {
-        checkEntityIsAvailable();
-        return null;
+        return readEntity(cls, new Annotation[]{});
     }
 
     public <T> T readEntity(GenericType<T> genType) throws MessageProcessingException,
IllegalStateException {
-        checkEntityIsAvailable();
-        return null;
+        return readEntity(genType, new Annotation[]{});
     }
 
     public <T> T readEntity(Class<T> cls, Annotation[] anns) throws MessageProcessingException,
         IllegalStateException {
-        checkEntityIsAvailable();
-        return null;
+        
+        return doReadEntity(cls, cls, anns);
     }
 
+    @SuppressWarnings("unchecked")
     public <T> T readEntity(GenericType<T> genType, Annotation[] anns) throws
MessageProcessingException,
         IllegalStateException {
-        checkEntityIsAvailable();
-        return null;
+        return doReadEntity((Class<T>)genType.getRawType(), 
+                            genType.getType(), anns);
     }
     
-    public boolean bufferEntity() throws MessageProcessingException {
-        if (entity instanceof InputStream) {
-            if (entity instanceof ByteArrayInputStream) {
-                return false;
-            } else {
+    public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws
MessageProcessingException,
+        IllegalStateException {
+        
+        checkEntityIsAvailable();
+        
+        if (cls.isAssignableFrom(entity.getClass())) {
+            T response = cls.cast(entity);
+            closeIfNotBufferred(cls);
+            return response;
+        }
+        
+        if (responseMessage != null && entity instanceof InputStream) {
+            MediaType mediaType = getMediaType();
+            MessageBodyReader<T> mbr 
+                = ProviderFactory.getInstance(responseMessage).createMessageBodyReader(
+                    cls, t, anns, mediaType, responseMessage);
+            if (mbr != null) {
                 try {
-                    InputStream oldEntity = (InputStream)entity;
-                    entity = IOUtils.loadIntoBAIS(oldEntity);
-                    return true;
-                } catch (IOException ex) {
+                    T response = mbr.readFrom(cls, t, anns, mediaType, getStringHeaders(),

+                                        InputStream.class.cast(entity));
+                    closeIfNotBufferred(cls);
+                    return response;
+                } catch (Exception ex) {
                     throw new MessageProcessingException(ex);
                 }
             }
         }
-        return false;
+        
+        throw new MessageProcessingException("No Message Body reader is available");
+    }
+    
+    private void closeIfNotBufferred(Class<?> responseCls) {
+        if (!entityBufferred && !InputStream.class.isAssignableFrom(responseCls))
{
+            close();
+        }
+    }
+    
+    public boolean bufferEntity() throws MessageProcessingException {
+        if (!entityBufferred && entity instanceof InputStream) {
+            try {
+                InputStream oldEntity = (InputStream)entity;
+                entity = IOUtils.loadIntoBAIS(oldEntity);
+                entityBufferred = true;
+            } catch (IOException ex) {
+                throw new MessageProcessingException(ex);
+            }
+        }
+        return entityBufferred;
     }
 
     public void close() throws MessageProcessingException {
@@ -275,9 +321,9 @@ public final class ResponseImpl extends 
         
     }
     
-    private void checkEntityIsAvailable() throws MessageProcessingException {
-        if (entity == null) {
-            throw new MessageProcessingException("Entity is not available");
+    private void checkEntityIsAvailable() {
+        if (entityClosed) {
+            throw new IllegalStateException("Entity is not available");
         }
     }
 }

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=1373920&r1=1373919&r2=1373920&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
Thu Aug 16 16:56:38 2012
@@ -968,7 +968,7 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
-    public void testGetBookFromResponseWithProxy() throws Exception {
+    public void testGetBookFromResponseWithProxyAndReader() throws Exception {
         ResponseReader reader = new ResponseReader();
         reader.setEntityClass(Book.class);
         
@@ -982,7 +982,17 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
-    public void testGetBookFromResponseWithWebClient() throws Exception {
+    public void testGetBookFromResponseWithProxy() throws Exception {
+        BookStore bs = JAXRSClientFactory.create("http://localhost:" + PORT, 
+                                                 BookStore.class);
+        Response r = bs.getGenericResponseBook("123");
+        assertEquals(200, r.getStatus());
+        Book book = r.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+    }
+    
+    @Test
+    public void testGetBookFromResponseWithWebClientAndReader() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/genericresponse/123";
         WebClient wc = WebClient.create(address, 
                                         Collections.singletonList(
@@ -994,6 +1004,16 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testGetBookFromResponseWithWebClient() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/genericresponse/123";
+        WebClient wc = WebClient.create(address);
+        Response r = wc.accept("application/xml").get();
+        assertEquals(200, r.getStatus());
+        Book book = r.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+    }
+    
+    @Test
     public void testUpdateWithProxy() throws Exception {
         BookStore bs = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
         Book book = new Book();



Mime
View raw message