james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r739602 - in /james/mime4j/trunk: examples/src/java/org/apache/james/mime4j/samples/dom/ src/main/java/org/apache/james/mime4j/field/ src/main/java/org/apache/james/mime4j/message/ src/test/java/org/apache/james/mime4j/field/ src/test/java/...
Date Sat, 31 Jan 2009 20:47:49 GMT
Author: mwiederkehr
Date: Sat Jan 31 20:47:48 2009
New Revision: 739602

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

Added:
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java
Modified:
    james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/dom/MultipartMessage.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java

Modified: james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/dom/MultipartMessage.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/dom/MultipartMessage.java?rev=739602&r1=739601&r2=739602&view=diff
==============================================================================
--- james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/dom/MultipartMessage.java
(original)
+++ james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/dom/MultipartMessage.java
Sat Jan 31 20:47:48 2009
@@ -125,6 +125,10 @@
         bodyPart.setBody(body, "image/png");
         bodyPart.setContentTransferEncoding("base64");
 
+        // Specify a filename in the Content-Disposition header (implicitly sets
+        // the disposition type to "attachment")
+        bodyPart.setFilename("smiley.png");
+
         return bodyPart;
     }
 

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=739602&r1=739601&r2=739602&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 31
20:47:48 2009
@@ -22,6 +22,7 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.TimeZone;
 
@@ -73,6 +74,9 @@
 
     public static ContentDispositionField contentDisposition(
             String dispositionType, Map<String, String> parameters) {
+        if (!isValidDispositionType(dispositionType))
+            throw new IllegalArgumentException();
+
         if (parameters == null || parameters.isEmpty()) {
             return parse(ContentDispositionField.class,
                     Field.CONTENT_DISPOSITION, dispositionType);
@@ -88,6 +92,44 @@
         }
     }
 
+    public static ContentDispositionField contentDisposition(
+            String dispositionType, String filename) {
+        return contentDisposition(dispositionType, filename, -1, null, null,
+                null);
+    }
+
+    public static ContentDispositionField contentDisposition(
+            String dispositionType, String filename, long size) {
+        return contentDisposition(dispositionType, filename, size, null, null,
+                null);
+    }
+
+    public static ContentDispositionField contentDisposition(
+            String dispositionType, String filename, long size,
+            Date creationDate, Date modificationDate, Date readDate) {
+        Map<String, String> parameters = new HashMap<String, String>();
+        if (filename != null) {
+            parameters.put(ContentDispositionField.PARAM_FILENAME, filename);
+        }
+        if (size >= 0) {
+            parameters.put(ContentDispositionField.PARAM_SIZE, Long
+                    .toString(size));
+        }
+        if (creationDate != null) {
+            parameters.put(ContentDispositionField.PARAM_CREATION_DATE,
+                    MimeUtil.formatDate(creationDate, null));
+        }
+        if (modificationDate != null) {
+            parameters.put(ContentDispositionField.PARAM_MODIFICATION_DATE,
+                    MimeUtil.formatDate(modificationDate, null));
+        }
+        if (readDate != null) {
+            parameters.put(ContentDispositionField.PARAM_READ_DATE, MimeUtil
+                    .formatDate(readDate, null));
+        }
+        return contentDisposition(dispositionType, parameters);
+    }
+
     public static DateTimeField date(Date date) {
         return date(Field.DATE, date, null);
     }
@@ -255,6 +297,13 @@
         return EncoderUtil.isToken(type) && EncoderUtil.isToken(subType);
     }
 
