cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject [1/2] git commit: [CXF-5589] Making JsonpPreStreamIntercptor implement JAX-RS WriterInterceptor
Date Mon, 03 Mar 2014 13:04:04 GMT
Repository: cxf
Updated Branches:
  refs/heads/master a02db39a1 -> 6738039a6


[CXF-5589] Making JsonpPreStreamIntercptor implement JAX-RS WriterInterceptor


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

Branch: refs/heads/master
Commit: aebae33d87cb107dbec5cd104367a85dd50ca806
Parents: c58518d
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Mon Mar 3 13:03:20 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Mon Mar 3 13:03:20 2014 +0000

----------------------------------------------------------------------
 .../jaxrs/impl/WriterInterceptorContextImpl.java  |  1 +
 .../provider/jsonp/JsonpPreStreamInterceptor.java | 18 +++++++++++++++++-
 .../jaxrs/JAXRSClientServerSpringBookTest.java    | 16 ++++++++++++++++
 .../src/test/resources/jaxrs/WEB-INF/beans.xml    | 17 +++++++++++++++++
 4 files changed, 51 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/aebae33d/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WriterInterceptorContextImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WriterInterceptorContextImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WriterInterceptorContextImpl.java
index 20f8217..0438e8e 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WriterInterceptorContextImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/WriterInterceptorContextImpl.java
@@ -110,6 +110,7 @@ public class WriterInterceptorContextImpl extends AbstractInterceptorContextImpl
         if (!getMediaType().isCompatible(mt)) {
             providerSelectionPropertyChanged();
         }
+        m.put(Message.CONTENT_TYPE, JAXRSUtils.mediaTypeToString(mt));
         getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, mt);
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/aebae33d/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
index 437469a..37731f8 100644
--- a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
+++ b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
@@ -19,20 +19,28 @@
 
 package org.apache.cxf.jaxrs.provider.jsonp;
 
+import java.io.IOException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.WriterInterceptor;
+import javax.ws.rs.ext.WriterInterceptorContext;
+
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.Phase;
+import org.apache.cxf.phase.PhaseInterceptorChain;
 
 /**
  * Appends the jsonp callback to json responses when the '_jsonp' parameter has been set
in the querystring.
  */
-public class JsonpPreStreamInterceptor extends AbstractJsonpOutInterceptor {
+public class JsonpPreStreamInterceptor extends AbstractJsonpOutInterceptor implements WriterInterceptor
{
 
     private String mediaType = JsonpInInterceptor.JSONP_TYPE;
     private String paddingEnd = "(";
@@ -74,4 +82,12 @@ public class JsonpPreStreamInterceptor extends AbstractJsonpOutInterceptor
{
     public String getPaddingEnd() {
         return paddingEnd;
     }
+
+    @Override
+    public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException
{
+        handleMessage(PhaseInterceptorChain.getCurrentMessage());
+        context.setMediaType(MediaType.APPLICATION_JSON_TYPE);
+        context.proceed();
+        context.setMediaType(JAXRSUtils.toMediaType(getMediaType()));
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/aebae33d/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
index c96a960..f794faa 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
@@ -383,6 +383,22 @@ public class JAXRSClientServerSpringBookTest extends AbstractBusClientServerTest
     }
     
     @Test
+    public void testGetBookJsonpJackson() throws Exception {
+        String url = "http://localhost:" + PORT + "/the/jsonp2/books/123";
+        WebClient client = WebClient.create(url);
+        WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(10000000);
+        client.accept("application/json, application/x-javascript");
+        client.query("_jsonp", "callback");
+        Response r = client.get();
+        assertEquals("application/x-javascript", r.getMetadata().getFirst("Content-Type"));
+        String response = IOUtils.readStringFromStream((InputStream)r.getEntity());
+        assertTrue(response.startsWith("callback({\"class\":\"org.apache.cxf.systest.jaxrs.Book\","));
+        assertTrue(response.endsWith("});"));
+        assertTrue(response.contains("\"id\":123"));
+        assertTrue(response.contains("\"name\":\"CXF in Action\""));
+    }
+    
+    @Test
     public void testGetBookWithoutJsonpCallback() throws Exception {
         String url = "http://localhost:" + PORT + "/the/jsonp/books/123";
         WebClient client = WebClient.create(url);

http://git-wip-us.apache.org/repos/asf/cxf/blob/aebae33d/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml b/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
index 75aa86f..4b095c1 100644
--- a/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
@@ -132,6 +132,23 @@
             <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
         </jaxrs:outInterceptors>
     </jaxrs:server>
+    <jaxrs:server id="bookJsonp2" address="/jsonp2">
+        <jaxrs:serviceBeans>
+            <ref bean="serviceBean"/>
+        </jaxrs:serviceBeans>
+        <jaxrs:providers>
+            <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
+            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>
+        </jaxrs:providers>
+        <jaxrs:inInterceptors>
+            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor">
+                <property name="acceptType" value="application/json"/>
+            </bean>
+        </jaxrs:inInterceptors>
+        <jaxrs:outInterceptors>
+            <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
+        </jaxrs:outInterceptors>
+    </jaxrs:server>
     <util:map id="outTemplates">
         <entry key="application/xhtml+xml" value="classpath:/WEB-INF/templates/template.xsl"/>
         <entry key="application/xml" value="classpath:/org/apache/cxf/systest/jaxrs/resources/template2.xsl"/>


Mime
View raw message