You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2021/02/18 13:57:22 UTC
[httpcomponents-core] branch 5.2.x updated: Add
URIBuilder#getFirstQueryParam(String) to query a parameter by name.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch 5.2.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/5.2.x by this push:
new 5406fbe Add URIBuilder#getFirstQueryParam(String) to query a parameter by name.
5406fbe is described below
commit 5406fbe07538c4f97ef62c1208fda6ea24991fa5
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.
---
.../main/java/org/apache/hc/core5/net/URIBuilder.java | 12 ++++++++++++
.../java/org/apache/hc/core5/net/TestURIBuilder.java | 18 ++++++++++++++++--
2 files changed, 28 insertions(+), 2 deletions(-)
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 22364ee..5372b58 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
@@ -343,6 +343,7 @@ public class URIBuilder {
this.host = PercentCodec.decode(uriAuthority.getHostName(), charset);
this.port = uriAuthority.getPort();
} catch (final URISyntaxException ignore) {
+ // ignore
}
}
this.encodedPath = uri.getRawPath();
@@ -812,6 +813,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 5bd5d83..3eb0473 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