cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1090701 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ systests/jaxrs/sr...
Date Sat, 09 Apr 2011 22:59:41 GMT
Author: sergeyb
Date: Sat Apr  9 22:59:40 2011
New Revision: 1090701

URL: http://svn.apache.org/viewvc?rev=1090701&view=rev
Log:
[CXF-3005] Adding simple JSONP interceptors, patch submitted by Josh Holtzman

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/AbstractJsonpOutInterceptor.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPostStreamInterceptor.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
  (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
  (with props)
Modified:
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/AbstractJsonpOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/AbstractJsonpOutInterceptor.java?rev=1090701&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/AbstractJsonpOutInterceptor.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/AbstractJsonpOutInterceptor.java
Sat Apr  9 22:59:40 2011
@@ -0,0 +1,51 @@
+/**
+ * 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.jsonp;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+
+public abstract class AbstractJsonpOutInterceptor extends AbstractPhaseInterceptor<Message>
{
+    
+    protected AbstractJsonpOutInterceptor(String phase) {
+        super(phase);
+    }
+
+    protected String getCallbackValue(Message message) {
+        Exchange exchange = message.getExchange();
+        return (String) exchange.get(JsonpInInterceptor.CALLBACK_KEY);
+    }
+    
+    protected void writeValue(Message message, String value) throws Fault {
+        HttpServletResponse response = (HttpServletResponse) message.get("HTTP.RESPONSE");
+        try {
+            response.getOutputStream().write(value.getBytes("UTF-8"));
+        } catch (IOException e) {
+            throw new Fault(e);
+        }
+    }
+
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/AbstractJsonpOutInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/AbstractJsonpOutInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java?rev=1090701&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
Sat Apr  9 22:59:40 2011
@@ -0,0 +1,81 @@
+/**
+ * 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.jsonp;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+
+/**
+ * Sets a callback key in the message exchange for HTTP requests containing the '_jsonp'
parameter in the
+ * querystring.
+ */
+public class JsonpInInterceptor extends AbstractPhaseInterceptor<Message> {
+    public static final String CALLBACK_PARAM = "_jsonp";
+    public static final String CALLBACK_KEY = "JSONP.CALLBACK";
+
+    private String callbackParam = CALLBACK_PARAM;
+    private String acceptType;
+    
+    public JsonpInInterceptor() {
+        this(Phase.UNMARSHAL);
+    }
+    
+    public JsonpInInterceptor(String phase) {
+        super(phase);
+    }
+
+    public void handleMessage(Message message) throws Fault {
+        String callbackValue = getCallbackValue(message);
+        if (!StringUtils.isEmpty(callbackValue)) {
+            if (getAcceptType() != null) {
+                // may be needed to enforce the selection of 
+                // JSON-awarenprovider
+                message.put(Message.CONTENT_TYPE, getAcceptType());
+            }
+            message.getExchange().put(CALLBACK_KEY, callbackValue);
+        }
+    }
+
+    protected String getCallbackValue(Message message) {
+        HttpServletRequest request = (HttpServletRequest) message.get("HTTP.REQUEST");
+        return request.getParameter(callbackParam);
+    }
+    
+    public void setCallbackParam(String callbackParam) {
+        this.callbackParam = callbackParam;
+    }
+
+    public String getCallbackParam() {
+        return callbackParam;
+    }
+
+    public void setAcceptType(String acceptType) {
+        this.acceptType = acceptType;
+    }
+
+    public String getAcceptType() {
+        return acceptType;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPostStreamInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPostStreamInterceptor.java?rev=1090701&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPostStreamInterceptor.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPostStreamInterceptor.java
Sat Apr  9 22:59:40 2011
@@ -0,0 +1,53 @@
+/**
+ * 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.jsonp;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+
+/**
+ * Appends the jsonp callback to json responses when the '_jsonp' parameter has been set
in the querystring.
+ */
+public class JsonpPostStreamInterceptor extends AbstractJsonpOutInterceptor {
+
+    private String paddingEnd = ");";
+    
+    public JsonpPostStreamInterceptor() {
+        super(Phase.POST_STREAM);
+    }
+
+    public void handleMessage(Message message) throws Fault {
+        if (!StringUtils.isEmpty(getCallbackValue(message))) {
+            writeValue(message, getPaddingEnd());
+        }
+    }
+
+    public void setPaddingEnd(String paddingEnd) {
+        this.paddingEnd = paddingEnd;
+    }
+
+    public String getPaddingEnd() {
+        return paddingEnd;
+    }
+
+    
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPostStreamInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPostStreamInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java?rev=1090701&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
Sat Apr  9 22:59:40 2011
@@ -0,0 +1,78 @@
+/**
+ * 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.jsonp;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+
+/**
+ * Appends the jsonp callback to json responses when the '_jsonp' parameter has been set
in the querystring.
+ */
+public class JsonpPreStreamInterceptor extends AbstractJsonpOutInterceptor {
+
+    private static final String JSONP_TYPE = "application/x+javascript";
+    private String mediaType = JSONP_TYPE;
+    private String paddingEnd = "(";
+    
+    public JsonpPreStreamInterceptor() {
+        super(Phase.PRE_STREAM);
+    }
+  
+    public void handleMessage(Message message) throws Fault {
+        String callback = getCallbackValue(message);
+        if (!StringUtils.isEmpty(callback)) {
+            setContentType(message);
+            writeValue(message, callback + getPaddingEnd());
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    protected void setContentType(Message message) {
+        Map<String, List<String>> headers = (Map<String, List<String>>)message.get(Message.PROTOCOL_HEADERS);
+        if (headers == null) {
+            headers = new HashMap<String, List<String>>();
+            message.put(Message.PROTOCOL_HEADERS, headers);
+        }
+        headers.put(Message.CONTENT_TYPE, Collections.singletonList(getMediaType()));   
        
+    }
+    
+    public void setMediaType(String mediaType) {
+        this.mediaType = mediaType;
+    }
+
+    public String getMediaType() {
+        return mediaType;
+    }
+
+    public void setPaddingEnd(String paddingEnd) {
+        this.paddingEnd = paddingEnd;
+    }
+
+    public String getPaddingEnd() {
+        return paddingEnd;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpPreStreamInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java?rev=1090701&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
Sat Apr  9 22:59:40 2011
@@ -0,0 +1,155 @@
+/**
+ * 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.jsonp;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
+import org.easymock.EasyMock;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class JsonpInterceptorTest {
+
+    public static final String JSON = "{}";
+
+    JsonpInInterceptor in;
+    JsonpPreStreamInterceptor preStream;
+    JsonpPostStreamInterceptor postStream;
+
+    @Before
+    public void setUp() throws Exception {
+        // Create the interceptors
+        in = new JsonpInInterceptor();
+        preStream = new JsonpPreStreamInterceptor();
+        postStream = new JsonpPostStreamInterceptor();
+    }
+
+    @Test
+    public void testJsonWithPadding() throws Exception {
+        // The callback value included in the request
+        String callback = "myCallback";
+
+        // Mock up an output stream as a strict mock. We want to verify that its
+        // being written to correctly.
+        ServletOutputStream out = org.easymock.classextension.EasyMock
+                .createMock(ServletOutputStream.class);
+        out.write((byte[]) EasyMock.anyObject());
+        // the interceptors write both "myCallback(" and ")"
+        org.easymock.classextension.EasyMock.expectLastCall().times(2);
+        org.easymock.classextension.EasyMock.replay(out);
+
+        // Mock up an HTTP request
+        HttpServletRequest request = EasyMock
+                .createNiceMock(HttpServletRequest.class);
+        EasyMock.expect(
+                request.getParameter(JsonpInInterceptor.CALLBACK_PARAM))
+                .andReturn(callback);
+        EasyMock.replay(request);
+
+        // Mock up an HTTP response
+        HttpServletResponse response = EasyMock
+                .createNiceMock(HttpServletResponse.class);
+        EasyMock.expect(response.getOutputStream()).andReturn(out).anyTimes();
+        EasyMock.replay(response);
+
+        // Mock up an exchange
+        Exchange exchange = EasyMock.createNiceMock(Exchange.class);
+        EasyMock.expect(exchange.get(JsonpInInterceptor.CALLBACK_KEY))
+                .andReturn(callback).anyTimes();
+        EasyMock.replay(exchange);
+
+        // Mock up a message
+        Message message = EasyMock.createNiceMock(Message.class);
+        EasyMock.expect(message.get("HTTP.REQUEST")).andReturn(request)
+                .anyTimes();
+        EasyMock.expect(message.get("HTTP.RESPONSE")).andReturn(response)
+                .anyTimes();
+        EasyMock.expect(message.get(Message.CONTENT_TYPE)).andReturn(
+                MediaType.APPLICATION_JSON).anyTimes();
+        EasyMock.expect(message.getExchange()).andReturn(exchange).anyTimes();
+        EasyMock.replay(message);
+
+        // Process the message
+        in.handleMessage(message);
+        preStream.handleMessage(message);
+        postStream.handleMessage(message);
+
+        // Verify that the mock response stream was written to as expected
+        org.easymock.classextension.EasyMock.verify(out);
+    }
+
+    @Test
+    public void testJsonWithoutPadding() throws Exception {
+        // Mock up an output stream as a strict mock. We want to verify that its
+        // being written to correctly.
+        ServletOutputStream out = org.easymock.classextension.EasyMock
+                .createMock(ServletOutputStream.class);
+        // the interceptors write nothing, so we expect no behaviors from the
+        // mock
+        org.easymock.classextension.EasyMock.replay(out);
+
+        // Mock up an HTTP request
+        HttpServletRequest request = EasyMock
+                .createNiceMock(HttpServletRequest.class);
+        EasyMock.expect(
+                request.getParameter(JsonpInInterceptor.CALLBACK_PARAM))
+                .andReturn(null);
+        EasyMock.replay(request);
+
+        // Mock up an HTTP response
+        HttpServletResponse response = EasyMock
+                .createNiceMock(HttpServletResponse.class);
+        EasyMock.expect(response.getOutputStream()).andReturn(out).anyTimes();
+        EasyMock.replay(response);
+
+        // Mock up an exchange
+        Exchange exchange = EasyMock.createNiceMock(Exchange.class);
+        EasyMock.expect(exchange.get(JsonpInInterceptor.CALLBACK_KEY))
+                .andReturn(null).anyTimes();
+        EasyMock.replay(exchange);
+
+        // Mock up a message
+        Message message = EasyMock.createNiceMock(Message.class);
+        EasyMock.expect(message.get("HTTP.REQUEST")).andReturn(request)
+                .anyTimes();
+        EasyMock.expect(message.get("HTTP.RESPONSE")).andReturn(response)
+                .anyTimes();
+        EasyMock.expect(message.get(Message.CONTENT_TYPE)).andReturn(
+                MediaType.APPLICATION_JSON).anyTimes();
+        EasyMock.expect(message.getExchange()).andReturn(exchange).anyTimes();
+        EasyMock.replay(message);
+
+        // Process the message
+        in.handleMessage(message);
+        preStream.handleMessage(message);
+        postStream.handleMessage(message);
+
+        // Verify that the mock response stream was written to as expected
+        org.easymock.classextension.EasyMock.verify(out);
+    }
+
+    
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java?rev=1090701&r1=1090700&r2=1090701&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
Sat Apr  9 22:59:40 2011
@@ -30,6 +30,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.ws.rs.core.Response;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
@@ -130,6 +131,32 @@ public class JAXRSClientServerSpringBook
                 "application/jettison");
     }
     
+    
+    @Test
+    public void testGetBookJsonp() throws Exception {
+        String url = "http://localhost:" + PORT + "/the/jsonp/books/123";
+        WebClient client = WebClient.create(url);
+        client.accept("application/json, application/x-javascript");
+        client.query("_jsonp", "callback");
+        WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        Response r = client.get();
+        assertEquals("application/x+javascript", r.getMetadata().getFirst("Content-Type"));
+        assertEquals("callback({\"Book\":{\"id\":123,\"name\":\"CXF in Action\"}});",
+                     IOUtils.readStringFromStream((InputStream)r.getEntity()));
+    }
+    
+    @Test
+    public void testGetBookWithoutJsonpCallback() throws Exception {
+        String url = "http://localhost:" + PORT + "/the/jsonp/books/123";
+        WebClient client = WebClient.create(url);
+        client.accept("application/json, application/x-javascript");
+        WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        Response r = client.get();
+        assertEquals("application/json", r.getMetadata().getFirst("Content-Type"));
+        assertEquals("{\"Book\":{\"id\":123,\"name\":\"CXF in Action\"}}",
+                     IOUtils.readStringFromStream((InputStream)r.getEntity()));
+    }
+    
     @Test
     public void testGetBookAsArray() throws Exception {
         URL url = new URL("http://localhost:" + PORT + "/the/bookstore/books/list/123");

Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?rev=1090701&r1=1090700&r2=1090701&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml (original)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml Sat Apr  9 22:59:40
2011
@@ -124,6 +124,20 @@ http://cxf.apache.org/schemas/core.xsd">
        <bean class="org.apache.cxf.systest.jaxrs.XMLStreamWriterOutInterceptor"/>
     </jaxrs:outInterceptors>
     
+  </jaxrs:server>
+  
+  <jaxrs:server id="bookJsonp" address="/jsonp">
+    <jaxrs:serviceBeans>
+      <ref bean="serviceBean" />
+    </jaxrs:serviceBeans>		  
+
+    <jaxrs:inInterceptors>
+       <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
+    </jaxrs:inInterceptors>
+    <jaxrs:outInterceptors>
+       <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>
+       <bean class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
+    </jaxrs:outInterceptors> 
   </jaxrs:server> 
   
   <util:map id="outTemplates">



Mime
View raw message