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"
This commit is contained in:
victorsosa
2016-02-15 08:28:01 -04:00
parent a2c5bc835e
commit fab687a07e
2 changed files with 23 additions and 20 deletions
@@ -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();
@@ -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