JAMES-2340 Add a WithStorageDirective mailet

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

Branch: refs/heads/master
Commit: a40b1c23cf95d16eb73f2af1a6c99a3a19b7549d
Parents: 9a92600
Author: benwa <btell...@linagora.com>
Authored: Wed Mar 21 16:17:41 2018 +0700
Committer: benwa <btell...@linagora.com>
Committed: Tue Mar 27 15:17:37 2018 +0700

----------------------------------------------------------------------
 .../transport/mailets/WithStorageDirective.java |  87 ++++++++++++++
 .../mailets/WithStorageDirectiveTest.java       | 117 +++++++++++++++++++
 2 files changed, 204 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/a40b1c23/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithStorageDirective.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithStorageDirective.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithStorageDirective.java
new file mode 100644
index 0000000..4070050
--- /dev/null
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithStorageDirective.java
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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.inject.Inject;
+import javax.mail.MessagingException;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.transport.mailets.delivery.MailStore;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+import com.github.fge.lambdas.consumers.ThrowingConsumer;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+
+/**
+ * WithStorageDirective position storage directive for the recipients of this 
email.
+ *
+ * These directives are used by <strong>LocalDelivery</strong> mailet when 
adding the email to the recipients mailboxes.
+ *
+ * The following storage directives can be set:
+ *  - targetFolderName: the folder to append the email in. (compulsory)
+ *
+ *  Example:
+ *
+ *  <mailet matcher="IsMarkedAsSpam" class="WithStorageDirective">
+ *      <targetFolderName>Spam</targetFolderName>
+ *  </mailet>
+ */
+public class WithStorageDirective extends GenericMailet {
+
+    public static final String TARGET_FOLDER_NAME = "targetFolderName";
+
+    private final UsersRepository usersRepository;
+
+    private String targetFolderName;
+
+    @Inject
+    public WithStorageDirective(UsersRepository usersRepository) {
+        this.usersRepository = usersRepository;
+    }
+
+    @Override
+    public void init() throws MessagingException {
+        targetFolderName = getInitParameter(TARGET_FOLDER_NAME);
+        validateMailetConfiguration();
+    }
+
+    public void validateMailetConfiguration() {
+        Preconditions.checkState(!Strings.isNullOrEmpty(targetFolderName), 
"You need to specify " + TARGET_FOLDER_NAME);
+    }
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        mail.getRecipients()
+            .forEach(addStorageDirective(mail));
+    }
+
+    public ThrowingConsumer<MailAddress> addStorageDirective(Mail mail) {
+        return recipient -> {
+            String attributeNameForUser = MailStore.DELIVERY_PATH_PREFIX + 
usersRepository.getUser(recipient);
+            mail.setAttribute(
+                attributeNameForUser,
+                targetFolderName);
+        };
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/a40b1c23/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithStorageDirectiveTest.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithStorageDirectiveTest.java
 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithStorageDirectiveTest.java
new file mode 100644
index 0000000..9fcebe5
--- /dev/null
+++ 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithStorageDirectiveTest.java
@@ -0,0 +1,117 @@
+/****************************************************************
+ * 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 static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.user.memory.MemoryUsersRepository;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.assertj.core.api.JUnitSoftAssertions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class WithStorageDirectiveTest {
+
+    private WithStorageDirective testee;
+
+    @Rule
+    public JUnitSoftAssertions softly = new JUnitSoftAssertions();
+
+    @Before
+    public void setUp() {
+        testee = new 
WithStorageDirective(MemoryUsersRepository.withVirtualHosting());
+    }
+
+    @Test
+    public void initShouldThrowWhenNoTargetFolderEntry() {
+        assertThatThrownBy(() -> testee.init(FakeMailetConfig.builder()
+            .build()))
+            .isInstanceOf(IllegalStateException.class);
+    }
+
+    @Test
+    public void initShouldThrowWhenEmptyTargetFolderEntry() {
+        assertThatThrownBy(() -> testee.init(FakeMailetConfig.builder()
+            .setProperty(WithStorageDirective.TARGET_FOLDER_NAME, "")
+            .build()))
+            .isInstanceOf(IllegalStateException.class);
+    }
+
+    @Test
+    public void serviceShouldAddDeliveryPathForRecipients() throws Exception {
+        String targetFolderName = "Spam";
+        testee.init(FakeMailetConfig.builder()
+            .setProperty(WithStorageDirective.TARGET_FOLDER_NAME, 
targetFolderName)
+            .build());
+
+        FakeMail mail = FakeMail.builder()
+            .recipients(MailAddressFixture.RECIPIENT1, 
MailAddressFixture.RECIPIENT2)
+            .build();
+
+        testee.service(mail);
+
+        softly.assertThat(mail.getAttributeNames())
+            .containsOnly("DeliveryPath_recipient2@localhost", 
"DeliveryPath_recipient1@localhost");
+        
softly.assertThat(mail.getAttribute("DeliveryPath_recipient1@localhost")).isEqualTo(targetFolderName);
+        
softly.assertThat(mail.getAttribute("DeliveryPath_recipient2@localhost")).isEqualTo(targetFolderName);
+    }
+
+    @Test
+    public void serviceShouldNotThrowWhenNoRecipients() throws Exception {
+        String targetFolderName = "Spam";
+        testee.init(FakeMailetConfig.builder()
+            .setProperty(WithStorageDirective.TARGET_FOLDER_NAME, 
targetFolderName)
+            .build());
+
+        FakeMail mail = FakeMail.builder()
+            .recipients()
+            .build();
+
+        testee.service(mail);
+
+        assertThat(mail.getAttributeNames())
+            .isEmpty();
+    }
+
+    @Test
+    public void serviceShouldOverridePreviousStorageDirectives() throws 
Exception {
+        String targetFolderName = "Spam";
+        testee.init(FakeMailetConfig.builder()
+            .setProperty(WithStorageDirective.TARGET_FOLDER_NAME, 
targetFolderName)
+            .build());
+
+        FakeMail mail = FakeMail.builder()
+            .recipients(MailAddressFixture.RECIPIENT1, 
MailAddressFixture.RECIPIENT2)
+            .attribute("DeliveryPath_recipient2@localhost", "otherFolder")
+            .build();
+
+        testee.service(mail);
+
+        softly.assertThat(mail.getAttributeNames())
+            .containsOnly("DeliveryPath_recipient2@localhost", 
"DeliveryPath_recipient1@localhost");
+        
softly.assertThat(mail.getAttribute("DeliveryPath_recipient1@localhost")).isEqualTo(targetFolderName);
+        
softly.assertThat(mail.getAttribute("DeliveryPath_recipient2@localhost")).isEqualTo(targetFolderName);
+    }
+
+}
\ No newline at end of file


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