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/24 08:28:39 UTC

[3/3] httpcomponents-core git commit: HTTPCORE-471: Add APIs URIBuilder.localhost() and setHost(InetAddress)

HTTPCORE-471: Add APIs URIBuilder.localhost() and setHost(InetAddress)


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

Branch: refs/heads/master
Commit: 7d518b5f6e2108329e61061d169f4737c7d8dc1d
Parents: bf8d50f
Author: Gary Gregory <gg...@apache.org>
Authored: Fri May 19 19:40:10 2017 -0700
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Wed May 24 10:25:46 2017 +0200

----------------------------------------------------------------------
 RELEASE_NOTES.txt                               |  4 ++
 .../org/apache/hc/core5/net/URIBuilder.java     | 32 +++++++++++
 .../org/apache/hc/core5/net/TestURIBuilder.java | 57 +++++++++++++++++++-
 3 files changed, 92 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/7d518b5f/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 6ea30f9..06cab72 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -5,6 +5,10 @@ Changelog
 -------------------
 
 * HTTPCORE-468: Allow HttpAsyncService subclasses to customize the HTTP status code.
+  Contributed by Gary Gregory <ggregory at apache.org>
+
+* HTTPCORE-471: Add APIs URIBuilder.localhost() and setHost(InetAddress)
+  Contributed by Gary Gregory <ggregory at apache.org>
 
 
 Release 5.0-ALPHA3

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/7d518b5f/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 fa103dd..19dce56 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
@@ -26,8 +26,10 @@
  */
 package org.apache.hc.core5.net;
 
+import java.net.InetAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.UnknownHostException;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -45,6 +47,24 @@ import org.apache.hc.core5.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());
+    }
+
+    /**
+     * Creates a new builder for the host {@link InetAddress#getLoopbackAddress()}.
+     *
+     * @since 5.0
+     */
+    public static URIBuilder loopbackAddress() {
+        return new URIBuilder().setHost(InetAddress.getLoopbackAddress());
+    }
+
     private String scheme;
     private String encodedSchemeSpecificPart;
     private String encodedAuthority;
@@ -247,6 +267,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-core/blob/7d518b5f/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 b1fa091..2929f55 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
@@ -26,6 +26,7 @@
  */
 package org.apache.hc.core5.net;
 
+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);
     }
@@ -99,6 +100,60 @@ 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 testLoopbackAddress() 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.getLoopbackAddress();
+       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.loopbackAddress()
+               .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 testEmpty() throws Exception {
         final URIBuilder uribuilder = new URIBuilder();
         final URI result = uribuilder.build();