MAILET-153 Extract DeliveredTo customization logic out of LocalDelivery

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

Branch: refs/heads/master
Commit: 65f0b6383371e72399338c9813d3da88cc717bd8
Parents: d1a9f8d
Author: benwa <btell...@linagora.com>
Authored: Sat Apr 8 10:35:04 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Tue Apr 11 07:53:48 2017 +0700

----------------------------------------------------------------------
 .../destination/conf/mailetcontainer.xml        |  1 +
 .../destination/conf/mailetcontainer.xml        |  1 +
 .../jpa/destination/conf/mailetcontainer.xml    |  1 +
 .../spring/destination/conf/mailetcontainer.xml |  1 +
 .../transport/mailets/AddDeliveredToHeader.java | 42 ++++++++++
 .../mailets/AddDeliveredToHeaderTest.java       | 81 ++++++++++++++++++++
 .../main/resources/mailetcontainer-template.xml |  1 +
 .../james/mailets/AddDeliveredToHeaderTest.java |  4 +-
 .../mailets/configuration/CommonProcessors.java |  4 +
 .../mailets/delivery/MailDispatcher.java        |  7 +-
 .../mailets/delivery/MailDispatcherTest.java    | 50 ------------
 11 files changed, 135 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git 
a/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml 
b/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
index 25d02d4..f3e466a 100644
--- a/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
@@ -89,6 +89,7 @@
             <mailet match="All" class="RecipientRewriteTable" />
             <mailet match="RecipientIsLocal" 
class="org.apache.james.jmap.mailet.VacationMailet"/>
             <mailet match="RecipientIsLocal" class="Sieve"/>
+            <mailet match="RecipientIsLocal" class="AddDeliveredToHeader"/>
             <mailet match="RecipientIsLocal" class="LocalDelivery"/>
             <!--
             <mailet match="HostIsLocal" class="ToProcessor">

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git 
a/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml 
b/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
index 25d02d4..f3e466a 100644
--- a/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
@@ -89,6 +89,7 @@
             <mailet match="All" class="RecipientRewriteTable" />
             <mailet match="RecipientIsLocal" 
class="org.apache.james.jmap.mailet.VacationMailet"/>
             <mailet match="RecipientIsLocal" class="Sieve"/>
+            <mailet match="RecipientIsLocal" class="AddDeliveredToHeader"/>
             <mailet match="RecipientIsLocal" class="LocalDelivery"/>
             <!--
             <mailet match="HostIsLocal" class="ToProcessor">

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml 
b/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
index 06f6f5c..35fca6a 100644
--- a/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
@@ -84,6 +84,7 @@
                 <name>bcc</name>
             </mailet>
             <mailet match="All" class="RecipientRewriteTable" />
+            <mailet match="RecipientIsLocal" class="AddDeliveredToHeader"/>
             <mailet match="RecipientIsLocal" class="LocalDelivery"/>
             <mailet match="SMTPAuthSuccessful" class="RemoteDelivery">
                 <outgoingQueue>outgoing</outgoingQueue>

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/spring/destination/conf/mailetcontainer.xml 
b/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
index 710fd8c..498e1ac 100644
--- a/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
@@ -82,6 +82,7 @@
             </mailet>
             <mailet match="All" class="RecipientRewriteTable" />
             <mailet match="RecipientIsLocal" class="Sieve"/>
