cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1243926 - in /cxf/trunk/rt/rs/extensions/providers/src: main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/ test/java/org/apache/cxf/jaxrs/ext/provider/jsonp/
Date Tue, 14 Feb 2012 14:10:18 GMT
Author: sergeyb
Date: Tue Feb 14 14:10:18 2012
New Revision: 1243926

URL: http://svn.apache.org/viewvc?rev=1243926&view=rev
Log:
[CXF-4107] Defaulting to 'callback' padding if no custom value is provided

Modified:
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInInterceptor.java
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpPreStreamInterceptor.java
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInterceptorTest.java

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInInterceptor.java?rev=1243926&r1=1243925&r2=1243926&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInInterceptor.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInInterceptor.java
Tue Feb 14 14:10:18 2012
@@ -32,11 +32,15 @@ import org.apache.cxf.phase.Phase;
  * querystring.
  */
 public class JsonpInInterceptor extends AbstractPhaseInterceptor<Message> {
+    public static final String JSONP_TYPE = "application/x-javascript";
     public static final String CALLBACK_PARAM = "_jsonp";
     public static final String CALLBACK_KEY = "JSONP.CALLBACK";
+    public static final String DEFAULT_CALLBACK_VALUE = "callback";
 
     private String callbackParam = CALLBACK_PARAM;
+    private String defaultCallback = DEFAULT_CALLBACK_VALUE;
     private String acceptType;
+    private String mediaType = JSONP_TYPE;
     
     public JsonpInInterceptor() {
         this(Phase.UNMARSHAL);
@@ -60,7 +64,14 @@ public class JsonpInInterceptor extends 
 
     protected String getCallbackValue(Message message) {
         HttpServletRequest request = (HttpServletRequest) message.get("HTTP.REQUEST");
-        return request.getParameter(callbackParam);
+        String callback = request.getParameter(callbackParam);
+        if (StringUtils.isEmpty(callback)) {
+            String httpAcceptType = (String)message.get(Message.ACCEPT_CONTENT_TYPE);
+            if (httpAcceptType != null && mediaType.equals(httpAcceptType)) {
+                return defaultCallback;
+            }
+        }
+        return callback;
     }
     
     public void setCallbackParam(String callbackParam) {
@@ -78,4 +89,20 @@ public class JsonpInInterceptor extends 
     public String getAcceptType() {
         return acceptType;
     }
+
+    public void setMediaType(String mediaType) {
+        this.mediaType = mediaType;
+    }
+
+    public String getMediaType() {
+        return mediaType;
+    }
+
+    public void setDefaultCallback(String defaultCallback) {
+        this.defaultCallback = defaultCallback;
+    }
+
+    public String getDefaultCallback() {
+        return defaultCallback;
+    }
 }

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpPreStreamInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpPreStreamInterceptor.java?rev=1243926&r1=1243925&r2=1243926&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpPreStreamInterceptor.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpPreStreamInterceptor.java
Tue Feb 14 14:10:18 2012
@@ -34,8 +34,7 @@ import org.apache.cxf.phase.Phase;
  */
 public class JsonpPreStreamInterceptor extends AbstractJsonpOutInterceptor {
 
-    private static final String JSONP_TYPE = "application/x+javascript";
-    private String mediaType = JSONP_TYPE;
+    private String mediaType = JsonpInInterceptor.JSONP_TYPE;
     private String paddingEnd = "(";
     
     public JsonpPreStreamInterceptor() {

Modified: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInterceptorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInterceptorTest.java?rev=1243926&r1=1243925&r2=1243926&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInterceptorTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/ext/provider/jsonp/JsonpInterceptorTest.java
Tue Feb 14 14:10:18 2012
@@ -19,19 +19,24 @@
 
 package org.apache.cxf.jaxrs.ext.provider.jsonp;
 
+import java.io.IOException;
+
 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.ExchangeImpl;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
 import org.easymock.EasyMock;
 
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
-public class JsonpInterceptorTest {
+public class JsonpInterceptorTest extends Assert {
 
     public static final String JSON = "{}";
 
@@ -100,6 +105,43 @@ public class JsonpInterceptorTest {
         // Verify that the mock response stream was written to as expected
         org.easymock.EasyMock.verify(out);
     }
+    
+    @Test
+    public void testJsonWithDefaultPadding() throws Exception {
+        // Mock up an output stream as a strict mock. We want to verify that its
+        // being written to correctly.
+        final TestServletOutputStream out = new TestServletOutputStream();
+
+        // 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).times(2);
+        EasyMock.replay(response);
+
+        // Mock up a message
+        Message message = new MessageImpl();
+        message.put("HTTP.REQUEST", request);
+        message.put("HTTP.RESPONSE", response);
+        message.put(Message.ACCEPT_CONTENT_TYPE, JsonpInInterceptor.JSONP_TYPE);
+
+        Exchange exchange = new ExchangeImpl();
+        message.setExchange(exchange);
+        
+        // Process the message
+        in.handleMessage(message);
+        preStream.handleMessage(message);
+        postStream.handleMessage(message);
+
+        assertEquals("callback();", out.getValue());
+    }
 
     @Test
     public void testJsonWithoutPadding() throws Exception {
@@ -151,5 +193,16 @@ public class JsonpInterceptorTest {
         org.easymock.EasyMock.verify(out);
     }
 
-    
+    private static class TestServletOutputStream extends ServletOutputStream {
+        private StringBuilder sb = new StringBuilder();
+        
+        public void write(byte[] data) throws IOException {
+            sb.append(new String(data, "UTF-8"));
+        }
+        public String getValue() {
+            return sb.toString();
+        }
+        public void write(int b) throws IOException {
+        }
+    };
 }



Mime
View raw message