james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [18/27] james-project git commit: JAMES-2132 Add MDN support for extensions
Date Fri, 01 Sep 2017 01:18:31 GMT
JAMES-2132 Add MDN support for extensions

https://tools.ietf.org/html/rfc8098#section-3.3 states:

   Additional MDN fields may be defined in the future by later revisions
   or extensions to this specification.  MDN field names MUST be
   registered with the Internet Assigned Numbers Authority (IANA) using
   the "Specification Required" registration policy.  (See Section 10
   for a registration form.)  MDN Extension-fields may be defined for
   the following reasons:

   a.  To allow additional information from foreign disposition reports
       to be tunneled through Internet MDNs.  The names of such MDN
       fields should begin with an indication of the foreign environment
       name (e.g., X400-Physical-Forwarding-Address).

   b.  To allow transmission of diagnostic information that is specific
       to a particular Mail User Agent (MUA).  The names of such MDN
       fields should begin with an indication of the MUA implementation
       that produced the MDN (e.g., Foomail-information).


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

Branch: refs/heads/master
Commit: 034eef67f4a52fdbef265954c3ea1a8e76b863a0
Parents: ea2b452
Author: benwa <btellier@linagora.com>
Authored: Mon Aug 28 20:47:57 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Thu Aug 31 18:00:20 2017 +0700

----------------------------------------------------------------------
 .../java/org/apache/james/mdn/MDNReport.java    | 39 ++++++++++--
 .../apache/james/mdn/fields/ExtensionField.java | 35 ++++++++++
 .../org/apache/james/mdn/MDNFactoryTest.java    | 67 ++++++++++++++++++++
 3 files changed, 136 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/034eef67/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
----------------------------------------------------------------------
diff --git a/mdn/src/main/java/org/apache/james/mdn/MDNReport.java b/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
index 127f0f4..1d3beee 100644
--- a/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
+++ b/mdn/src/main/java/org/apache/james/mdn/MDNReport.java
@@ -20,9 +20,11 @@
 package org.apache.james.mdn;
 
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 import org.apache.james.mdn.fields.Disposition;
 import org.apache.james.mdn.fields.Error;
+import org.apache.james.mdn.fields.ExtensionField;
 import org.apache.james.mdn.fields.FinalRecipient;
 import org.apache.james.mdn.fields.Gateway;
 import org.apache.james.mdn.fields.OriginalMessageId;
