james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r735356 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/field/Field.java main/java/org/apache/james/mime4j/field/Fields.java test/java/org/apache/james/mime4j/field/FieldsTest.java
Date Sat, 17 Jan 2009 23:19:18 GMT
Author: mwiederkehr
Date: Sat Jan 17 15:19:17 2009
New Revision: 735356

URL: http://svn.apache.org/viewvc?rev=735356&view=rev
Log:
MIME4J-100: methods for creating Message-ID and Subject header fields 

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java
    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/Field.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java?rev=735356&r1=735355&r2=735356&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java Sat Jan 17 15:19:17
2009
@@ -50,7 +50,9 @@
     public static final String CONTENT_TYPE = "Content-Type";
     public static final String CONTENT_TRANSFER_ENCODING = 
                                         "Content-Transfer-Encoding";
-    
+
+    public static final String MESSAGE_ID = "Message-ID";
+
     private static final String FIELD_NAME_PATTERN = 
         "^([\\x21-\\x39\\x3b-\\x7e]+):";
     private static final Pattern fieldNamePattern = 

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=735356&r1=735355&r2=735356&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 Sat Jan 17
15:19:17 2009
@@ -28,6 +28,7 @@
 import java.util.Map;
 import java.util.TimeZone;
 
+import org.apache.james.mime4j.decoder.EncoderUtil;
 import org.apache.james.mime4j.util.MimeUtil;
 
 public class Fields {
@@ -35,8 +36,8 @@
     }
 
     public static ContentTypeField contentType(String contentType) {
-        String body = MimeUtil.fold(contentType,
-                Field.CONTENT_TYPE.length() + 2);
+        int usedCharacters = Field.CONTENT_TYPE.length() + 2;
+        String body = MimeUtil.fold(contentType, usedCharacters);
 
         return (ContentTypeField) Field.parse(Field.CONTENT_TYPE, body);
     }
@@ -65,7 +66,7 @@
     }
 
     public static DateTimeField date(String fieldValue) {
-        return date("Date", fieldValue);
+        return date(Field.DATE, fieldValue);
     }
 
     public static DateTimeField date(String fieldName, String fieldValue) {
@@ -73,7 +74,7 @@
     }
 
     public static DateTimeField date(Date date) {
-        return date("Date", date, null);
+        return date(Field.DATE, date, null);
     }
 
     public static DateTimeField date(String fieldName, Date date) {
@@ -88,6 +89,20 @@
         return date(fieldName, df.format(date));
     }
 
+    public static Field messageId(String hostname) {
+        return Field.parse(Field.MESSAGE_ID, MimeUtil
+                .createUniqueMessageId(hostname));
+    }
+
+    public static UnstructuredField subject(String subject) {
+        int usedCharacters = Field.SUBJECT.length() + 2;
+        String encoded = EncoderUtil.encodeIfNecessary(subject,
+                EncoderUtil.Usage.TEXT_TOKEN, usedCharacters);
+        String rawValue = MimeUtil.fold(encoded, usedCharacters);
+
+        return (UnstructuredField) Field.parse(Field.SUBJECT, rawValue);
+    }
+
     private static String quote(String value) {
         for (int idx = 0; idx < value.length(); idx++) {
             if (isSpecial(value.charAt(idx))) {

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=735356&r1=735355&r2=735356&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 Sat Jan
17 15:19:17 2009
@@ -129,4 +129,28 @@
         assertFalse(field.isValidField());
     }
 
+    public void testMessageId() throws Exception {
+        Field messageId = Fields.messageId("acme.org");
+
+        String raw = messageId.getRaw();
+        assertTrue(raw.startsWith("Message-ID: <Mime4j."));
+        assertTrue(raw.endsWith("@acme.org>"));
+    }
+
+    public void testSubject() throws Exception {
+        assertEquals("Subject: ", Fields.subject("").getRaw());
+        assertEquals("Subject: test", Fields.subject("test").getRaw());
+        assertEquals("Subject: =?ISO-8859-1?Q?Sm=F8rebr=F8d?=", Fields.subject(
+                "Sm\370rebr\370d").getRaw());
+
+        String seventyNine = "123456789012345678901234567890123456789012345678901234567890123456789";
+        assertEquals("Subject: " + seventyNine, Fields.subject(seventyNine)
+                .getRaw());
+
+        String eighty = seventyNine + "0";
+        String expected = "Subject: =?US-ASCII?Q?12345678901234567890123456789012345?="
+                + "\r\n =?US-ASCII?Q?67890123456789012345678901234567890?=";
+        assertEquals(expected, Fields.subject(eighty).getRaw());
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message