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 2020/09/12 12:38:59 UTC

[httpcomponents-core] 06/18: HTTPCORE-627: Adding query parameters causes removal of the scheme specific part of URI by URIBuilder (#196)

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 51ff9cbec24cdcbe7bcee3ba1f10fa19f1b5b809
Author: TorstenR <ne...@users.noreply.github.com>
AuthorDate: Tue Apr 21 09:04:46 2020 +0200

    HTTPCORE-627: Adding query parameters causes removal of the scheme specific part of URI by URIBuilder (#196)
---
 .../java/org/apache/hc/core5/net/URIBuilder.java   | 55 ++++++++++++++++++++++
 .../org/apache/hc/core5/net/TestURIBuilder.java    | 34 +++++++++++++
 2 files changed, 89 insertions(+)

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 9863d6e..3fbe2d7 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
@@ -264,6 +264,51 @@ public class URIBuilder {
     }
 
     /**
+     * Sets the URI scheme specific part.
+     *
+     * @param schemeSpecificPart
+     * @return this.
+     * @since 5.1
+     */
+    public URIBuilder setSchemeSpecificPart(final String schemeSpecificPart) {
+        this.encodedSchemeSpecificPart = schemeSpecificPart;
+        return this;
+    }
+
+    /**
+     * Sets the URI scheme specific part and append a variable arguments list of NameValuePair instance(s) to this part.
+     *
+     * @param schemeSpecificPart
+     * @param nvps Optional, can be null. Variable arguments list of NameValuePair query parameters to be reused by the specific scheme part
+     * @return this.
+     * @since 5.1
+     */
+    public URIBuilder setSchemeSpecificPart(final String schemeSpecificPart, final NameValuePair... nvps) {
+        return setSchemeSpecificPart(schemeSpecificPart, nvps != null ? Arrays.asList(nvps) : null);
+    }
+
+    /**
+     * Sets the URI scheme specific part and append a list of NameValuePair to this part.
+     *
+     * @param schemeSpecificPart
+     * @param nvps Optional, can be null. List of query parameters to be reused by the specific scheme part
+     * @return this.
+     * @since 5.1
+     */
+    public URIBuilder setSchemeSpecificPart(final String schemeSpecificPart, final List <NameValuePair> nvps) {
+        this.encodedSchemeSpecificPart = null;
+        if (!TextUtils.isBlank(schemeSpecificPart)) {
+            final StringBuilder sb = new StringBuilder(schemeSpecificPart);
+            if (nvps != null && !nvps.isEmpty()) {
+                sb.append("?");
+                encodeUrlForm(sb, nvps);
+            }
+            this.encodedSchemeSpecificPart = sb.toString();
+        }
+        return this;
+    }
+
+    /**
      * Sets URI user info. The value is expected to be unescaped and may contain non ASCII
      * characters.
      *
@@ -555,6 +600,16 @@ public class URIBuilder {
         return this.scheme;
     }
 
+    /**
+     * Gets the scheme specific part
+     *
+     * @return String
+     * @since 5.1
+     */
+    public String getSchemeSpecificPart() {
+        return this.encodedSchemeSpecificPart;
+    }
+
     public String getUserInfo() {
         return this.userInfo;
     }
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 cf68126..f86d345 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
@@ -464,4 +464,38 @@ public class TestURIBuilder {
         Assert.assertEquals(uri, result);
     }
 
+    @Test
+    public void testSchemeSpecificPartParametersNull() throws Exception {
+       final URIBuilder uribuilder = new URIBuilder("http://host.com").setParameter("par", "parvalue")
+               .setSchemeSpecificPart("", (NameValuePair)null);
+       Assert.assertEquals(new URI("http://host.com?par=parvalue"), uribuilder.build());
+    }
+
+    @Test
+    public void testSchemeSpecificPartSetGet() throws Exception {
+       final URIBuilder uribuilder = new URIBuilder().setSchemeSpecificPart("specificpart");
+       Assert.assertEquals("specificpart", uribuilder.getSchemeSpecificPart());
+    }
+
+    /** Common use case: mailto: scheme. See https://tools.ietf.org/html/rfc6068#section-2 */
+    @Test
+    public void testSchemeSpecificPartNameValuePairByRFC6068Sample() throws Exception {
+       final URIBuilder uribuilder = new URIBuilder().setScheme("mailto")
+               .setSchemeSpecificPart("my@email.server", new BasicNameValuePair("subject", "mail subject"));
+       final String result = uribuilder.build().toString();
+       Assert.assertTrue("mail address as scheme specific part expected", result.contains("my@email.server"));
+       Assert.assertTrue("correct parameter encoding expected for that scheme", result.contains("mail%20subject"));
+    }
+
+    /** Common use case: mailto: scheme. See https://tools.ietf.org/html/rfc6068#section-2 */
+    @Test
+    public void testSchemeSpecificPartNameValuePairListByRFC6068Sample() throws Exception {
+        final List<NameValuePair> parameters = new ArrayList<>();
+        parameters.add(new BasicNameValuePair("subject", "mail subject"));
+
+       final URIBuilder uribuilder = new URIBuilder().setScheme("mailto").setSchemeSpecificPart("my@email.server", parameters);
+       final String result = uribuilder.build().toString();
+       Assert.assertTrue("mail address as scheme specific part expected", result.contains("my@email.server"));
+       Assert.assertTrue("correct parameter encoding expected for that scheme", result.contains("mail%20subject"));
+    }
 }