You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/03/13 08:12:47 UTC
[06/13] james-project git commit: JAMES-2346 Allow direct convertion
from MDN to Mime4J entities
JAMES-2346 Allow direct convertion from MDN to Mime4J entities
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0f11476b
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0f11476b
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0f11476b
Branch: refs/heads/master
Commit: 0f11476b3f727789c3d542f16f2a8d11b53488c0
Parents: 890cede
Author: benwa <bt...@linagora.com>
Authored: Thu Mar 8 13:36:47 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Tue Mar 13 15:11:54 2018 +0700
----------------------------------------------------------------------
mdn/pom.xml | 4 ++
mdn/src/main/java/org/apache/james/mdn/MDN.java | 40 +++++++++++++++++++-
.../test/java/org/apache/james/mdn/MDNTest.java | 34 +++++++++++++++++
3 files changed, 76 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/0f11476b/mdn/pom.xml
----------------------------------------------------------------------
diff --git a/mdn/pom.xml b/mdn/pom.xml
index 37a85c2..87511c1 100644
--- a/mdn/pom.xml
+++ b/mdn/pom.xml
@@ -35,6 +35,10 @@
<dependencies>
<dependency>
<groupId>org.apache.james</groupId>
+ <artifactId>apache-mime4j-dom</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.james</groupId>
<artifactId>james-server-util-java8</artifactId>
</dependency>
<dependency>
http://git-wip-us.apache.org/repos/asf/james-project/blob/0f11476b/mdn/src/main/java/org/apache/james/mdn/MDN.java
----------------------------------------------------------------------
diff --git a/mdn/src/main/java/org/apache/james/mdn/MDN.java b/mdn/src/main/java/org/apache/james/mdn/MDN.java
index e0d8965..9b7b22c 100644
--- a/mdn/src/main/java/org/apache/james/mdn/MDN.java
+++ b/mdn/src/main/java/org/apache/james/mdn/MDN.java
@@ -19,6 +19,7 @@
package org.apache.james.mdn;
+import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Properties;
@@ -30,9 +31,22 @@ import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
+import org.apache.james.mime4j.Charsets;
+import org.apache.james.mime4j.dom.Message;
+import org.apache.james.mime4j.dom.Multipart;
+import org.apache.james.mime4j.message.BasicBodyFactory;
+import org.apache.james.mime4j.message.BodyPartBuilder;
+import org.apache.james.mime4j.message.MultipartBuilder;
+import org.apache.james.mime4j.stream.NameValuePair;
+
import com.google.common.base.Preconditions;
public class MDN {
+ private static final NameValuePair UTF_8_CHARSET = new NameValuePair("charset", Charsets.UTF_8.name());
+
+ public static final String DISPOSITION_CONTENT_TYPE = "message/disposition-notification";
+ public static final String REPORT_SUB_TYPE = "report";
+
public static class Builder {
private String humanReadableText;
private MDNReport report;
@@ -80,7 +94,7 @@ public class MDN {
public MimeMultipart asMultipart() throws MessagingException {
MimeMultipart multipart = new MimeMultipart();
- multipart.setSubType("report");
+ multipart.setSubType(REPORT_SUB_TYPE);
multipart.addBodyPart(computeHumanReadablePart());
multipart.addBodyPart(computeReportPart());
// The optional third part, the original message is omitted.
@@ -107,10 +121,32 @@ public class MDN {
public BodyPart computeReportPart() throws MessagingException {
MimeBodyPart mdnPart = new MimeBodyPart();
- mdnPart.setContent(report.formattedValue(), "message/disposition-notification");
+ mdnPart.setContent(report.formattedValue(), DISPOSITION_CONTENT_TYPE);
return mdnPart;
}
+ public Message.Builder asMime4JMessageBuilder() throws IOException {
+ Message.Builder messageBuilder = Message.Builder.of();
+ messageBuilder.setBody(asMime4JMultipart());
+ return messageBuilder;
+ }
+
+ private Multipart asMime4JMultipart() throws IOException {
+ MultipartBuilder builder = MultipartBuilder.create(REPORT_SUB_TYPE);
+ builder.addBodyPart(BodyPartBuilder.create()
+ .use(new BasicBodyFactory())
+ .setBody(humanReadableText, Charsets.UTF_8)
+ .setContentType("text/plain", UTF_8_CHARSET)
+ .build());
+ builder.addBodyPart(BodyPartBuilder.create()
+ .use(new BasicBodyFactory())
+ .setBody(report.formattedValue(), Charsets.UTF_8)
+ .setContentType(DISPOSITION_CONTENT_TYPE, UTF_8_CHARSET)
+ .build());
+
+ return builder.build();
+ }
+
@Override
public final boolean equals(Object o) {
if (o instanceof MDN) {
http://git-wip-us.apache.org/repos/asf/james-project/blob/0f11476b/mdn/src/test/java/org/apache/james/mdn/MDNTest.java
----------------------------------------------------------------------
diff --git a/mdn/src/test/java/org/apache/james/mdn/MDNTest.java b/mdn/src/test/java/org/apache/james/mdn/MDNTest.java
index dede484..69236fe 100644
--- a/mdn/src/test/java/org/apache/james/mdn/MDNTest.java
+++ b/mdn/src/test/java/org/apache/james/mdn/MDNTest.java
@@ -30,6 +30,8 @@ import org.apache.james.mdn.action.mode.DispositionActionMode;
import org.apache.james.mdn.fields.Disposition;
import org.apache.james.mdn.sending.mode.DispositionSendingMode;
import org.apache.james.mdn.type.DispositionType;
+import org.apache.james.mime4j.dom.Message;
+import org.apache.james.mime4j.message.DefaultMessageWriter;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -164,4 +166,36 @@ public class MDNTest {
" - We should always write detailed unit tests\n" +
" - We should think of all edge cases\n");
}
+
+ @Test
+ public void mdnShouldBeConvertibleToMime4JMessage() throws Exception {
+ Message message = MDN.builder()
+ .humanReadableText("Explanation:\n" +
+ " - We should always write detailed unit tests\n" +
+ " - We should think of all edge cases\n")
+ .report(MINIMAL_REPORT)
+ .build()
+ .asMime4JMessageBuilder()
+ .build();
+
+ assertThat(asString(message))
+ .contains("MIME-Version: 1.0\r\n" +
+ "Content-Type: multipart/report;")
+ .contains("Content-Type: text/plain; charset=UTF-8\r\n" +
+ "\r\n" +
+ "Explanation:\n" +
+ " - We should always write detailed unit tests\n" +
+ " - We should think of all edge cases")
+ .contains("Content-Type: message/disposition-notification; charset=UTF-8\r\n" +
+ "\r\n" +
+ "Final-Recipient: rfc822; final@domain.com\r\n" +
+ "Disposition: automatic-action/MDN-sent-automatically;deleted");
+ }
+
+ private String asString(Message message) throws Exception {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ DefaultMessageWriter writer = new DefaultMessageWriter();
+ writer.writeMessage(message, buffer);
+ return new String(buffer.toByteArray(), StandardCharsets.UTF_8);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org