Improved: a series of cleanup mostly related to no more necessary handling of

default encoding for the requests and some fine tuning of ControlFilter 
settings; removed no more used code.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1761424 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacopo Cappellato
2016-09-19 12:03:13 +00:00
parent f94944b080
commit 2facce9042
6 changed files with 8 additions and 53 deletions
@@ -57,15 +57,10 @@ public class SimpleContentViewHandler extends AbstractViewHandler {
public static final String module = SimpleContentViewHandler.class.getName();
private String rootDir = null;
private String https = null;
private String defaultCharset = null;
public void init(ServletContext context) throws ViewHandlerException {
rootDir = context.getRealPath("/");
https = (String) context.getAttribute("https");
defaultCharset = context.getInitParameter("charset");
if (UtilValidate.isEmpty(defaultCharset)) {
defaultCharset = "UTF-8";
}
}
/**
* @see org.apache.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
@@ -131,10 +126,10 @@ public class SimpleContentViewHandler extends AbstractViewHandler {
// DEJ20080717: why are we rendering the DataResource directly instead of rendering the content?
ByteBuffer byteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, dataResourceId, https, webSiteId, locale, rootDir);
ByteArrayInputStream bais = new ByteArrayInputStream(byteBuffer.array());
// setup chararcter encoding and content type
// setup character encoding and content type
String charset = dataResource.getString("characterSetId");
if (UtilValidate.isEmpty(charset)) {
charset = defaultCharset;
charset = encoding;
}
if (UtilValidate.isEmpty(mimeTypeId)) {
mimeTypeId = DataResourceWorker.getMimeType(dataResource);
@@ -22,7 +22,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@@ -144,17 +143,6 @@ public final class WebAppUtil {
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;
@@ -22,7 +22,6 @@ import static org.apache.ofbiz.base.util.UtilGenerics.checkMap;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
@@ -41,7 +40,6 @@ import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.SSLUtil;
import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilCodec;
import org.apache.ofbiz.base.util.UtilFormatOut;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilHttp;
import org.apache.ofbiz.base.util.UtilMisc;
@@ -76,7 +74,6 @@ public class RequestHandler {
private final URL controllerConfigURL;
private final boolean trackServerHit;
private final boolean trackVisit;
private final String charset;
public static RequestHandler getRequestHandler(ServletContext servletContext) {
RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
@@ -101,7 +98,6 @@ public class RequestHandler {
this.trackServerHit = !"false".equalsIgnoreCase(context.getInitParameter("track-serverhit"));
this.trackVisit = !"false".equalsIgnoreCase(context.getInitParameter("track-visit"));
this.charset = context.getInitParameter("charset");
}
public ConfigXMLReader.ControllerConfig getControllerConfig() {
@@ -923,33 +919,21 @@ public class RequestHandler {
long viewStartTime = System.currentTimeMillis();
// setup character encoding and content type
String charset = UtilFormatOut.checkEmpty(this.charset, req.getCharacterEncoding(), "UTF-8");
// setup character encoding and content type of the response:
// encoding/charset: use the one set in the view or, if not set, the one set in the request
// content type: use the one set in the view or, if not set, use "text/html"
String charset = req.getCharacterEncoding();
String viewCharset = viewMap.encoding;
//NOTE: if the viewCharset is "none" then no charset will be used
if (UtilValidate.isNotEmpty(viewCharset)) {
charset = viewCharset;
}
if (!"none".equals(charset)) {
try {
req.setCharacterEncoding(charset);
} catch (UnsupportedEncodingException e) {
throw new RequestHandlerException("Could not set character encoding to " + charset, e);
} catch (IllegalStateException e) {
Debug.logInfo(e, "Could not set character encoding to " + charset + ", something has probably already committed the stream", module);
}
}
// setup content type
String contentType = "text/html";
String viewContentType = viewMap.contentType;
if (UtilValidate.isNotEmpty(viewContentType)) {
contentType = viewContentType;
}
if (charset.length() > 0 && !"none".equals(charset)) {
if (UtilValidate.isNotEmpty(charset) && !"none".equals(charset)) {
resp.setContentType(contentType + "; charset=" + charset);
} else {
resp.setContentType(contentType);
@@ -47,7 +47,6 @@ import org.apache.ofbiz.security.Security;
import org.apache.ofbiz.security.SecurityConfigurationException;
import org.apache.ofbiz.security.SecurityFactory;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.webapp.WebAppUtil;
import org.apache.ofbiz.webapp.control.RequestHandler;
import org.apache.ofbiz.webapp.stats.VisitHandler;
@@ -77,16 +76,8 @@ public class WebSiteFilter implements Filter {
Delegator delegator = (Delegator) httpRequest.getSession().getServletContext().getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
WebAppUtil.setCharacterEncoding(request);
WebAppUtil.setAttributesFromRequestBody(request);
// set the web context in the request for future use
dispatcher = WebAppUtil.makeWebappDispatcher(session.getServletContext(), delegator);
setWebContextObjects(httpRequest, httpResponse, delegator, dispatcher);
request.setAttribute("servletContext", httpRequest.getSession().getServletContext());
request.setAttribute("delegator", delegator);
GenericValue webSite = null;
try {
if (UtilValidate.isNotEmpty(webSiteAlias) && UtilValidate.isEmpty(webSite)) {
@@ -49,7 +49,7 @@ under the License.
</init-param>
<init-param>
<param-name>redirectPath</param-name>
<param-value>/control/main</param-value>
<param-value>/cms</param-value>
</init-param>
</filter>
<filter>
@@ -8,9 +8,6 @@
<description>Solr Configuration File</description>
<handler name="simplecontent" type="view" class="org.apache.ofbiz.content.view.SimpleContentViewHandler"/>
<handler name="jsp" type="view" class="org.apache.ofbiz.ecommerce.webapp.view.JspViewHandler"/>
<!-- Events to run on every request before security (chains exempt) -->
<preprocessor>
<event name="checkExternalLoginKey" type="java" path="org.apache.ofbiz.webapp.control.LoginWorker" invoke="checkExternalLoginKey" />