Skip to content
Permalink
Browse files

Improved: Use a predicate in ‘UtilHttp#getPathInfoOnlyParameterMap’

(OFBIZ-11138)


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863400 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 19, 2019
1 parent 54d873a commit 39ba0a716e2ce51f1d2650ecfc05bdb5c98796c2
@@ -53,6 +53,7 @@
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.function.Function;
import java.util.function.Predicate;

import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletRequest;
@@ -155,7 +156,8 @@ private UtilHttp () {}
.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(), nameSet, includeOrSkip));
params.putAll(getPathInfoOnlyParameterMap(req.getPathInfo(),
name -> nameSet == null || !(includeOrSkip ^ nameSet.contains(name))));

// 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();
@@ -317,12 +319,10 @@ private static Object transformParamValue(String[] value) {
* This is an obsolete syntax for passing parameters to request handlers.
*
* @param path the URI path part which can be {@code null}
* @param nameSet the set of parameters keys to include or skip
* @param includeOrSkip toggle where {@code true} means including and {@code false} means skipping
* @param pred the predicate filtering parameter names
* @return a canonicalized parameter map.
*/
static Map<String, Object> getPathInfoOnlyParameterMap(String path, Set<? extends String> nameSet,
boolean includeOrSkip) {
static Map<String, Object> getPathInfoOnlyParameterMap(String path, Predicate<String> pred) {
String path$ = Optional.ofNullable(path).orElse("");
Map<String, List<String>> allParams = Arrays.stream(path$.split("/"))
.filter(segment -> segment.startsWith("~") && segment.contains("="))
@@ -332,15 +332,15 @@ private static Object transformParamValue(String[] value) {
// Filter and canonicalize the parameter map.
Function<List<String>, Object> canonicalize = val -> (val.size() == 1) ? val.get(0) : val;
return allParams.entrySet().stream()
.filter(e -> nameSet == null || !(includeOrSkip ^ nameSet.contains(e.getKey())))
.filter(pair -> pred.test(pair.getKey()))
.collect(collectingAndThen(toMap(Map.Entry::getKey, canonicalize.compose(Map.Entry::getValue)),
UtilHttp::canonicalizeParameterMap));
}

public static Map<String, Object> getUrlOnlyParameterMap(HttpServletRequest request) {
// NOTE: these have already been through canonicalizeParameterMap, so not doing it again here
Map<String, Object> paramMap = getQueryStringOnlyParameterMap(request.getQueryString());
paramMap.putAll(getPathInfoOnlyParameterMap(request.getPathInfo(), null, true));
paramMap.putAll(getPathInfoOnlyParameterMap(request.getPathInfo(), x -> true));
return paramMap;
}

@@ -46,32 +46,32 @@ public void setup() {

@Test
public void basicGetPathInfoOnlyParameterMap() {
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", null, false),
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", x -> true),
allOf(hasEntry("foo", "1"), hasEntry("bar", "2")));

assertThat(getPathInfoOnlyParameterMap("/~foo=1/~foo=2", null, false),
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~foo=2", x -> true),
hasEntry("foo", Arrays.asList("1", "2")));

assertThat(getPathInfoOnlyParameterMap("/~foo=1/~foo=2/~foo=3/", null, false),
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~foo=2/~foo=3/", x -> true),
hasEntry("foo", Arrays.asList("1", "2", "3")));

assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2/~foo=3/", null, false),
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2/~foo=3/", x -> true),
Matchers.<Map<String,Object>>allOf(
hasEntry("foo", Arrays.asList("1", "3")),
hasEntry("bar", "2")));
}

@Test
public void emptyGetPathInfoOnlyParameterMap() {
assertThat(getPathInfoOnlyParameterMap(null, null, false), is(anEmptyMap()));
assertThat(getPathInfoOnlyParameterMap(null, x -> true), is(anEmptyMap()));
}

@Test
public void filteredGetPathInfoOnlyParameterMap() {
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", UtilMisc.toSet("foo"), false),
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", name -> !"foo".equals(name)),
allOf(not(hasEntry("foo", "1")), hasEntry("bar", "2")));

assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", UtilMisc.toSet("foo"), true),
assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", "foo"::equals),
allOf(hasEntry("foo", "1"), not(hasEntry("bar", "2"))));
}

0 comments on commit 39ba0a7

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