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 2017/05/24 08:06:53 UTC
[6/9] httpcomponents-core git commit: [HTTPCLIENT-1852] Add APIs
URIBuilder.localhost() loopbackAddress() and setHost(InetAddress).
[HTTPCLIENT-1852] Add APIs URIBuilder.localhost() loopbackAddress() 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/47c02ac6
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/47c02ac6
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/47c02ac6
Branch: refs/heads/master
Commit: 47c02ac661dbf15d29ca66ee5019fae920d61965
Parents: 4195288
Author: Gary Gregory <gg...@apache.org>
Authored: Fri May 19 19:40:10 2017 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Fri May 19 19:40:10 2017 -0700
----------------------------------------------------------------------
.../org/apache/hc/core5/net/URIBuilder.java | 32 +++++++++++
.../org/apache/hc/core5/net/TestURIBuilder.java | 57 +++++++++++++++++++-
2 files changed, 88 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/47c02ac6/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/47c02ac6/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();