cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Updating StreamingResponseProvider to support objects dynamically
Date Fri, 22 May 2015 11:13:56 GMT
Repository: cxf
Updated Branches:
  refs/heads/master e2ec36443 -> 029be16dd


Updating StreamingResponseProvider to support objects dynamically


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/029be16d
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/029be16d
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/029be16d

Branch: refs/heads/master
Commit: 029be16dd1ac1349dd5b4e8037babd604cab5ad7
Parents: e2ec364
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri May 22 12:13:20 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri May 22 12:13:20 2015 +0100

----------------------------------------------------------------------
 .../provider/StreamingResponseProvider.java     | 31 ++++++++++----------
 .../apache/cxf/systest/jaxrs/BookServer.java    |  2 ++
 .../org/apache/cxf/systest/jaxrs/BookStore.java | 17 +++++++++++
 .../jaxrs/JAXRSClientServerBookTest.java        | 25 ++++++----------
 4 files changed, 44 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/029be16d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StreamingResponseProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StreamingResponseProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StreamingResponseProvider.java
index 3cfde6e..aaad2db 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StreamingResponseProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StreamingResponseProvider.java
@@ -50,15 +50,11 @@ public class StreamingResponseProvider<T> implements
                         MediaType mt, MultivaluedMap<String, Object> headers, OutputStream
os)
         throws IOException, WebApplicationException {
         Class<?> actualCls = InjectionUtils.getActualType(t);
-        @SuppressWarnings("unchecked")
-        MessageBodyWriter<T> writer = 
-            (MessageBodyWriter<T>)providers.getMessageBodyWriter(actualCls, actualCls,
anns, mt);
-        if (writer == null) {
-            throw new InternalServerErrorException();
+        if (cls == actualCls) {
+            actualCls = Object.class;
         }
         //TODO: review the possibility of caching the providers
-        StreamingResponseWriter thewriter = 
-            new StreamingResponseWriter(writer, actualCls, anns, mt, headers, os);
+        StreamingResponseWriter thewriter = new StreamingResponseWriter(actualCls, anns,
mt, headers, os);
         p.writeTo(thewriter);
     }
 
@@ -68,31 +64,36 @@ public class StreamingResponseProvider<T> implements
     }
     
     private class StreamingResponseWriter implements StreamingResponse.Writer<T> {
-        private MessageBodyWriter<T> writer;
-        private Class<?> cls;
+        private volatile MessageBodyWriter<T> writer;
+        private Class<?> entityCls;
         private MediaType mt;
         private Annotation[] anns;
         private MultivaluedMap<String, Object> headers; 
         private OutputStream os;
                 
-        public StreamingResponseWriter(MessageBodyWriter<T> writer, 
-                                       Class<?> cls,
+        public StreamingResponseWriter(Class<?> entityCls,
                                        Annotation[] anns,
                                        MediaType mt,
                                        MultivaluedMap<String, Object> headers, 
                                        OutputStream os) {
-            this.writer = writer;
-            this.cls = cls;
+            this.entityCls = entityCls;
             this.anns = anns;
             this.mt = mt;
             this.headers = headers;
             this.os = os;
         }
         
+        @SuppressWarnings("unchecked")
         @Override
         public void write(T data) throws IOException {
-            writer.writeTo(data, cls, cls, anns, mt, headers, os);
-            
+            Class<?> actualCls = entityCls != Object.class ? entityCls : data.getClass();

+            if (writer == null) {
+                writer = (MessageBodyWriter<T>)providers.getMessageBodyWriter(actualCls,
actualCls, anns, mt);
+                if (writer == null) {
+                    throw new InternalServerErrorException();
+                }
+            }
+            writer.writeTo(data, actualCls, actualCls, anns, mt, headers, os);
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/cxf/blob/029be16d/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
index c721141..f89ea95 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
@@ -47,6 +47,7 @@ import org.apache.cxf.jaxrs.ext.search.sql.SQLPrinterVisitor;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.provider.BinaryDataProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
+import org.apache.cxf.jaxrs.provider.StreamingResponseProvider;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
     
@@ -86,6 +87,7 @@ public class BookServer extends AbstractBusTestServerBase {
         providers.add(new BookStore.PrimitiveDoubleArrayReaderWriter());
         providers.add(new BookStore.StringArrayBodyReaderWriter());
         providers.add(new BookStore.StringListBodyReaderWriter());
+        providers.add(new StreamingResponseProvider<Object>());
         providers.add(new ContentTypeModifyingMBW());
         JAXBElementProvider<?> jaxbProvider = new JAXBElementProvider<Object>();
         Map<String, String> jaxbElementClassMap = new HashMap<String, String>();


http://git-wip-us.apache.org/repos/asf/cxf/blob/029be16d/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index 05e53ef..ffa7617 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -91,6 +91,7 @@ import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.Oneway;
+import org.apache.cxf.jaxrs.ext.StreamingResponse;
 import org.apache.cxf.jaxrs.ext.search.QueryContext;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.SearchContext;
@@ -1197,6 +1198,22 @@ public class BookStore {
         return new ResponseStreamingOutputImpl();
     }
     
+    @SuppressWarnings("rawtypes")
+    @GET
+    @Path("/books/streamingresponse")
+    @Produces("text/xml")
+    public Response getBookStreamingResponse() {
+        return Response.ok(new StreamingResponse() {
+
+            @SuppressWarnings("unchecked")
+            @Override
+            public void writeTo(Writer writer) throws IOException {
+                writer.write(new Book("stream", 124L));
+            }
+            
+        }).build();
+    }
+    
     @GET
     @Path("/books/fail-late")
     @Produces("application/bar")

http://git-wip-us.apache.org/repos/asf/cxf/blob/029be16d/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 4b65226..f8925c2 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -115,6 +115,14 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         assertEquals("root", book.getName());
     }
     @Test
+    public void testGetBookUntypedStreamingResponse() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/books/streamingresponse";
+        WebClient wc = WebClient.create(address);
+        Book book = wc.get(Book.class);
+        assertEquals(124L, book.getId());
+        assertEquals("stream", book.getName());
+    }
+    @Test
     public void testNonExistent() throws Exception {
         String address = "http://localhostt/bookstore";
         WebClient wc = WebClient.create(address, 
@@ -153,7 +161,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     public void testGetBookAcceptWildcard() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/books/wildcard";
         WebClient wc = WebClient.create(address);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000);
         Response r = wc.accept("text/*").get();
         assertEquals(406, r.getStatus());
     }
@@ -323,7 +330,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     public void testUseMapperOnBus() {
         String address = "http://localhost:" + PORT + "/bookstore/mapperonbus";
         WebClient wc = WebClient.create(address);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
         Response r = wc.post(null);
         assertEquals(500, r.getStatus());
         MediaType mt = r.getMediaType();
@@ -373,9 +379,7 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     
     private void doTestGetChapterFromSelectedBook(String address) {
         
-        WebClient wc = 
-            WebClient.create(address);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000);
+        WebClient wc = WebClient.create(address);
         wc.accept("application/xml");
         Chapter chapter = wc.get(Chapter.class);
         assertEquals("chapter 1", chapter.getTitle());    
@@ -476,7 +480,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         String base = "http://localhost:" + PORT;
         String endpointAddress = base + "/bookstore/name-in-query"; 
         WebClient wc = WebClient.create(endpointAddress);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000);
         String name = "Many        spaces";
         wc.query("name", name);
         String content = wc.get(String.class);
@@ -523,7 +526,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         String endpointAddress =
             "http://localhost:" + PORT + "/bookstore/infault"; 
         WebClient wc = WebClient.create(endpointAddress);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
         Response r = wc.get();
         assertEquals(401, r.getStatus());
     }
@@ -534,7 +536,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         String endpointAddress =
             "http://localhost:" + PORT + "/bookstore/outfault"; 
         WebClient wc = WebClient.create(endpointAddress);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
         Response r = wc.get();
         assertEquals(403, r.getStatus());
     }
