hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1388164 - in /httpcomponents/httpcore/trunk: ./ httpcore/src/main/java/org/apache/http/entity/ httpcore/src/main/java/org/apache/http/util/ httpcore/src/test/java/org/apache/http/util/
Date Thu, 20 Sep 2012 19:04:00 GMT
Author: olegk
Date: Thu Sep 20 19:04:00 2012
New Revision: 1388164

URL: http://svn.apache.org/viewvc?rev=1388164&view=rev
Log:
HTTPCORE-313: ContentType#parse to ignore empty and blank charset attributes; HttpEntityUtils#toString
to throw checked I/O exception if it encounters an unsupported charset

Added:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
      - copied, changed from r1387655, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestAsserts.java
      - copied, changed from r1387655, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java
Modified:
    httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.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/Asserts.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestArgs.java

Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=1388164&r1=1388163&r2=1388164&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Thu Sep 20 19:04:00 2012
@@ -1,5 +1,9 @@
 Changes since 4.2.1
 
+* [HTTPCORE-313] ContentType#parse now ignores empty and blank charset attributes. 
+  HttpEntityUtils#toString now throws checked I/O exception ifit  encounters an unsupported
charset. 
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * [HTTPCORE-312] NIO length delimited content encoder incorrectly handles messages larger
than 2GB.
   Contributed by Oleg Kalnichevski <olegk at apache.org>
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java?rev=1388164&r1=1388163&r2=1388164&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
Thu Sep 20 19:04:00 2012
@@ -41,6 +41,7 @@ import org.apache.http.ParseException;
 import org.apache.http.annotation.Immutable;
 import org.apache.http.message.BasicHeaderValueParser;
 import org.apache.http.util.Args;
+import org.apache.http.util.TextUtils;
 
 /**
  * Content type information consisting of a MIME type and an optional charset.
@@ -143,7 +144,7 @@ public final class ContentType implement
      * @return content type
      */
     public static ContentType create(final String mimeType, final Charset charset) {
-        String type = Args.notEmpty(mimeType, "MIME type").trim().toLowerCase(Locale.US);
+        String type = Args.notBlank(mimeType, "MIME type").toLowerCase(Locale.US);
         Args.check(valid(type), "MIME type may not contain reserved characters");
         return new ContentType(type, charset);
     }
