Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 67FA0200B7A for ; Mon, 5 Sep 2016 20:55:50 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 66540160ACC; Mon, 5 Sep 2016 18:55:50 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 86A66160AB0 for ; Mon, 5 Sep 2016 20:55:49 +0200 (CEST) Received: (qmail 60961 invoked by uid 500); 5 Sep 2016 18:55:38 -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 60952 invoked by uid 99); 5 Sep 2016 18:55:38 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Sep 2016 18:55:38 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 55233C7DFD for ; Mon, 5 Sep 2016 18:55:38 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.281 X-Spam-Level: * X-Spam-Status: No, score=1.281 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.519] autolearn=disabled Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id W0QqZJ3J15M3 for ; Mon, 5 Sep 2016 18:55:37 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTP id 7DC0B5FBE2 for ; Mon, 5 Sep 2016 18:55:36 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id AFB5EE021A for ; Mon, 5 Sep 2016 18:55:35 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id AC4E53A0DD0 for ; Mon, 5 Sep 2016 18:55:35 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1759345 - in /httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity: ContentType.java EntityUtils.java Date: Mon, 05 Sep 2016 18:55:35 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160905185535.AC4E53A0DD0@svn01-us-west.apache.org> archived-at: Mon, 05 Sep 2016 18:55:50 -0000 Author: olegk Date: Mon Sep 5 18:55:35 2016 New Revision: 1759345 URL: http://svn.apache.org/viewvc?rev=1759345&view=rev Log: HTTPCORE-431: correct character encoding default for application/json Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/ContentType.java httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/EntityUtils.java Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/ContentType.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/ContentType.java?rev=1759345&r1=1759344&r2=1759345&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/ContentType.java (original) +++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/ContentType.java Mon Sep 5 18:55:35 2016 @@ -32,6 +32,8 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -90,6 +92,28 @@ public final class ContentType implement public static final ContentType WILDCARD = create( "*/*", (Charset) null); + + private static final Map CONTENT_TYPE_MAP; + static { + + final ContentType[] contentTypes = { + APPLICATION_ATOM_XML, + APPLICATION_FORM_URLENCODED, + APPLICATION_JSON, + APPLICATION_SVG_XML, + APPLICATION_XHTML_XML, + APPLICATION_XML, + MULTIPART_FORM_DATA, + TEXT_HTML, + TEXT_PLAIN, + TEXT_XML }; + final HashMap map = new HashMap(); + for (ContentType contentType: contentTypes) { + map.put(contentType.getMimeType(), contentType); + } + CONTENT_TYPE_MAP = Collections.unmodifiableMap(map); + } + // defaults public static final ContentType DEFAULT_TEXT = TEXT_PLAIN; public static final ContentType DEFAULT_BINARY = APPLICATION_OCTET_STREAM; @@ -352,6 +376,22 @@ public final class ContentType implement return contentType != null ? contentType : DEFAULT_TEXT; } + + /** + * Returns {@code Content-Type} for the given MIME type. + * + * @param mimeType MIME type + * @return content type or {@code null} if not known. + * + * @since 4.5 + */ + public static ContentType getByMimeType(final String mimeType) { + if (mimeType == null) { + return null; + } + return CONTENT_TYPE_MAP.get(mimeType); + } + /** * Creates a new instance with this MIME type and the given Charset. * Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/EntityUtils.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/EntityUtils.java?rev=1759345&r1=1759344&r2=1759345&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/EntityUtils.java (original) +++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/entity/EntityUtils.java Mon Sep 5 18:55:35 2016 @@ -36,8 +36,8 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; -import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.ByteArrayBuffer; @@ -142,25 +142,9 @@ public final class EntityUtils { } } - /** - * Get the entity content as a String, using the provided default character set - * if none is found in the entity. - * If defaultCharset is null, the default "ISO-8859-1" is used. - * - * @param entity must not be null - * @param defaultCharset character set to be applied if none found in the entity, - * or if the entity provided charset is invalid or not available. - * @return the entity content as a String. May be null if - * {@link HttpEntity#getContent()} is null. - * @throws ParseException if header elements cannot be parsed - * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE - * @throws IOException if an error occurs reading the input stream - * @throws UnsupportedCharsetException Thrown when the named entity's charset is not available in - * this instance of the Java virtual machine and no defaultCharset is provided. - */ - public static String toString( - final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException { - Args.notNull(entity, "Entity"); + private static String toString( + final HttpEntity entity, + final ContentType contentType) throws IOException { final InputStream instream = entity.getContent(); if (instream == null) { return null; @@ -173,20 +157,14 @@ public final class EntityUtils { i = 4096; } Charset charset = null; - try { - final ContentType contentType = ContentType.get(entity); - if (contentType != null) { - charset = contentType.getCharset(); - } - } catch (final UnsupportedCharsetException ex) { - if (defaultCharset == null) { - throw new UnsupportedEncodingException(ex.getMessage()); + if (contentType != null) { + charset = contentType.getCharset(); + if (charset == null) { + final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType()); + charset = defaultContentType != null ? defaultContentType.getCharset() : null; } } if (charset == null) { - charset = defaultCharset; - } - if (charset == null) { charset = StandardCharsets.ISO_8859_1; } final Reader reader = new InputStreamReader(instream, charset); @@ -208,13 +186,50 @@ public final class EntityUtils { * If defaultCharset is null, the default "ISO-8859-1" is used. * * @param entity must not be null + * @param defaultCharset character set to be applied if none found in the entity, + * or if the entity provided charset is invalid or not available. + * @return the entity content as a String. May be null if + * {@link HttpEntity#getContent()} is null. + * @throws ParseException if header elements cannot be parsed + * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE + * @throws IOException if an error occurs reading the input stream + * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in + * this instance of the Java virtual machine and no defaultCharset is provided. + */ + public static String toString( + final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException { + Args.notNull(entity, "Entity"); + ContentType contentType = null; + try { + contentType = ContentType.get(entity); + } catch (final UnsupportedCharsetException ex) { + if (defaultCharset == null) { + throw new UnsupportedEncodingException(ex.getMessage()); + } + } + if (contentType != null) { + if (contentType.getCharset() == null) { + contentType = contentType.withCharset(defaultCharset); + } + } else { + contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset); + } + return toString(entity, contentType); + } + + /** + * Get the entity content as a String, using the provided default character set + * if none is found in the entity. + * If defaultCharset is null, the default "ISO-8859-1" is used. + * + * @param entity must not be null * @param defaultCharset character set to be applied if none found in the entity * @return the entity content as a String. May be null if * {@link HttpEntity#getContent()} is null. * @throws ParseException if header elements cannot be parsed * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE * @throws IOException if an error occurs reading the input stream - * @throws UnsupportedCharsetException Thrown when the named charset is not available in + * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ public static String toString( @@ -232,12 +247,12 @@ public final class EntityUtils { * @throws ParseException if header elements cannot be parsed * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE * @throws IOException if an error occurs reading the input stream - * @throws UnsupportedCharsetException Thrown when the named charset is not available in + * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ - public static String toString(final HttpEntity entity) - throws IOException, ParseException { - return toString(entity, (Charset)null); + public static String toString(final HttpEntity entity) throws IOException, ParseException { + Args.notNull(entity, "Entity"); + return toString(entity, ContentType.get(entity)); } }