MIME4J-271 Make possible to define a Content-Type parameter

Project: http://git-wip-us.apache.org/repos/asf/james-mime4j/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-mime4j/commit/abd229b3
Tree: http://git-wip-us.apache.org/repos/asf/james-mime4j/tree/abd229b3
Diff: http://git-wip-us.apache.org/repos/asf/james-mime4j/diff/abd229b3

Branch: refs/heads/master
Commit: abd229b3ffac7ee2734469f4f71f86363168bf65
Parents: 06852dd
Author: Matthieu Baechler <matthieu.baech...@gmail.com>
Authored: Wed Mar 28 17:01:14 2018 +0200
Committer: benwa <btell...@linagora.com>
Committed: Tue Apr 17 10:26:53 2018 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/mime4j/dom/Multipart.java     |  3 +++
 .../james/mime4j/internal/AbstractEntityBuilder.java    |  7 +++++--
 .../apache/james/mime4j/message/AbstractMultipart.java  | 10 +++++++++-
 .../james/mime4j/message/DefaultMessageBuilder.java     |  8 +++++++-
 .../apache/james/mime4j/message/MultipartBuilder.java   |  9 ++++++++-
 .../org/apache/james/mime4j/message/MultipartImpl.java  | 12 ++++++++++--
 .../james/mime4j/message/AbstractEntityBuilderTest.java |  6 +++++-
 7 files changed, 47 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/abd229b3/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java
----------------------------------------------------------------------
diff --git a/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java 
b/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java
index 54aa652..98d9399 100644
--- a/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java
+++ b/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java
@@ -21,6 +21,8 @@ package org.apache.james.mime4j.dom;
 
 import java.util.List;
 
+import org.apache.james.mime4j.stream.NameValuePair;
+
 /**
  * A MIME multipart body (as defined in RFC 2045). A multipart body has a 
ordered list of
  * body parts. The multipart body also has a preamble and epilogue. The 
preamble consists of
@@ -139,4 +141,5 @@ public interface Multipart extends Body {
      */
     void setEpilogue(String epilogue);
 
+    List<NameValuePair> getContentTypeParameters();
 }

