Skip to content
Permalink
Browse files

Improved: Refactor ‘UtilHttp#getParameterMap’

(OFBIZ-11138)


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863398 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 19, 2019
1 parent 1d3d3be commit 54d873a387ffc342b35a989cf57902c5ac799392
Showing with 42 additions and 40 deletions.
  1. +42 −40 framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
@@ -125,58 +125,60 @@ private UtilHttp () {}
}

/**
* Create a map from a HttpServletRequest (parameters) object
* @return The resulting Map
* Creates a canonicalized parameter map from a HTTP request.
* <p>
* If parameters are empty, the multi-part parameter map will be used.
*
* @param request the HTTP request containing the parameters
* @return a canonicalized parameter map.
*/
public static Map<String, Object> getParameterMap(HttpServletRequest request) {
return getParameterMap(request, null, null);
return getParameterMap(request, null, true);
}

/**
* Create a map from a HttpServletRequest (parameters) object
* @param onlyIncludeOrSkip If true only include, if false skip, the named parameters in the nameSet. If this is null and nameSet is not null, default to skip.
* @return The resulting Map
* Creates a canonicalized parameter map from a HTTP request.
* <p>
* If parameters are empty, the multi-part parameter map will be used.
*
* @param req the HTTP request containing the parameters
* @param nameSet the set of parameters keys to include or skip
* @param includeOrSkip a toggle where {@code true} means including and {@code false} means skipping
* @return a canonicalized parameter map.
*/
public static Map<String, Object> getParameterMap(HttpServletRequest request, Set<? extends String> nameSet, Boolean onlyIncludeOrSkip) {
boolean onlyIncludeOrSkipPrim = onlyIncludeOrSkip == null ? true : onlyIncludeOrSkip;
Map<String, Object> paramMap = new HashMap<>();

// add all the actual HTTP request parameters
Map<String, String[]> origParams = request.getParameterMap();
origParams.forEach((name, paramArr) -> {
if (nameSet != null && (onlyIncludeOrSkipPrim ^ nameSet.contains(name))) {
return;
}

Object value = null;
if (paramArr != null) {
if (paramArr.length > 1) {
value = Arrays.asList(paramArr);
} else {
value = paramArr[0];
// does the same thing basically, nothing better about it as far as I can see: value = request.getParameter(name);
}
}
paramMap.put(name, value);
});
public static Map<String, Object> getParameterMap(HttpServletRequest req, Set<? extends String> nameSet,
boolean includeOrSkip) {
// Add all the actual HTTP request parameters
Map<String, String[]> origParams = req.getParameterMap();
Map<String, Object> params = origParams.entrySet().stream()
.filter(pair -> nameSet == null || !(includeOrSkip ^ nameSet.contains(pair.getKey())))
.collect(toMap(Map.Entry::getKey, pair -> transformParamValue(pair.getValue())));

paramMap.putAll(getPathInfoOnlyParameterMap(request.getPathInfo(), nameSet, onlyIncludeOrSkipPrim));
// Pseudo-parameters passed in the URI path overrides the ones from the regular URI parameters
params.putAll(getPathInfoOnlyParameterMap(req.getPathInfo(), nameSet, includeOrSkip));

Map<String, Object> multiPartMap = new HashMap<>();
if (paramMap.size() == 0) {
// nothing found in the parameters; maybe we read the stream instead
multiPartMap = getMultiPartParameterMap(request);
if (UtilValidate.isNotEmpty(multiPartMap)) {
paramMap.putAll(multiPartMap);
}
}
request.setAttribute("multiPartMap", multiPartMap);
// If nothing is found in the parameters, try to find something in the multi-part map.
Map<String, Object> multiPartMap = params.isEmpty() ? getMultiPartParameterMap(req) : Collections.emptyMap();
params.putAll(multiPartMap);
req.setAttribute("multiPartMap", multiPartMap);

if (Debug.verboseOn()) {
Debug.logVerbose("Made Request Parameter Map with [" + paramMap.size() + "] Entries", module);
Debug.logVerbose("Made Request Parameter Map with [" + params.size() + "] Entries", module);
}
return canonicalizeParameterMap(params);
}

return canonicalizeParameterMap(paramMap);
/**
* Transforms a string array into either a list of string or string.
* <p>
* This is meant to facilitate the work of request handlers.
*
* @param value the array of string to prepare
* @return the adapted value.
* @throws NullPointerException when {@code value} is {@code null}.
*/
private static Object transformParamValue(String[] value) {
return value.length == 1 ? value[0] : Arrays.asList(value);
}

public static Map<String, Object> getMultiPartParameterMap(HttpServletRequest request) {

0 comments on commit 54d873a

Please sign in to comment.
You can’t perform that action at this time.