Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-1854 Sieve should be handled as a separate mailet
- Loading branch information
Showing
31 changed files
with
593 additions
and
784 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SieveDelivery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,98 @@ | |||
/**************************************************************** | |||
* 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.mailets; | |||
|
|||
import org.apache.james.mailbox.model.MailboxConstants; | |||
import org.apache.james.mailets.configuration.CommonProcessors; | |||
import org.apache.james.mailets.configuration.MailetContainer; | |||
import org.apache.james.mailets.utils.IMAPMessageReader; | |||
import org.apache.james.mailets.utils.SMTPMessageSender; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
|
|||
import com.jayway.awaitility.Awaitility; | |||
import com.jayway.awaitility.Duration; | |||
import com.jayway.awaitility.core.ConditionFactory; | |||
|
|||
public class SieveDelivery { | |||
|
|||
private static final String DEFAULT_DOMAIN = "james.org"; | |||
private static final String LOCALHOST_IP = "127.0.0.1"; | |||
private static final int IMAP_PORT = 1143; | |||
private static final int SMTP_PORT = 1025; | |||
private static final String PASSWORD = "secret"; | |||
|
|||
@Rule | |||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | |||
|
|||
private TemporaryJamesServer jamesServer; | |||
private ConditionFactory calmlyAwait; | |||
|
|||
@Before | |||
public void setup() throws Exception { | |||
MailetContainer mailetContainer = MailetContainer.builder() | |||
.postmaster("postmaster@" + DEFAULT_DOMAIN) | |||
.threads(5) | |||
.addProcessor(CommonProcessors.root()) | |||
.addProcessor(CommonProcessors.error()) | |||
.addProcessor(CommonProcessors.transport()) | |||
.addProcessor(CommonProcessors.spam()) | |||
.addProcessor(CommonProcessors.localAddressError()) | |||
.addProcessor(CommonProcessors.relayDenied()) | |||
.addProcessor(CommonProcessors.bounces()) | |||
.addProcessor(CommonProcessors.sieveManagerCheck()) | |||
.build(); | |||
|
|||
jamesServer = new TemporaryJamesServer(temporaryFolder, mailetContainer); | |||
Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS; | |||
calmlyAwait = Awaitility.with().pollInterval(slowPacedPollInterval).and().with().pollDelay(slowPacedPollInterval).await(); | |||
} | |||
|
|||
@After | |||
public void tearDown() { | |||
jamesServer.shutdown(); | |||
} | |||
|
|||
@Test | |||
public void simpleMailShouldBeSent() throws Exception { | |||
String from = "user@" + DEFAULT_DOMAIN; | |||
String recipient = "user2@" + DEFAULT_DOMAIN; | |||
String targetedMailbox = "INBOX.any"; | |||
|
|||
jamesServer.getServerProbe().addDomain(DEFAULT_DOMAIN); | |||
jamesServer.getServerProbe().addUser(from, PASSWORD); | |||
jamesServer.getServerProbe().addUser(recipient, PASSWORD); | |||
jamesServer.getServerProbe().createMailbox(MailboxConstants.USER_NAMESPACE, recipient, "INBOX"); | |||
jamesServer.getServerProbe().createMailbox(MailboxConstants.USER_NAMESPACE, recipient, targetedMailbox); | |||
jamesServer.getServerProbe().addActiveSieveScript(recipient, "myscript.sieve", "require \"fileinto\";\n" + | |||
"\n" + | |||
"fileinto \"" + targetedMailbox + "\";"); | |||
|
|||
try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, DEFAULT_DOMAIN); | |||
IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) { | |||
messageSender.sendMessage(from, recipient); | |||
calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent); | |||
calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> imapMessageReader.userReceivedMessageInMailbox(recipient, PASSWORD, targetedMailbox)); | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Sieve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,135 @@ | |||
/**************************************************************** | |||
* 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 java.util.ArrayList; | |||
import java.util.List; | |||
|
|||
import javax.inject.Inject; | |||
import javax.mail.MessagingException; | |||
|
|||
import org.apache.commons.logging.Log; | |||
import org.apache.james.mailbox.model.MailboxConstants; | |||
import org.apache.james.sieverepository.api.SieveRepository; | |||
import org.apache.james.transport.mailets.delivery.MailStore; | |||
import org.apache.james.transport.mailets.jsieve.CommonsLoggingAdapter; | |||
import org.apache.james.transport.mailets.jsieve.ResourceLocator; | |||
import org.apache.james.transport.mailets.jsieve.delivery.SieveExecutor; | |||
import org.apache.james.transport.mailets.jsieve.delivery.SievePoster; | |||
import org.apache.james.user.api.UsersRepository; | |||
import org.apache.james.user.api.UsersRepositoryException; | |||
import org.apache.mailet.Mail; | |||
import org.apache.mailet.MailAddress; | |||
import org.apache.mailet.base.GenericMailet; | |||
|
|||
import com.google.common.base.Predicate; | |||
import com.google.common.collect.FluentIterable; | |||
import com.google.common.collect.ImmutableList; | |||
|
|||
/** | |||
* Execute Sieve scripts for incoming emails, and set the result of the execution as attributes of the mail | |||
*/ | |||
public class Sieve extends GenericMailet { | |||
|
|||
private final UsersRepository usersRepository; | |||
private final ResourceLocator resourceLocator; | |||
private SieveExecutor sieveExecutor; | |||
|
|||
@Inject | |||
public Sieve(UsersRepository usersRepository, SieveRepository sieveRepository) throws MessagingException { | |||
this(usersRepository, ResourceLocatorImpl.instanciate(usersRepository, sieveRepository)); | |||
} | |||
|
|||
public Sieve(UsersRepository usersRepository, ResourceLocator resourceLocator) throws MessagingException { | |||
this.usersRepository = usersRepository; | |||
this.resourceLocator = resourceLocator; | |||
} | |||
|
|||
@Override | |||
public String getMailetInfo() { | |||
return "Sieve Mailet"; | |||
} | |||
|
|||
@Override | |||
public void init() throws MessagingException { | |||
Log log = CommonsLoggingAdapter.builder() | |||
.wrappedLogger(getMailetContext().getLogger()) | |||
.quiet(getInitParameter("quiet", false)) | |||
.verbose(getInitParameter("verbose", false)) | |||
.build(); | |||
sieveExecutor = SieveExecutor.builder() | |||
.resourceLocator(resourceLocator) | |||
.usersRepository(usersRepository) | |||
.mailetContext(getMailetContext()) | |||
.log(log) | |||
.sievePoster(new SievePoster(usersRepository, MailboxConstants.INBOX)) | |||
.build(); | |||
} | |||
|
|||
@Override | |||
public void service(Mail mail) throws MessagingException { | |||
for(MailAddress recipient: mail.getRecipients()) { | |||
executeSieveScript(mail, recipient); | |||
} | |||
mail.setRecipients(keepNonDiscardedRecipients(mail)); | |||
} | |||
|
|||
private void executeSieveScript(Mail mail, MailAddress recipient) { | |||
try { | |||
sieveExecutor.execute(recipient, mail); | |||
} catch (Exception e) { | |||
getMailetContext().getLogger().warn("Failed to execute Sieve script for user " + recipient.asPrettyString(), e); | |||
} | |||
} | |||
|
|||
|
|||
private ImmutableList<MailAddress> keepNonDiscardedRecipients(Mail mail) { | |||
final List<MailAddress> discardedRecipients = retrieveDiscardedRecipients(mail); | |||
return FluentIterable.from(mail.getRecipients()).filter(new Predicate<MailAddress>() { | |||
@Override | |||
public boolean apply(MailAddress input) { | |||
return !discardedRecipients.contains(input); | |||
} | |||
}).toList(); | |||
} | |||
|
|||
private List<MailAddress> retrieveDiscardedRecipients(Mail mail) { | |||
final List<MailAddress> discardedRecipients = new ArrayList<MailAddress>(); | |||
for(MailAddress recipient: mail.getRecipients()) { | |||
if (isDiscarded(mail, recipient)) { | |||
discardedRecipients.add(recipient); | |||
} | |||
} | |||
return discardedRecipients; | |||
} | |||
|
|||
private boolean isDiscarded(Mail mail, MailAddress recipient) { | |||
return !(mail.getAttribute(MailStore.DELIVERY_PATH_PREFIX + retrieveUser(recipient)) instanceof String); | |||
} | |||
|
|||
private String retrieveUser(MailAddress recipient) { | |||
try { | |||
return usersRepository.getUser(recipient); | |||
} catch (UsersRepositoryException e) { | |||
log("Can not retrieve username for mail address " + recipient.asPrettyString(), e); | |||
return recipient.asString(); | |||
} | |||
} | |||
} |
Oops, something went wrong.