@@ -30,9 +32,13 @@ import org.apache.james.mdn.fields.OriginalRecipient;
 import org.apache.james.mdn.fields.ReportingUserAgent;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
 
 public class MDNReport {
 
+    public static final String LINE_END = "\r\n";
+    public static final String EXTENSION_DELIMITER = LINE_END;
+
     public static class Builder {
         private Optional<ReportingUserAgent> reportingUserAgentField = Optional.empty();
         private Optional<Gateway> gatewayField = Optional.empty();
@@ -41,6 +47,7 @@ public class MDNReport {
         private Optional<OriginalMessageId> originalMessageIdField = Optional.empty();
         private Optional<Disposition> dispositionField = Optional.empty();
         private Optional<Error> errorField = Optional.empty();
+        private ImmutableList.Builder<ExtensionField> extensionFields = ImmutableList.builder();
 
         public Builder reportingUserAgentField(ReportingUserAgent reportingUserAgentField)
{
             this.reportingUserAgentField = Optional.of(reportingUserAgentField);
@@ -88,6 +95,16 @@ public class MDNReport {
             return this;
         }
 
+        public Builder withExtensionField(ExtensionField extensionField) {
+            this.extensionFields.add(extensionField);
+            return this;
+        }
+
+        public Builder withExtensionFields(ExtensionField... extensionField) {
+            this.extensionFields.add(extensionField);
+            return this;
+        }
+
         public MDNReport build() {
             Preconditions.checkState(finalRecipientField.isPresent());
             Preconditions.checkState(dispositionField.isPresent());
@@ -96,7 +113,9 @@ public class MDNReport {
                 gatewayField, originalRecipientField,
                 finalRecipientField.get(),
                 originalMessageIdField,
-                dispositionField.get(), errorField);
+                dispositionField.get(),
+                errorField,
+                extensionFields.build());
         }
 
     }
@@ -105,8 +124,6 @@ public class MDNReport {
         return new Builder();
     }
 
-    public static final String LINE_END = "\r\n";
-
     private final Optional<ReportingUserAgent> reportingUserAgentField;
     private final Optional<Gateway> gatewayField;
     private final Optional<OriginalRecipient> originalRecipientField;
@@ -114,9 +131,11 @@ public class MDNReport {
     private final Optional<OriginalMessageId> originalMessageIdField;
     private final Disposition dispositionField;
     private final Optional<Error> errorField;
+    private final ImmutableList<ExtensionField> extensionFields;
 
     private MDNReport(Optional<ReportingUserAgent> reportingUserAgentField, Optional<Gateway>
gatewayField, Optional<OriginalRecipient> originalRecipientField,
-                      FinalRecipient finalRecipientField, Optional<OriginalMessageId>
originalMessageIdField, Disposition dispositionField, Optional<Error> errorField) {
+                      FinalRecipient finalRecipientField, Optional<OriginalMessageId>
originalMessageIdField, Disposition dispositionField, Optional<Error> errorField,
+                      ImmutableList<ExtensionField> extensionFields) {
         this.reportingUserAgentField = reportingUserAgentField;
         this.gatewayField = gatewayField;
         this.originalRecipientField = originalRecipientField;
@@ -124,6 +143,7 @@ public class MDNReport {
         this.originalMessageIdField = originalMessageIdField;
         this.dispositionField = dispositionField;
         this.errorField = errorField;
+        this.extensionFields = extensionFields;
     }
 
     public Optional<ReportingUserAgent> getReportingUserAgentField() {
@@ -161,7 +181,16 @@ public class MDNReport {
             + finalRecipientField.formattedValue() + LINE_END
             + originalMessageIdField.map(value -> value.formattedValue() + LINE_END).orElse("")
             + dispositionField.formattedValue() + LINE_END
-            + errorField.map(value -> value.formattedValue() + LINE_END).orElse("");
+            + errorField.map(value -> value.formattedValue() + LINE_END).orElse("")
+            + formattedExtensionValue();
+    }
 
+    private String formattedExtensionValue() {
+        if (extensionFields.isEmpty()) {
+            return "";
+        }
+        return extensionFields.stream()
+            .map(ExtensionField::formattedValue)
+            .collect(Collectors.joining(EXTENSION_DELIMITER)) + LINE_END;
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/034eef67/mdn/src/main/java/org/apache/james/mdn/fields/ExtensionField.java
----------------------------------------------------------------------
diff --git a/mdn/src/main/java/org/apache/james/mdn/fields/ExtensionField.java b/mdn/src/main/java/org/apache/james/mdn/fields/ExtensionField.java
new file mode 100644
index 0000000..65d9e84
--- /dev/null
+++ b/mdn/src/main/java/org/apache/james/mdn/fields/ExtensionField.java
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.mdn.fields;
+
+public class ExtensionField implements Field {
+    private final String fieldName;
+    private final String rawValue;
+
+    public ExtensionField(String fieldName, String rawValue) {
+        this.fieldName = fieldName;
+        this.rawValue = rawValue;
+    }
+
+    @Override
+    public String formattedValue() {
+        return fieldName + ": " +rawValue;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/034eef67/mdn/src/test/java/org/apache/james/mdn/MDNFactoryTest.java
----------------------------------------------------------------------
diff --git a/mdn/src/test/java/org/apache/james/mdn/MDNFactoryTest.java b/mdn/src/test/java/org/apache/james/mdn/MDNFactoryTest.java
index 24b81f1..7d7ac83 100644
--- a/mdn/src/test/java/org/apache/james/mdn/MDNFactoryTest.java
+++ b/mdn/src/test/java/org/apache/james/mdn/MDNFactoryTest.java
@@ -26,6 +26,7 @@ import java.util.Optional;
 import org.apache.james.mdn.action.mode.DispositionActionMode;
 import org.apache.james.mdn.fields.Disposition;
 import org.apache.james.mdn.fields.Error;
+import org.apache.james.mdn.fields.ExtensionField;
 import org.apache.james.mdn.fields.FinalRecipient;
 import org.apache.james.mdn.fields.Gateway;
 import org.apache.james.mdn.fields.OriginalMessageId;
@@ -667,4 +668,70 @@ public class MDNFactoryTest {
                 "Error: An error message\r\n" +
                 " on several lines\r\n");
     }
+
+    @Test
+    public void generateMDNReportShouldFormatOneExtension() {
+        Disposition disposition = Disposition.builder()
+            .actionMode(DispositionActionMode.Automatic)
+            .sendingMode(DispositionSendingMode.Automatic)
+            .type(DispositionType.Processed)
+            .addModifier(DispositionModifier.Error)
+            .addModifier(DispositionModifier.Failed)
+            .build();
+
+        String report = MDNReport.builder()
+            .reportingUserAgentField(new ReportingUserAgent(
+                "UA_name",
+                Optional.of("UA_product")))
+            .finalRecipientField(new FinalRecipient(Text.fromRawText("final_recipient")))
+            .originalRecipientField(new OriginalRecipient(Text.fromRawText("originalRecipient")))
+            .originalMessageIdField(new OriginalMessageId("original_message_id"))
+            .dispositionField(disposition)
+         .withExtensionField(new ExtensionField("X-OPENPAAS-IP", "177.177.177.77"))
+            .build()
+            .formattedValue();
+
+        assertThat(report)
+            .isEqualTo("Reporting-UA: UA_name; UA_product\r\n" +
+                "Original-Recipient: rfc822; originalRecipient\r\n" +
+                "Final-Recipient: rfc822; final_recipient\r\n" +
+                "Original-Message-ID: original_message_id\r\n" +
+                "Disposition: automatic-action/MDN-sent-automatically;processed/error,failed\r\n"
+
+                "X-OPENPAAS-IP: 177.177.177.77\r\n");
+    }
+
+
+    @Test
+    public void generateMDNReportShouldFormatManyExtensions() {
+        Disposition disposition = Disposition.builder()
+            .actionMode(DispositionActionMode.Automatic)
+            .sendingMode(DispositionSendingMode.Automatic)
+            .type(DispositionType.Processed)
+            .addModifier(DispositionModifier.Error)
+            .addModifier(DispositionModifier.Failed)
+            .build();
+
+        String report = MDNReport.builder()
+            .reportingUserAgentField(new ReportingUserAgent(
+                "UA_name",
+                Optional.of("UA_product")))
+            .finalRecipientField(new FinalRecipient(Text.fromRawText("final_recipient")))
+            .originalRecipientField(new OriginalRecipient(Text.fromRawText("originalRecipient")))
+            .originalMessageIdField(new OriginalMessageId("original_message_id"))
+            .dispositionField(disposition)
+            .withExtensionFields(
+                new ExtensionField("X-OPENPAAS-IP", "177.177.177.77"),
+                new ExtensionField("X-OPENPAAS-PORT", "8000"))
+            .build()
+            .formattedValue();
+
+        assertThat(report)
+            .isEqualTo("Reporting-UA: UA_name; UA_product\r\n" +
+                "Original-Recipient: rfc822; originalRecipient\r\n" +
+                "Final-Recipient: rfc822; final_recipient\r\n" +
+                "Original-Message-ID: original_message_id\r\n" +
+                "Disposition: automatic-action/MDN-sent-automatically;processed/error,failed\r\n"
+
+                "X-OPENPAAS-IP: 177.177.177.77\r\n" +
+                "X-OPENPAAS-PORT: 8000\r\n");
+    }
 }


---------------------------------------------------------------------
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