diff --git a/framework/base/build.xml b/framework/base/build.xml index d4f48fb6d9..767e7f06ee 100644 --- a/framework/base/build.xml +++ b/framework/base/build.xml @@ -71,12 +71,12 @@ under the License. - + - + @@ -91,7 +91,7 @@ under the License. - + diff --git a/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java b/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java index 198b66a337..33ffb6da41 100644 --- a/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java +++ b/framework/base/src/base/org/ofbiz/base/util/UtilHttp.java @@ -44,12 +44,14 @@ import java.util.Map; import java.util.Set; import java.util.StringTokenizer; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javolution.util.FastList; import javolution.util.FastMap; +import javolution.util.FastSet; /** * HttpUtil - Misc TTP Utility Functions @@ -65,18 +67,51 @@ public class UtilHttp { public static final int ROW_SUBMIT_PREFIX_LENGTH = ROW_SUBMIT_PREFIX.length(); public static final int COMPOSITE_DELIMITER_LENGTH = COMPOSITE_DELIMITER.length(); + /** + * Create a combined map from servlet context, session, attributes and parameters + * @return The resulting Map + */ + public static Map getCombinedMap(HttpServletRequest request) { + return getCombinedMap(request, null); + } /** - * Create a map from an HttpServletRequest object + * Create a combined map from servlet context, session, attributes and parameters + * -- this method will only use the skip names for session and servlet context attributes + * @return The resulting Map + */ + public static Map getCombinedMap(HttpServletRequest request, Set namesToSkip) { + FastMap combinedMap = FastMap.newInstance(); + combinedMap.putAll(getServletContextMap(request, namesToSkip)); // bottom level application attributes + combinedMap.putAll(getSessionMap(request, namesToSkip)); // session overrides application + combinedMap.putAll(getParameterMap(request)); // parameters override session + combinedMap.putAll(getAttributeMap(request)); // attributes trump them all + + return combinedMap; + } + + /** + * Create a map from a HttpServletRequest (parameters) object * @return The resulting Map */ public static Map getParameterMap(HttpServletRequest request) { + return getParameterMap(request, null); + } + + /** + * Create a map from a HttpServletRequest (parameters) object + * @return The resulting Map + */ + public static Map getParameterMap(HttpServletRequest request, Set namesToSkip) { Map paramMap = FastMap.newInstance(); // add all the actual HTTP request parameters Enumeration e = request.getParameterNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); + if (namesToSkip != null && namesToSkip.contains(name)) + continue; + Object value = null; String[] paramArr = request.getParameterValues(name); if (paramArr != null) { @@ -133,20 +168,119 @@ public class UtilHttp { paramMap.putAll(multiPartMap); } } - - //Debug.logInfo("Made parameterMap: \n" + UtilMisc.printMap(paramMap), module); + if (Debug.verboseOn()) { Debug.logVerbose("Made Request Parameter Map with [" + paramMap.size() + "] Entries", module); - Iterator entryIter = paramMap.entrySet().iterator(); - while (entryIter.hasNext()) { - Map.Entry entry = (Map.Entry) entryIter.next(); - Debug.logVerbose("Request Parameter Map Entry: [" + entry.getKey() + "] --> " + entry.getValue(), module); - } + Debug.logVerbose("Request Parameter Map Entries: " + System.getProperty("line.separator") + UtilMisc.printMap(paramMap), module); } return paramMap; } + /** + * Create a map from a HttpRequest (attributes) object + * @return The resulting Map + */ + public static Map getAttributeMap(HttpServletRequest request) { + return getAttributeMap(request, null); + } + + /** + * Create a map from a HttpRequest (attributes) object + * @return The resulting Map + */ + public static Map getAttributeMap(HttpServletRequest request, Set namesToSkip) { + Map attributeMap = FastMap.newInstance(); + + // look at all request attributes + Enumeration requestAttrNames = request.getAttributeNames(); + while (requestAttrNames.hasMoreElements()) { + String attrName = (String) requestAttrNames.nextElement(); + if (namesToSkip != null && namesToSkip.contains(attrName)) + continue; + + Object attrValue = request.getAttribute(attrName); + attributeMap.put(attrName, attrValue); + } + + if (Debug.verboseOn()) { + Debug.logVerbose("Made Request Attribute Map with [" + attributeMap.size() + "] Entries", module); + Debug.logVerbose("Request Attribute Map Entries: " + System.getProperty("line.separator") + UtilMisc.printMap(attributeMap), module); + } + + return attributeMap; + } + + /** + * Create a map from a HttpSession object + * @return The resulting Map + */ + public static Map getSessionMap(HttpServletRequest request) { + return getSessionMap(request, null); + } + + /** + * Create a map from a HttpSession object + * @return The resulting Map + */ + public static Map getSessionMap(HttpServletRequest request, Set namesToSkip) { + Map sessionMap = FastMap.newInstance(); + HttpSession session = request.getSession(); + + // look at all the session attributes + Enumeration sessionAttrNames = session.getAttributeNames(); + while (sessionAttrNames.hasMoreElements()) { + String attrName = (String) sessionAttrNames.nextElement(); + if (namesToSkip != null && namesToSkip.contains(attrName)) + continue; + + Object attrValue = session.getAttribute(attrName); + sessionMap.put(attrName, attrValue); + } + + if (Debug.verboseOn()) { + Debug.logVerbose("Made Session Attribute Map with [" + sessionMap.size() + "] Entries", module); + Debug.logVerbose("Session Attribute Map Entries: " + System.getProperty("line.separator") + UtilMisc.printMap(sessionMap), module); + } + + return sessionMap; + } + + /** + * Create a map from a ServletContext object + * @return The resulting Map + */ + public static Map getServletContextMap(HttpServletRequest request) { + return getServletContextMap(request, null); + } + + /** + * Create a map from a ServletContext object + * @return The resulting Map + */ + public static Map getServletContextMap(HttpServletRequest request, Set namesToSkip) { + Map servletCtxMap = FastMap.newInstance(); + + // look at all servlet context attributes + ServletContext servletContext = (ServletContext) request.getAttribute("servletContext"); + Enumeration applicationAttrNames = servletContext.getAttributeNames(); + while (applicationAttrNames.hasMoreElements()) { + String attrName = (String) applicationAttrNames.nextElement(); + if (namesToSkip != null && namesToSkip.contains(attrName)) + continue; + + Object attrValue = servletContext.getAttribute(attrName); + servletCtxMap.put(attrName, attrValue); + } + + if (Debug.verboseOn()) { + Debug.logVerbose("Made ServletContext Attribute Map with [" + servletCtxMap.size() + "] Entries", module); + Debug.logVerbose("ServletContext Attribute Map Entries: " + System.getProperty("line.separator") + UtilMisc.printMap(servletCtxMap), module); + } + + return servletCtxMap; + } + public static Map makeParamMapWithPrefix(HttpServletRequest request, String prefix, String suffix) { return makeParamMapWithPrefix(request, null, prefix, suffix); } @@ -906,4 +1040,5 @@ public class UtilHttp { HttpSession session = request.getSession(); return (session == null ? "unknown" : session.getId()); } + } diff --git a/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java b/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java index bc5975b4fb..b58e1eb77e 100644 --- a/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java +++ b/framework/widget/src/org/ofbiz/widget/screen/ScreenRenderer.java @@ -20,7 +20,6 @@ package org.ofbiz.widget.screen; import java.io.IOException; import java.io.Writer; -import java.util.Enumeration; import java.util.LinkedList; import java.util.List; import java.util.Locale; @@ -36,7 +35,6 @@ import javax.xml.parsers.ParserConfigurationException; import javolution.util.FastMap; import javolution.util.FastSet; -import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilFormatOut; @@ -158,69 +156,7 @@ public class ScreenRenderer { attrNamesToSkip.add("security"); attrNamesToSkip.add("webSiteId"); - Map parameterMap = UtilHttp.getParameterMap(request); - // go through all request attributes and for each name that is not already in the parameters Map add the attribute value - Enumeration requestAttrNames = request.getAttributeNames(); - while (requestAttrNames.hasMoreElements()) { - String attrName = (String) requestAttrNames.nextElement(); - Object attrValue = request.getAttribute(attrName); - - // NOTE: this is being set to false by default now - //this change came after the realization that it is common to want a request attribute to override a request parameter, but I can't think of even ONE reason why a request parameter should override a request attribute... - final boolean preserveRequestParameters = false; - if (preserveRequestParameters) { - Object param = parameterMap.get(attrName); - if (param == null) { - parameterMap.put(attrName, attrValue); - } else if (param instanceof String && ((String) param).length() == 0) { - // also put the attribute value in if the parameter is empty - parameterMap.put(attrName, attrValue); - } else { - // do nothing, just log something - Debug.logInfo("Found request attribute that conflicts with parameter name, leaving request parameter in place for name: " + attrName, module); - } - } else { - parameterMap.put(attrName, attrValue); - } - } - - // do the same for session attributes, for convenience - Enumeration sessionAttrNames = session.getAttributeNames(); - while (sessionAttrNames.hasMoreElements()) { - String attrName = (String) sessionAttrNames.nextElement(); - if (attrNamesToSkip.contains(attrName)) continue; - Object attrValue = session.getAttribute(attrName); - Object param = parameterMap.get(attrName); - if (param == null) { - parameterMap.put(attrName, attrValue); - } else if (param instanceof String && ((String) param).length() == 0) { - // also put the attribute value in if the parameter is empty - parameterMap.put(attrName, attrValue); - } else { - // do nothing, just log something - Debug.logInfo("Found session attribute that conflicts with parameter name, leaving request parameter in place for name: " + attrName, module); - } - } - - // do the same for servlet context (application) attribute, for convenience - Enumeration applicationAttrNames = servletContext.getAttributeNames(); - while (applicationAttrNames.hasMoreElements()) { - String attrName = (String) applicationAttrNames.nextElement(); - if (attrNamesToSkip.contains(attrName)) continue; - Object param = parameterMap.get(attrName); - Object attrValue = servletContext.getAttribute(attrName); - if (Debug.verboseOn()) Debug.logVerbose("Getting parameter from application attrbute with name [" + attrName + "] and value [" + attrValue + "]", module); - if (param == null) { - parameterMap.put(attrName, attrValue); - } else if (param instanceof String && ((String) param).length() == 0) { - // also put the attribute value in if the parameter is empty - parameterMap.put(attrName, attrValue); - } else { - // do nothing, just log something - Debug.logInfo("Found servlet context (application) attribute that conflicts with parameter name, leaving request parameter in place for name: " + attrName, module); - } - } - + Map parameterMap = UtilHttp.getCombinedMap(request, attrNamesToSkip); GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); populateBasicContext(context, screens, parameterMap, (GenericDelegator) request.getAttribute("delegator"), @@ -320,7 +256,7 @@ public class ScreenRenderer { context.put("isError", Boolean.FALSE); } // if a parameter was passed saying this is an error, it is an error - if ("true".equals((String) parameterMap.get("isError"))) { + if ("true".equals(parameterMap.get("isError"))) { context.put("isError", Boolean.TRUE); } context.put("nowTimestamp", UtilDateTime.nowTimestamp());