You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/03/12 21:39:09 UTC

[httpcomponents-core] 08/08: Add URIBuilder#getFirstQueryParam(String) to query a parameter by name. #264.

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 2e79c62f76674ba4f5aaa517172f4d8a2bd08d2f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Feb 17 20:04:26 2021 -0500

    Add URIBuilder#getFirstQueryParam(String) to query a parameter by name. #264.
---
 RELEASE_NOTES.txt                                      |  3 +++
 .../main/java/org/apache/hc/core5/net/URIBuilder.java  | 12 ++++++++++++
 .../java/org/apache/hc/core5/net/TestURIBuilder.java   | 18 ++++++++++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 966a34c..9011b34 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -7,6 +7,9 @@ new features as well as bug fixes from the stable 5.0.x and 5.1.x branches.
 * Let TimeValue/Timeout convert to and from Duration. #263.
   Contributed by Gary Gregory <garydgregory at gmail.com>
 
+* Add URIBuilder#getFirstQueryParam(String) to query a parameter by name. #264.
+  Contributed by Gary Gregory <garydgregory at gmail.com>
+
 
 Release 5.1
 -----------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
index bdb6c23..a7f2f82 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
@@ -345,6 +345,7 @@ public class URIBuilder {
                 this.host = PercentCodec.decode(uriAuthority.getHostName(), charset);
                 this.port = uriAuthority.getPort();
             } catch (final URISyntaxException ignore) {
+                // ignore
             }
         }
         this.encodedPath = uri.getRawPath();
@@ -814,6 +815,17 @@ public class URIBuilder {
         return this.queryParams != null ? new ArrayList<>(this.queryParams) : Collections.<NameValuePair>emptyList();
     }
 
+    /**
+     * Gets the first {@link NameValuePair} for a given name.
+     *
+     * @param name the name
+     * @return the first named {@link NameValuePair} or null if not found.
+     * @since 5.2
+     */
+    public NameValuePair getFirstQueryParam(final String name) {
+        return queryParams.stream().filter(e -> name.equals(e.getName())).findFirst().orElse(null);
+    }
+
     public String getFragment() {
         return this.fragment;
     }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
index 784c1a0..125dd5b 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
@@ -301,9 +301,23 @@ public class TestURIBuilder {
     public void testSetParameter() throws Exception {
         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
         final URIBuilder uribuilder = new URIBuilder(uri).setParameter("param", "some other stuff")
-            .setParameter("blah", "blah");
+                .setParameter("blah", "blah")
+                .setParameter("blah", "blah2");
         final URI result = uribuilder.build();
-        Assert.assertEquals(new URI("http://localhost:80/?param=some%20other%20stuff&blah=blah"), result);
+        Assert.assertEquals(new URI("http://localhost:80/?param=some%20other%20stuff&blah=blah2"), result);
+    }
+
+    @Test
+    public void testGetFirstNamedParameter() throws Exception {
+        final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
+        URIBuilder uribuilder = new URIBuilder(uri).setParameter("param", "some other stuff")
+            .setParameter("blah", "blah");
+        Assert.assertEquals("some other stuff", uribuilder.getFirstQueryParam("param").getValue());
+        Assert.assertEquals("blah", uribuilder.getFirstQueryParam("blah").getValue());
+        Assert.assertNull(uribuilder.getFirstQueryParam("DoesNotExist"));
+        //
+        uribuilder = new URIBuilder("http://localhost:80/?param=some%20other%20stuff&blah=blah&blah=blah2");
+        Assert.assertEquals("blah", uribuilder.getFirstQueryParam("blah").getValue());
     }
 
     @Test