From 175c852eefbd1353991db6f691264af072ab058c Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 5 Sep 2016 12:14:12 +0200 Subject: [PATCH] WW-4684 Uses charset and adds some logging --- .../org/apache/struts2/json/JSONInterceptor.java | 12 +++++++++--- .../org/apache/struts2/json/JSONInterceptorTest.java | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) 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 d7836eb71..66be65f4f 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 @@ -187,19 +187,25 @@ public class JSONInterceptor extends AbstractInterceptor { protected String readContentType(HttpServletRequest request) { String contentType = request.getHeader("Content-Type"); + LOG.debug("Content Type from request: {}", contentType); + if (contentType != null && contentType.contains(";")) { - contentType = contentType.substring(0, contentType.indexOf(";")); + contentType = contentType.substring(0, contentType.indexOf(";")).trim(); } return contentType; } protected String readContentTypeEncoding(HttpServletRequest request) { String contentTypeEncoding = request.getHeader("Content-Type"); - if (contentTypeEncoding != null && contentTypeEncoding.contains(";encoding=")) { - contentTypeEncoding = contentTypeEncoding.substring(contentTypeEncoding.indexOf(";encoding=") + ";encoding=".length()); + LOG.debug("Content Type encoding from request: {}", contentTypeEncoding); + + if (contentTypeEncoding != null && contentTypeEncoding.contains(";charset=")) { + contentTypeEncoding = contentTypeEncoding.substring(contentTypeEncoding.indexOf(";charset=") + ";charset=".length()).trim(); } else { contentTypeEncoding = defaultEncoding; } + + LOG.debug("Content Type encoding to be used in de-serialisation: {}", contentTypeEncoding); return contentTypeEncoding; } 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 ac4c39bed..5ff929caa 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;encoding=UTF-8"); + this.request.addHeader("Content-Type", "application/json; charset=UTF-8"); JSONInterceptor interceptor = new JSONInterceptor(); interceptor.setEnableSMD(true);