james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r739014 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/field/Fields.java test/java/org/apache/james/mime4j/field/FieldsTest.java
Date Thu, 29 Jan 2009 21:07:05 GMT
Author: mwiederkehr
Date: Thu Jan 29 21:07:05 2009
New Revision: 739014

URL: http://svn.apache.org/viewvc?rev=739014&view=rev
Log:
MIME4J-100, MIME4J-108: methods for creating Content-Disposition fields

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=739014&r1=739013&r2=739014&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 Thu Jan 29
21:07:05 2009
@@ -62,6 +62,29 @@
                 Field.CONTENT_TRANSFER_ENCODING, contentTransferEncoding);
     }
 
+    public static ContentDispositionField contentDisposition(
+            String contentDisposition) {
+        return parse(ContentDispositionField.class, Field.CONTENT_DISPOSITION,
+                contentDisposition);
+    }
+
+    public static ContentDispositionField contentDisposition(
+            String dispositionType, Map<String, String> parameters) {
+        if (parameters == null || parameters.isEmpty()) {
+            return parse(ContentDispositionField.class,
+                    Field.CONTENT_DISPOSITION, dispositionType);
+        } else {
+            StringBuilder sb = new StringBuilder(dispositionType);
+            for (Map.Entry<String, String> entry : parameters.entrySet()) {
+                sb.append("; ");
+                sb.append(EncoderUtil.encodeHeaderParameter(entry.getKey(),
+                        entry.getValue()));
+            }
+            String contentDisposition = sb.toString();
+            return contentDisposition(contentDisposition);
+        }
+    }
+
     public static DateTimeField date(Date date) {
         return date(Field.DATE, date, null);
     }

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=739014&r1=739013&r2=739014&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 Thu Jan
29 21:07:05 2009
@@ -29,6 +29,7 @@
 
 import org.apache.james.mime4j.field.address.Group;
 import org.apache.james.mime4j.field.address.Mailbox;
+import org.apache.james.mime4j.util.MimeUtil;
 
 public class FieldsTest extends TestCase {
 
@@ -95,6 +96,50 @@
         assertEquals("Content-Transfer-Encoding: base64", field.getRaw());
     }
 
+    public void testContentDispositionString() throws Exception {
+        ContentDispositionField field = Fields.contentDisposition("inline; "
+                + "filename=\"testing 1 2.dat\"; size=12345; "
+                + "creation-date=\"Thu, 1 Jan 1970 00:00:00 +0000\"");
+        assertTrue(field.isValidField());
+
+        String expectedRaw = "Content-Disposition: inline; filename="
+                + "\"testing 1 2.dat\"; size=12345;\r\n creation-date="
+                + "\"Thu, 1 Jan 1970 00:00:00 +0000\"";
+        assertEquals(expectedRaw, field.getRaw());
+    }
+
+    public void testContentDispositionStringParameters() throws Exception {
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("creation-date", MimeUtil.formatDate(new Date(0),
+                TimeZone.getTimeZone("GMT")));
+        ContentDispositionField field = Fields.contentDisposition("attachment",
+                parameters);
+        assertTrue(field.isValidField());
+
+        String expectedRaw = "Content-Disposition: attachment; "
+                + "creation-date=\"Thu, 1 Jan 1970 00:00:00\r\n +0000\"";
+        assertEquals(expectedRaw, field.getRaw());
+
+        assertEquals(new Date(0), field.getCreationDate());
+    }
+
+    public void testContentDispositionStringNullParameters() throws Exception {
+        ContentDispositionField field = Fields.contentDisposition("inline",
+                null);
+        assertTrue(field.isValidField());
+
+        String expectedRaw = "Content-Disposition: inline";
+        assertEquals(expectedRaw, field.getRaw());
+    }
+
+    public void testInvalidContentDisposition() throws Exception {
+        ContentDispositionField field = Fields.contentDisposition("inline; "
+                + "filename=some file.dat");
+        assertFalse(field.isValidField());
+
+        assertEquals("inline", field.getDispositionType());
+    }
+
     public void testDateString() throws Exception {
         DateTimeField field = Fields.date("Thu, 1 Jan 1970 00:00:00 +0000");
         assertTrue(field.isValidField());
@@ -160,8 +205,8 @@
                 "Sm\370rebr\370d").getRaw());
 
         String seventyEight = "12345678901234567890123456789012345678901234567890123456789012345678";
-        assertEquals("Subject:\r\n " + seventyEight, Fields.subject(seventyEight)
-                .getRaw());
+        assertEquals("Subject:\r\n " + seventyEight, Fields.subject(
+                seventyEight).getRaw());
 
         String seventyNine = seventyEight + "9";
         String expected = "Subject: =?US-ASCII?Q?1234567890123456789012345678901234?="



Mime
View raw message