@@ -170,7 +171,7 @@ public final class ContentType implement
      */
     public static ContentType create(
             final String mimeType, final String charset) throws UnsupportedCharsetException
{
-        return create(mimeType, charset != null ? Charset.forName(charset) : null);
+        return create(mimeType, !TextUtils.isBlank(charset) ? Charset.forName(charset) :
null);
     }
 
     private static ContentType create(final HeaderElement helem) {
@@ -236,7 +237,8 @@ public final class ContentType implement
      * @throws ParseException if the given text does not represent a valid
      * <code>Content-Type</code> value.
      */
-    public static ContentType getOrDefault(final HttpEntity entity) throws ParseException
{
+    public static ContentType getOrDefault(
+            final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
         ContentType contentType = get(entity);
         return contentType != null ? contentType : DEFAULT_TEXT;
     }

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=1388164&r1=1388163&r2=1388164&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 Thu
Sep 20 19:04:00 2012
@@ -48,12 +48,20 @@ public class Args {
         if (argument == null) {
             throw new IllegalArgumentException(name + " may not be null");
         }
-        for (int i = 0; i < argument.length(); i++) {
-            if (!Character.isWhitespace(argument.charAt(i))) {
-                return argument;
-            }
+        if (TextUtils.isEmpty(argument)) {
+            throw new IllegalArgumentException(name + " may not be empty");
         }
-        throw new IllegalArgumentException(name + " may not be empty");
+        return argument;
+    }    
+    
+    public static <T extends CharSequence> T notBlank(final T argument, final String
name) {
+        if (argument == null) {
+            throw new IllegalArgumentException(name + " may not be null");
+        }
+        if (TextUtils.isBlank(argument)) {
+            throw new IllegalArgumentException(name + " may not be blank");
+        }
+        return argument;
     }    
     
     public static <E, T extends Collection<E>> T notEmpty(final T argument, final
String name) {

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java?rev=1388164&r1=1388163&r2=1388164&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java
Thu Sep 20 19:04:00 2012
@@ -41,4 +41,16 @@ public class Asserts {
         }
     }
     
+    public static void notEmpty(final CharSequence s, final String name) {
+        if (TextUtils.isEmpty(s)) {
+            throw new IllegalStateException(name + " is empty");
+        }
+    }
+
+    public static void notBlank(final CharSequence s, final String name) {
+        if (TextUtils.isBlank(s)) {
+            throw new IllegalStateException(name + " is blank");
+        }
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java?rev=1388164&r1=1388163&r2=1388164&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
Thu Sep 20 19:04:00 2012
@@ -31,7 +31,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
 
 import org.apache.http.HeaderElement;
 import org.apache.http.HttpEntity;
@@ -218,8 +220,13 @@ public final class EntityUtils {
             if (i < 0) {
                 i = 4096;
             }
-            ContentType contentType = ContentType.getOrDefault(entity);
-            Charset charset = contentType.getCharset();
+            Charset charset = null;
+            try {
+                ContentType contentType = ContentType.getOrDefault(entity);
+                charset = contentType.getCharset();
+            } catch (UnsupportedCharsetException ex) {
+                throw new UnsupportedEncodingException(ex.getMessage());
+            }
             if (charset == null) {
                 charset = defaultCharset;
             }

Copied: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
(from r1387655, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java?p2=httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java&p1=httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java&r1=1387655&r2=1388164&rev=1388164&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/TextUtils.java
Thu Sep 20 19:04:00 2012
@@ -27,18 +27,28 @@
 
 package org.apache.http.util;
 
-public class Asserts {
+/**
+ * @since 4.3
+ */
+public final class TextUtils {
 
-    public static void check(boolean expression, final String message) {
-        if (!expression) {
-            throw new IllegalStateException(message);
+    public static boolean isEmpty(final CharSequence s) {
+        if (s == null) {
+            return true;
         }
+        return s.length() == 0;
     }
-    
-    public static void notNull(final Object object, final String name) {
-        if (object == null) {
-            throw new IllegalStateException(name + " is null");
+
+    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;
     }
-    
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestArgs.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestArgs.java?rev=1388164&r1=1388163&r2=1388164&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestArgs.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestArgs.java
Thu Sep 20 19:04:00 2012
@@ -78,8 +78,18 @@ public class TestArgs {
     }
 
     @Test(expected=IllegalArgumentException.class)
-    public void testArgNotEmptyFail3() {
-        Args.notEmpty(" \t \n\r", "Stuff");
+    public void testArgNotBlankFail1() {
+        Args.notBlank((String) null, "Stuff");
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testArgNotBlankFail2() {
+        Args.notBlank("", "Stuff");
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testArgNotBlankFail3() {
+        Args.notBlank(" \t \n\r", "Stuff");
     }
 
     @Test

Copied: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestAsserts.java
(from r1387655, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestAsserts.java?p2=httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestAsserts.java&p1=httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java&r1=1387655&r2=1388164&rev=1388164&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/Asserts.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestAsserts.java
Thu Sep 20 19:04:00 2012
@@ -27,18 +27,51 @@
 
 package org.apache.http.util;
 
-public class Asserts {
+import org.junit.Test;
 
-    public static void check(boolean expression, final String message) {
-        if (!expression) {
-            throw new IllegalStateException(message);
-        }
-    }
-    
-    public static void notNull(final Object object, final String name) {
-        if (object == null) {
-            throw new IllegalStateException(name + " is null");
-        }
+/**
+ * Unit tests for {@link Asserts}.
+ */
+public class TestAsserts {
+
+    @Test
+    public void testExpressionCheckPass() {
+        Asserts.check(true, "All is well");
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testExpressionCheckFail() {
+        Asserts.check(false, "Oopsie");
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testExpressionNotNullFail() {
+        Asserts.notNull(null, "Stuff");
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testExpressionNotEmptyFail1() {
+        Asserts.notEmpty((String) null, "Stuff");
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testExpressionNotEmptyFail2() {
+        Asserts.notEmpty("", "Stuff");
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testExpressionNotEmptyBlank1() {
+        Asserts.notBlank((String) null, "Stuff");
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testExpressionNotEmptyBlank2() {
+        Asserts.notBlank("", "Stuff");
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testExpressionNotBlankFail3() {
+        Asserts.notBlank(" \t \n\r", "Stuff");
     }
-    
+
 }



Mime
View raw message