http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/abd229b3/dom/src/main/java/org/apache/james/mime4j/internal/AbstractEntityBuilder.java
----------------------------------------------------------------------
diff --git 
a/dom/src/main/java/org/apache/james/mime4j/internal/AbstractEntityBuilder.java 
b/dom/src/main/java/org/apache/james/mime4j/internal/AbstractEntityBuilder.java
index 011a2d7..19ea879 100644
--- 
a/dom/src/main/java/org/apache/james/mime4j/internal/AbstractEntityBuilder.java
+++ 
b/dom/src/main/java/org/apache/james/mime4j/internal/AbstractEntityBuilder.java
@@ -556,8 +556,11 @@ public abstract class AbstractEntityBuilder {
     public AbstractEntityBuilder setBody(Multipart multipart) {
         this.body = multipart;
         if (multipart != null) {
-                setField(Fields.contentType("multipart/" + 
multipart.getSubType(),
-                        new NameValuePair("boundary", 
MimeUtil.createUniqueBoundary())));
+            List<NameValuePair> parameters =
+                new 
ArrayList<NameValuePair>(multipart.getContentTypeParameters());
+            parameters.add(new NameValuePair("boundary", 
MimeUtil.createUniqueBoundary()));
+            setField(Fields.contentType("multipart/" + multipart.getSubType(),
+                        parameters.toArray(new NameValuePair[0])));
         } else {
             removeFields(FieldName.CONTENT_TYPE);
         }

http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/abd229b3/dom/src/main/java/org/apache/james/mime4j/message/AbstractMultipart.java
----------------------------------------------------------------------
diff --git 
a/dom/src/main/java/org/apache/james/mime4j/message/AbstractMultipart.java 
b/dom/src/main/java/org/apache/james/mime4j/message/AbstractMultipart.java
index a38825c..42deb8b 100644
--- a/dom/src/main/java/org/apache/james/mime4j/message/AbstractMultipart.java
+++ b/dom/src/main/java/org/apache/james/mime4j/message/AbstractMultipart.java
@@ -25,6 +25,7 @@ import java.util.List;
 
 import org.apache.james.mime4j.dom.Entity;
 import org.apache.james.mime4j.dom.Multipart;
+import org.apache.james.mime4j.stream.NameValuePair;
 
 /**
  * Abstract MIME multipart body.
@@ -35,12 +36,14 @@ public abstract class AbstractMultipart implements 
Multipart {
     private Entity parent = null;
 
     private String subType;
+    private final List<NameValuePair> contentTypeParameters;
 
     /**
      * Creates a new empty <code>Multipart</code> instance.
      */
-    public AbstractMultipart(String subType) {
+    public AbstractMultipart(String subType, List<NameValuePair> 
contentTypeParameters) {
         this.subType = subType;
+        this.contentTypeParameters = contentTypeParameters;
     }
 
     /**
@@ -222,6 +225,11 @@ public abstract class AbstractMultipart implements 
Multipart {
      */
     public abstract void setEpilogue(String epilogue);
 
+    @Override
+    public List<NameValuePair> getContentTypeParameters() {
+        return contentTypeParameters;
+    }
+
     /**
      * Disposes of the BodyParts of this Multipart. Note that the dispose call
      * does not get forwarded to the parent entity of this Multipart.

http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/abd229b3/dom/src/main/java/org/apache/james/mime4j/message/DefaultMessageBuilder.java
----------------------------------------------------------------------
diff --git 
a/dom/src/main/java/org/apache/james/mime4j/message/DefaultMessageBuilder.java 
b/dom/src/main/java/org/apache/james/mime4j/message/DefaultMessageBuilder.java
index 0f35dab..9405bfd 100644
--- 
a/dom/src/main/java/org/apache/james/mime4j/message/DefaultMessageBuilder.java
+++ 
b/dom/src/main/java/org/apache/james/mime4j/message/DefaultMessageBuilder.java
@@ -21,6 +21,7 @@ package org.apache.james.mime4j.message;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
 
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.MimeIOException;
@@ -43,6 +44,7 @@ import org.apache.james.mime4j.parser.MimeStreamParser;
 import org.apache.james.mime4j.stream.BodyDescriptorBuilder;
 import org.apache.james.mime4j.stream.Field;
 import org.apache.james.mime4j.stream.MimeConfig;
+import org.apache.james.mime4j.stream.NameValuePair;
 
 /**
  * Default implementation of {@link MessageBuilder}.
@@ -159,7 +161,7 @@ public class DefaultMessageBuilder implements 
MessageBuilder {
      *             {@link SingleBody}.
      */
     public Multipart copy(Multipart other) {
-        MultipartImpl copy = new MultipartImpl(other.getSubType());
+        MultipartImpl copy = new MultipartImpl(other.getSubType(), 
other.getContentTypeParameters());
         for (Entity otherBodyPart : other.getBodyParts()) {
             copy.addBodyPart(copy(otherBodyPart));
         }
@@ -248,6 +250,10 @@ public class DefaultMessageBuilder implements 
MessageBuilder {
         return new MultipartImpl(subType);
     }
 
+    public Multipart newMultipart(final String subType, NameValuePair... 
contentTypeParameters) {
+        return new MultipartImpl(subType, 
Arrays.asList(contentTypeParameters));
+    }
+
     public Multipart newMultipart(final Multipart source) {
         return copy(source);
     }

http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/abd229b3/dom/src/main/java/org/apache/james/mime4j/message/MultipartBuilder.java
----------------------------------------------------------------------
diff --git 
a/dom/src/main/java/org/apache/james/mime4j/message/MultipartBuilder.java 
b/dom/src/main/java/org/apache/james/mime4j/message/MultipartBuilder.java
index 6c6190a..baeeaf4 100644
--- a/dom/src/main/java/org/apache/james/mime4j/message/MultipartBuilder.java
+++ b/dom/src/main/java/org/apache/james/mime4j/message/MultipartBuilder.java
@@ -49,6 +49,7 @@ public class MultipartBuilder {
     private String epilogue;
 
     private BodyFactory bodyFactory;
+    private List<NameValuePair> parameters;
 
     public static MultipartBuilder create(String subType) {
         return new MultipartBuilder().setSubType(subType);
@@ -64,6 +65,7 @@ public class MultipartBuilder {
 
     private MultipartBuilder() {
         this.bodyParts = new LinkedList<Entity>();
+        this.parameters = new LinkedList<NameValuePair>();
     }
 
     public MultipartBuilder use(final BodyFactory bodyFactory) {
@@ -217,6 +219,11 @@ public class MultipartBuilder {
         return this;
     }
 
+    public MultipartBuilder addContentTypeParameter(NameValuePair parameter) {
+        this.parameters.add(parameter);
+        return this;
+    }
+
     public MultipartBuilder addTextPart(String text, Charset charset) throws 
IOException {
         Charset cs = charset != null ? charset : Charsets.ISO_8859_1;
         TextBody body = bodyFactory != null ? bodyFactory.textBody(
@@ -277,7 +284,7 @@ public class MultipartBuilder {
     }
 
     public Multipart build() {
-        MultipartImpl multipart = new MultipartImpl(subType);
+        MultipartImpl multipart = new MultipartImpl(subType, parameters);
         for (Entity part : bodyParts) {
             multipart.addBodyPart(part);
         }

http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/abd229b3/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java
----------------------------------------------------------------------
diff --git 
a/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java 
b/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java
index 40606a9..7dd090a 100644
--- a/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java
+++ b/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java
@@ -19,7 +19,11 @@
 
 package org.apache.james.mime4j.message;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.james.mime4j.dom.Multipart;
+import org.apache.james.mime4j.stream.NameValuePair;
 import org.apache.james.mime4j.util.ByteSequence;
 import org.apache.james.mime4j.util.ContentUtil;
 
@@ -35,11 +39,15 @@ public class MultipartImpl extends AbstractMultipart {
     private transient String epilogueStrCache;
     private transient boolean epilogueComputed = false;
 
+    public MultipartImpl(String subType) {
+        this(subType, Collections.<NameValuePair>emptyList());
+    }
+
     /**
      * Creates a new empty <code>Multipart</code> instance.
      */
-    public MultipartImpl(String subType) {
-        super(subType);
+    public MultipartImpl(String subType, List<NameValuePair> parameters) {
+        super(subType, parameters);
         preamble = null;
         preambleStrCache = null;
         preambleComputed = true;

http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/abd229b3/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
----------------------------------------------------------------------
diff --git 
a/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
 
b/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
index e033d2b..c7493da 100644
--- 
a/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
+++ 
b/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
@@ -20,6 +20,8 @@
 package org.apache.james.mime4j.message;
 
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.TimeZone;
@@ -263,7 +265,8 @@ public class AbstractEntityBuilderTest {
     public void testSetMultipartBody() throws Exception {
         AbstractEntityBuilder builder = new AbstractEntityBuilderImpl();
 
-        Multipart multipart = new MultipartImpl("stuff");
+        Multipart multipart = new MultipartImpl("stuff",
+            Collections.singletonList(new NameValuePair("report-type", 
"disposition-notification")));
         builder.setBody(multipart);
 
         Assert.assertSame(multipart, builder.getBody());
@@ -273,6 +276,7 @@ public class AbstractEntityBuilderTest {
 
         final ContentTypeField field = (ContentTypeField) 
builder.getField("Content-Type");
         Assert.assertNotNull(field.getBoundary());
+        Assert.assertEquals("disposition-notification", 
field.getParameter("report-type"));
 
         builder.setBody((Message) null);
         Assert.assertFalse(builder.containsField("Content-Type"));


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

Reply via email to