+    private static boolean isValidDispositionType(String dispositionType) {
+        if (dispositionType == null)
+            return false;
+
+        return EncoderUtil.isToken(dispositionType);
+    }
+
     private static <F extends Field> F parse(Class<F> fieldClass,
             String fieldName, String fieldBody) {
         try {

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java?rev=739602&r1=739601&r2=739602&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java Sat Jan 31
20:47:48 2009
@@ -22,11 +22,13 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.james.mime4j.MimeIOException;
 import org.apache.james.mime4j.codec.CodecUtil;
+import org.apache.james.mime4j.field.ContentDispositionField;
 import org.apache.james.mime4j.field.ContentTransferEncodingField;
 import org.apache.james.mime4j.field.ContentTypeField;
 import org.apache.james.mime4j.field.Field;
@@ -313,6 +315,156 @@
     }
 
     /**
+     * Return the disposition type of the content disposition of this
+     * <code>Entity</code>.
+     * 
+     * @return the disposition type or <code>null</code> if no disposition
+     *         type has been set.
+     */
+    public String getDispositionType() {
+        ContentDispositionField field = obtainField(Field.CONTENT_DISPOSITION);
+        if (field == null)
+            return null;
+
+        return field.getDispositionType();
+    }
+    
+    /**
+     * Sets the content disposition of this <code>Entity</code> to the
+     * specified disposition type. No filename, size or date parameters
+     * are included in the content disposition.
+     * 
+     * @param dispositionType
+     *            disposition type value (usually <code>inline</code> or
+     *            <code>attachment</code>).
+     */
+    public void setContentDisposition(String dispositionType) {
+        Header header = obtainHeader();
+        header.setField(Fields.contentDisposition(dispositionType, null, -1,
+                null, null, null));
+    }
+
+    /**
+     * Sets the content disposition of this <code>Entity</code> to the
+     * specified disposition type and filename. No size or date parameters are
+     * included in the content disposition.
+     * 
+     * @param dispositionType
+     *            disposition type value (usually <code>inline</code> or
+     *            <code>attachment</code>).
+     * @param filename
+     *            filename parameter value or <code>null</code> if the
+     *            parameter should not be included.
+     */
+    public void setContentDisposition(String dispositionType, String filename) {
+        Header header = obtainHeader();
+        header.setField(Fields.contentDisposition(dispositionType, filename,
+                -1, null, null, null));
+    }
+
+    /**
+     * Sets the content disposition of this <code>Entity</code> to the
+     * specified values. No date parameters are included in the content
+     * disposition.
+     * 
+     * @param dispositionType
+     *            disposition type value (usually <code>inline</code> or
+     *            <code>attachment</code>).
+     * @param filename
+     *            filename parameter value or <code>null</code> if the
+     *            parameter should not be included.
+     * @param size
+     *            size parameter value or <code>-1</code> if the parameter
+     *            should not be included.
+     */
+    public void setContentDisposition(String dispositionType, String filename,
+            long size) {
+        Header header = obtainHeader();
+        header.setField(Fields.contentDisposition(dispositionType, filename,
+                size, null, null, null));
+    }
+
+    /**
+     * Sets the content disposition of this <code>Entity</code> to the
+     * specified values.
+     * 
+     * @param dispositionType
+     *            disposition type value (usually <code>inline</code> or
+     *            <code>attachment</code>).
+     * @param filename
+     *            filename parameter value or <code>null</code> if the
+     *            parameter should not be included.
+     * @param size
+     *            size parameter value or <code>-1</code> if the parameter
+     *            should not be included.
+     * @param creationDate
+     *            creation-date parameter value or <code>null</code> if the
+     *            parameter should not be included.
+     * @param modificationDate
+     *            modification-date parameter value or <code>null</code> if
+     *            the parameter should not be included.
+     * @param readDate
+     *            read-date parameter value or <code>null</code> if the
+     *            parameter should not be included.
+     */
+    public void setContentDisposition(String dispositionType, String filename,
+            long size, Date creationDate, Date modificationDate, Date readDate) {
+        Header header = obtainHeader();
+        header.setField(Fields.contentDisposition(dispositionType, filename,
+                size, creationDate, modificationDate, readDate));
+    }
+
+    /**
+     * Returns the filename parameter of the content disposition of this
+     * <code>Entity</code>.
+     * 
+     * @return the filename parameter of the content disposition or
+     *         <code>null</code> if the filename has not been set.
+     */
+    public String getFilename() {
+        ContentDispositionField field = obtainField(Field.CONTENT_DISPOSITION);
+        if (field == null)
+            return null;
+
+        return field.getFilename();
+    }
+    
+    /**
+     * Sets the filename parameter of the content disposition of this
+     * <code>Entity</code> to the specified value. If this entity does not
+     * have a content disposition header field a new one with disposition type
+     * <code>attachment</code> is created.
+     * 
+     * @param filename
+     *            filename parameter value or <code>null</code> if the
+     *            parameter should be removed.
+     */
+    public void setFilename(String filename) {
+        Header header = obtainHeader();
+        ContentDispositionField field = (ContentDispositionField) header
+                .getField(Field.CONTENT_DISPOSITION);
+        if (field == null) {
+            if (filename != null) {
+                header.setField(Fields.contentDisposition(
+                        ContentDispositionField.DISPOSITION_TYPE_ATTACHMENT,
+                        filename, -1, null, null, null));
+            }
+        } else {
+            String dispositionType = field.getDispositionType();
+            Map<String, String> parameters = new HashMap<String, String>(field
+                    .getParameters());
+            if (filename == null) {
+                parameters.remove(ContentDispositionField.PARAM_FILENAME);
+            } else {
+                parameters
+                        .put(ContentDispositionField.PARAM_FILENAME, filename);
+            }
+            header.setField(Fields.contentDisposition(dispositionType,
+                    parameters));
+        }
+    }
+
+    /**
      * Determines if the MIME type of this <code>Entity</code> matches the
      * given one. MIME types are case-insensitive.
      * 
@@ -397,4 +549,24 @@
         return header;
     }
 
+    /**
+     * Obtains the header field with the specified name.
+     * 
+     * @param <F>
+     *            concrete field type.
+     * @param fieldName
+     *            name of the field to retrieve.
+     * @return the header field or <code>null</code> if this entity has no
+     *         header or the header contains no such field.
+     */
+    <F extends Field> F obtainField(String fieldName) {
+        Header header = getHeader();
+        if (header == null)
+            return null;
+
+        @SuppressWarnings("unchecked")
+        F field = (F) header.getField(fieldName);
+        return field;
+    }
+
 }

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java?rev=739602&r1=739601&r2=739602&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java Sat Jan
31 20:47:48 2009
@@ -404,14 +404,4 @@
         }
     }
 
