[java-support] branch maint-7 updated: Add utility method to convert list of query params to a map.

Brent Putman putmanb at georgetown.edu
Thu Oct 4 19:48:06 EDT 2018


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

putmanb pushed a commit to branch maint-7
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=03c7c4b8f19c596a99c3da30cb80ce0d8f5ff43c

The following commit(s) were added to refs/heads/maint-7 by this push:
       new  03c7c4b   Add utility method to convert list of query params to a map.
03c7c4b is described below

commit 03c7c4b8f19c596a99c3da30cb80ce0d8f5ff43c
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Thu Oct 4 19:48:05 2018 -0400

    Add utility method to convert list of query params to a map.
---
 .../utilities/java/support/net/URISupport.java     | 26 ++++++++++++++++++
 .../utilities/java/support/net/URISupportTest.java | 32 ++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/net/URISupport.java b/src/main/java/net/shibboleth/utilities/java/support/net/URISupport.java
index 946239e..5b65c7f 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/net/URISupport.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/net/URISupport.java
@@ -23,8 +23,12 @@ import java.net.URISyntaxException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import com.google.common.annotations.Beta;
@@ -229,6 +233,28 @@ public final class URISupport {
 
         return builder.toString();
     }
+    
+    /**
+     * 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. Values may be null.
+     */
+    @Nonnull public static Map<String,String> buildQueryMap(@Nullable final List<Pair<String, String>> parameters) {
+        if (parameters == null || parameters.size() == 0) {
+            return Collections.emptyMap();
+        }
+        
+        final HashMap<String,String> map = new HashMap<>();
+        for (final Pair<String,String> param : parameters) {
+            if (param.getFirst() != null) {
+                map.put(param.getFirst(), param.getSecond());
+            }
+        }
+        
+        return map;
+    }
 
     /**
      * Get the first raw (i.e.RFC-3968 encoded) query string component with the specified parameter name. This method
diff --git a/src/test/java/net/shibboleth/utilities/java/support/net/URISupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/net/URISupportTest.java
index 1f03400..2378fa6 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/net/URISupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/net/URISupportTest.java
@@ -17,13 +17,18 @@
 
 package net.shibboleth.utilities.java.support.net;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import net.shibboleth.utilities.java.support.collection.Pair;
 
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.Lists;
+
 /** Unit test for {@link URISupport}. */
 public class URISupportTest {
     
@@ -67,6 +72,33 @@ public class URISupportTest {
         
     }
     
+    @Test public void testBuildQueryMap() {
+        Map<String,String> map = null;
+        
+        map = URISupport.buildQueryMap(null);
+        Assert.assertNotNull(map);
+        Assert.assertTrue(map.isEmpty());
+        
+        map = URISupport.buildQueryMap(Collections.<Pair<String,String>>emptyList());
+        Assert.assertNotNull(map);
+        Assert.assertTrue(map.isEmpty());
+        
+        List<Pair<String,String>> params = new ArrayList<>();
+        params.add(new Pair<String,String>("one", "1"));
+        params.add(new Pair<String,String>("two", "2"));
+        params.add(new Pair<String,String>("three", "3"));
+        
+        map = URISupport.buildQueryMap(params);
+        Assert.assertNotNull(map);
+        Assert.assertEquals(map.size(), 3);
+        Assert.assertTrue(map.containsKey("one"));
+        Assert.assertEquals(map.get("one"), "1");
+        Assert.assertTrue(map.containsKey("two"));
+        Assert.assertEquals(map.get("two"), "2");
+        Assert.assertTrue(map.containsKey("three"));
+        Assert.assertEquals(map.get("three"), "3");
+    }
+    
     @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