From fb0f2d3d1df3af994a40b28d4e8e10a44d8567d8 Mon Sep 17 00:00:00 2001 From: Jacopo Cappellato Date: Thu, 9 Apr 2015 17:33:44 +0000 Subject: [PATCH] Fix for bug reported by Eric de Maulde in OFBIZ-5035: character encoding issue happening in the ecommerce application; the issue was caused by the CatalogUrlFilter and ContentUrlFilter because, under special conditions, they are preventing the ContextFilter (where encoding is set) to be executed. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1672430 13f79535-47bb-0310-9956-ffa450edef68 --- .../content/content/ContentUrlFilter.java | 6 ++- .../product/category/CatalogUrlFilter.java | 5 ++- .../ofbiz/webapp/control/ContextFilter.java | 45 +++++++++++++------ .../ofbiz/webapp/control/ControlServlet.java | 11 +---- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/applications/content/src/org/ofbiz/content/content/ContentUrlFilter.java b/applications/content/src/org/ofbiz/content/content/ContentUrlFilter.java index 4f2c779eba..71ebe51ca5 100644 --- a/applications/content/src/org/ofbiz/content/content/ContentUrlFilter.java +++ b/applications/content/src/org/ofbiz/content/content/ContentUrlFilter.java @@ -56,6 +56,9 @@ public class ContentUrlFilter extends ContextFilter { //Get ServletContext ServletContext servletContext = config.getServletContext(); + + ContextFilter.setCharacterEncoding(request); + //Set request attribute and session UrlServletHelper.setRequestAttributes(request, delegator, servletContext); String urlContentId = null; @@ -84,7 +87,8 @@ public class ContentUrlFilter extends ContextFilter { StringBuilder urlBuilder = new StringBuilder(); urlBuilder.append("/" + CONTROL_MOUNT_POINT); urlBuilder.append("/" + config.getInitParameter("viewRequest") + "?contentId=" + urlContentId); - + + ContextFilter.setAttributesFromRequestBody(request); //Set view query parameters UrlServletHelper.setViewQueryParameters(request, urlBuilder); Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module); diff --git a/applications/product/src/org/ofbiz/product/category/CatalogUrlFilter.java b/applications/product/src/org/ofbiz/product/category/CatalogUrlFilter.java index d046f4f365..ebfbf673ed 100644 --- a/applications/product/src/org/ofbiz/product/category/CatalogUrlFilter.java +++ b/applications/product/src/org/ofbiz/product/category/CatalogUrlFilter.java @@ -66,7 +66,9 @@ public class CatalogUrlFilter extends ContextFilter { //Get ServletContext ServletContext servletContext = config.getServletContext(); - + + ContextFilter.setCharacterEncoding(request); + //Set request attribute and session UrlServletHelper.setRequestAttributes(request, delegator, servletContext); @@ -323,6 +325,7 @@ public class CatalogUrlFilter extends ContextFilter { UrlServletHelper.setViewQueryParameters(request, urlBuilder); if (UtilValidate.isNotEmpty(productId) || UtilValidate.isNotEmpty(productCategoryId) || UtilValidate.isNotEmpty(urlContentId)) { Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module); + ContextFilter.setAttributesFromRequestBody(request); RequestDispatcher dispatch = request.getRequestDispatcher(urlBuilder.toString()); dispatch.forward(request, response); return; diff --git a/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java b/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java index cdb7164842..dafc9d6057 100644 --- a/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java +++ b/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java @@ -21,6 +21,7 @@ package org.ofbiz.webapp.control; import static org.ofbiz.base.util.UtilGenerics.checkMap; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.util.Enumeration; import java.util.List; import java.util.Map; @@ -317,22 +318,11 @@ public class ContextFilter implements Filter { } } - // read the body (for JSON requests) and set the parameters as attributes: - Map requestBodyMap = null; - try { - requestBodyMap = RequestBodyMapHandlerFactory.extractMapFromRequestBody(request); - } catch (IOException ioe) { - Debug.logWarning(ioe, module); - } - if (requestBodyMap != null) { - Set parameterNames = requestBodyMap.keySet(); - for (String parameterName: parameterNames) { - httpRequest.setAttribute(parameterName, requestBodyMap.get(parameterName)); - } - } + setCharacterEncoding(request); + setAttributesFromRequestBody(request); // we're done checking; continue on - chain.doFilter(httpRequest, httpResponse); + chain.doFilter(request, httpResponse); } /** @@ -353,6 +343,33 @@ public class ContextFilter implements Filter { return dispatcher; } + public static void setCharacterEncoding(ServletRequest request) throws UnsupportedEncodingException { + String charset = request.getServletContext().getInitParameter("charset"); + if (UtilValidate.isEmpty(charset)) charset = request.getCharacterEncoding(); + if (UtilValidate.isEmpty(charset)) charset = "UTF-8"; + if (Debug.verboseOn()) Debug.logVerbose("The character encoding of the request is: [" + request.getCharacterEncoding() + "]. The character encoding we will use for the request is: [" + charset + "]", module); + + if (!"none".equals(charset)) { + request.setCharacterEncoding(charset); + } + } + + public static void setAttributesFromRequestBody(ServletRequest request) { + // read the body (for JSON requests) and set the parameters as attributes: + Map requestBodyMap = null; + try { + requestBodyMap = RequestBodyMapHandlerFactory.extractMapFromRequestBody(request); + } catch (IOException ioe) { + Debug.logWarning(ioe, module); + } + if (requestBodyMap != null) { + Set parameterNames = requestBodyMap.keySet(); + for (String parameterName: parameterNames) { + request.setAttribute(parameterName, requestBodyMap.get(parameterName)); + } + } + } + /** This method only sets up a dispatcher for the current webapp and passed in delegator, it does not save it to the ServletContext or anywhere else, just returns it */ public static LocalDispatcher makeWebappDispatcher(ServletContext servletContext, Delegator delegator) { if (delegator == null) { diff --git a/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java b/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java index 1cece0a00f..9bea2ee0ca 100644 --- a/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java +++ b/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java @@ -98,18 +98,11 @@ public class ControlServlet extends HttpServlet { HttpSession session = request.getSession(); // setup DEFAULT character encoding and content type, this will be overridden in the RequestHandler for view rendering - String charset = getServletContext().getInitParameter("charset"); - if (UtilValidate.isEmpty(charset)) charset = request.getCharacterEncoding(); - if (UtilValidate.isEmpty(charset)) charset = "UTF-8"; - if (Debug.verboseOn()) Debug.logVerbose("The character encoding of the request is: [" + request.getCharacterEncoding() + "]. The character encoding we will use for the request and response is: [" + charset + "]", module); - - if (!"none".equals(charset)) { - request.setCharacterEncoding(charset); - } + String charset = request.getCharacterEncoding(); // setup content type String contentType = "text/html"; - if (charset.length() > 0 && !"none".equals(charset)) { + if (UtilValidate.isNotEmpty(charset) && !"none".equals(charset)) { response.setContentType(contentType + "; charset=" + charset); response.setCharacterEncoding(charset); } else {