cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: Making JsonpPreStreamIntercptor implement JAX-RS WriterInterceptor
Date Mon, 03 Mar 2014 13:17:50 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 396025738 -> 0b3a9ec32


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

Branch: refs/heads/2.7.x-fixes
Commit: 0b3a9ec3265ca77aaee1172679754ce929ecc3a0
Parents: 3960257
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:17:23 2014 +0000

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


http://git-wip-us.apache.org/repos/asf/cxf/blob/0b3a9ec3/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 4912c82..624be75 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
@@ -108,6 +108,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/0b3a9ec3/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/0b3a9ec3/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 41f7f50..c1611ed 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
@@ -382,6 +382,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/0b3a9ec3/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 52e3613..af06224 100644
--- a/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
@@ -166,6 +166,25 @@ http://cxf.apache.org/schemas/core.xsd">
     </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"/>
@@ -311,5 +330,6 @@ http://cxf.apache.org/schemas/core.xsd">
       <property name="produceMediaTypes" ref="jsonTypes"/>
       <property name="consumeMediaTypes" ref="jsonTypes"/>
   </bean>
+  
 </beans>
 <!-- END SNIPPET: beans -->


Mime
View raw message