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 BC1806E97 for ; Thu, 21 Jul 2011 19:18:31 +0000 (UTC) Received: (qmail 3444 invoked by uid 500); 21 Jul 2011 19:18:31 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 3401 invoked by uid 500); 21 Jul 2011 19:18:30 -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 3393 invoked by uid 99); 21 Jul 2011 19:18:30 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Jul 2011 19:18:30 +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, 21 Jul 2011 19:18:24 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id D42C823889BB for ; Thu, 21 Jul 2011 19:18:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1149326 - in /httpcomponents/httpcore/trunk: httpcore-nio/src/main/java/org/apache/http/nio/entity/ httpcore-nio/src/test/java/org/apache/http/nio/protocol/ httpcore/src/main/java/org/apache/http/entity/ httpcore/src/main/java/org/apache/h... Date: Thu, 21 Jul 2011 19:17:59 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110721191801.D42C823889BB@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Thu Jul 21 19:17:52 2011 New Revision: 1149326 URL: http://svn.apache.org/viewvc?rev=1149326&view=rev Log: Improved support for common operations involving Content-Type Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java (with props) httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java (with props) Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NStringEntity.java httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestThrottlingNHttpHandlers.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/FileEntity.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestFileEntity.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NFileEntity.java Thu Jul 21 19:17:52 2011 @@ -36,6 +36,7 @@ import java.nio.channels.FileChannel; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.entity.AbstractHttpEntity; +import org.apache.http.entity.ContentType; import org.apache.http.nio.ContentEncoder; import org.apache.http.nio.ContentEncoderChannel; import org.apache.http.nio.FileContentEncoder; @@ -67,7 +68,37 @@ public class NFileEntity extends Abstrac * @param contentType the content type of the file. * @param useFileChannels flag whether the direct transfer from the file * channel should be attempted. + * + * @since 4.2 */ + public NFileEntity(final File file, final ContentType contentType, boolean useFileChannels) { + if (file == null) { + throw new IllegalArgumentException("File may not be null"); + } + this.file = file; + this.useFileChannels = useFileChannels; + if (contentType != null) { + setContentType(contentType.toString()); + } + } + + /** + * Creates new instance of NFileEntity from the given source {@link File} + * with the given content type. + * + * @param file the source file. + * @param contentType the content type of the file. + * + * @since 4.2 + */ + public NFileEntity(final File file, final ContentType contentType) { + this(file, contentType, true); + } + + /** + * @deprecated use {@link #NFileEntity(File, ContentType, boolean)} + */ + @Deprecated public NFileEntity(final File file, final String contentType, boolean useFileChannels) { if (file == null) { throw new IllegalArgumentException("File may not be null"); @@ -77,6 +108,10 @@ public class NFileEntity extends Abstrac setContentType(contentType); } + /** + * @deprecated use {@link #NFileEntity(File, ContentType)} + */ + @Deprecated public NFileEntity(final File file, final String contentType) { this(file, contentType, true); } Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NStringEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NStringEntity.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NStringEntity.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/entity/NStringEntity.java Thu Jul 21 19:17:52 2011 @@ -36,6 +36,7 @@ import java.nio.ByteBuffer; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.entity.AbstractHttpEntity; +import org.apache.http.entity.ContentType; import org.apache.http.nio.ContentEncoder; import org.apache.http.nio.IOControl; import org.apache.http.nio.protocol.AsyncNHttpServiceHandler; @@ -55,21 +56,32 @@ public class NStringEntity extends Abstr protected final byte[] content; protected final ByteBuffer buffer; - public NStringEntity(final String s, String charset) + /** + * @since 4.2 + */ + public NStringEntity(final String s, final ContentType contentType) throws UnsupportedEncodingException { if (s == null) { throw new IllegalArgumentException("Source string may not be null"); } + String charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = HTTP.DEFAULT_CONTENT_CHARSET; } this.content = s.getBytes(charset); this.buffer = ByteBuffer.wrap(this.content); - setContentType(HTTP.PLAIN_TEXT_TYPE + HTTP.CHARSET_PARAM + charset); + if (contentType != null) { + setContentType(contentType.toString()); + } + } + + public NStringEntity(final String s, final String charset) + throws UnsupportedEncodingException { + this(s, ContentType.create(HTTP.PLAIN_TEXT_TYPE, charset)); } public NStringEntity(final String s) throws UnsupportedEncodingException { - this(s, null); + this(s, ContentType.DEFAULT_TEXT); } public boolean isRepeatable() { Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestThrottlingNHttpHandlers.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestThrottlingNHttpHandlers.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestThrottlingNHttpHandlers.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestThrottlingNHttpHandlers.java Thu Jul 21 19:17:52 2011 @@ -55,6 +55,7 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; +import org.apache.http.entity.ContentType; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.DefaultConnectionReuseStrategy; @@ -68,6 +69,7 @@ import org.apache.http.nio.reactor.Liste import org.apache.http.nio.reactor.SessionRequest; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.CoreProtocolPNames; +import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpExpectationVerifier; import org.apache.http.protocol.HttpProcessor; @@ -85,7 +87,6 @@ import org.apache.http.protocol.Response import org.apache.http.testserver.SimpleEventListener; import org.apache.http.testserver.SimpleHttpRequestHandlerResolver; import org.apache.http.util.EncodingUtils; -import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -892,8 +893,12 @@ public class TestThrottlingNHttpHandlers Thread.sleep(1); outstream.write(tmp, 0, l); } - content = new String(outstream.toByteArray(), - EntityUtils.getContentCharSet(entity)); + ContentType contentType = ContentType.getOrDefault(entity); + String charset = contentType.getCharset(); + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } + content = new String(outstream.toByteArray(), charset); } catch (InterruptedException ex) { content = "Interrupted: " + ex.getMessage(); } catch (IOException ex) { Added: 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=1149326&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java (added) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java Thu Jul 21 19:17:52 2011 @@ -0,0 +1,194 @@ +/* + * ==================================================================== + * 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 + * . + * + */ + +package org.apache.http.entity; + +import java.util.Locale; + +import org.apache.http.Header; +import org.apache.http.HeaderElement; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.ParseException; +import org.apache.http.annotation.Immutable; +import org.apache.http.message.BasicHeaderValueParser; +import org.apache.http.protocol.HTTP; + +/** + * Content type information consisting of a MIME type and an optional charset. + *

