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
This commit is contained in:
Jacopo Cappellato
2015-04-09 17:33:44 +00:00
parent a0c043a64d
commit fb0f2d3d1d
4 changed files with 42 additions and 25 deletions
@@ -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);
@@ -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;
@@ -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<String, Object> requestBodyMap = null;
try {
requestBodyMap = RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
} catch (IOException ioe) {
Debug.logWarning(ioe, module);
}
if (requestBodyMap != null) {
Set<String> 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<String, Object> requestBodyMap = null;
try {
requestBodyMap = RequestBodyMapHandlerFactory.extractMapFromRequestBody(request);
} catch (IOException ioe) {
Debug.logWarning(ioe, module);
}
if (requestBodyMap != null) {
Set<String> 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) {
@@ -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 {