hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r884242 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/util/EntityUtils.java test/java/org/apache/http/util/TestEntityUtils.java
Date Wed, 25 Nov 2009 20:07:13 GMT
Author: olegk
Date: Wed Nov 25 20:07:13 2009
New Revision: 884242

URL: http://svn.apache.org/viewvc?rev=884242&view=rev
Log:
HTTPCORE-210: get entity MimeType utility method

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java

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=884242&r1=884241&r2=884242&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
Wed Nov 25 20:07:13 2009
@@ -92,9 +92,7 @@
      * @throws ParseException if header elements cannot be parsed
      * @throws IllegalArgumentException if entity is null
      */
-    public static String getContentCharSet(final HttpEntity entity)
-        throws ParseException {
-
+    public static String getContentCharSet(final HttpEntity entity) throws ParseException
{
         if (entity == null) {
             throw new IllegalArgumentException("HTTP entity may not be null");
         }
@@ -112,6 +110,30 @@
     }
 
     /**
+     * Obtains mime type of the entity, if known.
+     * 
+     * @param entity must not be null
+     * @return the character set, or null if not found
+     * @throws ParseException if header elements cannot be parsed
+     * @throws IllegalArgumentException if entity is null
+     * 
+     * @since 4.1
+     */
+    public static String getContentMimeType(final HttpEntity entity) throws ParseException
{
+        if (entity == null) {
+            throw new IllegalArgumentException("HTTP entity may not be null");
+        }
+        String mimeType = null;
+        if (entity.getContentType() != null) { 
+            HeaderElement values[] = entity.getContentType().getElements();
+            if (values.length > 0) {
+                mimeType = values[0].getName();
+            }
+        }
+        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.

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=884242&r1=884241&r2=884242&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
Wed Nov 25 20:07:13 2009
@@ -139,6 +139,28 @@
         assertEquals("UTF-8", EntityUtils.getContentCharSet(httpentity));
     }
     
+    public void testGetContentMimeTypeWithCharset() throws Exception {
+        BasicHttpEntity httpentity = new BasicHttpEntity();
+        httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; " +
+        		"whatever; charset = UTF-8"));
+        assertEquals("text/plain", EntityUtils.getContentMimeType(httpentity));
+    }
+    
+    public void testGetContentMimeTypeWithoutCharset() throws Exception {
+        BasicHttpEntity httpentity = new BasicHttpEntity();
+        httpentity.setContentType(new BasicHeader("Content-Type", "text/whatever"));
+        assertEquals("text/whatever", EntityUtils.getContentMimeType(httpentity));
+    }
+    
+    public void testNullEntityGetMimeType() throws Exception {
+        try {
+            EntityUtils.getContentMimeType(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
     public void testNullEntityToString() throws Exception {
         try {
             EntityUtils.toString(null);



Mime
View raw message