You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/05/09 20:01:28 UTC
[02/50] httpcomponents-core git commit: HTTPCORE-431: correct
character encoding default for application/json
HTTPCORE-431: correct character encoding default for application/json
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.4.x@1759344 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/5b2dee7d
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/5b2dee7d
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/5b2dee7d
Branch: refs/heads/4.4.x
Commit: 5b2dee7d4257a488d0dc68f91947093eba43d31a
Parents: 37c5562
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Mon Sep 5 18:55:04 2016 +0000
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Sep 5 18:55:04 2016 +0000
----------------------------------------------------------------------
.../org/apache/http/entity/ContentType.java | 42 +++++++++-
.../java/org/apache/http/util/EntityUtils.java | 85 ++++++++++++--------
2 files changed, 91 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/5b2dee7d/httpcore/src/main/java/org/apache/http/entity/ContentType.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/entity/ContentType.java b/httpcore/src/main/java/org/apache/http/entity/ContentType.java
index 516bfa8..122d17f 100644
--- a/httpcore/src/main/java/org/apache/http/entity/ContentType.java
+++ b/httpcore/src/main/java/org/apache/http/entity/ContentType.java
@@ -31,6 +31,8 @@ import java.io.Serializable;
import java.nio.charset.Charset;
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;
@@ -42,8 +44,8 @@ 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.ThreadingBehavior;
import org.apache.http.annotation.Contract;
+import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.message.BasicHeaderValueFormatter;
import org.apache.http.message.BasicHeaderValueParser;
import org.apache.http.message.BasicNameValuePair;
@@ -92,6 +94,28 @@ public final class ContentType implements Serializable {
public static final ContentType WILDCARD = create(
"*/*", (Charset) null);
+
+ private static final Map<String, ContentType> 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<String, ContentType> map = new HashMap<String, ContentType>();
+ 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;
@@ -362,6 +386,22 @@ public final class ContentType implements Serializable {
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.
*
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/5b2dee7d/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/util/EntityUtils.java b/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
index c58625c..83297f0 100644
--- a/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
+++ b/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
@@ -193,25 +193,9 @@ public final class EntityUtils {
return mimeType;
}
- /**
- * 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;
@@ -224,18 +208,12 @@ public final class EntityUtils {
i = 4096;
}
Charset charset = null;
- try {
- final ContentType contentType = ContentType.get(entity);
- if (contentType != null) {
- charset = contentType.getCharset();
+ if (contentType != null) {
+ charset = contentType.getCharset();
+ if (charset == null) {
+ final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType());
+ charset = defaultContentType != null ? defaultContentType.getCharset() : null;
}
- } catch (final UnsupportedCharsetException ex) {
- if (defaultCharset == null) {
- throw new UnsupportedEncodingException(ex.getMessage());
- }
- }
- if (charset == null) {
- charset = defaultCharset;
}
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
@@ -259,13 +237,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(
@@ -283,12 +298,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));
}
}