hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
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 GMT
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<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;
@@ -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 &gt; 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 &gt; 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 &gt; 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 &gt; 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));
     }
 
 }



Mime
View raw message