hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1769680 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/http/ main/java/org/apache/hc/core5/net/ test/java/org/apache/hc/core5/http/ test/java/org/apache/hc/core5/net/
Date Mon, 14 Nov 2016 19:11:02 GMT
Author: olegk
Date: Mon Nov 14 19:11:02 2016
New Revision: 1769680

URL: http://svn.apache.org/viewvc?rev=1769680&view=rev
Log:
URIHost class

Added:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java
  (with props)
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpHost.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java?rev=1769680&r1=1769679&r2=1769680&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java
Mon Nov 14 19:11:02 2016
@@ -34,6 +34,7 @@ import java.util.Locale;
 import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.net.NamedEndpoint;
+import org.apache.hc.core5.net.URIHost;
 import org.apache.hc.core5.util.Args;
 import org.apache.hc.core5.util.LangUtils;
 
@@ -118,7 +119,9 @@ public final class HttpHost implements N
      * @since 4.4
      */
     public static HttpHost create(final String s) {
-        Args.containsNoBlanks(s, "HTTP Host");
+        if (s == null) {
+            return null;
+        }
         String text = s;
         String scheme = null;
         final int schemeIdx = text.indexOf("://");
@@ -217,18 +220,17 @@ public final class HttpHost implements N
     }
 
     /**
-     * Copy constructor for {@link HttpHost HttpHost}.
-     *
-     * @param httphost the HTTP host to copy details from
+     * @since 5.0
      */
