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 2018/11/08 18:27:53 UTC

httpcomponents-core git commit: Add org.apache.hc.core5.net.URIBuilder.setHttpHost(HttpHost) to further facilitate URI construction.

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master 8c0f3f044 -> 4af40b3cf


Add org.apache.hc.core5.net.URIBuilder.setHttpHost(HttpHost) to further
facilitate URI construction.

Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/4af40b3c
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/4af40b3c
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/4af40b3c

Branch: refs/heads/master
Commit: 4af40b3cf95123fd19673296d614ffec1d9b3a59
Parents: 8c0f3f0
Author: Gary Gregory <ga...@gmail.com>
Authored: Thu Nov 8 11:27:49 2018 -0700
Committer: Gary Gregory <ga...@gmail.com>
Committed: Thu Nov 8 11:27:49 2018 -0700

----------------------------------------------------------------------
 RELEASE_NOTES.txt                               |  3 ++
 .../org/apache/hc/core5/net/URIBuilder.java     | 41 +++++++++++++++++++-
 .../org/apache/hc/core5/net/TestURIBuilder.java | 11 ++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4af40b3c/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index b9ec046..fcf9263 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -28,6 +28,9 @@ Release 5.0-BETA6
 * Add org.apache.hc.core5.http.HttpRequest.setUri(URI)
   Contributed by Gary Gregory <ggregory at apache.org>
 
+* org.apache.hc.core5.net.URIBuilder.setHttpHost(HttpHost)
+  Contributed by Gary Gregory <ggregory at apache.org>
+
 
 Release 5.0-BETA5
 -------------------

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4af40b3c/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
----------------------------------------------------------------------
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 a5cada4..16dbcf9 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
@@ -36,6 +36,7 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.NameValuePair;
 import org.apache.hc.core5.http.message.BasicNameValuePair;
 import org.apache.hc.core5.util.TextUtils;
@@ -242,6 +243,8 @@ public class URIBuilder {
 
     /**
      * Sets URI scheme.
+     *
+     * @return this.
      */
     public URIBuilder setScheme(final String scheme) {
         this.scheme = !TextUtils.isBlank(scheme) ? scheme : null;
@@ -251,6 +254,8 @@ public class URIBuilder {
     /**
      * Sets URI user info. The value is expected to be unescaped and may contain non ASCII
      * characters.
+     *
+     * @return this.
      */
     public URIBuilder setUserInfo(final String userInfo) {
         this.userInfo = !TextUtils.isBlank(userInfo) ? userInfo : null;
@@ -263,6 +268,8 @@ public class URIBuilder {
     /**
      * Sets URI user info as a combination of username and password. These values are expected to
      * be unescaped and may contain non ASCII characters.
+     *
+     * @return this.
      */
     public URIBuilder setUserInfo(final String username, final String password) {
         return setUserInfo(username + ':' + password);
@@ -271,6 +278,7 @@ public class URIBuilder {
     /**
      * Sets URI host.
      *
+     * @return this.
      * @since 4.6
      */
     public URIBuilder setHost(final InetAddress host) {
@@ -282,17 +290,33 @@ public class URIBuilder {
 
     /**
      * Sets URI host.
+     *
+     * @return this.
      */
     public URIBuilder setHost(final String host) {
         this.host = !TextUtils.isBlank(host) ? host : null;
-        this.host = host;
         this.encodedSchemeSpecificPart = null;
         this.encodedAuthority = null;
         return this;
     }
 
     /**
+     * Sets the scheme, host name, and port.
+     *
+     * @param httpHost the scheme, host name, and port.
+     * @return this.
+     */
+    public URIBuilder setHttpHost(final HttpHost httpHost ) {
+        setScheme(httpHost.getSchemeName());
+        setHost(httpHost.getHostName());
+        setPort(httpHost.getPort());
+        return this;
+    }
+
+    /**
      * Sets URI port.
+     *
+     * @return this.
      */
     public URIBuilder setPort(final int port) {
         this.port = port < 0 ? -1 : port;
@@ -303,6 +327,8 @@ public class URIBuilder {
 
     /**
      * Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.
+     *
+     * @return this.
      */
     public URIBuilder setPath(final String path) {
         this.path = !TextUtils.isBlank(path) ? path : null;
@@ -313,6 +339,8 @@ public class URIBuilder {
 
     /**
      * Removes URI query.
+     *
+     * @return this.
      */
     public URIBuilder removeQuery() {
         this.queryParams = null;
@@ -330,6 +358,7 @@ public class URIBuilder {
      * will remove custom query if present.
      * </p>
      *
+     * @return this.
      * @since 4.3
      */
     public URIBuilder setParameters(final List <NameValuePair> nvps) {
@@ -353,6 +382,7 @@ public class URIBuilder {
      * will remove custom query if present.
      * </p>
      *
+     * @return this.
      * @since 4.3
      */
     public URIBuilder addParameters(final List <NameValuePair> nvps) {
@@ -374,6 +404,7 @@ public class URIBuilder {
      * will remove custom query if present.
      * </p>
      *
+     * @return this.
      * @since 4.3
      */
     public URIBuilder setParameters(final NameValuePair... nvps) {
@@ -398,6 +429,8 @@ public class URIBuilder {
      * Please note query parameters and custom query component are mutually exclusive. This method
      * will remove custom query if present.
      * </p>
+     *
+     * @return this.
      */
     public URIBuilder addParameter(final String param, final String value) {
         if (this.queryParams == null) {
@@ -417,6 +450,8 @@ public class URIBuilder {
      * Please note query parameters and custom query component are mutually exclusive. This method
      * will remove custom query if present.
      * </p>
+     *
+     * @return this.
      */
     public URIBuilder setParameter(final String param, final String value) {
         if (this.queryParams == null) {
@@ -440,6 +475,7 @@ public class URIBuilder {
     /**
      * Clears URI query parameters.
      *
+     * @return this.
      * @since 4.3
      */
     public URIBuilder clearParameters() {
@@ -457,6 +493,7 @@ public class URIBuilder {
      * will remove query parameters if present.
      * </p>
      *
+     * @return this.
      * @since 4.3
      */
     public URIBuilder setCustomQuery(final String query) {
@@ -470,6 +507,8 @@ public class URIBuilder {
     /**
      * Sets URI fragment. The value is expected to be unescaped and may contain non ASCII
      * characters.
+     *
+     * @return this.
      */
     public URIBuilder setFragment(final String fragment) {
         this.fragment = !TextUtils.isBlank(fragment) ? fragment : null;

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4af40b3c/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
----------------------------------------------------------------------
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 c069ea1..96808e8 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
@@ -35,6 +35,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.NameValuePair;
 import org.apache.hc.core5.http.message.BasicNameValuePair;
 import org.hamcrest.CoreMatchers;
@@ -413,4 +414,14 @@ public class TestURIBuilder {
                 CoreMatchers.equalTo(URI.create("//localhost:8443")));
     }
 
+    @Test
+    public void testHttpHost() throws Exception {
+        final HttpHost httpHost = new HttpHost("example.com", 1234, "http");
+        final URIBuilder uribuilder = new URIBuilder();
+        uribuilder.setHttpHost(httpHost);
+        final URI result = uribuilder.build();
+        Assert.assertEquals(URI.create(httpHost.toURI()), result);
+    }
+
+
 }