applied patch from Ashish Vijaywargiya making generic use of pulling data from request, session, etc into a combined map; refactored to remain backwards compatible JIRA issue OFBIZ-967

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@536030 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Zeneski
2007-05-08 00:25:01 +00:00
parent a3c54d8f18
commit 5cd5d63cb9
3 changed files with 148 additions and 77 deletions
+3 -3
View File
@@ -71,12 +71,12 @@ under the License.
</target>
<!-- ================================================================== -->
<!-- Compilation of the source files -->
<!-- Compilation of the source files -->
<!-- ================================================================== -->
<target name="classes" depends="prepare,classpath">
<!-- compile start -->
<javac debug="on" source="1.4" deprecation="on" destdir="${build.dir}/classes/start">
<javac debug="on" source="1.5" deprecation="on" destdir="${build.dir}/classes/start">
<src path="${src.dir}/start"/>
</javac>
<copy todir="${build.dir}/classes/start">
@@ -91,7 +91,7 @@ under the License.
</condition>
<!-- compile base -->
<javac debug="on" source="1.4" deprecation="on" destdir="${build.dir}/classes/base">
<javac debug="on" source="1.5" deprecation="on" destdir="${build.dir}/classes/base">
<classpath>
<path refid="local.class.path"/>
<pathelement path="${build.dir}/classes/start"/>
@@ -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<String> 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<String> 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<String> 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<String> 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());
}
}
@@ -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());