cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1552363 - in /cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src: main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
Date Thu, 19 Dec 2013 17:22:04 GMT
Author: sergeyb
Date: Thu Dec 19 17:22:04 2013
New Revision: 1552363

URL: http://svn.apache.org/r1552363
Log:
[CXF-5469] Removing HttpServletRequest cast from JsonpInInterceptor

Modified:
    cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
    cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java

Modified: cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java?rev=1552363&r1=1552362&r2=1552363&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInInterceptor.java
Thu Dec 19 17:22:04 2013
@@ -19,10 +19,9 @@
 
 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.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
@@ -63,8 +62,8 @@ public class JsonpInInterceptor extends 
     }
 
     protected String getCallbackValue(Message message) {
-        HttpServletRequest request = (HttpServletRequest) message.get("HTTP.REQUEST");
-        String callback = request.getParameter(callbackParam);
+        String theQuery = (String)message.get(Message.QUERY_STRING);
+        String callback = JAXRSUtils.getStructuredParams(theQuery, "&", false, false).getFirst(CALLBACK_KEY);
         if (StringUtils.isEmpty(callback)) {
             String httpAcceptType = (String)message.get(Message.ACCEPT_CONTENT_TYPE);
             if (httpAcceptType != null && mediaType.equals(httpAcceptType)) {

Modified: cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java?rev=1552363&r1=1552362&r2=1552363&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsonp/JsonpInterceptorTest.java
Thu Dec 19 17:22:04 2013
@@ -19,18 +19,14 @@
 
 package org.apache.cxf.jaxrs.provider.jsonp;
 
-import java.io.IOException;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
 
-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;
@@ -54,155 +50,49 @@ public class JsonpInterceptorTest extend
 
     @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.EasyMock
-                .createMock(ServletOutputStream.class);
-        out.write((byte[]) EasyMock.anyObject());
-        // the interceptors write both "myCallback(" and ")"
-        org.easymock.EasyMock.expectLastCall().times(2);
-        org.easymock.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);
-
+        Message message = new MessageImpl();
+        message.put(Message.CONTENT_TYPE, MediaType.APPLICATION_JSON);
+        message.setExchange(new ExchangeImpl());
+        message.put(Message.QUERY_STRING, JsonpInInterceptor.CALLBACK_KEY + "=" + "myCallback");
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, bos);
+        
         // Process the message
         in.handleMessage(message);
         preStream.handleMessage(message);
         postStream.handleMessage(message);
+        assertEquals("myCallback();", bos.toString());
 
-        // 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);
+        message.setExchange(new ExchangeImpl());
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, bos);
         
         // Process the message
         in.handleMessage(message);
         preStream.handleMessage(message);
         postStream.handleMessage(message);
-
-        assertEquals("callback();", out.getValue());
+        assertEquals("callback();", bos.toString());
     }
 
     @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.EasyMock
-                .createMock(ServletOutputStream.class);
-        // the interceptors write nothing, so we expect no behaviors from the
-        // mock
-        org.easymock.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);
-
+        Message message = new MessageImpl();
+        message.put(Message.CONTENT_TYPE, MediaType.APPLICATION_JSON);
+        message.setExchange(new ExchangeImpl());
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, bos);
+        
         // 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.EasyMock.verify(out);
+        assertEquals("", bos.toString());
     }
 
-    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