-    public HttpHost (final HttpHost httphost) {
-        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;
+    public HttpHost(final URIHost uriHost, final String scheme) {
+        this(Args.notNull(uriHost, "URI host").getHostName(), uriHost.getPort(), scheme);
+    }
+
+    /**
+     * @since 5.0
+     */
+    public HttpHost(final URIHost uriHost) {
+        this(uriHost, null);
     }
 
     /**

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java?rev=1769680&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java
Mon Nov 14 19:11:02 2016
@@ -0,0 +1,120 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.net;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+import org.apache.hc.core5.annotation.Contract;
+import org.apache.hc.core5.annotation.ThreadingBehavior;
+import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.LangUtils;
+
+/**
+ * Represents authority component of HTTP request URI.
+ *
+ * @since 5.0
+ */
+@Contract(threading = ThreadingBehavior.IMMUTABLE)
+public final class URIHost implements NamedEndpoint, Serializable {
+
+    private final String hostname;
+    private final int port;
+
+    public URIHost(final String hostname, final int port) {
+        super();
+        this.hostname = Args.containsNoBlanks(hostname, "Host name").toLowerCase(Locale.ROOT);
+        this.port = port;
+    }
+
+    /**
+     * Creates {@code URIHost} instance from string. Text may not contain any blanks.
+     */
+    public static URIHost create(final String s) {
+        if (s == null) {
+            return null;
+        }
+        String hostname = s;
+        int port = -1;
+        final int portIdx = hostname.lastIndexOf(":");
+        if (portIdx > 0) {
+            try {
+                port = Integer.parseInt(hostname.substring(portIdx + 1));
+            } catch (final NumberFormatException ex) {
+                throw new IllegalArgumentException("Invalid URI host: " + s);
+            }
+            hostname = hostname.substring(0, portIdx);
+        }
+        return new URIHost(hostname, port);
+    }
+
+    public URIHost(final String hostname) {
+        this(hostname, -1);
+    }
+
+    public String getHostName() {
+        return this.hostname;
+    }
+
+    public int getPort() {
+        return this.port;
+    }
+
+    @Override
+    public String toString() {
+        if (this.port != -1) {
+            final StringBuilder buffer = new StringBuilder();
+            buffer.append(hostname);
+            buffer.append(":");
+            buffer.append(Integer.toString(port));
+            return buffer.toString();
+        }
+        return hostname;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof URIHost) {
+            final URIHost that = (URIHost) obj;
+            return this.hostname.equals(that.hostname) && this.port == that.port;
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        int hash = LangUtils.HASH_SEED;
+        hash = LangUtils.hashCode(hash, this.hostname);
+        hash = LangUtils.hashCode(hash, this.port);
+        return hash;
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIHost.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpHost.java?rev=1769680&r1=1769679&r2=1769680&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpHost.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpHost.java
Mon Nov 14 19:11:02 2016
@@ -205,11 +205,6 @@ public class TestHttpHost {
     @Test
     public void testCreateFromStringInvalid() throws Exception {
         try {
-            HttpHost.create(null);
-            Assert.fail("IllegalArgumentException expected");
-        } catch (final IllegalArgumentException expected) {
-        }
-        try {
             HttpHost.create(" host ");
             Assert.fail("IllegalArgumentException expected");
         } catch (final IllegalArgumentException expected) {

Added: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java?rev=1769680&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java
Mon Nov 14 19:11:02 2016
@@ -0,0 +1,133 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.net;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link URIHost}.
+ *
+ */
+public class TestURIHost {
+
+    @Test
+    public void testConstructor() {
+        final URIHost host1 = new URIHost("somehost");
+        Assert.assertEquals("somehost", host1.getHostName());
+        Assert.assertEquals(-1, host1.getPort());
+        final URIHost host2 = new URIHost("somehost", 8080);
+        Assert.assertEquals("somehost", host2.getHostName());
+        Assert.assertEquals(8080, host2.getPort());
+        final URIHost host3 = new URIHost("somehost", -1);
+        Assert.assertEquals("somehost", host3.getHostName());
+        Assert.assertEquals(-1, host3.getPort());
+    }
+
+    @Test
+    public void testHashCode() throws Exception {
+        final URIHost host1 = new URIHost("somehost", 8080);
+        final URIHost host2 = new URIHost("somehost", 80);
+        final URIHost host3 = new URIHost("someotherhost", 8080);
+        final URIHost host4 = new URIHost("somehost", 80);
+        final URIHost host5 = new URIHost("SomeHost", 80);
+
+        Assert.assertTrue(host1.hashCode() == host1.hashCode());
+        Assert.assertTrue(host1.hashCode() != host2.hashCode());
+        Assert.assertTrue(host1.hashCode() != host3.hashCode());
+        Assert.assertTrue(host2.hashCode() == host4.hashCode());
+        Assert.assertTrue(host2.hashCode() == host5.hashCode());
+    }
+
+    @Test
+    public void testEquals() throws Exception {
+        final URIHost host1 = new URIHost("somehost", 8080);
+        final URIHost host2 = new URIHost("somehost", 80);
+        final URIHost host3 = new URIHost("someotherhost", 8080);
+        final URIHost host4 = new URIHost("somehost", 80);
+        final URIHost host5 = new URIHost("SomeHost", 80);
+
+        Assert.assertTrue(host1.equals(host1));
+        Assert.assertFalse(host1.equals(host2));
+        Assert.assertFalse(host1.equals(host3));
+        Assert.assertTrue(host2.equals(host4));
+        Assert.assertTrue(host2.equals(host5));
+    }
+
+    @Test
+    public void testToString() throws Exception {
+        final URIHost host1 = new URIHost("somehost");
+        Assert.assertEquals("somehost", host1.toString());
+        final URIHost host2 = new URIHost("somehost", -1);
+        Assert.assertEquals("somehost", host2.toString());
+        final URIHost host3 = new URIHost("somehost", 8888);
+        Assert.assertEquals("somehost:8888", host3.toString());
+    }
+
+    @Test
+    public void testSerialization() throws Exception {
+        final URIHost orig = new URIHost("somehost", 8080);
+        final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
+        final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
+        outstream.writeObject(orig);
+        outstream.close();
+        final byte[] raw = outbuffer.toByteArray();
+        final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
+        final ObjectInputStream instream = new ObjectInputStream(inbuffer);
+        final URIHost clone = (URIHost) instream.readObject();
+        Assert.assertEquals(orig, clone);
+    }
+
+    @Test
+    public void testCreateFromString() throws Exception {
+        Assert.assertEquals(new URIHost("somehost", 8080), URIHost.create("somehost:8080"));
+        Assert.assertEquals(new URIHost("somehost", 8080), URIHost.create("SomeHost:8080"));
+        Assert.assertEquals(new URIHost("somehost", 1234), URIHost.create("somehost:1234"));
+        Assert.assertEquals(new URIHost("somehost", -1), URIHost.create("somehost"));
+    }
+
+    @Test
+    public void testCreateFromStringInvalid() throws Exception {
+        try {
+            URIHost.create(" host ");
+            Assert.fail("IllegalArgumentException expected");
+        } catch (final IllegalArgumentException expected) {
+        }
+        try {
+            URIHost.create("host :8080");
+            Assert.fail("IllegalArgumentException expected");
+        } catch (final IllegalArgumentException expected) {
+        }
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIHost.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message