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 2013/06/07 14:18:51 UTC

svn commit: r1490607 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/HttpHost.java test/java/org/apache/http/TestHttpHost.java

Author: olegk
Date: Fri Jun  7 12:18:50 2013
New Revision: 1490607

URL: http://svn.apache.org/r1490607
Log:
HTTPCORE-344: added HttpHost constructors that take InetAddress as a parameter instead of plain String

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java?rev=1490607&r1=1490606&r2=1490607&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java Fri Jun  7 12:18:50 2013
@@ -28,6 +28,7 @@
 package org.apache.http;
 
 import java.io.Serializable;
+import java.net.InetAddress;
 import java.util.Locale;
 
 import org.apache.http.annotation.Immutable;
@@ -62,6 +63,7 @@ public final class HttpHost implements C
     /** The scheme (lowercased) */
     protected final String schemeName;
 
+    protected final InetAddress address;
 
     /**
      * Creates a new {@link HttpHost HttpHost}, specifying all values.
@@ -84,6 +86,7 @@ public final class HttpHost implements C
             this.schemeName = DEFAULT_SCHEME_NAME;
         }
         this.port = port;
+        this.address = null;
     }
 
     /**
@@ -107,12 +110,68 @@ public final class HttpHost implements C
     }
 
     /**
+     * Creates a new {@link HttpHost HttpHost}, specifying all values.
+     * Constructor for HttpHost.
+     *
+     * @param address   the inet address.
+     * @param port      the port number.
+     *                  <code>-1</code> indicates the scheme default port.
+     * @param scheme    the name of the scheme.
+     *                  <code>null</code> indicates the
+     *                  {@link #DEFAULT_SCHEME_NAME default scheme}
+     *
+     * @since 4.3
+     */
+    public HttpHost(final InetAddress address, final int port, final String scheme) {
+        super();
+        this.address = Args.notNull(address, "Inet address");
+        this.hostname = address.getHostAddress();
+        this.lcHostname = this.hostname.toLowerCase(Locale.ENGLISH);
+        if (scheme != null) {
+            this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
+        } else {
+            this.schemeName = DEFAULT_SCHEME_NAME;
+        }
+        this.port = port;
+    }
+
+    /**
+     * Creates a new {@link HttpHost HttpHost}, with default scheme.
+     *
+     * @param address   the inet address.
+     * @param port      the port number.
+     *                  <code>-1</code> indicates the scheme default port.
+     *
+     * @since 4.3
+     */
+    public HttpHost(final InetAddress address, final int port) {
+        this(address, port, null);
+    }
+
+    /**
+     * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
+     *
+     * @param address   the inet address.
+     *
+     * @since 4.3
+     */
+    public HttpHost(final InetAddress address) {
+        this(address, -1, null);
+    }
+
+    /**
      * Copy constructor for {@link HttpHost HttpHost}.
      *
      * @param httphost the HTTP host to copy details from
      */
     public HttpHost (final HttpHost httphost) {
-        this(httphost.hostname, httphost.port, httphost.schemeName);
+        super();
+        Args.notNull(httphost, "HTTP host");
+        this.hostname   = httphost.hostname;
+        this.lcHostname = httphost.lcHostname;
+        this.schemeName = httphost.schemeName;
+        this.port = httphost.port;
+        this.address = httphost.address;
     }
 
     /**
@@ -143,6 +202,17 @@ public final class HttpHost implements C
     }
 
     /**
+     * Returns the inet address if explicitly set by a constructor,
+     *   <code>null</code> otherwise.
+     * @return the inet address
+     *
+     * @since 4.3
+     */
+    public InetAddress getAddress() {
+        return this.address;
+    }
+
+    /**
      * Return the host URI, as a string.
      *
      * @return the host URI

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java?rev=1490607&r1=1490606&r2=1490607&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java Fri Jun  7 12:18:50 2013
@@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.net.InetAddress;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -60,21 +61,28 @@ public class TestHttpHost {
         Assert.assertEquals(443, host4.getPort());
         Assert.assertEquals("https", host4.getSchemeName());
         try {
-            new HttpHost(null, -1, null);
+            new HttpHost((String) null, -1, null);
             Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (final IllegalArgumentException ex) {
-            //expected
+        } catch (final IllegalArgumentException expected) {
+        }
+        try {
+            new HttpHost((InetAddress) null, -1, null);
+            Assert.fail("IllegalArgumentException should have been thrown");
+        } catch (final IllegalArgumentException expected) {
         }
     }
 
     @Test
-    public void testHashCode() {
+    public void testHashCode() throws Exception {
         final HttpHost host1 = new HttpHost("somehost", 8080, "http");
         final HttpHost host2 = new HttpHost("somehost", 80, "http");
         final HttpHost host3 = new HttpHost("someotherhost", 8080, "http");
         final HttpHost host4 = new HttpHost("somehost", 80, "http");
         final HttpHost host5 = new HttpHost("SomeHost", 80, "http");
         final HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
+        final HttpHost host7 = new HttpHost(
+                InetAddress.getByAddress(new byte[] {127,0,0,1}), 80, "http");
+        final HttpHost host8 = new HttpHost("127.0.0.1", 80, "http");
 
         Assert.assertTrue(host1.hashCode() == host1.hashCode());
         Assert.assertTrue(host1.hashCode() != host2.hashCode());
@@ -82,16 +90,20 @@ public class TestHttpHost {
         Assert.assertTrue(host2.hashCode() == host4.hashCode());
         Assert.assertTrue(host2.hashCode() == host5.hashCode());
         Assert.assertTrue(host5.hashCode() != host6.hashCode());
+        Assert.assertTrue(host7.hashCode() == host8.hashCode());
     }
 
     @Test
-    public void testEquals() {
+    public void testEquals() throws Exception {
         final HttpHost host1 = new HttpHost("somehost", 8080, "http");
         final HttpHost host2 = new HttpHost("somehost", 80, "http");
         final HttpHost host3 = new HttpHost("someotherhost", 8080, "http");
         final HttpHost host4 = new HttpHost("somehost", 80, "http");
         final HttpHost host5 = new HttpHost("SomeHost", 80, "http");
         final HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
+        final HttpHost host7 = new HttpHost(
+                InetAddress.getByAddress(new byte[] {127,0,0,1}), 80, "http");
+        final HttpHost host8 = new HttpHost("127.0.0.1", 80, "http");
 
         Assert.assertTrue(host1.equals(host1));
         Assert.assertFalse(host1.equals(host2));
@@ -99,12 +111,13 @@ public class TestHttpHost {
         Assert.assertTrue(host2.equals(host4));
         Assert.assertTrue(host2.equals(host5));
         Assert.assertFalse(host5.equals(host6));
+        Assert.assertTrue(host7.equals(host8));
         Assert.assertFalse(host1.equals(null));
         Assert.assertFalse(host1.equals("http://somehost"));
     }
 
     @Test
-    public void testToString() {
+    public void testToString() throws Exception {
         final HttpHost host1 = new HttpHost("somehost");
         Assert.assertEquals("http://somehost", host1.toString());
         final HttpHost host2 = new HttpHost("somehost", -1);
@@ -117,6 +130,9 @@ public class TestHttpHost {
         Assert.assertEquals("myhttp://somehost", host5.toString());
         final HttpHost host6 = new HttpHost("somehost", 80, "myhttp");
         Assert.assertEquals("myhttp://somehost:80", host6.toString());
+        final HttpHost host7 = new HttpHost(
+                InetAddress.getByAddress(new byte[] {127,0,0,1}), 80, "http");
+        Assert.assertEquals("http://127.0.0.1:80", host7.toString());
     }
 
     @Test