+            <mailet match="RecipientIsLocal" class="AddDeliveredToHeader"/>
             <mailet match="RecipientIsLocal" class="LocalDelivery"/>
             <mailet match="HostIsLocal" class="ToProcessor">
                 <processor>local-address-error</processor>

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddDeliveredToHeader.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddDeliveredToHeader.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddDeliveredToHeader.java
new file mode 100644
index 0000000..ac339b1
--- /dev/null
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/AddDeliveredToHeader.java
@@ -0,0 +1,42 @@
+/****************************************************************
+ * 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.transport.mailets;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.PerRecipientHeaders;
+import org.apache.mailet.base.GenericMailet;
+
+public class AddDeliveredToHeader extends GenericMailet {
+
+    public static final String DELIVERED_TO = "Delivered-To";
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        for (MailAddress recipient: mail.getRecipients()) {
+            
mail.addSpecificHeaderForRecipient(PerRecipientHeaders.Header.builder()
+                .name(DELIVERED_TO)
+                .value(recipient.asString())
+                .build(), recipient);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddDeliveredToHeaderTest.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddDeliveredToHeaderTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddDeliveredToHeaderTest.java
new file mode 100644
index 0000000..4e95400
--- /dev/null
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddDeliveredToHeaderTest.java
@@ -0,0 +1,81 @@
+/****************************************************************
+ * 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.transport.mailets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.mailet.PerRecipientHeaders;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.Before;
+import org.junit.Test;
+
+public class AddDeliveredToHeaderTest {
+
+    private AddDeliveredToHeader testee;
+
+    @Before
+    public void setUp() throws Exception {
+        testee = new AddDeliveredToHeader();
+        testee.init(FakeMailetConfig.builder()
+            .mailetName("AddDeliveredToHeader")
+            .mailetContext(FakeMailContext.defaultContext())
+            .build());
+    }
+
+    @Test
+    public void serviceShouldHandleMailWithoutRecipient() throws Exception {
+        FakeMail mail = FakeMail.builder().build();
+
+        testee.service(mail);
+
+        assertThat(mail.getPerRecipientSpecificHeaders())
+            .isEqualTo(new PerRecipientHeaders());
+    }
+
+    @Test
+    public void serviceShouldAddPerRecipientDeliveredToSpecificHeader() throws 
Exception {
+        FakeMail mail = FakeMail.builder()
+            .recipients(MailAddressFixture.ANY_AT_JAMES, 
MailAddressFixture.OTHER_AT_JAMES)
+            .build();
+
+        testee.service(mail);
+
+        PerRecipientHeaders expectedResult = new PerRecipientHeaders();
+        expectedResult.addHeaderForRecipient(
+            PerRecipientHeaders.Header.builder()
+                .name(AddDeliveredToHeader.DELIVERED_TO)
+                .value(MailAddressFixture.ANY_AT_JAMES.asString())
+                .build(),
+            MailAddressFixture.ANY_AT_JAMES);
+        expectedResult.addHeaderForRecipient(
+            PerRecipientHeaders.Header.builder()
+                .name(AddDeliveredToHeader.DELIVERED_TO)
+                .value(MailAddressFixture.OTHER_AT_JAMES.asString())
+                .build(),
+            MailAddressFixture.OTHER_AT_JAMES);
+
+        assertThat(mail.getPerRecipientSpecificHeaders())
+            .isEqualTo(expectedResult);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/server/app/src/main/resources/mailetcontainer-template.xml
----------------------------------------------------------------------
diff --git a/server/app/src/main/resources/mailetcontainer-template.xml 
b/server/app/src/main/resources/mailetcontainer-template.xml
index 2e3901a..fd205d2 100644
--- a/server/app/src/main/resources/mailetcontainer-template.xml
+++ b/server/app/src/main/resources/mailetcontainer-template.xml
@@ -431,6 +431,7 @@ Regards, Postmaster XXX.YYY
        
        <!-- Is the recipient is for a local account, deliver it locally -->
        <mailet match="RecipientIsLocal" class="Sieve"/>
+       <mailet match="RecipientIsLocal" class="AddDeliveredToHeader"/>
        <mailet match="RecipientIsLocal" class="LocalDelivery"/>
 
        <!-- If the host is handled by this server and it did not get -->

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/AddDeliveredToHeaderTest.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/AddDeliveredToHeaderTest.java
 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/AddDeliveredToHeaderTest.java
index f72adf1..be54831 100644
--- 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/AddDeliveredToHeaderTest.java
+++ 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/AddDeliveredToHeaderTest.java
@@ -25,7 +25,7 @@ import org.apache.james.mailets.configuration.MailetContainer;
 import org.apache.james.mailets.utils.SMTPMessageSender;
 import org.apache.james.modules.MailboxProbeImpl;
 import org.apache.james.probe.DataProbe;
-import org.apache.james.transport.mailets.delivery.MailDispatcher;
+import org.apache.james.transport.mailets.AddDeliveredToHeader;
 import org.apache.james.utils.DataProbeImpl;
 import org.apache.james.utils.IMAPMessageReader;
 import org.junit.After;
@@ -100,7 +100,7 @@ public class AddDeliveredToHeaderTest {
             messageSender.sendMessage(from, recipient);
             
calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
             calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> 
imapMessageReader.readFirstMessageHeadersInInbox(recipient, PASSWORD)
-                .contains(MailDispatcher.DELIVERED_TO + ": " + recipient));
+                .contains(AddDeliveredToHeader.DELIVERED_TO + ": " + 
recipient));
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/CommonProcessors.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/CommonProcessors.java
 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/CommonProcessors.java
index a259504..0c042de 100644
--- 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/CommonProcessors.java
+++ 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/CommonProcessors.java
@@ -124,6 +124,10 @@ public class CommonProcessors {
                         .build())
                 .addMailet(MailetConfiguration.builder()
                         .match("RecipientIsLocal")
+                        .clazz("AddDeliveredToHeader")
+                        .build())
+                .addMailet(MailetConfiguration.builder()
+                        .match("RecipientIsLocal")
                         .clazz("LocalDelivery")
                         .build())
                 .addMailet(MailetConfiguration.builder()

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/delivery/MailDispatcher.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/delivery/MailDispatcher.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/delivery/MailDispatcher.java
index a6f48a5..5579f90 100644
--- 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/delivery/MailDispatcher.java
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/delivery/MailDispatcher.java
@@ -37,11 +37,9 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Iterables;
 
 public class MailDispatcher {
 
-    public static final String DELIVERED_TO = "Delivered-To";
     public static final String[] NO_HEADERS = {};
 
     public static Builder builder() {
@@ -145,8 +143,7 @@ public class MailDispatcher {
 
     private Map<String, List<String>> saveHeaders(Mail mail, MailAddress 
recipient) throws MessagingException {
         ImmutableMap.Builder<String, List<String>> backup = 
ImmutableMap.builder();
-        Collection<String> headersForRecipient = 
mail.getPerRecipientSpecificHeaders().getHeaderNamesForRecipient(recipient);
-        Iterable<String> headersToSave = Iterables.concat(headersForRecipient, 
ImmutableList.of(DELIVERED_TO));
+        Collection<String> headersToSave = 
mail.getPerRecipientSpecificHeaders().getHeaderNamesForRecipient(recipient);
         for (String headerName: headersToSave) {
             List<String> values = ImmutableList.copyOf(
                         
Optional.fromNullable(mail.getMessage().getHeader(headerName))
@@ -170,7 +167,5 @@ public class MailDispatcher {
         for (Header header: 
mail.getPerRecipientSpecificHeaders().getHeadersForRecipient(recipient)) {
             message.addHeader(header.getName(), header.getValue());
         }
-        // Add qmail's de facto standard Delivered-To header
-        message.addHeader(DELIVERED_TO, recipient.toString());
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/65f0b638/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
index 73cca4e..56f16be 100644
--- 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
+++ 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
@@ -188,56 +188,6 @@ public class MailDispatcherTest {
     }
 
     @Test
-    public void dispatchShouldPreserveDeliveredTo() throws Exception {
-        MailDispatcher testee = MailDispatcher.builder()
-            .log(mock(Log.class))
-            .mailetContext(fakeMailContext)
-            .mailStore(mailStore)
-            .consume(false)
-            .build();
-
-        String delivered_to_1 = "delivered_to_1";
-        String delivered_to_2 = "delivered_to_2";
-        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
-            .addHeaders(new 
MimeMessageBuilder.Header(MailDispatcher.DELIVERED_TO, delivered_to_1),
-                new MimeMessageBuilder.Header(MailDispatcher.DELIVERED_TO, 
delivered_to_2))
-            .build();
-        FakeMail mail = FakeMail.builder()
-            .sender(MailAddressFixture.OTHER_AT_JAMES)
-            .recipients(MailAddressFixture.ANY_AT_JAMES)
-            .mimeMessage(mimeMessage)
-            .state("state")
-            .build();
-        testee.dispatch(mail);
-
-        
assertThat(mimeMessage.getHeader(MailDispatcher.DELIVERED_TO)).containsOnly(delivered_to_1,
 delivered_to_2);
-    }
-
-    @Test
-    public void dispatchShouldCustomizeDeliveredToHeader() throws Exception {
-        AccumulatorHeaderMailStore accumulatorDeliveredToHeaderMailStore = new 
AccumulatorHeaderMailStore(MailDispatcher.DELIVERED_TO);
-        MailDispatcher testee = MailDispatcher.builder()
-            .log(mock(Log.class))
-            .mailetContext(fakeMailContext)
-            .mailStore(accumulatorDeliveredToHeaderMailStore)
-            .consume(false)
-            .build();
-
-        FakeMail mail = FakeMail.builder()
-            .sender(MailAddressFixture.OTHER_AT_JAMES)
-            .recipients(MailAddressFixture.ANY_AT_JAMES, 
MailAddressFixture.ANY_AT_JAMES2)
-            .mimeMessage(MimeMessageBuilder.defaultMimeMessage())
-            .state("state")
-            .build();
-        testee.dispatch(mail);
-
-        
assertThat(accumulatorDeliveredToHeaderMailStore.getHeaderValues(MailAddressFixture.ANY_AT_JAMES))
-            .containsOnly(new 
String[]{MailAddressFixture.ANY_AT_JAMES.toString()});
-        
assertThat(accumulatorDeliveredToHeaderMailStore.getHeaderValues(MailAddressFixture.ANY_AT_JAMES2))
-            .containsOnly(new 
String[]{MailAddressFixture.ANY_AT_JAMES2.toString()});
-    }
-
-    @Test
     public void dispatchShouldNotAddSpecificHeaderIfRecipientDoesNotMatch() 
throws Exception {
         AccumulatorHeaderMailStore accumulatorTestHeaderMailStore = new 
AccumulatorHeaderMailStore(TEST_HEADER_NAME);
         MailDispatcher testee = MailDispatcher.builder()


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