[java-shib-shared] branch main updated: Add URISupport#buildQueryMultiMap to support params with multiple values

Brent Putman putmanb at georgetown.edu
Sun Mar 23 00:25:50 UTC 2025


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch main
in repository java-shib-shared.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=4a6c11de25469b1c67740af14e357ffffa65d630

The following commit(s) were added to refs/heads/main by this push:
     new 4a6c11de Add URISupport#buildQueryMultiMap to support params with multiple values
4a6c11de is described below

commit 4a6c11de25469b1c67740af14e357ffffa65d630
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Sat Mar 22 20:25:24 2025 -0400

    Add URISupport#buildQueryMultiMap to support params with multiple values
---
 .../java/net/shibboleth/shared/net/URISupport.java | 37 +++++++++++++++++++++-
 .../net/shibboleth/shared/net/URISupportTest.java  | 31 ++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/shib-networking/src/main/java/net/shibboleth/shared/net/URISupport.java b/shib-networking/src/main/java/net/shibboleth/shared/net/URISupport.java
index 04ddac3e..488a3674 100644
--- a/shib-networking/src/main/java/net/shibboleth/shared/net/URISupport.java
+++ b/shib-networking/src/main/java/net/shibboleth/shared/net/URISupport.java
@@ -246,6 +246,11 @@ public final class URISupport {
     /**
      * Builds a map from a collection of parameters.
      * 
+     * <p>
+     * Note that this method only properly supports parameters which have a single value.
+     * If support for parameters with multiple values is required, instead use {@link #buildQueryMultiMap(List)}.
+     * </p>
+     * 
      * @param parameters collection of parameters from which to build the corresponding, may be null or empty
      * 
      * @return a non-null map of query parameter name-> value. Keys will be non-null. Values may be null.
@@ -264,7 +269,37 @@ public final class URISupport {
         }
         
         // Allow for null values.
-        return Collections.unmodifiableMap(map);
+        return CollectionSupport.copyToMap(map);
+    }
+
+    /**
+     * Builds a map from a collection of parameters.
+     * 
+     * @param parameters collection of parameters from which to build the corresponding, may be null or empty
+     * 
+     * @return a non-null map of query parameter name-> value. Keys will be non-null. List values will be non-null,
+     *          but may contain null elements
+     */
+    @Nonnull @NullableElements @Unmodifiable @NotLive public static Map<String,List<String>> buildQueryMultiMap(
+            @Nullable @NullableElements final List<Pair<String, String>> parameters) {
+        if (parameters == null || parameters.size() == 0) {
+            return CollectionSupport.emptyMap();
+        }
+        
+        final HashMap<String,List<String>> map = new HashMap<>();
+        for (final Pair<String,String> param : parameters) {
+            if (param.getFirst() != null) {
+                List<String> currentValue = map.get(param.getFirst());
+                if (currentValue == null) {
+                    currentValue = new ArrayList<>();
+                    map.put(param.getFirst(), currentValue);
+                }
+                currentValue.add(param.getSecond());
+            }
+        }
+        
+        // Allow for null values.
+        return CollectionSupport.copyToMap(map);
     }
 
     /**
diff --git a/shib-networking/src/test/java/net/shibboleth/shared/net/URISupportTest.java b/shib-networking/src/test/java/net/shibboleth/shared/net/URISupportTest.java
index 90fdc099..3835b422 100644
--- a/shib-networking/src/test/java/net/shibboleth/shared/net/URISupportTest.java
+++ b/shib-networking/src/test/java/net/shibboleth/shared/net/URISupportTest.java
@@ -95,6 +95,37 @@ public class URISupportTest {
         Assert.assertEquals(map.get("three"), "3");
     }
     
+    @Test public void testBuildQueryMultiMap() {
+        Map<String,List<String>> map = null;
+        
+        map = URISupport.buildQueryMultiMap(null);
+        Assert.assertNotNull(map);
+        Assert.assertTrue(map.isEmpty());
+        
+        map = URISupport.buildQueryMultiMap(CollectionSupport.emptyList());
+        Assert.assertNotNull(map);
+        Assert.assertTrue(map.isEmpty());
+        
+        List<Pair<String,String>> params = new ArrayList<>();
+        params.add(new Pair<>("one", "1A"));
+        params.add(new Pair<>("two", "2A"));
+        params.add(new Pair<>("three", "3A"));
+        params.add(new Pair<>("one", "1B"));
+        params.add(new Pair<>("three", "3B"));
+        params.add(new Pair<>("two", "2B"));
+        params.add(new Pair<>("one", "1C"));
+        
+        map = URISupport.buildQueryMultiMap(params);
+        Assert.assertNotNull(map);
+        Assert.assertEquals(map.size(), 3);
+        Assert.assertTrue(map.containsKey("one"));
+        Assert.assertEquals(map.get("one"), List.of("1A", "1B", "1C"));
+        Assert.assertTrue(map.containsKey("two"));
+        Assert.assertEquals(map.get("two"), List.of("2A", "2B"));
+        Assert.assertTrue(map.containsKey("three"));
+        Assert.assertEquals(map.get("three"), List.of("3A", "3B"));
+    }
+    
     @Test public void testGetRawQueryStringParameter() {
         // Chad's original java-support tests
         String param = URISupport.getRawQueryStringParameter(null, null);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list