JAMES-2341 SpamAssassin listener implementation for SpamEventListener
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/bd259078 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/bd259078 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/bd259078 Branch: refs/heads/master Commit: bd259078b11fd104d6ccd8ec523e109fe114adde Parents: e640b76 Author: Antoine Duprat <adup...@linagora.com> Authored: Fri Feb 23 09:48:27 2018 +0100 Committer: Antoine Duprat <adup...@linagora.com> Committed: Wed Feb 28 13:29:32 2018 +0100 ---------------------------------------------------------------------- mailbox/plugin/spamassassin/pom.xml | 9 ++ .../mailbox/spamassassin/SpamAssassin.java | 40 +++++++ .../spamassassin/SpamAssassinListener.java | 75 +++++++++++++ .../spamassassin/SpamAssassinListenerTest.java | 105 +++++++++++++++++++ 4 files changed, 229 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/bd259078/mailbox/plugin/spamassassin/pom.xml ---------------------------------------------------------------------- diff --git a/mailbox/plugin/spamassassin/pom.xml b/mailbox/plugin/spamassassin/pom.xml index e88cc2d..b510523 100644 --- a/mailbox/plugin/spamassassin/pom.xml +++ b/mailbox/plugin/spamassassin/pom.xml @@ -37,6 +37,10 @@ <artifactId>apache-james-mailbox-api</artifactId> </dependency> <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>apache-james-mailbox-store</artifactId> + </dependency> + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> @@ -51,6 +55,11 @@ <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/james-project/blob/bd259078/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassin.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassin.java b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassin.java new file mode 100644 index 0000000..0dfb30e --- /dev/null +++ b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassin.java @@ -0,0 +1,40 @@ +/**************************************************************** + * 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.mailbox.spamassassin; + +import java.io.InputStream; +import java.util.List; + +import javax.inject.Inject; + +public class SpamAssassin { + + private final SpamAssassinConfiguration spamAssassinConfiguration; + + @Inject + public SpamAssassin(SpamAssassinConfiguration spamAssassinConfiguration) { + this.spamAssassinConfiguration = spamAssassinConfiguration; + } + + public void learnSpam(List<InputStream> messages) { + if (spamAssassinConfiguration.isEnable()) { + // Will call SpamAssassinInvoker + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/bd259078/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java new file mode 100644 index 0000000..d0888f6 --- /dev/null +++ b/mailbox/plugin/spamassassin/src/main/java/org/apache/james/mailbox/spamassassin/SpamAssassinListener.java @@ -0,0 +1,75 @@ +/**************************************************************** + * 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.mailbox.spamassassin; + +import java.io.InputStream; + +import javax.inject.Inject; + +import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.store.event.EventFactory; +import org.apache.james.mailbox.store.event.SpamEventListener; +import org.apache.james.mailbox.store.mail.model.Message; + +import com.github.fge.lambdas.Throwing; +import com.github.steveash.guavate.Guavate; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; + +public class SpamAssassinListener implements SpamEventListener { + + private final SpamAssassin spamAssassin; + + @Inject + public SpamAssassinListener(SpamAssassin spamAssassin) { + this.spamAssassin = spamAssassin; + } + + @Override + public ListenerType getType() { + return ListenerType.ONCE; + } + + @Override + public ExecutionMode getExecutionMode() { + return ExecutionMode.ASYNCHRONOUS; + } + + @Override + public void event(Event event) { + if (event instanceof EventFactory.AddedImpl) { + EventFactory.AddedImpl addedToMailboxEvent = (EventFactory.AddedImpl) event; + if (isEventOnSpamMailbox(addedToMailboxEvent)) { + ImmutableList<InputStream> messages = addedToMailboxEvent.getAvailableMessages() + .values() + .stream() + .map(Throwing.function(Message::getFullContent)) + .collect(Guavate.toImmutableList()); + spamAssassin.learnSpam(messages); + } + } + } + + @VisibleForTesting + boolean isEventOnSpamMailbox(Event event) { + return Role.from(event.getMailboxPath().getName()) + .filter(role -> role.equals(Role.SPAM)) + .isPresent(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/bd259078/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java b/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java new file mode 100644 index 0000000..c6fe083 --- /dev/null +++ b/mailbox/plugin/spamassassin/src/test/java/org/apache/james/mailbox/spamassassin/SpamAssassinListenerTest.java @@ -0,0 +1,105 @@ +/**************************************************************** + * 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.mailbox.spamassassin; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.Map; +import java.util.SortedMap; + +import org.apache.james.mailbox.DefaultMailboxes; +import org.apache.james.mailbox.MailboxListener.Added; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageUid; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.model.MessageMetaData; +import org.apache.james.mailbox.store.event.EventFactory; +import org.apache.james.mailbox.store.mail.model.Mailbox; +import org.apache.james.mailbox.store.mail.model.MailboxMessage; +import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSortedMap; + +public class SpamAssassinListenerTest { + + private SpamAssassin spamAssassin; + private SpamAssassinListener listener; + + @Before + public void setup() { + spamAssassin = mock(SpamAssassin.class); + listener = new SpamAssassinListener(spamAssassin); + } + + @Test + public void isEventOnSpamMailboxShouldReturnFalseWhenMailboxIsNotSpam() { + MailboxSession mailboxSession = null; + int uidValidity = 1; + Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser("user", "mbx"), uidValidity); + SortedMap<MessageUid, MessageMetaData> uids = ImmutableSortedMap.of(); + Map<MessageUid, MailboxMessage> availableMessages = ImmutableMap.of(); + Added added = new EventFactory().added(mailboxSession, uids, mailbox, availableMessages); + + assertThat(listener.isEventOnSpamMailbox(added)).isFalse(); + } + + @Test + public void isEventOnSpamMailboxShouldReturnTrueWhenMailboxIsSpam() { + MailboxSession mailboxSession = null; + int uidValidity = 1; + Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser("user", DefaultMailboxes.SPAM), uidValidity); + SortedMap<MessageUid, MessageMetaData> uids = ImmutableSortedMap.of(); + Map<MessageUid, MailboxMessage> availableMessages = ImmutableMap.of(); + Added added = new EventFactory().added(mailboxSession, uids, mailbox, availableMessages); + + assertThat(listener.isEventOnSpamMailbox(added)).isTrue(); + } + + @Test + public void isEventOnSpamMailboxShouldReturnFalseWhenMailboxIsSpamOtherCase() { + MailboxSession mailboxSession = null; + int uidValidity = 1; + Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser("user", "SPAM"), uidValidity); + SortedMap<MessageUid, MessageMetaData> uids = ImmutableSortedMap.of(); + Map<MessageUid, MailboxMessage> availableMessages = ImmutableMap.of(); + Added added = new EventFactory().added(mailboxSession, uids, mailbox, availableMessages); + + assertThat(listener.isEventOnSpamMailbox(added)).isFalse(); + } + + @Test + public void eventShouldCallSpamAssassinWhenTheEventMatches() { + MailboxSession mailboxSession = null; + int uidValidity = 1; + Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser("user", "Spam"), uidValidity); + SortedMap<MessageUid, MessageMetaData> uids = ImmutableSortedMap.of(); + Map<MessageUid, MailboxMessage> availableMessages = ImmutableMap.of(); + Added added = new EventFactory().added(mailboxSession, uids, mailbox, availableMessages); + + listener.event(added); + + verify(spamAssassin).learnSpam(any()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org