cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [27/36] cxf git commit: [CXF-6833] Generalizing Observable writer to support arbitrary types
Date Sun, 01 May 2016 19:36:43 GMT
[CXF-6833] Generalizing Observable writer to support arbitrary types


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

Branch: refs/heads/master-jaxrs-2.1
Commit: 4ddf74b1acd8f65b685c8e11b809a06807799f84
Parents: 5c0c7df
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Apr 29 15:23:35 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Apr 29 15:23:35 2016 +0100

----------------------------------------------------------------------
 .../jaxrs/reactive/JAXRSReactiveTest.java       |  9 +++++-
 .../jaxrs/reactive/ObservableWriter.java        | 31 ++++++++++++++------
 .../systest/jaxrs/reactive/ReactiveServer.java  | 10 ++++++-
 .../systest/jaxrs/reactive/ReactiveService.java |  7 +++++
 4 files changed, 46 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/4ddf74b1/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java
index c92e8fa..582ab4b 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/JAXRSReactiveTest.java
@@ -49,6 +49,13 @@ public class JAXRSReactiveTest extends AbstractBusClientServerTestBase
{
         String text = wc.accept("text/plain").get(String.class);
         assertEquals("Hello, world!", text);
     }
-    
+    @Test
+    public void testGetHelloWorldJson() throws Exception {
+        String address = "http://localhost:" + PORT + "/reactive/textJson";
+        WebClient wc = WebClient.create(address);
+        String text = wc.accept("application/json").get(String.class);
+        assertTrue("{\"audience\":\"World\",\"greeting\":\"Hello\"}".equals(text)
+                   || "{\"greeting\":\"Hello\",\"audience\":\"World\"}".equals(text));
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/4ddf74b1/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java
index 57e7aec..88b7e49 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ObservableWriter.java
@@ -22,22 +22,25 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
-import java.nio.charset.StandardCharsets;
 
 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.utils.ExceptionUtils;
-import org.apache.cxf.jaxrs.utils.HttpUtils;
 
 import rx.Observable;
 
-public class ObservableWriter implements MessageBodyWriter<Observable<String>>
{
+public class ObservableWriter<T> implements MessageBodyWriter<Observable<T>>
{
 
+    @Context
+    private Providers providers;
+    
     @Override
-    public long getSize(Observable<String> arg0, Class<?> arg1, Type arg2, Annotation[]
arg3, MediaType arg4) {
+    public long getSize(Observable<T> arg0, Class<?> arg1, Type arg2, Annotation[]
arg3, MediaType arg4) {
         // TODO Auto-generated method stub
         return -1;
     }
@@ -48,16 +51,26 @@ public class ObservableWriter implements MessageBodyWriter<Observable<String>>
{
     }
 
     @Override
-    public void writeTo(Observable<String> obs, Class<?> cls, Type t, Annotation[]
anns, MediaType mt,
+    public void writeTo(Observable<T> obs, Class<?> cls, Type t, Annotation[]
anns, MediaType mt,
                         MultivaluedMap<String, Object> headers, OutputStream os)
                             throws IOException, WebApplicationException {
-        final String enc = HttpUtils.getSetEncoding(mt, headers, StandardCharsets.UTF_8.name());
-        obs.subscribe(s -> writeToOutputStream(s, os, enc));   
+        obs.subscribe(value -> writeToOutputStream(value, anns, mt, headers, os));   
     }
 
-    private static void writeToOutputStream(String s, OutputStream os, String enc) {
+    private void writeToOutputStream(T value,
+                                     Annotation[] anns,
+                                     MediaType mt,
+                                     MultivaluedMap<String, Object> headers, 
+                                     OutputStream os) {
+        @SuppressWarnings("unchecked")
+        MessageBodyWriter<T> writer = 
+            (MessageBodyWriter<T>)providers.getMessageBodyWriter(value.getClass(),
value.getClass(), anns, mt);
+        if (writer == null) {
+            throw ExceptionUtils.toInternalServerErrorException(null, null);
+        }
+    
         try {
-            os.write(s.getBytes(enc));
+            writer.writeTo(value, value.getClass(), value.getClass(), anns, mt, headers,
os);    
         } catch (IOException ex) {
             throw ExceptionUtils.toInternalServerErrorException(ex, null);
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/4ddf74b1/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java
index b4e3f54..aee7e1b 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveServer.java
@@ -19,6 +19,10 @@
 
 package org.apache.cxf.systest.jaxrs.reactive;
 
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
@@ -31,8 +35,12 @@ public class ReactiveServer extends AbstractBusTestServerBase {
     }
     
     protected void run() {
+        Bus bus = BusFactory.getDefaultBus();
+        // Make sure default JSONProvider is not loaded
+        bus.setProperty("skip.default.json.provider.registration", true);
         JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
-        sf.setProvider(new ObservableWriter());
+        sf.setProvider(new ObservableWriter<Object>());
+        sf.setProvider(new JacksonJsonProvider());
         sf.setResourceClasses(ReactiveService.class);
         sf.setResourceProvider(ReactiveService.class,
                                new SingletonResourceProvider(new ReactiveService(), true));

http://git-wip-us.apache.org/repos/asf/cxf/blob/4ddf74b1/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java
index 7127214..f716bbc 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/reactive/ReactiveService.java
@@ -49,6 +49,13 @@ public class ReactiveService {
         
     }
     
+    @GET
+    @Produces("application/json")
+    @Path("textJson")
+    public Observable<HelloWorldBean> getJsonText() {
+        return Observable.just(new HelloWorldBean());
+    }
+    
     private class AsyncResponseSubscriber extends Subscriber<String> {
         
         private StringBuilder sb = new StringBuilder();


Mime
View raw message