Author: mthl
Date: Fri Jul 19 14:29:31 2019 New Revision: 1863401 URL: http://svn.apache.org/viewvc?rev=1863401&view=rev Log: Improved: Use a predicate in ‘UtilHttp#getParameterMap’ (OFBIZ-11138) Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java?rev=1863401&r1=1863400&r2=1863401&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java Fri Jul 19 14:29:31 2019 @@ -134,7 +134,7 @@ public final class 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 @@ public final class 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(); Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java?rev=1863401&r1=1863400&r2=1863401&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java Fri Jul 19 14:29:31 2019 @@ -27,6 +27,7 @@ import static org.mockito.Mockito.when; 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 class UtilHttpTest { "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")))); } Modified: ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java?rev=1863401&r1=1863400&r2=1863401&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java Fri Jul 19 14:29:31 2019 @@ -32,6 +32,7 @@ import java.util.List; 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 class LoginWorker { 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); } |
Free forum by Nabble | Edit this page |