-    private <F extends Field> F obtainField(String fieldName) {
-        Header header = getHeader();
-        if (header == null)
-            return null;
-
-        @SuppressWarnings("unchecked")
-        F field = (F) header.getField(fieldName);
-        return field;
-    }
-
 }

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=739602&r1=739601&r2=739602&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
31 20:47:48 2009
@@ -125,13 +125,45 @@
 
     public void testContentDispositionStringNullParameters() throws Exception {
         ContentDispositionField field = Fields.contentDisposition("inline",
-                null);
+                (Map<String, String>) null);
         assertTrue(field.isValidField());
 
         String expectedRaw = "Content-Disposition: inline";
         assertEquals(expectedRaw, field.getRaw());
     }
 
+    public void testContentDispositionFilename() throws Exception {
+        ContentDispositionField field = Fields.contentDisposition("attachment",
+                "some file.dat");
+        assertTrue(field.isValidField());
+
+        assertEquals("attachment", field.getDispositionType());
+        assertEquals("some file.dat", field.getFilename());
+    }
+
+    public void testContentDispositionFilenameSize() throws Exception {
+        ContentDispositionField field = Fields.contentDisposition("attachment",
+                "some file.dat", 300);
+        assertTrue(field.isValidField());
+
+        assertEquals("attachment", field.getDispositionType());
+        assertEquals("some file.dat", field.getFilename());
+        assertEquals(300, field.getSize());
+    }
+
+    public void testContentDispositionFilenameSizeDate() throws Exception {
+        ContentDispositionField field = Fields.contentDisposition("attachment",
+                "some file.dat", 300, new Date(1000), new Date(2000), new Date(3000));
+        assertTrue(field.isValidField());
+
+        assertEquals("attachment", field.getDispositionType());
+        assertEquals("some file.dat", field.getFilename());
+        assertEquals(300, field.getSize());
+        assertEquals(new Date(1000), field.getCreationDate());
+        assertEquals(new Date(2000), field.getModificationDate());
+        assertEquals(new Date(3000), field.getReadDate());
+    }
+
     public void testInvalidContentDisposition() throws Exception {
         ContentDispositionField field = Fields.contentDisposition("inline; "
                 + "filename=some file.dat");

Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java?rev=739602&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java (added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/EntityTest.java Sat Jan
31 20:47:48 2009
@@ -0,0 +1,86 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mime4j.message;
+
+import org.apache.james.mime4j.field.Field;
+import org.apache.james.mime4j.message.Entity;
+
+import junit.framework.TestCase;
+
+public class EntityTest extends TestCase {
+
+    public void testGetDispositionType() throws Exception {
+        Entity entity = new BodyPart();
+
+        assertNull(entity.getDispositionType());
+
+        Header header = new Header();
+        header.setField(Field.parse("Content-Disposition", "inline"));
+        entity.setHeader(header);
+
+        assertEquals("inline", entity.getDispositionType());
+    }
+
+    public void testSetContentDispositionType() throws Exception {
+        Entity entity = new BodyPart();
+
+        entity.setContentDisposition("attachment");
+
+        assertEquals("attachment", entity.getHeader().getField(
+                "Content-Disposition").getBody());
+    }
+
+    public void testSetContentDispositionTypeFilename() throws Exception {
+        Entity entity = new BodyPart();
+
+        entity.setContentDisposition("attachment", "some file.dat");
+
+        assertEquals("attachment; filename=\"some file.dat\"", entity
+                .getHeader().getField("Content-Disposition").getBody());
+    }
+
+    public void testGetFilename() throws Exception {
+        Entity entity = new BodyPart();
+
+        assertNull(entity.getFilename());
+
+        Header header = new Header();
+        header.setField(Field.parse("Content-Disposition",
+                "attachment; filename=\"some file.dat\""));
+        entity.setHeader(header);
+
+        assertEquals("some file.dat", entity.getFilename());
+    }
+
+    public void testSetFilename() throws Exception {
+        Entity entity = new BodyPart();
+
+        entity.setFilename("file name.ext");
+
+        assertEquals("attachment; filename=\"file name.ext\"", entity
+                .getHeader().getField("Content-Disposition").getBody());
+
+        entity.setFilename(null);
+
+        assertEquals("attachment", entity.getHeader().getField(
+                "Content-Disposition").getBody());
+    }
+
+}



Mime
View raw message