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>
This commit is contained in:
victorsosa
2016-02-15 08:28:01 -04:00
parent a2c5bc835e
commit 0d49050381
2 changed files with 36 additions and 28 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;
@@ -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;
}
}
@@ -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