james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [3/5] james-project git commit: JAMES-2271 Added delay support in MailetContext.sendMail
Date Thu, 04 Jan 2018 02:24:50 GMT
JAMES-2271 Added delay support in MailetContext.sendMail


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

Branch: refs/heads/master
Commit: 4fd780eef11a56fa1fbfdfdb743dd29fa3069ca7
Parents: 71284f5
Author: apptaro <apptaro@gmail.com>
Authored: Fri Dec 22 13:52:52 2017 +0900
Committer: benwa <btellier@linagora.com>
Committed: Thu Jan 4 09:22:36 2018 +0700

----------------------------------------------------------------------
 .../java/org/apache/mailet/MailetContext.java   | 36 +++++++++++++++
 .../mailet/base/test/FakeMailContext.java       | 13 ++++++
 .../impl/JamesMailetContext.java                | 13 ++++++
 .../impl/JamesMailetContextTest.java            | 46 ++++++++++++++++++++
 4 files changed, 108 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4fd780ee/mailet/api/src/main/java/org/apache/mailet/MailetContext.java
----------------------------------------------------------------------
diff --git a/mailet/api/src/main/java/org/apache/mailet/MailetContext.java b/mailet/api/src/main/java/org/apache/mailet/MailetContext.java
index e43b201..11d658c 100644
--- a/mailet/api/src/main/java/org/apache/mailet/MailetContext.java
+++ b/mailet/api/src/main/java/org/apache/mailet/MailetContext.java
@@ -23,6 +23,7 @@ package org.apache.mailet;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
@@ -338,6 +339,21 @@ public interface MailetContext {
     void sendMail(Mail mail)
             throws MessagingException;
 
+    /**
+     * Sends an outgoing message to the top of this mailet container's root queue.
+     * This is the equivalent of opening an SMTP session to localhost.
+     * The Mail object provides all envelope and content information
+     * 
+     * The given delay and unit are used to calculate the time when
+     * the Mail will be available for deQueue
+     *
+     * @param mail - the message that is to sent
+     * @param delay - the delay value for deQueue
+     * @param unit - the delay unit for deQueue
+     * @throws MessagingException if an error occurs accessing or sending the message
+     */
+    void sendMail(Mail mail, long delay, TimeUnit unit)
+            throws MessagingException;
 
 
     /**
@@ -354,6 +370,26 @@ public interface MailetContext {
      * @throws MessagingException if an error occurs accessing or sending the message
      */
     void sendMail(Mail mail, String state) throws MessagingException;
+    
+    /**
+     * Sends an outgoing message to the top of this mailet container's root queue,
+     * targeting a specific processing state.
+     *
+     * This functionally allows mail treatment done out of the MailetProcessor to be sent
+     * to a specific processor inside the MailetContainer. This is for instance useful for
bouncing mail
+     * being remote delivered (asynchronously to original mail treatment)
+     * 
+     * The given delay and unit are used to calculate the time when
+     * the Mail will be available for deQueue
+     *
+     * @param message The message to send
+     * @param state   The state of the message, indicating the name of the processor for
+     *                which the message will be queued
+     * @param delay   The delay value for deQueue
+     * @param unit    The delay unit for deQueue
+     * @throws MessagingException if an error occurs accessing or sending the message
+     */
+    void sendMail(Mail mail, String state, long delay, TimeUnit unit) throws MessagingException;
 
     /**
      * Bounces the message using a standard format with the given message.

http://git-wip-us.apache.org/repos/asf/james-project/blob/4fd780ee/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
index 62d2c9d..3283d9b 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
@@ -27,6 +27,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.concurrent.TimeUnit;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
@@ -404,16 +405,28 @@ public class FakeMailContext implements MailetContext {
             .build());
     }
 
+    @Override
     public void sendMail(Mail mail) throws MessagingException {
         sendMail(mail, Mail.DEFAULT);
     }
 
     @Override
+    public void sendMail(Mail mail, long delay, TimeUnit unit) throws MessagingException
{
+        sendMail(mail, Mail.DEFAULT, delay, unit);
+    }
+
+    @Override
     public void sendMail(Mail mail, String state) throws MessagingException {
         mail.setState(state);
         sentMails.add(fromMail(mail));
     }
 
+    @Override
+    public void sendMail(Mail mail, String state, long delay, TimeUnit unit) throws MessagingException
{
+        mail.setState(state);
+        sentMails.add(fromMail(mail)); // FIXME delay ignored here for now
+    }
+
     public void setAttribute(String name, Serializable object) {
         attributes.put(name,object);
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/4fd780ee/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
index 70f2411..c445af0 100644
--- a/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
+++ b/server/mailet/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/JamesMailetContext.java
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
 
 import javax.inject.Inject;
 import javax.mail.Address;
@@ -400,11 +401,23 @@ public class JamesMailetContext implements MailetContext, Configurable
{
     }
 
     @Override
+    public void sendMail(Mail mail, long delay, TimeUnit unit) throws MessagingException
{
+        sendMail(mail, Mail.DEFAULT, delay, unit);
+    }
+
+    @Override
     public void sendMail(Mail mail, String state) throws MessagingException {
         mail.setAttribute(Mail.SENT_BY_MAILET, "true");
         mail.setState(state);
         rootMailQueue.enQueue(mail);
     }
+    
+    @Override
+    public void sendMail(Mail mail, String state, long delay, TimeUnit unit) throws MessagingException
{
+        mail.setAttribute(Mail.SENT_BY_MAILET, "true");
+        mail.setState(state);
+        rootMailQueue.enQueue(mail, delay, unit);
+    }
 
     public void sendMail(MailAddress sender, Collection<MailAddress> recipients, MimeMessage
message, String state) throws MessagingException {
         MailImpl mail = new MailImpl(MailImpl.getId(), sender, recipients, message);

http://git-wip-us.apache.org/repos/asf/james-project/blob/4fd780ee/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java
b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java
index 61abeeb..5341325 100644
--- a/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java
+++ b/server/mailet/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/JamesMailetContextTest.java
@@ -27,6 +27,8 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
+import java.util.concurrent.TimeUnit;
+
 import javax.mail.internet.MimeMessage;
 
 import org.apache.commons.configuration.HierarchicalConfiguration;
@@ -40,7 +42,9 @@ import org.apache.james.server.core.MailImpl;
 import org.apache.james.user.memory.MemoryUsersRepository;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.test.MimeMessageBuilder;
+import org.assertj.core.api.JUnitSoftAssertions;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
@@ -53,6 +57,9 @@ public class JamesMailetContextTest {
     public static final String PASSWORD = "password";
     public static final DNSService DNS_SERVICE = null;
 
+    @Rule
+    public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
+    
     private MemoryDomainList domainList;
     private MemoryUsersRepository usersRepository;
     private JamesMailetContext testee;
@@ -215,6 +222,45 @@ public class JamesMailetContextTest {
     }
 
     @Test
+    public void sendMailShouldEnqueueEmailWithRootStateAndDelayWhenSpecified() throws Exception
{
+        MailImpl mail = new MailImpl();
+        mail.setSender(mailAddress);
+        mail.setRecipients(ImmutableList.of(mailAddress));
+        mail.setMessage(MimeMessageBuilder.defaultMimeMessage());
+        testee.sendMail(mail, 5, TimeUnit.MINUTES);
+
+        ArgumentCaptor<Mail> mailArgumentCaptor = ArgumentCaptor.forClass(Mail.class);
+        ArgumentCaptor<Long> delayArgumentCaptor = ArgumentCaptor.forClass(Long.class);
+        ArgumentCaptor<TimeUnit> timeUnitArgumentCaptor = ArgumentCaptor.forClass(TimeUnit.class);
+        verify(spoolMailQueue).enQueue(mailArgumentCaptor.capture(), delayArgumentCaptor.capture(),
timeUnitArgumentCaptor.capture());
+        verifyNoMoreInteractions(spoolMailQueue);
+
+        softly.assertThat(mailArgumentCaptor.getValue().getState()).isEqualTo(Mail.DEFAULT);
+        softly.assertThat(delayArgumentCaptor.getValue()).isEqualTo(5L);
+        softly.assertThat(timeUnitArgumentCaptor.getValue()).isEqualTo(TimeUnit.MINUTES);
+    }
+
+    @Test
+    public void sendMailShouldEnqueueEmailWithOtherStateAndDelayWhenSpecified() throws Exception
{
+        MailImpl mail = new MailImpl();
+        mail.setSender(mailAddress);
+        mail.setRecipients(ImmutableList.of(mailAddress));
+        mail.setMessage(MimeMessageBuilder.defaultMimeMessage());
+        String other = "other";
+        testee.sendMail(mail, other, 5, TimeUnit.MINUTES);
+
+        ArgumentCaptor<Mail> mailArgumentCaptor = ArgumentCaptor.forClass(Mail.class);
+        ArgumentCaptor<Long> delayArgumentCaptor = ArgumentCaptor.forClass(Long.class);
+        ArgumentCaptor<TimeUnit> timeUnitArgumentCaptor = ArgumentCaptor.forClass(TimeUnit.class);
+        verify(spoolMailQueue).enQueue(mailArgumentCaptor.capture(), delayArgumentCaptor.capture(),
timeUnitArgumentCaptor.capture());
+        verifyNoMoreInteractions(spoolMailQueue);
+
+        softly.assertThat(mailArgumentCaptor.getValue().getState()).isEqualTo(other);
+        softly.assertThat(delayArgumentCaptor.getValue()).isEqualTo(5L);
+        softly.assertThat(timeUnitArgumentCaptor.getValue()).isEqualTo(TimeUnit.MINUTES);
+    }
+
+    @Test
     public void sendMailForMessageShouldEnqueueEmailWithRootState() throws Exception {
         MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
             .addFrom(mailAddress.asString())


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