Repository: struts
Updated Branches:
  refs/heads/master 104bd92b8 -> 880c1d54a


fix patch WW-4558

contentType override ignored for JSONInterceptor

>From now on:

The "accept" request header parameter must be "application/json" or
"application/json-rpc"

Also the default encoding is "UTF-8"



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

Branch: refs/heads/master
Commit: fab687a07e8d873c82465b29188100debf61097f
Parents: a2c5bc8
Author: victorsosa <victor.s...@peopleware.do>
Authored: Mon Feb 15 08:28:01 2016 -0400
Committer: victorsosa <victor.s...@peopleware.do>
Committed: Mon Feb 15 08:28:01 2016 -0400

----------------------------------------------------------------------
 .../apache/struts2/json/JSONInterceptor.java    |  6 ++--
 .../struts2/json/JSONInterceptorTest.java       | 37 +++++++++++---------
 2 files changed, 23 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/fab687a0/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..11488fe 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;
@@ -76,7 +76,7 @@ public class JSONInterceptor extends AbstractInterceptor {
     public String intercept(ActionInvocation invocation) throws Exception {
         HttpServletRequest request = ServletActionContext.getRequest();
         HttpServletResponse response = ServletActionContext.getResponse();
-        String contentType = request.getHeader("content-type");
+        contentType = request.getHeader("accept");
         if (contentType != null) {
             int iSemicolonIdx;
             if ((iSemicolonIdx = contentType.indexOf(";")) != -1)
@@ -181,7 +181,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 ", contentType);
         }
 
         return invocation.invoke();

http://git-wip-us.apache.org/repos/asf/struts/blob/fab687a0/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

Reply via email to