+ * This class makes no attempts to verify validity of mime-type and charset attributes. + * The input parameters of the {@link #create(String, String)} method, however, may not + * contain characters <">, <;>, <,> reserved by the HTTP specification. + * + * @since 4.2 + */ +@Immutable +public final class ContentType { + + public static final ContentType DEFAULT_TEXT = new ContentType( + HTTP.PLAIN_TEXT_TYPE, HTTP.DEFAULT_CONTENT_CHARSET); + public static final ContentType DEFAULT_BINARY = new ContentType( + "application/octet-stream", null); + + private final String mimeType; + private final String charset; + + ContentType(final String mimeType, final String charset) { + super(); + this.mimeType = mimeType; + this.charset = charset; + } + + public String getMimeType() { + return this.mimeType; + } + + public String getCharset() { + return this.charset; + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append(this.mimeType); + if (this.charset != null) { + buf.append("; charset="); + buf.append(this.charset); + } + return buf.toString(); + } + + private static boolean valid(final String s) { + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + if (ch == '"' || ch == ',' || ch == ';') { + return false; + } + } + return true; + } + + /** + * Creates a new instance of {@link ContentType}. + * + * @param mimeType MIME type. It may not be null or empty. It may not contain + * characters <">, <;>, <,> reserved by the HTTP specification. + * @param charset charset. It may not contain characters <">, <;>, <,> reserved by the HTTP + * specification. This parameter is optional. + * @return content type + */ + public static ContentType create(final String mimeType, final String charset) { + if (mimeType == null) { + throw new IllegalArgumentException("MIME type may not be null"); + } + String type = mimeType.trim().toLowerCase(Locale.US); + if (type.length() == 0) { + throw new IllegalArgumentException("MIME type may not be empty"); + } + if (!valid(type)) { + throw new IllegalArgumentException("MIME type may not contain reserved characters"); + } + String cs = null; + if (charset != null) { + cs = charset.trim().toLowerCase(Locale.US); + if (!valid(cs)) { + throw new IllegalArgumentException("Charset may not contain reserved characters"); + } + } + return new ContentType(type, cs); + } + + private static ContentType create(final HeaderElement helem) { + String mimeType = helem.getName(); + String charset = null; + NameValuePair param = helem.getParameterByName("charset"); + if (param != null) { + charset = param.getValue(); + } + return create(mimeType, charset); + } + + /** + * Parses textual representation of Content-Type value. + * + * @param s text + * @return content type + * @throws ParseException if the given text does not represent a valid + * Content-Type value. + */ + public static ContentType parse(final String s) throws ParseException { + if (s == null) { + throw new IllegalArgumentException("Content type may not be null"); + } + HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null); + if (elements.length > 0) { + return create(elements[0]); + } else { + throw new ParseException("Invalid content type: " + s); + } + } + + /** + * Extracts Content-Type value from {@link HttpEntity} exactly as + * specified by the Content-Type header of the entity. Returns null + * if not specified. + * + * @param entity HTTP entity + * @return content type + * @throws ParseException if the given text does not represent a valid + * Content-Type value. + */ + public static ContentType get(final HttpEntity entity) throws ParseException { + if (entity == null) { + return null; + } + Header header = entity.getContentType(); + if (header != null) { + HeaderElement[] elements = header.getElements(); + if (elements.length > 0) { + return create(elements[0]); + } + } + return null; + } + + /** + * Extracts Content-Type value from {@link HttpEntity} or returns default value + * if not explicitly specified. + * + * @param entity HTTP entity + * @return content type + * @throws ParseException if the given text does not represent a valid + * Content-Type value. + */ + public static ContentType getOrDefault(final HttpEntity entity) throws ParseException { + ContentType contentType = get(entity); + return contentType != null ? contentType : DEFAULT_TEXT; + } + +} Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/FileEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/FileEntity.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/FileEntity.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/FileEntity.java Thu Jul 21 19:17:52 2011 @@ -45,6 +45,10 @@ public class FileEntity extends Abstract protected final File file; + /** + * @deprecated {@link #FileEntity(File, ContentType)} + */ + @Deprecated public FileEntity(final File file, final String contentType) { super(); if (file == null) { @@ -54,6 +58,20 @@ public class FileEntity extends Abstract setContentType(contentType); } + /** + * @since 4.2 + */ + public FileEntity(final File file, final ContentType contentType) { + super(); + if (file == null) { + throw new IllegalArgumentException("File may not be null"); + } + this.file = file; + if (contentType != null) { + setContentType(contentType.toString()); + } + } + public boolean isRepeatable() { return true; } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java Thu Jul 21 19:17:52 2011 @@ -48,12 +48,22 @@ public class InputStreamEntity extends A private final long length; public InputStreamEntity(final InputStream instream, long length) { + this(instream, length, null); + } + + /** + * @since 4.2 + */ + public InputStreamEntity(final InputStream instream, long length, final ContentType contentType) { super(); if (instream == null) { throw new IllegalArgumentException("Source input stream may not be null"); } this.content = instream; this.length = length; + if (contentType != null) { + setContentType(contentType.toString()); + } } public boolean isRepeatable() { Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java Thu Jul 21 19:17:52 2011 @@ -48,6 +48,34 @@ public class StringEntity extends Abstra protected final byte[] content; /** + * Creates a StringEntity with the specified content and content type. + * + * @param string content to be used. Not {@code null}. + * @param contentType content type to be used. May be {@code null}, in which case the default + * MIME type {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain" and the default charset + * {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1" are assumed. + * + * @since 4.2 + * + * @throws IllegalArgumentException if the string parameter is null + */ + public StringEntity(final String string, final ContentType contentType) + throws UnsupportedEncodingException { + super(); + if (string == null) { + throw new IllegalArgumentException("Source string may not be null"); + } + String charset = contentType != null ? contentType.getCharset() : null; + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } + this.content = string.getBytes(charset); + if (contentType != null) { + setContentType(contentType.toString()); + } + } + + /** * Creates a StringEntity with the specified content, mimetype and charset * * @param string content to be used. Not {@code null}. @@ -56,7 +84,10 @@ public class StringEntity extends Abstra * * @since 4.1 * @throws IllegalArgumentException if the string parameter is null + * + * @deprecated use {@link #StringEntity(String, ContentType)} */ + @Deprecated public StringEntity(final String string, String mimeType, String charset) throws UnsupportedEncodingException { super(); @@ -79,13 +110,14 @@ public class StringEntity extends Abstra * The mime type defaults to {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain". * * @param string content to be used. Not {@code null}. - * @param charset character set to be used. May be {@code null}, in which case the default is {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1" + * @param charset character set to be used. May be {@code null}, in which case the default + * is {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1" * * @throws IllegalArgumentException if the string parameter is null */ - public StringEntity(final String string, String charset) + public StringEntity(final String string, final String charset) throws UnsupportedEncodingException { - this(string, null, charset); + this(string, ContentType.create(HTTP.PLAIN_TEXT_TYPE, charset)); } /** @@ -101,7 +133,7 @@ public class StringEntity extends Abstra */ public StringEntity(final String string) throws UnsupportedEncodingException { - this(string, null); + this(string, ContentType.DEFAULT_TEXT); } public boolean isRepeatable() { 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=1149326&r1=1149325&r2=1149326&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 Jul 21 19:17:52 2011 @@ -36,6 +36,7 @@ import org.apache.http.HeaderElement; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.ParseException; +import org.apache.http.entity.ContentType; import org.apache.http.protocol.HTTP; /** @@ -113,7 +114,10 @@ public final class EntityUtils { * @return the character set, or null if not found * @throws ParseException if header elements cannot be parsed * @throws IllegalArgumentException if entity is null + * + * @deprecated use {@link ContentType#getOrDefault(HttpEntity)} */ + @Deprecated public static String getContentCharSet(final HttpEntity entity) throws ParseException { if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); @@ -140,7 +144,10 @@ public final class EntityUtils { * @throws IllegalArgumentException if entity is null * * @since 4.1 + * + * @deprecated use {@link ContentType#getOrDefault(HttpEntity)} */ + @Deprecated public static String getContentMimeType(final HttpEntity entity) throws ParseException { if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java?rev=1149326&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java (added) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java Thu Jul 21 19:17:52 2011 @@ -0,0 +1,142 @@ +/* + * ==================================================================== + * 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 + * . + * + */ + +package org.apache.http.entity; + +import org.apache.http.Header; +import org.apache.http.ParseException; +import org.apache.http.entity.BasicHttpEntity; +import org.apache.http.message.BasicHeader; +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit tests for {@link ContentType}. + * + */ +public class TestContentType { + + @Test + public void testBasis() throws Exception { + ContentType contentType = ContentType.create("text/plain", "ascii"); + Assert.assertEquals("text/plain", contentType.getMimeType()); + Assert.assertEquals("ascii", contentType.getCharset()); + Assert.assertEquals("text/plain; charset=ascii", contentType.toString()); + } + + @Test + public void testLowCaseText() throws Exception { + ContentType contentType = ContentType.create("Text/Plain", "ASCII"); + Assert.assertEquals("text/plain", contentType.getMimeType()); + Assert.assertEquals("ascii", contentType.getCharset()); + } + + @Test + public void testCreateInvalidInput() throws Exception { + try { + ContentType.create(null, null); + Assert.fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + ContentType.create(" ", null); + Assert.fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + ContentType.create("stuff;", null); + Assert.fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + ContentType.create("text/plain", ","); + Assert.fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + + @Test + public void testParse() throws Exception { + ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\""); + Assert.assertEquals("text/plain", contentType.getMimeType()); + Assert.assertEquals("ascii", contentType.getCharset()); + Assert.assertEquals("text/plain; charset=ascii", contentType.toString()); + } + + @Test + public void testParseInvalidInput() throws Exception { + try { + ContentType.parse(null); + Assert.fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + ContentType.parse(";"); + Assert.fail("ParseException should have been thrown"); + } catch (ParseException ex) { + // expected + } + } + + @Test + public void testExtractNullInput() throws Exception { + Assert.assertNull(ContentType.get(null)); + } + + @Test + public void testExtractNullContentType() throws Exception { + BasicHttpEntity httpentity = new BasicHttpEntity(); + httpentity.setContentType((Header)null); + Assert.assertNull(ContentType.get(httpentity)); + } + + @Test + public void testExtract() throws Exception { + BasicHttpEntity httpentity = new BasicHttpEntity(); + httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = UTF-8")); + ContentType contentType = ContentType.get(httpentity); + Assert.assertNotNull(contentType); + Assert.assertEquals("text/plain", contentType.getMimeType()); + Assert.assertEquals("utf-8", contentType.getCharset()); + } + + @Test + public void testExtractNoCharset() throws Exception { + BasicHttpEntity httpentity = new BasicHttpEntity(); + httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; param=yadayada")); + ContentType contentType = ContentType.get(httpentity); + Assert.assertNotNull(contentType); + Assert.assertEquals("text/plain", contentType.getMimeType()); + Assert.assertNull(contentType.getCharset()); + } + +} Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestEntityTemplate.java Thu Jul 21 19:17:52 2011 @@ -39,6 +39,7 @@ import org.junit.Test; * Unit tests for {@link EntityTemplate}. * */ +@Deprecated public class TestEntityTemplate { @Test Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestFileEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestFileEntity.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestFileEntity.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestFileEntity.java Thu Jul 21 19:17:52 2011 @@ -46,7 +46,8 @@ public class TestFileEntity { public void testBasics() throws Exception { File tmpfile = File.createTempFile("testfile", ".txt"); tmpfile.deleteOnExit(); - FileEntity httpentity = new FileEntity(tmpfile, HTTP.ISO_8859_1); + FileEntity httpentity = new FileEntity(tmpfile, + ContentType.create("text/plain", HTTP.ISO_8859_1)); Assert.assertEquals(tmpfile.length(), httpentity.getContentLength()); final InputStream content = httpentity.getContent(); @@ -62,7 +63,7 @@ public class TestFileEntity { @Test public void testIllegalConstructor() throws Exception { try { - new FileEntity(null, null); + new FileEntity(null, ContentType.create("text/plain", HTTP.ISO_8859_1)); Assert.fail("IllegalArgumentException should have been thrown"); } catch (IllegalArgumentException ex) { // expected @@ -81,7 +82,8 @@ public class TestFileEntity { outstream.write(3); outstream.close(); - FileEntity httpentity = new FileEntity(tmpfile, HTTP.ISO_8859_1); + FileEntity httpentity = new FileEntity(tmpfile, + ContentType.create("text/plain", HTTP.ISO_8859_1)); ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java Thu Jul 21 19:17:52 2011 @@ -64,11 +64,11 @@ public class TestStringEntity { @Test public void testDefaultContent() throws Exception { String s = "Message content"; - StringEntity httpentity = new StringEntity(s, "text/csv", "ANSI_X3.4-1968"); - Assert.assertEquals("text/csv; charset=ANSI_X3.4-1968", + StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968")); + Assert.assertEquals("text/csv; charset=ansi_x3.4-1968", httpentity.getContentType().getValue()); httpentity = new StringEntity(s, HTTP.US_ASCII); - Assert.assertEquals("text/plain; charset=US-ASCII", + Assert.assertEquals("text/plain; charset=us-ascii", httpentity.getContentType().getValue()); httpentity = new StringEntity(s); Assert.assertEquals("text/plain; charset=ISO-8859-1", Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java Thu Jul 21 19:17:52 2011 @@ -50,6 +50,7 @@ import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.DefaultHttpClientConnection; import org.apache.http.message.BasicHttpEntityEnclosingRequest; @@ -702,8 +703,11 @@ public class TestProtocolIntegration { HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity(); String line = EntityUtils.toString(incoming); - String charset = EntityUtils.getContentCharSet(incoming); - + ContentType contentType = ContentType.getOrDefault(incoming); + String charset = contentType.getCharset(); + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } RepeatingEntity outgoing = new RepeatingEntity(line, charset, n); outgoing.setChunked(n % 2 == 0); response.setEntity(outgoing); @@ -737,9 +741,10 @@ public class TestProtocolIntegration { HttpEntity incoming = response.getEntity(); Assert.assertNotNull(incoming); InputStream instream = incoming.getContent(); - String charset = EntityUtils.getContentCharSet(incoming); + ContentType contentType = ContentType.getOrDefault(incoming); + String charset = contentType.getCharset(); if (charset == null) { - charset = "US-ASCII"; + charset = HTTP.DEFAULT_CONTENT_CHARSET; } Assert.assertNotNull(instream); BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset)); Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java?rev=1149326&r1=1149325&r2=1149326&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java Thu Jul 21 19:17:52 2011 @@ -30,7 +30,6 @@ package org.apache.http.util; import java.io.ByteArrayInputStream; import java.io.InputStream; -import org.apache.http.Header; import org.apache.http.entity.BasicHttpEntity; import org.apache.http.message.BasicHeader; import org.junit.Assert; @@ -102,62 +101,6 @@ public class TestEntityUtils { } @Test - public void testNullEntityGetContentCharset() throws Exception { - try { - EntityUtils.getContentCharSet(null); - Assert.fail("IllegalArgumentException should have been thrown"); - } catch (IllegalArgumentException ex) { - // expected - } - } - - @Test - public void testNullContentTypeGetContentCharset() throws Exception { - BasicHttpEntity httpentity = new BasicHttpEntity(); - httpentity.setContentType((Header)null); - Assert.assertNull(EntityUtils.getContentCharSet(httpentity)); - } - - @Test - public void testNoCharsetGetContentCharset() throws Exception { - BasicHttpEntity httpentity = new BasicHttpEntity(); - httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; param=yadayada")); - Assert.assertNull(EntityUtils.getContentCharSet(httpentity)); - } - - @Test - public void testGetContentCharset() throws Exception { - BasicHttpEntity httpentity = new BasicHttpEntity(); - httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = UTF-8")); - Assert.assertEquals("UTF-8", EntityUtils.getContentCharSet(httpentity)); - } - - @Test - public void testGetContentMimeTypeWithCharset() throws Exception { - BasicHttpEntity httpentity = new BasicHttpEntity(); - httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; " + - "whatever; charset = UTF-8")); - Assert.assertEquals("text/plain", EntityUtils.getContentMimeType(httpentity)); - } - - @Test - public void testGetContentMimeTypeWithoutCharset() throws Exception { - BasicHttpEntity httpentity = new BasicHttpEntity(); - httpentity.setContentType(new BasicHeader("Content-Type", "text/whatever")); - Assert.assertEquals("text/whatever", EntityUtils.getContentMimeType(httpentity)); - } - - @Test - public void testNullEntityGetMimeType() throws Exception { - try { - EntityUtils.getContentMimeType(null); - Assert.fail("IllegalArgumentException should have been thrown"); - } catch (IllegalArgumentException ex) { - // expected - } - } - - @Test public void testNullEntityToString() throws Exception { try { EntityUtils.toString(null);