Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 83AB5D8B8 for ; Thu, 20 Sep 2012 19:04:46 +0000 (UTC) Received: (qmail 33584 invoked by uid 500); 20 Sep 2012 19:04:46 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 33529 invoked by uid 500); 20 Sep 2012 19:04:46 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 33517 invoked by uid 99); 20 Sep 2012 19:04:46 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Sep 2012 19:04:46 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Sep 2012 19:04:43 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0CB40238899C for ; Thu, 20 Sep 2012 19:04:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120920190401.0CB40238899C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 + * [HTTPCORE-312] NIO length delimited content encoder incorrectly handles messages larger than 2GB. Contributed by Oleg Kalnichevski 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 * Content-Type 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 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 > 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"); } - + }