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/09/09 14:15:34 UTC

svn commit: r1521066 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Author: sergeyb
Date: Mon Sep  9 12:15:34 2013
New Revision: 1521066

URL: http://svn.apache.org/r1521066
Log:
[CXF-5135] Updating Response.readEntity to support reading entities of diff types if IS has been buffered

Modified:
    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/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=1521066&r1=1521065&r2=1521066&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 Mon Sep  9 12:15:34 2013
@@ -67,6 +67,7 @@ public final class ResponseImpl extends 
     private Message responseMessage;
     private boolean entityClosed;    
     private boolean entityBufferred;
+    private Object lastEntity;
     
     ResponseImpl(int s) {
         this.status = s;
@@ -131,7 +132,7 @@ public final class ResponseImpl extends 
     
     public Object getActualEntity() {
         checkEntityIsClosed();
-        return entity;
+        return lastEntity != null ? lastEntity : entity;
     }
     
     public Object getEntity() {
@@ -325,20 +326,13 @@ public final class ResponseImpl extends 
         
         checkEntityIsClosed();
         
-        if (!hasEntity()) {
-            throw new ProcessingException("Null entity");
-        }
+        if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass())
+            && !(lastEntity instanceof InputStream)) {
+            return cls.cast(lastEntity);
+        } 
         
-        if (cls.isAssignableFrom(entity.getClass())) {
-            return cls.cast(entity);
-        }
         if (entity instanceof InputStream) {
             
-            if (responseMessage == null) {
-                // won't happen, just in case
-                throw new RuntimeException();    
-            }
-            
             MediaType mediaType = getMediaType();
             if (mediaType == null) {
                 mediaType = MediaType.WILDCARD_TYPE;
@@ -351,27 +345,34 @@ public final class ResponseImpl extends 
             if (readers != null) {
                 try {
                     responseMessage.put(Message.PROTOCOL_HEADERS, this.getMetadata());
-                    Object newEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, 
+                    lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, 
                                                                            anns, 
                                                                            InputStream.class.cast(entity), 
                                                                            mediaType, 
                                                                            responseMessage);
-                    if (responseStreamCanBeClosed(cls)) {
-                        InputStream.class.cast(entity).close();
+                    if (!entityBufferred) {
+                        if (responseStreamCanBeClosed(cls)) {
+                            InputStream.class.cast(entity).close();
+                            entity = null;
+                        }
+                    } else {
+                        InputStream.class.cast(entity).reset();
                     }
-                    entity = newEntity;
-                    entityBufferred = true;
                     
-                    return cls.cast(entity);
+                    return cls.cast(lastEntity);
                 } catch (Exception ex) {
                     throw new ResponseProcessingException(this, ex.getMessage(), ex);    
                 }
             } else {
                 throw new ResponseProcessingException(this, "No message body reader for class: " + cls, null);
             }
+        } else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
+            lastEntity = entity;
+            return cls.cast(lastEntity);
         }
+        
         throw new IllegalStateException("The entity is not backed by an input stream, entity class is : "
-            + entity.getClass().getName());
+            + entity != null ? entity.getClass().getName() : null);
         
     }
     

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=1521066&r1=1521065&r2=1521066&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 Mon Sep  9 12:15:34 2013
@@ -188,6 +188,23 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testGetCustomBookBufferedResponse() {
+        String address = "http://localhost:" + PORT + "/bookstore/customresponse";
+        WebClient wc = WebClient.create(address);
+        Response r = wc.accept("application/xml").get(Response.class);
+        
+        r.bufferEntity();
+        
+        String bookStr = r.readEntity(String.class);
+        assertTrue(bookStr.endsWith("</Book>"));
+        
+        Book book = r.readEntity(Book.class);
+        assertEquals(222L, book.getId());
+        assertEquals("OK", r.getHeaderString("customresponse"));
+    }
+    
+    
+    @Test
     public void testGetCustomBookText() {
         String address = "http://localhost:" + PORT + "/bookstore/customtext";
         WebClient wc = WebClient.create(address);