james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r738064 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/field/Fields.java test/java/org/apache/james/mime4j/field/FieldsTest.java
Date Tue, 27 Jan 2009 12:01:21 GMT
Author: mwiederkehr
Date: Tue Jan 27 12:01:21 2009
New Revision: 738064

URL: http://svn.apache.org/viewvc?rev=738064&view=rev
Log:
MIME4J-100: fold entire header field not only field value

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java?rev=738064&r1=738063&r2=738064&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java Tue Jan 27
12:01:21 2009
@@ -30,6 +30,7 @@
 import java.util.Map;
 import java.util.TimeZone;
 
+import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.codec.EncoderUtil;
 import org.apache.james.mime4j.field.address.Address;
 import org.apache.james.mime4j.field.address.Mailbox;
@@ -41,10 +42,7 @@
     }
 
     public static ContentTypeField contentType(String contentType) {
-        int usedCharacters = Field.CONTENT_TYPE.length() + 2;
-        String body = MimeUtil.fold(contentType, usedCharacters);
-
-        return parse(ContentTypeField.class, Field.CONTENT_TYPE, body);
+        return parse(ContentTypeField.class, Field.CONTENT_TYPE, contentType);
     }
 
     public static ContentTypeField contentType(String mimeType,
@@ -96,17 +94,16 @@
     }
 
     public static Field messageId(String hostname) {
-        return Field.parse(Field.MESSAGE_ID, MimeUtil
-                .createUniqueMessageId(hostname));
+        String fieldValue = MimeUtil.createUniqueMessageId(hostname);
+        return parse(UnstructuredField.class, Field.MESSAGE_ID, fieldValue);
     }
 
     public static UnstructuredField subject(String subject) {
         int usedCharacters = Field.SUBJECT.length() + 2;
-        String encoded = EncoderUtil.encodeIfNecessary(subject,
+        String fieldValue = EncoderUtil.encodeIfNecessary(subject,
                 EncoderUtil.Usage.TEXT_TOKEN, usedCharacters);
-        String rawValue = MimeUtil.fold(encoded, usedCharacters);
 
-        return parse(UnstructuredField.class, Field.SUBJECT, rawValue);
+        return parse(UnstructuredField.class, Field.SUBJECT, fieldValue);
     }
 
     public static MailboxField sender(Mailbox mailbox) {
@@ -186,10 +183,8 @@
      * @return the newly created mailbox field.
      */
     public static MailboxField mailbox(String fieldName, Mailbox mailbox) {
-        String value = encodeAddresses(Collections.singleton(mailbox));
-
-        String folded = MimeUtil.fold(value, fieldName.length() + 2);
-        return parse(MailboxField.class, fieldName, folded);
+        String fieldValue = encodeAddresses(Collections.singleton(mailbox));
+        return parse(MailboxField.class, fieldName, fieldValue);
     }
 
     /**
@@ -206,10 +201,8 @@
      */
     public static MailboxListField mailboxList(String fieldName,
             Iterable<Mailbox> mailboxes) {
-        String value = encodeAddresses(mailboxes);
-
-        String folded = MimeUtil.fold(value, fieldName.length() + 2);
-        return parse(MailboxListField.class, fieldName, folded);
+        String fieldValue = encodeAddresses(mailboxes);
+        return parse(MailboxListField.class, fieldName, fieldValue);
     }
 
     /**
@@ -230,21 +223,26 @@
      */
     public static AddressListField addressList(String fieldName,
             Iterable<Address> addresses) {
-        String value = encodeAddresses(addresses);
-
-        String folded = MimeUtil.fold(value, fieldName.length() + 2);
-        return parse(AddressListField.class, fieldName, folded);
+        String fieldValue = encodeAddresses(addresses);
+        return parse(AddressListField.class, fieldName, fieldValue);
     }
 
     private static <F extends Field> F parse(Class<F> fieldClass,
             String fieldName, String fieldBody) {
-        Field field = Field.parse(fieldName, fieldBody);
-        if (!fieldClass.isInstance(field)) {
+        try {
+            String raw = MimeUtil.fold(fieldName + ": " + fieldBody, 0);
+
+            Field field = Field.parse(raw);
+            if (!fieldClass.isInstance(field)) {
+                throw new IllegalArgumentException("Incompatible field name: "
+                        + fieldName);
+            }
+
+            return fieldClass.cast(field);
+        } catch (MimeException e) {
             throw new IllegalArgumentException("Illegal field name: "
                     + fieldName);
         }
-
-        return fieldClass.cast(field);
     }
 
     private static String encodeAddresses(Iterable<? extends Address> addresses) {

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java?rev=738064&r1=738063&r2=738064&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java Tue Jan
27 12:01:21 2009
@@ -160,7 +160,7 @@
                 "Sm\370rebr\370d").getRaw());
 
         String seventyEight = "12345678901234567890123456789012345678901234567890123456789012345678";
-        assertEquals("Subject: " + seventyEight, Fields.subject(seventyEight)
+        assertEquals("Subject:\r\n " + seventyEight, Fields.subject(seventyEight)
                 .getRaw());
 
         String seventyNine = seventyEight + "9";



Mime
View raw message