Skip to content
Permalink
Browse files

Improved: Use a predicate in ‘UtilHttp#getParameterMap’

(OFBIZ-11138)


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863401 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 19, 2019
1 parent 39ba0a7 commit abf0a9ac4b34ceb7088feadc7157ec50c6818758
@@ -134,7 +134,7 @@ private UtilHttp () {}
* @return a canonicalized parameter map.
*/
public static Map<String, Object> getParameterMap(HttpServletRequest request) {
return getParameterMap(request, null, true);
return getParameterMap(request, x -> true);
}

/**
@@ -143,21 +143,18 @@ private UtilHttp () {}
* 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
* @param pred the predicate filtering the parameter names
* @return a canonicalized parameter map.
*/
public static Map<String, Object> getParameterMap(HttpServletRequest req, Set<? extends String> nameSet,
boolean includeOrSkip) {
public static Map<String, Object> getParameterMap(HttpServletRequest req, Predicate<String> pred) {
// 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())))
.filter(pair -> pred.test(pair.getKey()))
.collect(toMap(Map.Entry::getKey, pair -> transformParamValue(pair.getValue())));

// Pseudo-parameters passed in the URI path overrides the ones from the regular URI parameters
params.putAll(getPathInfoOnlyParameterMap(req.getPathInfo(),
name -> nameSet == null || !(includeOrSkip ^ nameSet.contains(name))));
params.putAll(getPathInfoOnlyParameterMap(req.getPathInfo(), pred));

// 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();
@@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.function.Predicate;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HttpMethod;
@@ -113,10 +114,11 @@ public void filteredGetParameterMap() {
"foo", new String[] {"1"},
"bar", new String[] {"2", "3"}));
when(req.getPathInfo()).thenReturn("/foo");
assertThat(UtilHttp.getParameterMap(req, UtilMisc.toSet("bar"), false), Matchers.<Map<String, Object>>allOf(
Predicate<String> equalsBar = "bar"::equals;
assertThat(UtilHttp.getParameterMap(req, equalsBar.negate()), Matchers.<Map<String, Object>>allOf(
hasEntry("foo", "1"),
not(hasEntry("bar", Arrays.asList("2", "3")))));
assertThat(UtilHttp.getParameterMap(req, UtilMisc.toSet("bar"), true), Matchers.<Map<String, Object>>allOf(
assertThat(UtilHttp.getParameterMap(req, equalsBar), Matchers.<Map<String, Object>>allOf(
not(hasEntry("foo", "1")),
hasEntry("bar", Arrays.asList("2", "3"))));
}
@@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@@ -354,7 +355,8 @@ public static String checkLogin(HttpServletRequest request, HttpServletResponse
if (UtilValidate.isNotEmpty(urlParams)) {
session.setAttribute("_PREVIOUS_PARAM_MAP_URL_", urlParams);
}
Map<String, Object> formParams = UtilHttp.getParameterMap(request, urlParams.keySet(), false);
Predicate<String> isUrlParam = urlParams.keySet()::contains;
Map<String, Object> formParams = UtilHttp.getParameterMap(request, isUrlParam.negate());
if (UtilValidate.isNotEmpty(formParams)) {
session.setAttribute("_PREVIOUS_PARAM_MAP_FORM_", formParams);
}

0 comments on commit abf0a9a

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