From df8586eccf1a19f68d3491096d7f430de04e31fb Mon Sep 17 00:00:00 2001 From: Mathieu Lirzin Date: Fri, 19 Jul 2019 22:08:53 +0000 Subject: [PATCH] =?UTF-8?q?Improved:=20Refactor=20=E2=80=98Makeparamlistwi?= =?UTF-8?q?thsuffix=E2=80=99=20(OFBIZ-11138)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863441 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/ofbiz/base/util/UtilHttp.java | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java index 4473aafcc5..139847a790 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java @@ -37,7 +37,6 @@ import java.net.URLConnection; import java.nio.ByteBuffer; import java.sql.Timestamp; import java.time.LocalDateTime; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -49,6 +48,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.StringTokenizer; @@ -56,6 +56,7 @@ import java.util.TimeZone; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.net.ssl.SSLContext; import javax.servlet.http.HttpServletRequest; @@ -594,40 +595,46 @@ public final class UtilHttp { return paramMap; } + /** + * Constructs a list of parameter values whose keys are matching a given prefix and suffix. + * + * @param request the HTTP request containing the parameters + * @param suffix the suffix that must be matched which can be {@code null} + * @param prefix the prefix that must be matched which can be {@code null} + * @return the list of parameter values whose keys are matching {@code prefix} and {@code suffix}. + * @throws NullPointerException when {@code request} is {@code null}. + */ public static List makeParamListWithSuffix(HttpServletRequest request, String suffix, String prefix) { - return makeParamListWithSuffix(request, null, suffix, prefix); + return makeParamListWithSuffix(request, Collections.emptyMap(), suffix, prefix); } - public static List makeParamListWithSuffix(HttpServletRequest request, Map additionalFields, String suffix, String prefix) { - List paramList = new ArrayList<>(); - request.getParameterMap().forEach((parameterName, values) -> { - if (parameterName.endsWith(suffix)) { - if (UtilValidate.isNotEmpty(prefix)) { - if (parameterName.startsWith(prefix)) { - String value = values[0]; - paramList.add(value); - } - } else { - String value = values[0]; - paramList.add(value); - } - } - }); - if (additionalFields != null) { - for (Map.Entry entry: additionalFields.entrySet()) { - String fieldName = entry.getKey(); - if (fieldName.endsWith(suffix)) { - if (UtilValidate.isNotEmpty(prefix)) { - if (fieldName.startsWith(prefix)) { - paramList.add(entry.getValue()); - } - } else { - paramList.add(entry.getValue()); - } - } - } - } - return paramList; + /** + * Constructs a list of parameter values whose keys are matching a given prefix and suffix. + * + * @param request the HTTP request containing the parameters + * @param additionalFields the additional parameters + * @param suffix the suffix that must be matched which can be {@code null} + * @param prefix the prefix that must be matched which can be {@code null} + * @return the list of parameter values whose keys are matching {@code prefix} and {@code suffix}. + * @throws NullPointerException when {@code request} or {@code additionalFields} are {@code null}. + */ + public static List makeParamListWithSuffix(HttpServletRequest request, Map additionalFields, + String suffix, String prefix) { + Objects.requireNonNull(request); + Objects.requireNonNull(additionalFields); + Predicate> pred = UtilValidate.isEmpty(prefix) + ? e -> e.getKey().endsWith(suffix) + : e -> e.getKey().endsWith(suffix) && e.getKey().startsWith(prefix); + + Stream params = request.getParameterMap().entrySet().stream() + .filter(pred) + .map(e -> e.getValue()[0]); + + Stream additionalParams = additionalFields.entrySet().stream() + .filter(pred) + .map(Map.Entry::getValue); + + return Stream.concat(params, additionalParams).collect(Collectors.toList()); } /**