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 43a3dd9705..3589a3669c 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 @@ -142,15 +142,13 @@ public final class UtilHttp { Map paramMap = new HashMap<>(); // add all the actual HTTP request parameters - Enumeration e = UtilGenerics.cast(request.getParameterNames()); - while (e.hasMoreElements()) { - String name = e.nextElement(); + Map origParams = request.getParameterMap(); + origParams.forEach((name, paramArr) -> { if (nameSet != null && (onlyIncludeOrSkipPrim ^ nameSet.contains(name))) { - continue; + return; } Object value = null; - String[] paramArr = request.getParameterValues(name); if (paramArr != null) { if (paramArr.length > 1) { value = Arrays.asList(paramArr); @@ -160,7 +158,7 @@ public final class UtilHttp { } } paramMap.put(name, value); - } + }); paramMap.putAll(getPathInfoOnlyParameterMap(request.getPathInfo(), nameSet, onlyIncludeOrSkipPrim)); diff --git a/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java b/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java index de5d89ee54..2b15126578 100644 --- a/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java +++ b/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilHttpTest.java @@ -22,14 +22,27 @@ import static org.apache.ofbiz.base.util.UtilHttp.getPathInfoOnlyParameterMap; import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.hasEntry; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; import java.util.Arrays; +import java.util.Collections; import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.HttpMethod; + import org.hamcrest.Matchers; +import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; public class UtilHttpTest { + private HttpServletRequest req; + + @Before + public void setup() { + req = Mockito.mock(HttpServletRequest.class); + } @Test public void basicGetPathInfoOnlyParameterMap() { @@ -61,4 +74,50 @@ public class UtilHttpTest { assertThat(getPathInfoOnlyParameterMap("/~foo=1/~bar=2", UtilMisc.toSet("foo"), true), allOf(hasEntry("foo", "1"), not(hasEntry("bar", "2")))); } + + @Test + public void basicGetParameterMap() { + when(req.getParameterMap()).thenReturn(UtilMisc.toMap( + "foo", new String[] {"1"}, + "bar", new String[] {"2", "3"})); + when(req.getPathInfo()).thenReturn("/foo"); + assertThat(UtilHttp.getParameterMap(req), Matchers.>allOf( + hasEntry("foo", "1"), + hasEntry("bar", Arrays.asList("2", "3")))); + } + + @Test + public void pathInfoOverrideGetParameterMap() { + when(req.getParameterMap()).thenReturn(UtilMisc.toMap( + "foo", new String[] {"1"}, + "bar", new String[] {"2"})); + when(req.getPathInfo()).thenReturn("/foo/~bar=3"); + assertThat(UtilHttp.getParameterMap(req), Matchers.>allOf( + hasEntry("foo", "1"), + hasEntry("bar", "3"))); + } + + @Test + public void emptyParameterMap() { + when(req.getParameterMap()).thenReturn(Collections.emptyMap()); + when(req.getPathInfo()).thenReturn("/foo/bar"); + when(req.getMethod()).thenReturn(HttpMethod.POST); + UtilHttp.getParameterMap(req); + // Check that multi-part arguments are looked up + Mockito.verify(req).getContentType(); + } + + @Test + public void filteredGetParameterMap() { + when(req.getParameterMap()).thenReturn(UtilMisc.toMap( + "foo", new String[] {"1"}, + "bar", new String[] {"2", "3"})); + when(req.getPathInfo()).thenReturn("/foo"); + assertThat(UtilHttp.getParameterMap(req, UtilMisc.toSet("bar"), false), Matchers.>allOf( + hasEntry("foo", "1"), + not(hasEntry("bar", Arrays.asList("2", "3"))))); + assertThat(UtilHttp.getParameterMap(req, UtilMisc.toSet("bar"), true), Matchers.>allOf( + not(hasEntry("foo", "1")), + hasEntry("bar", Arrays.asList("2", "3")))); + } }