Skip to content

Commit

Permalink
JAMES-1854 Rework MailDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Nov 18, 2016
1 parent 135f8e1 commit 58ef3d2
Showing 1 changed file with 25 additions and 33 deletions.
Expand Up @@ -34,9 +34,6 @@

import com.google.common.base.Preconditions;

/**
* Contains resource bindings.
*/
public class MailDispatcher {

public static final String DELIVERED_TO = "Delivered-To";
Expand Down Expand Up @@ -92,32 +89,40 @@ private MailDispatcher(MailStorer mailStorer, boolean consume, Log log, MailetCo
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());
Expand All @@ -128,19 +133,6 @@ public void dispatch(Mail mail) throws MessagingException {
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);
Expand Down

0 comments on commit 58ef3d2

Please sign in to comment.