hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1599242 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/ main/java/org/apache/http/util/ test/java/org/apache/http/ test/java/org/apache/http/util/
Date Mon, 02 Jun 2014 15:34:16 GMT
Author: olegk
Date: Mon Jun  2 15:34:16 2014
New Revision: 1599242

URL: http://svn.apache.org/r1599242
Log:
HTTPCORE-365: added method to create HttpHost instances from String

Added:
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestTextUtils.java
      - copied, changed from r1599241, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Args.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.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=1599242&r1=1599241&r2=1599242&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 Mon
Jun  2 15:34:16 2014
@@ -66,8 +66,7 @@ public final class HttpHost implements C
     protected final InetAddress address;
 
     /**
-     * Creates a new {@link HttpHost HttpHost}, specifying all values.
-     * Constructor for HttpHost.
+     * Creates <tt>HttpHost<tt/> instance with the given scheme, hostname and
port.
      *
      * @param hostname  the hostname (IP or DNS name)
      * @param port      the port number.
@@ -78,7 +77,7 @@ public final class HttpHost implements C
      */
     public HttpHost(final String hostname, final int port, final String scheme) {
         super();
-        this.hostname   = Args.notBlank(hostname, "Host name");
+        this.hostname   = Args.containsNoBlanks(hostname, "Host name");
         this.lcHostname = hostname.toLowerCase(Locale.ROOT);
         if (scheme != null) {
             this.schemeName = scheme.toLowerCase(Locale.ROOT);
@@ -90,7 +89,7 @@ public final class HttpHost implements C
     }
 
     /**
-     * Creates a new {@link HttpHost HttpHost}, with default scheme.
+     * Creates <tt>HttpHost<tt/> instance with the default scheme and the given
hostname and port.
      *
      * @param hostname  the hostname (IP or DNS name)
      * @param port      the port number.
@@ -101,7 +100,34 @@ public final class HttpHost implements C
     }
 
     /**
-     * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
+     * Creates <tt>HttpHost<tt/> instance from string. Text may not contain any
blanks.
+     *
+     * @since 4.4
+     */
+    public static HttpHost create(final String s) {
+        Args.containsNoBlanks(s, "HTTP Host");
+        String text = s;
+        String scheme = null;
+        final int schemeIdx = text.indexOf("://");
+        if (schemeIdx > 0) {
+            scheme = text.substring(0, schemeIdx);
+            text = text.substring(schemeIdx + 3);
+        }
+        int port = -1;
+        final int portIdx = text.lastIndexOf(":");
+        if (portIdx > 0) {
+            try {
+                port = Integer.parseInt(text.substring(portIdx + 1));
+            } catch (NumberFormatException ex) {
+                throw new IllegalArgumentException("Invalid HTTP host: " + text);
+            }
+            text = text.substring(0, portIdx);
+        }
+        return new HttpHost(text, port, scheme);
+    }
+
+    /**
+     * Creates <tt>HttpHost<tt/> instance with the default scheme and port and
the given hostname.
      *
      * @param hostname  the hostname (IP or DNS name)
      */
@@ -110,8 +136,7 @@ public final class HttpHost implements C
     }
 
     /**
-     * Creates a new {@link HttpHost HttpHost}, specifying all values.
-     * Constructor for HttpHost.
+     * Creates <tt>HttpHost<tt/> instance with the given scheme, inet address
and port.
      *
      * @param address   the inet address.
      * @param port      the port number.
@@ -136,7 +161,8 @@ public final class HttpHost implements C
     }
 
     /**
-     * Creates a new {@link HttpHost HttpHost}, with default scheme.
+     * Creates <tt>HttpHost<tt/> instance with the default scheme and the given
inet address
+     * and port.
      *
      * @param address   the inet address.
      * @param port      the port number.
@@ -149,7 +175,8 @@ public final class HttpHost implements C
     }
 
     /**
-     * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
+     * Creates <tt>HttpHost<tt/> instance with the default scheme and port and
the given inet
+     * address.
      *
      * @param address   the inet address.
      *

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Args.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Args.java?rev=1599242&r1=1599241&r2=1599242&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Args.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Args.java Mon
Jun  2 15:34:16 2014
@@ -76,6 +76,16 @@ public class Args {
         return argument;
     }
 
+    public static <T extends CharSequence> T containsNoBlanks(final T argument, final
String name) {
+        if (argument == null) {
+            throw new IllegalArgumentException(name + " may not be null");
+        }
+        if (TextUtils.containsBlanks(argument)) {
+            throw new IllegalArgumentException(name + " may not contain blanks");
+        }
+        return argument;
+    }
+
     public static <E, T extends Collection<E>> T notEmpty(final T argument, final
String name) {
         if (argument == null) {
             throw new IllegalArgumentException(name + " may not be null");

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java?rev=1599242&r1=1599241&r2=1599242&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
Mon Jun  2 15:34:16 2014
@@ -51,4 +51,19 @@ public final class TextUtils {
         return true;
     }
 
+    /**
+     * @since 4.4
+     */
+    public static boolean containsBlanks(final CharSequence s) {
+        if (s == null) {
+            return false;
+        }
+        for (int i = 0; i < s.length(); i++) {
+            if (Character.isWhitespace(s.charAt(i))) {
+                return true;
+            }
+        }
+        return false;
+    }
+
 }

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=1599242&r1=1599241&r2=1599242&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
Mon Jun  2 15:34:16 2014
@@ -173,4 +173,31 @@ public class TestHttpHost {
         Assert.assertEquals(orig, clone);
     }
 
