struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [2/7] struts git commit: fix patch WW-4558
Date Sun, 22 May 2016 06:30:59 GMT
fix patch WW-4558

contentType override ignored for JSONInterceptor

use of accept parameter st by the interceptor params


Signed-off-by: victorsosa <victor.sosa@peopleware.do>

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

Branch: refs/heads/master
Commit: 0d4905038119f614f021af5738154079b30bca24
Parents: a2c5bc8
Author: victorsosa <victor.sosa@peopleware.do>
Authored: Mon Feb 15 08:28:01 2016 -0400
Committer: victorsosa <victor.sosa@peopleware.do>
Committed: Mon Feb 15 10:31:36 2016 -0400

----------------------------------------------------------------------
 .../apache/struts2/json/JSONInterceptor.java    | 27 ++++++++------
 .../struts2/json/JSONInterceptorTest.java       | 37 +++++++++++---------
 2 files changed, 36 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/0d490503/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
----------------------------------------------------------------------
diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
index 6731871..207d014 100644
--- a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
+++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java
@@ -58,7 +58,7 @@ public class JSONInterceptor extends AbstractInterceptor {
     private boolean enableGZIP = false;
     private boolean wrapWithComments;
     private boolean prefix;
-    private String defaultEncoding = "ISO-8859-1";
+    private String defaultEncoding = "UTF-8";
     private boolean ignoreHierarchy = true;
     private String root;
     private List<Pattern> excludeProperties;
@@ -70,17 +70,22 @@ public class JSONInterceptor extends AbstractInterceptor {
     private boolean noCache = false;
     private boolean excludeNullProperties;
     private String callbackParameter;
-    private String contentType;
+    private String accept;
 
     @SuppressWarnings("unchecked")
     public String intercept(ActionInvocation invocation) throws Exception {
         HttpServletRequest request = ServletActionContext.getRequest();
         HttpServletResponse response = ServletActionContext.getResponse();
-        String contentType = request.getHeader("content-type");
-        if (contentType != null) {
+        
+        //parameter wasn't set by the interceptor
+        if (accept == null) {
+            accept = request.getHeader("accept");
+        }
+        
+        if (accept != null) {
             int iSemicolonIdx;
-            if ((iSemicolonIdx = contentType.indexOf(";")) != -1)
-                contentType = contentType.substring(0, iSemicolonIdx);
+            if ((iSemicolonIdx = accept.indexOf(";")) != -1)
+                accept = accept.substring(0, iSemicolonIdx);
         }
 
         Object rootObject = null;
@@ -93,7 +98,7 @@ public class JSONInterceptor extends AbstractInterceptor {
             }
         }
 
-        if ((contentType != null) && contentType.equalsIgnoreCase("application/json"))
{
+        if ((accept != null) && accept.equalsIgnoreCase("application/json")) {
             // load JSON object
             Object obj = JSONUtil.deserialize(request.getReader());
 
@@ -133,7 +138,7 @@ public class JSONInterceptor extends AbstractInterceptor {
                 LOG.error("Unable to deserialize JSON object from request");
                 throw new JSONException("Unable to deserialize JSON object from request");
             }
-        } else if ((contentType != null) && contentType.equalsIgnoreCase("application/json-rpc"))
{
+        } else if ((accept != null) && accept.equalsIgnoreCase("application/json-rpc"))
{
             Object result;
             if (this.enableSMD) {
                 // load JSON object
@@ -181,7 +186,7 @@ public class JSONInterceptor extends AbstractInterceptor {
 
             return Action.NONE;
         } else {
-            LOG.debug("Content type must be 'application/json' or 'application/json-rpc'.
Ignoring request with content type ", contentType);
+            LOG.debug("Accept header parameter must be 'application/json' or 'application/json-rpc'.
Ignoring request with accept ", accept);
         }
 
         return invocation.invoke();
@@ -535,7 +540,7 @@ public class JSONInterceptor extends AbstractInterceptor {
         this.prefix = prefix;
     }
 
-    public void setContentType(String contentType) {
-        this.contentType = contentType;
+    public void setAccept(String accept) {
+        this.accept = accept;
     }
 }

http://git-wip-us.apache.org/repos/asf/struts/blob/0d490503/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
----------------------------------------------------------------------
diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
index 7bf53d3..ddae716 100644
--- a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
+++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
@@ -71,7 +71,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     private void tryBadJSON(String fileName) throws Exception {
         // request
         setRequestContent(fileName);
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -92,7 +92,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     public void testSMDDisabledSMD() throws Exception {
         // request
         setRequestContent("smd-3.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         SMDActionTest1 action = new SMDActionTest1();
@@ -111,7 +111,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     public void testSMDAliasedMethodCall1() throws Exception {
         // request
         setRequestContent("smd-14.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -129,7 +129,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     public void testSMDAliasedMethodCall2() throws Exception {
         // request
         setRequestContent("smd-15.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -147,7 +147,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     public void testSMDNoMethod() throws Exception {
         // request
         setRequestContent("smd-4.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -171,7 +171,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     public void testSMDMethodWithoutAnnotations() throws Exception {
         // request
         setRequestContent("smd-9.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -192,7 +192,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     public void testSMDPrimitivesNoResult() throws Exception {
         // request
         setRequestContent("smd-6.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -221,13 +221,13 @@ public class JSONInterceptorTest extends StrutsTestCase {
         String normalizedExpected = TestUtils.normalize(JSONResultTest.class.getResource("smd-11.txt"));
         assertEquals(normalizedExpected, normalizedActual);
 
-        assertEquals("application/json;charset=ISO-8859-1", response.getContentType());
+        assertEquals("application/json;charset=UTF-8", response.getContentType());
     }
 
     public void testSMDReturnObject() throws Exception {
         // request
         setRequestContent("smd-10.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -245,14 +245,14 @@ public class JSONInterceptorTest extends StrutsTestCase {
         String normalizedExpected = TestUtils.normalize(JSONResultTest.class.getResource("smd-12.txt"));
         assertEquals(normalizedExpected, normalizedActual);
 
-        assertEquals("application/json;charset=ISO-8859-1", response.getContentType());
+        assertEquals("application/json;charset=UTF-8", response.getContentType());
     }
 
     @SuppressWarnings("unchecked")
     public void testSMDObjectsNoResult() throws Exception {
         // request
         setRequestContent("smd-7.txt");
-        this.request.addHeader("content-type", "application/json-rpc");
+        this.request.addHeader("accept", "application/json-rpc");
 
         JSONInterceptor interceptor = new JSONInterceptor();
         interceptor.setEnableSMD(true);
@@ -293,14 +293,14 @@ public class JSONInterceptorTest extends StrutsTestCase {
         String normalizedExpected = TestUtils.normalize(JSONResultTest.class.getResource("smd-11.txt"));
         assertEquals(normalizedExpected, normalizedActual);
 
-        assertEquals("application/json;charset=ISO-8859-1", response.getContentType());
+        assertEquals("application/json;charset=UTF-8", response.getContentType());
     }
 
     @SuppressWarnings( { "unchecked", "unchecked" })
     public void testReadEmpty() throws Exception {
         // request
         setRequestContent("json-6.txt");
-        this.request.addHeader("content-type", "application/json");
+        this.request.addHeader("accept", "application/json");
 
         // interceptor
         JSONInterceptor interceptor = new JSONInterceptor();
@@ -315,7 +315,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     public void test() throws Exception {
         // request
         setRequestContent("json-1.txt");
-        this.request.addHeader("content-type", "application/json");
+        this.request.addHeader("accept", "application/json");
 
         // interceptor
         JSONInterceptor interceptor = new JSONInterceptor();
@@ -437,7 +437,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
 
     public void testRoot() throws Exception {
         setRequestContent("json-5.txt");
-        this.request.addHeader("content-type", "application/json");
+        this.request.addHeader("accept", "application/json");
 
         // interceptor
         JSONInterceptor interceptor = new JSONInterceptor();
@@ -462,7 +462,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
     
     public void testJSONArray() throws Exception {
         setRequestContent("json-12.txt");
-        this.request.addHeader("content-type", "application/json");
+        this.request.addHeader("accept", "application/json");
 
         // interceptor
         JSONInterceptor interceptor = new JSONInterceptor();
@@ -488,7 +488,7 @@ public class JSONInterceptorTest extends StrutsTestCase {
 
     public void testJSONArray2() throws Exception {
         setRequestContent("json-12.txt");
-        this.request.addHeader("content-type", "application/json");
+        this.request.addHeader("accept", "application/json");
 
         // interceptor
         JSONInterceptor interceptor = new JSONInterceptor();
@@ -536,6 +536,9 @@ public class JSONInterceptorTest extends StrutsTestCase {
 }
 
 class MockActionInvocationEx extends MockActionInvocation {
+
+    private static final long serialVersionUID = 3057703805130170757L;
+
     private boolean invoked;
 
     @Override


Mime
View raw message