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:53:29 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 77774b51d -> 8095ff7e4


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/8095ff7e
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8095ff7e
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8095ff7e

Branch: refs/heads/2.7.x-fixes
Commit: 8095ff7e43df52b629c1b8d06d366301ed32b2cb
Parents: 77774b5
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:52:48 2015 +0100

----------------------------------------------------------------------
 .../apache/cxf/jaxrs/ext/StreamingResponse.java |  31 ++++++
 .../provider/StreamingResponseProvider.java     | 105 +++++++++++++++++++
 .../apache/cxf/systest/jaxrs/BookServer.java    |   2 +
 .../org/apache/cxf/systest/jaxrs/BookStore.java |  19 +++-
 .../jaxrs/JAXRSClientServerBookTest.java        |  20 ++--
 5 files changed, 166 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/8095ff7e/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/StreamingResponse.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/StreamingResponse.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/StreamingResponse.java
new file mode 100644
index 0000000..ccd9359
--- /dev/null
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/StreamingResponse.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public interface StreamingResponse<T> {
+    interface Writer<T> {
+        void write(T data) throws IOException;
+        OutputStream getEntityStream();
+    }
+    void writeTo(Writer<T> writer) throws IOException;
+}
+

http://git-wip-us.apache.org/repos/asf/cxf/blob/8095ff7e/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
new file mode 100644
index 0000000..aaad2db
--- /dev/null
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StreamingResponseProvider.java
@@ -0,0 +1,105 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.provider;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Providers;
+
+import org.apache.cxf.jaxrs.ext.StreamingResponse;
+import org.apache.cxf.jaxrs.utils.InjectionUtils;
+
+public class StreamingResponseProvider<T> implements 
+    MessageBodyWriter<StreamingResponse<T>> {
+
+    @Context
+    private Providers providers;
+    
+    @Override
+    public boolean isWriteable(Class<?> cls, Type type, Annotation[] anns, MediaType
mt) {
+        return StreamingResponse.class.isAssignableFrom(cls);
+    }
+
+    @Override
+    public void writeTo(StreamingResponse<T> p, Class<?> cls, Type t, Annotation[]
anns,
+                        MediaType mt, MultivaluedMap<String, Object> headers, OutputStream
os)
+        throws IOException, WebApplicationException {
+        Class<?> actualCls = InjectionUtils.getActualType(t);
+        if (cls == actualCls) {
+            actualCls = Object.class;
+        }
+        //TODO: review the possibility of caching the providers
+        StreamingResponseWriter thewriter = new StreamingResponseWriter(actualCls, anns,
mt, headers, os);
+        p.writeTo(thewriter);
+    }
+
+    @Override
+    public long getSize(StreamingResponse<T> arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4) {
+        return -1;
+    }
+    
+    private class StreamingResponseWriter implements StreamingResponse.Writer<T> {
+        private volatile MessageBodyWriter<T> writer;
+        private Class<?> entityCls;
+        private MediaType mt;
+        private Annotation[] anns;
+        private MultivaluedMap<String, Object> headers; 
+        private OutputStream os;
+                
+        public StreamingResponseWriter(Class<?> entityCls,
+                                       Annotation[] anns,
+                                       MediaType mt,
+                                       MultivaluedMap<String, Object> headers, 
+                                       OutputStream os) {
+            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 {
+            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
+        public OutputStream getEntityStream() {
+            return os;
+        }
+        
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/8095ff7e/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 52a3d9b..dd8f3a4 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
@@ -41,6 +41,7 @@ import org.apache.cxf.jaxrs.interceptor.JAXRSOutExceptionMapperInterceptor;
 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;
     
@@ -69,6 +70,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/8095ff7e/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 c314463..3e4b0a9 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
@@ -89,13 +89,14 @@ 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;
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
 import org.apache.cxf.jaxrs.ext.xml.XSISchemaLocation;
-import org.apache.cxf.jaxrs.impl.ResourceContextImpl;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.impl.ResourceContextImpl;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.PhaseInterceptorChain;
@@ -1123,6 +1124,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/8095ff7e/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 1a90cd8..86375fe 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
@@ -109,6 +109,15 @@ 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);
+        wc.accept("text/xml");
+        Book book = wc.get(Book.class);
+        assertEquals(124L, book.getId());
+        assertEquals("stream", book.getName());
+    }
+    @Test
     public void testGetBookQueryGZIP() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/";
         WebClient wc = WebClient.create(address);
@@ -293,7 +302,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();
@@ -343,9 +351,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());    
@@ -446,7 +452,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);
@@ -503,7 +508,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());
     }
@@ -1856,7 +1860,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();
@@ -1932,7 +1935,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\"}]}",
@@ -1958,7 +1960,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());
     }
@@ -2032,7 +2033,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