+    @Test
+    public void testCreateFromString() throws Exception {
+        Assert.assertEquals(new HttpHost("somehost", 8080, "https"), HttpHost.create("https://somehost:8080"));
+        Assert.assertEquals(new HttpHost("somehost", 8080, "https"), HttpHost.create("HttpS://SomeHost:8080"));
+        Assert.assertEquals(new HttpHost("somehost", 1234, null), HttpHost.create("somehost:1234"));
+        Assert.assertEquals(new HttpHost("somehost", -1, null), HttpHost.create("somehost"));
+    }
+
+    @Test
+    public void testCreateFromStringInvalid() throws Exception {
+        try {
+            HttpHost.create(null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpHost.create(" host ");
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpHost.create("host :8080");
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
 }

Copied: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestTextUtils.java
(from r1599241, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestTextUtils.java?p2=httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestTextUtils.java&p1=httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java&r1=1599241&r2=1599242&rev=1599242&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestTextUtils.java
Mon Jun  2 15:34:16 2014
@@ -27,28 +27,38 @@
 
 package org.apache.http.util;
 
+import org.junit.Assert;
+import org.junit.Test;
+
 /**
- * @since 4.3
+ * Unit tests for {@link org.apache.http.util.TextUtils}.
+ *
  */
-public final class TextUtils {
+public class TestTextUtils {
+
+    @Test
+    public void testTextEmpty() {
+        Assert.assertTrue(TextUtils.isEmpty(null));
+        Assert.assertTrue(TextUtils.isEmpty(""));
+        Assert.assertFalse(TextUtils.isEmpty("\t"));
+    }
 
-    public static boolean isEmpty(final CharSequence s) {
-        if (s == null) {
-            return true;
-        }
-        return s.length() == 0;
+    @Test
+    public void testTextBlank() {
+        Assert.assertTrue(TextUtils.isBlank(null));
+        Assert.assertTrue(TextUtils.isBlank(""));
+        Assert.assertTrue(TextUtils.isBlank("   "));
+        Assert.assertTrue(TextUtils.isBlank("\t"));
     }
 
-    public static boolean isBlank(final CharSequence s) {
-        if (s == null) {
-            return true;
-        }
-        for (int i = 0; i < s.length(); i++) {
-            if (!Character.isWhitespace(s.charAt(i))) {
-                return false;
-            }
-        }
-        return true;
+    @Test
+    public void testTextContainsBlanks() {
+        Assert.assertFalse(TextUtils.containsBlanks(null));
+        Assert.assertFalse(TextUtils.containsBlanks(""));
+        Assert.assertTrue(TextUtils.containsBlanks("   "));
+        Assert.assertTrue(TextUtils.containsBlanks("\t"));
+        Assert.assertTrue(TextUtils.containsBlanks(" a"));
+        Assert.assertFalse(TextUtils.containsBlanks("a"));
     }
 
 }



Mime
View raw message