@@ -558,7 +559,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         String endpointAddress =
             "http://localhost:" + PORT + "/bookstore/collections3"; 
         WebClient wc = WebClient.create(endpointAddress);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000);
         wc.accept("application/xml").type("application/xml");
         Book b1 = new Book("CXF in Action", 123L);
         Book b2 = new Book("CXF Rocks", 124L);
@@ -1038,7 +1038,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     public void testGetJAXBElementXmlRootBookCollection() throws Exception {
         BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT,
                                                     BookStore.class);
-        WebClient.getConfig(store).getHttpConduit().getClient().setReceiveTimeout(10000000);
         Book b1 = new Book("CXF in Action", 123L);
         Book b2 = new Book("CXF Rocks", 124L);
         List<JAXBElement<Book>> books = 
@@ -1062,7 +1061,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     public void testGetJAXBElementXmlRootBookCollectionWebClient() throws Exception {
         WebClient store = WebClient.create("http://localhost:" + PORT
                                            + "/bookstore/jaxbelementxmlrootcollections");
-        WebClient.getConfig(store).getHttpConduit().getClient().setReceiveTimeout(10000000);
         Book b1 = new Book("CXF in Action", 123L);
         Book b2 = new Book("CXF Rocks", 124L);
         List<Book> books = new ArrayList<Book>();
@@ -1202,7 +1200,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     }
     private void doTestExplicitOptions(String address) throws Exception {
         WebClient wc = WebClient.create(address);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
         WebClient.getConfig(wc).getRequestContext().put("org.apache.cxf.http.header.split",
true);
         Response response = wc.options();
         List<Object> values = response.getMetadata().get("Allow");
@@ -1965,7 +1962,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     public void testGetBookByHeaderPerRequestContextFault() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore2/bookheaders";
         WebClient wc = WebClient.create(address);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000);
         wc.accept("application/xml");
         wc.header("BOOK", "1", "3", "4");
         Response r = wc.get();
@@ -2050,7 +2046,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         provider.setOutTransformElements(outMap);
         WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/adapter-list",
                                         Collections.singletonList(provider));
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000);
         Response r = wc.type("application/xml").accept("application/json")
             .post(new Books(new Book("CXF", 123L)));
         assertEquals("{\"Book\":[{\"id\":123,\"name\":\"CXF\"}]}",
@@ -2076,7 +2071,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     @Test
     public void testGetBookAdapterInterfaceProxy() throws Exception {
         BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
-        WebClient.getConfig(store).getHttpConduit().getClient().setReceiveTimeout(10000000L);
         BookInfoInterface info = store.getBookAdapterInterface();
         assertEquals(123L, info.getId());
     }
@@ -2150,7 +2144,6 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     
     private void doTestGetBookWithResourceContext(String address) throws Exception {
         WebClient wc = WebClient.create(address);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000);
         wc.accept("application/xml");
         wc.query("bookid", "12345");
         wc.query("bookname", "bookcontext");


Mime
View raw message