JAMES-1854 Rework MailDispatcher

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

Branch: refs/heads/master
Commit: 58ef3d2187309b38027a6adca56c71ca74e30766
Parents: 135f8e1
Author: Benoit Tellier <[email protected]>
Authored: Fri Oct 28 09:30:16 2016 +0200
Committer: Benoit Tellier <[email protected]>
Committed: Fri Nov 18 18:46:46 2016 +0700

----------------------------------------------------------------------
 .../mailets/delivery/MailDispatcher.java        | 58 +++++++++-----------
 1 file changed, 25 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/58ef3d21/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 fe62364..a2d3aa2 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
@@ -34,9 +34,6 @@ import org.apache.mailet.base.RFC2822Headers;
 
 import com.google.common.base.Preconditions;
 
-/**
- * Contains resource bindings.
- */
 public class MailDispatcher {
 
     public static final String DELIVERED_TO = "Delivered-To";
@@ -92,32 +89,40 @@ public class MailDispatcher {
         this.mailetContext = mailetContext;
     }
 
-    /**
-     * Delivers a mail to a local mailbox.
-     *
-     * @param mail
-     *            the mail being processed
-     *
-     * @throws MessagingException
-     *             if an error occurs while storing the mail
-     */
-    @SuppressWarnings("unchecked")
     public void dispatch(Mail mail) throws MessagingException {
-        Collection<MailAddress> recipients = mail.getRecipients();
         Collection<MailAddress> errors = new Vector<MailAddress>();
+        dispatchNeedingErrorsManaged(mail, errors);
+        if (!errors.isEmpty()) {
+            // If there were errors, we redirect the email to the ERROR
+            // processor.
+            // In order for this server to meet the requirements of the SMTP
+            // specification, mails on the ERROR processor must be returned to
+            // the sender. Note that this email doesn't include any details
+            // regarding the details of the failure(s).
+            // In the future we may wish to address this.
+            mailetContext.sendMail(mail.getSender(), errors, 
mail.getMessage(), Mail.ERROR);
+        }
+    }
 
+    @SuppressWarnings("unchecked")
+    private void dispatchNeedingErrorsManaged(Mail mail, 
Collection<MailAddress> errors) throws MessagingException {
         MimeMessage message = mail.getMessage();
-
-        // Set Return-Path and remove all other Return-Path headers from the
-        // message
-        // This only works because there is a placeholder inserted by
-        // MimeMessageWrapper
+        // Set Return-Path and remove all other Return-Path headers from the 
message
+        // This only works because there is a placeholder inserted by 
MimeMessageWrapper
         message.setHeader(RFC2822Headers.RETURN_PATH, 
DeliveryUtils.prettyPrint(mail.getSender()));
 
         List<String> deliveredToHeader = 
Collections.list(message.getMatchingHeaders(new String[] { DELIVERED_TO }));
         message.removeHeader(DELIVERED_TO);
 
-        for (MailAddress recipient : recipients) {
+        dispatchNeedingSavedDeliveredToHeader(mail, errors, message);
+
+        for (String deliveredTo : deliveredToHeader) {
+            message.addHeader(DELIVERED_TO, deliveredTo);
+        }
+    }
+
+    private void dispatchNeedingSavedDeliveredToHeader(Mail mail, 
Collection<MailAddress> errors, MimeMessage message) {
+        for (MailAddress recipient : mail.getRecipients()) {
             try {
                 // Add qmail's de facto standard Delivered-To header
                 message.addHeader(DELIVERED_TO, recipient.toString());
@@ -128,19 +133,6 @@ public class MailDispatcher {
                 errors.add(recipient);
             }
         }
-        for (String deliveredTo : deliveredToHeader) {
-            message.addHeader(DELIVERED_TO, deliveredTo);
-        }
-        if (!errors.isEmpty()) {
-            // If there were errors, we redirect the email to the ERROR
-            // processor.
-            // In order for this server to meet the requirements of the SMTP
-            // specification, mails on the ERROR processor must be returned to
-            // the sender. Note that this email doesn't include any details
-            // regarding the details of the failure(s).
-            // In the future we may wish to address this.
-            mailetContext.sendMail(mail.getSender(), errors, 
mail.getMessage(), Mail.ERROR);
-        }
         if (consume) {
             // Consume this message
             mail.setState(Mail.GHOST);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to