cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1521943 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
Date Wed, 11 Sep 2013 17:17:56 GMT
Author: sergeyb
Date: Wed Sep 11 17:17:55 2013
New Revision: 1521943

URL: http://svn.apache.org/r1521943
Log:
[CXF-5135] Resetting buffered streams after a client reader returns an object can have side-effects

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.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=1521943&r1=1521942&r2=1521943&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
Wed Sep 11 17:17:55 2013
@@ -344,20 +344,20 @@ public final class ResponseImpl extends 
                                                     null);
             if (readers != null) {
                 try {
+                    if (entityBufferred) {
+                        InputStream.class.cast(entity).reset();
+                    }
+                    
                     responseMessage.put(Message.PROTOCOL_HEADERS, this.getMetadata());
                     lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, 
                                                                            anns, 
                                                                            InputStream.class.cast(entity),

                                                                            mediaType, 
                                                                            responseMessage);
-                    if (!entityBufferred) {
-                        if (responseStreamCanBeClosed(cls)) {
-                            InputStream.class.cast(entity).close();
-                            entity = null;
-                        }
-                    } else {
-                        InputStream.class.cast(entity).reset();
-                    }
+                    if (!entityBufferred && responseStreamCanBeClosed(cls)) {
+                        InputStream.class.cast(entity).close();
+                        entity = null;
+                    } 
                     
                     return cls.cast(lastEntity);
                 } catch (Exception ex) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java?rev=1521943&r1=1521942&r2=1521943&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
Wed Sep 11 17:17:55 2013
@@ -42,9 +42,24 @@ import javax.ws.rs.core.Variant;
 import javax.ws.rs.core.Variant.VariantListBuilder;
 import javax.ws.rs.ext.RuntimeDelegate;
 import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
-
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
+
+import org.w3c.dom.Document;
+
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
 import org.apache.cxf.jaxrs.resources.Book;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.easymock.EasyMock;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -53,6 +68,66 @@ import org.junit.Test;
 public class ResponseImplTest extends Assert {
     
     @Test
+    public void testReadBufferedStaxUtils() throws Exception {
+        ResponseImpl r = new ResponseImpl(200);
+        Source responseSource = readResponseSource(r);
+        Document doc = StaxUtils.read(responseSource);
+        assertEquals("Response", doc.getDocumentElement().getLocalName());
+    }
+    
+    @Test
+    public void testReadBufferedStaxSource() throws Exception {
+        ResponseImpl r = new ResponseImpl(200);
+        Source responseSource = readResponseSource(r);
+        Transformer trans = TransformerFactory.newInstance().newTransformer();
+        DOMResult res = new DOMResult();
+        trans.transform(responseSource, res);
+        Document doc = (Document)res.getNode();
+        assertEquals("Response", doc.getDocumentElement().getLocalName());
+    }
+    
+    private Source readResponseSource(ResponseImpl r) {
+        String content = "<Response " 
+            + " xmlns=\"urn:oasis:names:tc:xacml:2.0:context:schema:os\"" 
+            + " xmlns:ns2=\"urn:oasis:names:tc:xacml:2.0:policy:schema:os\">"
+            + "<Result><Decision>Permit</Decision><Status><StatusCode"

+            + " Value=\"urn:oasis:names:tc:xacml:1.0:status:ok\"/></Status></Result></Response>";
+        
+        
+        MultivaluedMap<String, Object> headers = new MetadataMap<String, Object>();
+        headers.putSingle("Content-Type", "text/xml");
+        r.addMetadata(headers);
+        r.setEntity(new ByteArrayInputStream(content.getBytes()), null);
+        r.setMessage(createMessage());
+        r.bufferEntity();
+        return r.readEntity(Source.class);
+    }
+    
+    private Message createMessage() {
+        ProviderFactory factory = ServerProviderFactory.getInstance();
+        Message m = new MessageImpl();
+        m.put("org.apache.cxf.http.case_insensitive_queries", false);
+        Exchange e = new ExchangeImpl();
+        m.setExchange(e);
+        e.setInMessage(m);
+        e.setOutMessage(new MessageImpl());
+        Endpoint endpoint = EasyMock.createMock(Endpoint.class);
+        endpoint.getEndpointInfo();
+        EasyMock.expectLastCall().andReturn(null).anyTimes();
+        endpoint.get(Application.class.getName());
+        EasyMock.expectLastCall().andReturn(null);
+        endpoint.size();
+        EasyMock.expectLastCall().andReturn(0).anyTimes();
+        endpoint.isEmpty();
+        EasyMock.expectLastCall().andReturn(true).anyTimes();
+        endpoint.get(ServerProviderFactory.class.getName());
+        EasyMock.expectLastCall().andReturn(factory).anyTimes();
+        EasyMock.replay(endpoint);
+        e.put(Endpoint.class, endpoint);
+        return m;
+    }
+    
+    @Test
     public void testResourceImpl() {
         String entity = "bar";
         ResponseImpl ri = new ResponseImpl(200, entity);
@@ -273,21 +348,6 @@ public class ResponseImplTest extends As
     }
     
     @Test
-    public void testGetLinksSameRel() {
-        ResponseImpl ri = new ResponseImpl(200);
-        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
-        ri.addMetadata(meta);
-        
-        meta.add(HttpHeaders.LINK, "<http://link1>");
-        meta.add(HttpHeaders.LINK, "<http://link2>");
-        
-        Set<Link> links = ri.getLinks();
-        assertEquals(2, links.size());
-        assertTrue(links.contains(Link.valueOf("<http://link1>")));
-        assertTrue(links.contains(Link.valueOf("<http://link2>")));
-    }
-    
-    @Test
     public void testGetLinks() {
         ResponseImpl ri = new ResponseImpl(200);
         MetadataMap<String, Object> meta = new MetadataMap<String, Object>();



Mime
View raw message