hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject httpcomponents-client git commit: [HTTPCLIENT-1852] Add APIs URIBuilder.localhost() and setHost(InetAddress) [Forced Update!]
Date Wed, 24 May 2017 08:12:53 GMT
Repository: httpcomponents-client
Updated Branches:
  refs/heads/4.6.x 2671a24a6 -> cb5a49b4b (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/cb5a49b4
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/cb5a49b4
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/cb5a49b4

Branch: refs/heads/4.6.x
Commit: cb5a49b4b5f529d3cfec8876bda220d61151a0e7
Parents: 9aff64d
Author: Gary Gregory <ggregory@apache.org>
Authored: Fri May 19 19:20:11 2017 -0700
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Wed May 24 10:06:24 2017 +0200

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


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/cb5a49b4/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 8b4a804..c42c3a1 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -39,6 +39,9 @@ Change log:
 * [HTTPCLIENT-1828] HttpDelete, HttpGet, HttpHead do not extend HttpEntityEnclosingRequestBase.
   Contributed by Saravanakumar Selvaraj <saravanakumar dot java at gmail dot com> and
Gary Gregory <ggregory at apache.org>
 
+* [HTTPCLIENT-1852] Add APIs URIBuilder.localhost() and setHost(InetAddress)
+  Contributed by Gary Gregory <ggregory at apache.org>
+
 
 Release 4.5.3
 -------------------

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/cb5a49b4/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/cb5a49b4/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/cb5a49b4/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";


Mime
View raw message