Author: rdonkin
Date: Sun Jul  6 03:23:15 2008
New Revision: 674281

URL: http://svn.apache.org/viewvc?rev=674281&view=rev
Log:
This change breaks backwards compatibility but I think is justified. I agree 
that defaulting is appropriate for TEXT types but is very unreliable for other 
types. For many types conversion to characters is not appropriate (for example, 
image/png). For other types, the type itself contains charset meta-data (for 
example application/xhtml+xml). By defaulting in these cases information is 
lost: it is not possible to destinguish between the case when the charset 
parameters is explicitly set to us-ascii and when the charset is unset. Non 
TEXT types cannot be assumed to default to us-ascii and so this behaviour is 
potentially dangerous.

Modified:
    
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/ContentDescriptor.java
    
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
    
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/BaseTestForBodyDescriptors.java

Modified: 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/ContentDescriptor.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/ContentDescriptor.java?rev=674281&r1=674280&r2=674281&view=diff
==============================================================================
--- 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/ContentDescriptor.java 
(original)
+++ 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/ContentDescriptor.java 
Sun Jul  6 03:23:15 2008
@@ -56,10 +56,15 @@
     String getSubType();
     
     /**
-     * The body descriptors character set.
+     * <p>The body descriptors character set, defaulted appropriately for the 
MIME type.</p>
+     * <p>
+     * For <code>TEXT</code> types, this will be defaulted to 
<code>us-ascii</code>.
+     * For other types, when the charset parameter is missing this property 
will be null.
+     * </p>
      * @return Character set, which has been parsed from the
-     *   content-type definition. Must not be null, but "US-ASCII",
-     *   if no content-type was specified.
+     *   content-type definition. Not null for <code>TEXT</code> types, when 
unset will
+     *   be set to default <code>us-ascii</code>. For other types, when unset,
+     *   null will be returnedsvn s
      */
     String getCharset();
 

Modified: 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java?rev=674281&r1=674280&r2=674281&view=diff
==============================================================================
--- 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
 (original)
+++ 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/DefaultBodyDescriptor.java
 Sun Jul  6 03:23:15 2008
@@ -33,6 +33,8 @@
  * @version $Id: BodyDescriptor.java,v 1.4 2005/02/11 10:08:37 ntherning Exp $
  */
 public class DefaultBodyDescriptor implements MutableBodyDescriptor {
+    private static final String US_ASCII = "us-ascii";
+
     private static final String SUB_TYPE_EMAIL = "rfc822";
 
     private static final String MEDIA_TYPE_TEXT = "text";
@@ -53,7 +55,7 @@
     private String subType = DEFAULT_SUB_TYPE;
     private String mimeType = DEFAULT_MIME_TYPE;
     private String boundary = null;
-    private String charset = "us-ascii";
+    private String charset = US_ASCII;
     private String transferEncoding = "7bit";
     private Map parameters = new HashMap();
     private boolean contentTypeSet;
@@ -158,12 +160,16 @@
         }
         
         String c = (String) params.get("charset");
+        charset = null;
         if (c != null) {
             c = c.trim();
             if (c.length() > 0) {
                 charset = c.toLowerCase();
             }
         }
+        if (charset == null && MEDIA_TYPE_TEXT.equals(mediaType)) {
+            charset = US_ASCII;
+        }
         
         /*
          * Add all other parameters to parameters.

Modified: 
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/BaseTestForBodyDescriptors.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/BaseTestForBodyDescriptors.java?rev=674281&r1=674280&r2=674281&view=diff
==============================================================================
--- 
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/BaseTestForBodyDescriptors.java
 (original)
+++ 
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/BaseTestForBodyDescriptors.java
 Sun Jul  6 03:23:15 2008
@@ -146,12 +146,12 @@
          */
         bd = newBodyDescriptor();
         assertEquals("us-ascii", bd.getCharset());
-        bd.addField("Content-Type ", "some/type; charset=ISO-8859-1");
+        bd.addField("Content-Type ", "text/type; charset=ISO-8859-1");
         assertEquals("iso-8859-1", bd.getCharset());
         
         bd = newBodyDescriptor();
         assertEquals("us-ascii", bd.getCharset());
-        bd.addField("Content-Type ", "some/type");
+        bd.addField("Content-Type ", "text/type");
         assertEquals("us-ascii", bd.getCharset());
         
         /*
@@ -184,5 +184,16 @@
         assertEquals("\"hepp\"  =us\t-ascii", bd.getCharset());
         
     }
+    
+    public void testDoDefaultToUsAsciiWhenUntyped() throws Exception {
+        MutableBodyDescriptor descriptor = newBodyDescriptor();
+        descriptor.addField("To", "[EMAIL PROTECTED]");
+        assertEquals("us-ascii", descriptor.getCharset());
+    }
 
+    public void testDoNotDefaultToUsAsciiForNonTextTypes() throws Exception {
+        MutableBodyDescriptor descriptor = newBodyDescriptor();
+        descriptor.addField("Content-Type", "image/png; name=blob.png");
+        assertNull(descriptor.getCharset());
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to