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 2017/05/20 08:34:13 UTC

httpcomponents-client git commit: [HTTPCLIENT-1852] Add APIs URIBuilder.localhost() and setHost(InetAddress) [Forced Update!]

Repository: httpcomponents-client
Updated Branches:
  refs/heads/4.6.x f030988e8 -> 5ca2eda57 (forced update)


[HTTPCLIENT-1852] Add APIs URIBuilder.localhost() and
setHost(InetAddress)


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/5ca2eda5
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/5ca2eda5
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/5ca2eda5

Branch: refs/heads/4.6.x
Commit: 5ca2eda57aa37599ff45e75d2d8176314253e1ec
Parents: 9aff64d
Author: Gary Gregory <gg...@apache.org>
Authored: Fri May 19 19:20:11 2017 -0700
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat May 20 10:31:54 2017 +0200

----------------------------------------------------------------------
 .../apache/http/client/utils/URIBuilder.java    | 23 +++++++++++++++
 .../org/apache/http/client/utils/URIUtils.java  |  2 +-
 .../http/client/utils/TestURIBuilder.java       | 31 +++++++++++++++++++-
 3 files changed, 54 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/5ca2eda5/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
index 91f198c..5d9d897 100644
--- a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
+++ b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
@@ -26,8 +26,10 @@
  */
 package org.apache.http.client.utils;
 
+import java.net.InetAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.UnknownHostException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -46,6 +48,15 @@ import org.apache.http.util.TextUtils;
  */
 public class URIBuilder {
 
+    /**
+     * Creates a new builder for the host {@link InetAddress#getLocalHost()}.
+     *
+     * @since 4.6
+     */
+    public static URIBuilder localhost() throws UnknownHostException {
+        return new URIBuilder().setHost(InetAddress.getLocalHost());
+    }
+
     private String scheme;
     private String encodedSchemeSpecificPart;
     private String encodedAuthority;
@@ -248,6 +259,18 @@ public class URIBuilder {
 
     /**
      * Sets URI host.
+     *
+     * @since 4.6
+     */
+    public URIBuilder setHost(final InetAddress host) {
+        this.host = host.getHostAddress();
+        this.encodedSchemeSpecificPart = null;
+        this.encodedAuthority = null;
+        return this;
+    }
+
+    /**
+     * Sets URI host.
      */
     public URIBuilder setHost(final String host) {
         this.host = host;

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/5ca2eda5/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
----------------------------------------------------------------------
diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
index 02f8c1a..2ab4b58 100644
--- a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
+++ b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
@@ -141,7 +141,7 @@ public class URIUtils {
             uribuilder.setPort(target.getPort());
         } else {
             uribuilder.setScheme(null);
-            uribuilder.setHost(null);
+            uribuilder.setHost((String) null);
             uribuilder.setPort(-1);
         }
         if (dropFragment) {

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/5ca2eda5/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
----------------------------------------------------------------------
diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
index 8bca89e..5095d39 100644
--- a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
+++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
@@ -26,6 +26,7 @@
  */
 package org.apache.http.client.utils;
 
+import java.net.InetAddress;
 import java.net.URI;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
@@ -51,7 +52,7 @@ public class TestURIBuilder {
     @Test
     public void testMutationToRelativeUri() throws Exception {
         final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
-        final URIBuilder uribuilder = new URIBuilder(uri).setHost(null);
+        final URIBuilder uribuilder = new URIBuilder(uri).setHost((String) null);
         final URI result = uribuilder.build();
         Assert.assertEquals(new URI("http:///stuff?param=stuff#fragment"), result);
     }
@@ -211,6 +212,34 @@ public class TestURIBuilder {
     }
 
     @Test
+    public void testLocalhost() throws Exception {
+        // Check that the URI generated by URI builder agrees with that generated by using URI directly
+        final String scheme="https";
+        final InetAddress host=InetAddress.getLocalHost();
+        final String specials="/abcd!$&*()_-+.,=:;'~@[]?<>|#^%\"{}\\\u00a3`\u00ac\u00a6xyz"; // N.B. excludes space
+        final URI uri = new URI(scheme, specials, host.getHostAddress(), 80, specials, specials, specials);
+
+        final URI bld = URIBuilder.localhost()
+                .setScheme(scheme)
+                .setUserInfo(specials)
+                .setPath(specials)
+                .setCustomQuery(specials)
+                .setFragment(specials)
+                .build();
+
+        Assert.assertEquals(uri.getHost(), bld.getHost());
+
+        Assert.assertEquals(uri.getUserInfo(), bld.getUserInfo());
+
+        Assert.assertEquals(uri.getPath(), bld.getPath());
+
+        Assert.assertEquals(uri.getQuery(), bld.getQuery());
+
+        Assert.assertEquals(uri.getFragment(), bld.getFragment());
+
+    }
+
+    @Test
     public void testAgainstURIEncoded() throws Exception {
         // Check that the encoded URI generated by URI builder agrees with that generated by using URI directly
         final String scheme="https";