You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/12/12 10:51:54 UTC

[01/16] james-project git commit: MAILBOX-357 Wire MailboxSessionMapperFactory in search indexes

Repository: james-project
Updated Branches:
  refs/heads/master e7048f41c -> 8051692fe


MAILBOX-357 Wire MailboxSessionMapperFactory in search indexes

This allows relying on the MailboxSessionMapperFactory for retrieving mailboxes, which
are not part of the core mailbox API


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

Branch: refs/heads/master
Commit: 8d2db2c33af6a8d29389049935297ea8acffef01
Parents: 3713109
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 13:58:00 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:57 2018 +0700

----------------------------------------------------------------------
 .../ElasticSearchListeningMessageSearchIndex.java       |  4 ++--
 .../ElasticSearchListeningMessageSearchIndexTest.java   |  4 ++--
 .../mailbox/lucene/search/LuceneMessageSearchIndex.java | 12 +++++-------
 .../mailbox/store/search/LazyMessageSearchIndex.java    |  6 +++---
 .../store/search/ListeningMessageSearchIndex.java       |  6 +++---
 5 files changed, 15 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8d2db2c3/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
index 2b76986..955ca63 100644
--- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
+++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
@@ -46,7 +46,7 @@ import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MessageId;
 import org.apache.james.mailbox.model.SearchQuery;
 import org.apache.james.mailbox.model.UpdatedFlags;
-import org.apache.james.mailbox.store.mail.MessageMapperFactory;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
@@ -68,7 +68,7 @@ public class ElasticSearchListeningMessageSearchIndex extends ListeningMessageSe
     private final MessageToElasticSearchJson messageToElasticSearchJson;
 
     @Inject
-    public ElasticSearchListeningMessageSearchIndex(MessageMapperFactory factory,
+    public ElasticSearchListeningMessageSearchIndex(MailboxSessionMapperFactory factory,
                                                     @Named(MailboxElasticSearchConstants.InjectionNames.MAILBOX) ElasticSearchIndexer indexer,
                                                     ElasticSearchSearcher searcher, MessageToElasticSearchJson messageToElasticSearchJson,
                                                     MailboxManager mailboxManager) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/8d2db2c3/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
----------------------------------------------------------------------
diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
index 90f012d..d8ed910 100644
--- a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
+++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
@@ -42,7 +42,7 @@ import org.apache.james.mailbox.elasticsearch.search.ElasticSearchSearcher;
 import org.apache.james.mailbox.mock.MockMailboxSession;
 import org.apache.james.mailbox.model.TestId;
 import org.apache.james.mailbox.model.UpdatedFlags;
-import org.apache.james.mailbox.store.mail.MessageMapperFactory;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.elasticsearch.ElasticsearchException;
@@ -74,7 +74,7 @@ public class ElasticSearchListeningMessageSearchIndexTest {
     
     @Before
     public void setup() {
-        MessageMapperFactory mapperFactory = mock(MessageMapperFactory.class);
+        MailboxSessionMapperFactory mapperFactory = mock(MailboxSessionMapperFactory.class);
         messageToElasticSearchJson = mock(MessageToElasticSearchJson.class);
         ElasticSearchSearcher elasticSearchSearcher = mock(ElasticSearchSearcher.class);
         MailboxManager mockMailboxManager = mock(MailboxManager.class);

http://git-wip-us.apache.org/repos/asf/james-project/blob/8d2db2c3/mailbox/lucene/src/main/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/lucene/src/main/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndex.java b/mailbox/lucene/src/main/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndex.java
index 9d091b9..e6619b8 100644
--- a/mailbox/lucene/src/main/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndex.java
+++ b/mailbox/lucene/src/main/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndex.java
@@ -66,7 +66,7 @@ import org.apache.james.mailbox.model.SearchQuery.NumericOperator;
 import org.apache.james.mailbox.model.SearchQuery.UidCriterion;
 import org.apache.james.mailbox.model.SearchQuery.UidRange;
 import org.apache.james.mailbox.model.UpdatedFlags;
-import org.apache.james.mailbox.store.mail.MessageMapperFactory;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 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.PropertyBuilder;
@@ -363,24 +363,22 @@ public class LuceneMessageSearchIndex extends ListeningMessageSearchIndex {
 
     @Inject
     public LuceneMessageSearchIndex(
-        MessageMapperFactory factory,
+        MailboxSessionMapperFactory factory,
         MailboxId.Factory mailboxIdFactory,
         Directory directory,
         MessageId.Factory messageIdFactory,
-        MailboxManager mailboxManager
-    ) throws IOException {
+        MailboxManager mailboxManager) throws IOException {
         this(factory, mailboxIdFactory, directory, false, true, messageIdFactory, mailboxManager);
     }
 
     public LuceneMessageSearchIndex(
-            MessageMapperFactory factory,
+            MailboxSessionMapperFactory factory,
             MailboxId.Factory mailboxIdFactory,
             Directory directory,
             boolean dropIndexOnStart,
             boolean lenient,
             MessageId.Factory messageIdFactory,
-            MailboxManager mailboxManager
-    ) throws IOException {
+            MailboxManager mailboxManager) throws IOException {
         super(factory, mailboxManager);
         this.mailboxIdFactory = mailboxIdFactory;
         this.messageIdFactory = messageIdFactory;

http://git-wip-us.apache.org/repos/asf/james-project/blob/8d2db2c3/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java
index 5ede660..850fef0 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java
@@ -35,8 +35,8 @@ import org.apache.james.mailbox.model.MessageId;
 import org.apache.james.mailbox.model.MessageRange;
 import org.apache.james.mailbox.model.SearchQuery;
 import org.apache.james.mailbox.model.UpdatedFlags;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
-import org.apache.james.mailbox.store.mail.MessageMapperFactory;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.slf4j.Logger;
@@ -59,10 +59,10 @@ public class LazyMessageSearchIndex extends ListeningMessageSearchIndex {
 
     private final ListeningMessageSearchIndex index;
     private final ConcurrentHashMap<MailboxId, Object> indexed = new ConcurrentHashMap<>();
-    private final MessageMapperFactory factory;
+    private final MailboxSessionMapperFactory factory;
     
     
-    public LazyMessageSearchIndex(ListeningMessageSearchIndex index, MessageMapperFactory factory, MailboxManager mailboxManager) {
+    public LazyMessageSearchIndex(ListeningMessageSearchIndex index, MailboxSessionMapperFactory factory, MailboxManager mailboxManager) {
         super(factory, mailboxManager);
         this.index = index;
         this.factory = factory;

http://git-wip-us.apache.org/repos/asf/james-project/blob/8d2db2c3/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
index badfe3f..01a0d21 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
@@ -29,9 +29,9 @@ import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MessageRange;
 import org.apache.james.mailbox.model.UpdatedFlags;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.event.EventFactory;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
-import org.apache.james.mailbox.store.mail.MessageMapperFactory;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.slf4j.Logger;
@@ -47,10 +47,10 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
     private static final Logger LOGGER = LoggerFactory.getLogger(ListeningMessageSearchIndex.class);
 
     private static final int UNLIMITED = -1;
-    private final MessageMapperFactory factory;
+    private final MailboxSessionMapperFactory factory;
     private final MailboxManager mailboxManager;
 
-    public ListeningMessageSearchIndex(MessageMapperFactory factory, MailboxManager mailboxManager) {
+    public ListeningMessageSearchIndex(MailboxSessionMapperFactory factory, MailboxManager mailboxManager) {
         this.factory = factory;
         this.mailboxManager = mailboxManager;
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[09/16] james-project git commit: MAILBOX-357 Rework SpamAssassin listener test

Posted by bt...@apache.org.
MAILBOX-357 Rework SpamAssassin listener test

We now need the messages to be persisted as they are not carried along.

Also, we need to re-create mappers to isolate tests between each others.


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

Branch: refs/heads/master
Commit: d972ed51006f822b44aaf3f3e8fe8b9226119bc7
Parents: f1cf245
Author: Benoit Tellier <bt...@linagora.com>
Authored: Tue Dec 11 13:14:58 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../spamassassin/SpamAssassinListenerTest.java  | 105 +++++++++----------
 1 file changed, 52 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d972ed51/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
index dc1d1d0..04baa1d 100644
--- 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
@@ -41,6 +41,7 @@ import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
 import org.apache.james.mailbox.mock.MockMailboxSession;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageMetaData;
 import org.apache.james.mailbox.model.MessageMoves;
 import org.apache.james.mailbox.model.TestMessageId;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
@@ -50,14 +51,13 @@ import org.apache.james.mailbox.store.SystemMailboxesProviderImpl;
 import org.apache.james.mailbox.store.event.EventFactory;
 import org.apache.james.mailbox.store.event.MessageMoveEvent;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-import com.github.fge.lambdas.Throwing;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSortedMap;
 
@@ -66,17 +66,19 @@ public class SpamAssassinListenerTest {
     public static final String USER = "user";
     private static final MockMailboxSession MAILBOX_SESSION = new MockMailboxSession(USER);
     private static final int UID_VALIDITY = 43;
+    private static final MessageUid UID = MessageUid.of(45);
+
     private SpamAssassin spamAssassin;
     private SpamAssassinListener listener;
     private SimpleMailbox inbox;
-    private MailboxId inboxId;
     private SimpleMailbox mailbox1;
     private MailboxId mailboxId1;
     private MailboxId mailboxId2;
     private MailboxId spamMailboxId;
     private MailboxId spamCapitalMailboxId;
     private MailboxId trashMailboxId;
-    private MailboxMapper mailboxMapper;
+    private MailboxSessionMapperFactory mapperFactory;
+    private SimpleMailbox mailbox2;
 
     @Before
     public void setup() throws Exception {
@@ -86,13 +88,14 @@ public class SpamAssassinListenerTest {
             .thenReturn(MAILBOX_SESSION);
 
         spamAssassin = mock(SpamAssassin.class);
-        MailboxSessionMapperFactory mapperFactory = mailboxManager.getMapperFactory();
-        mailboxMapper = mapperFactory.getMailboxMapper(MAILBOX_SESSION);
+        mapperFactory = mailboxManager.getMapperFactory();
+        MailboxMapper mailboxMapper = mapperFactory.createMailboxMapper(MAILBOX_SESSION);
         inbox = new SimpleMailbox(MailboxPath.forUser(USER, DefaultMailboxes.INBOX), UID_VALIDITY);
-        inboxId = mailboxMapper.save(inbox);
         mailbox1 = new SimpleMailbox(MailboxPath.forUser(USER, "mailbox1"), UID_VALIDITY);
+        mailbox2 = new SimpleMailbox(MailboxPath.forUser(USER, "mailbox2"), UID_VALIDITY);
+        mailboxMapper.save(inbox);
         mailboxId1 = mailboxMapper.save(mailbox1);
-        mailboxId2 = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "mailbox2"), UID_VALIDITY));
+        mailboxId2 = mailboxMapper.save(mailbox2);
         spamMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "Spam"), UID_VALIDITY));
         spamCapitalMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "SPAM"), UID_VALIDITY));
         trashMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "Trash"), UID_VALIDITY));
@@ -100,67 +103,61 @@ public class SpamAssassinListenerTest {
         listener = new SpamAssassinListener(spamAssassin, systemMailboxesProvider, mailboxManager, mapperFactory, MailboxListener.ExecutionMode.SYNCHRONOUS);
     }
 
-    @After
-    public void tearDown() throws MailboxException {
-        mailboxMapper.list()
-            .forEach(Throwing.consumer(mailboxMapper::delete));
-    }
-
     @Test
-    public void isEventOnSpamMailboxShouldReturnFalseWhenMessageIsMovedToANonSpamMailbox() {
+    public void isEventOnSpamMailboxShouldReturnFalseWhenMessageIsMovedToANonSpamMailbox() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(mailboxId1)
                 .targetMailboxIds(mailboxId2)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId2)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox2)))
             .build();
 
         assertThat(listener.isMessageMovedToSpamMailbox(messageMoveEvent)).isFalse();
     }
 
     @Test
-    public void isEventOnSpamMailboxShouldReturnTrueWhenMailboxIsSpam() {
+    public void isEventOnSpamMailboxShouldReturnTrueWhenMailboxIsSpam() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(mailboxId1)
                 .targetMailboxIds(spamMailboxId)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId1)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox1)))
             .build();
 
         assertThat(listener.isMessageMovedToSpamMailbox(messageMoveEvent)).isTrue();
     }
 
     @Test
-    public void isEventOnSpamMailboxShouldReturnFalseWhenMailboxIsSpamOtherCase() {
+    public void isEventOnSpamMailboxShouldReturnFalseWhenMailboxIsSpamOtherCase() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(mailboxId1)
                 .targetMailboxIds(spamCapitalMailboxId)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId1)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox1)))
             .build();
 
         assertThat(listener.isMessageMovedToSpamMailbox(messageMoveEvent)).isFalse();
     }
 
     @Test
-    public void eventShouldCallSpamAssassinSpamLearningWhenTheEventMatches() {
+    public void eventShouldCallSpamAssassinSpamLearningWhenTheEventMatches() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(mailboxId1)
                 .targetMailboxIds(spamMailboxId)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId1)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox1)))
             .build();
 
         listener.event(messageMoveEvent);
@@ -169,75 +166,75 @@ public class SpamAssassinListenerTest {
     }
 
     @Test
-    public void isMessageMovedOutOfSpamMailboxShouldReturnFalseWhenMessageMovedBetweenNonSpamMailboxes() {
+    public void isMessageMovedOutOfSpamMailboxShouldReturnFalseWhenMessageMovedBetweenNonSpamMailboxes() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(mailboxId1)
                 .targetMailboxIds(mailboxId2)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId2)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox2)))
             .build();
 
         assertThat(listener.isMessageMovedOutOfSpamMailbox(messageMoveEvent)).isFalse();
     }
 
     @Test
-    public void isMessageMovedOutOfSpamMailboxShouldReturnFalseWhenMessageMovedOutOfCapitalSpamMailbox() {
+    public void isMessageMovedOutOfSpamMailboxShouldReturnFalseWhenMessageMovedOutOfCapitalSpamMailbox() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(spamCapitalMailboxId)
                 .targetMailboxIds(mailboxId2)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId2)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox2)))
             .build();
 
         assertThat(listener.isMessageMovedOutOfSpamMailbox(messageMoveEvent)).isFalse();
     }
 
     @Test
-    public void isMessageMovedOutOfSpamMailboxShouldReturnTrueWhenMessageMovedOutOfSpamMailbox() {
+    public void isMessageMovedOutOfSpamMailboxShouldReturnTrueWhenMessageMovedOutOfSpamMailbox() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(spamMailboxId)
                 .targetMailboxIds(mailboxId2)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId2)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox2)))
             .build();
 
         assertThat(listener.isMessageMovedOutOfSpamMailbox(messageMoveEvent)).isTrue();
     }
 
     @Test
-    public void isMessageMovedOutOfSpamMailboxShouldReturnFalseWhenMessageMovedToTrash() {
+    public void isMessageMovedOutOfSpamMailboxShouldReturnFalseWhenMessageMovedToTrash() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(spamMailboxId)
                 .targetMailboxIds(trashMailboxId)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId2)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox2)))
             .build();
 
         assertThat(listener.isMessageMovedOutOfSpamMailbox(messageMoveEvent)).isFalse();
     }
 
     @Test
-    public void eventShouldCallSpamAssassinHamLearningWhenTheEventMatches() {
+    public void eventShouldCallSpamAssassinHamLearningWhenTheEventMatches() throws Exception {
         MessageMoveEvent messageMoveEvent = MessageMoveEvent.builder()
             .session(MAILBOX_SESSION)
             .messageMoves(MessageMoves.builder()
                 .previousMailboxIds(spamMailboxId)
                 .targetMailboxIds(mailboxId1)
                 .build())
-            .messages(ImmutableMap.of(MessageUid.of(45),
-                createMessage(mailboxId1)))
+            .messages(ImmutableMap.of(UID,
+                createMessage(mailbox1)))
             .build();
 
         listener.event(messageMoveEvent);
@@ -246,13 +243,12 @@ public class SpamAssassinListenerTest {
     }
 
     @Test
-    public void eventShouldCallSpamAssassinHamLearningWhenTheMessageIsAddedInInbox() {
-        SimpleMailboxMessage message = createMessage(inboxId);
+    public void eventShouldCallSpamAssassinHamLearningWhenTheMessageIsAddedInInbox() throws Exception {
+        SimpleMailboxMessage message = createMessage(inbox);
 
+        ImmutableSortedMap<MessageUid, MessageMetaData> sortedMap = ImmutableSortedMap.of(UID, new SimpleMessageMetaData(message));
         MailboxListener.Added addedEvent = new EventFactory().added(
-                MAILBOX_SESSION,
-                ImmutableSortedMap.of(MessageUid.of(45), new SimpleMessageMetaData(message)),
-                inbox);
+                MAILBOX_SESSION, sortedMap, inbox);
 
         listener.event(addedEvent);
 
@@ -260,25 +256,28 @@ public class SpamAssassinListenerTest {
     }
 
     @Test
-    public void eventShouldNotCallSpamAssassinHamLearningWhenTheMessageIsAddedInAMailboxOtherThanInbox() {
-        SimpleMailboxMessage message = createMessage(mailboxId1);
+    public void eventShouldNotCallSpamAssassinHamLearningWhenTheMessageIsAddedInAMailboxOtherThanInbox() throws Exception {
+        SimpleMailboxMessage message = createMessage(mailbox1);
 
         MailboxListener.Added addedEvent = new EventFactory().added(
             MAILBOX_SESSION,
-            ImmutableSortedMap.of(MessageUid.of(45), new SimpleMessageMetaData(message)),
-            inbox);
+            ImmutableSortedMap.of(UID, new SimpleMessageMetaData(message)),
+            mailbox1);
 
         listener.event(addedEvent);
 
         verifyNoMoreInteractions(spamAssassin);
     }
 
-    private SimpleMailboxMessage createMessage(MailboxId mailboxId) {
+    private SimpleMailboxMessage createMessage(Mailbox mailbox) throws MailboxException {
         int size = 45;
         int bodyStartOctet = 25;
         byte[] content = "Subject: test\r\n\r\nBody\r\n".getBytes(StandardCharsets.UTF_8);
-        return new SimpleMailboxMessage(TestMessageId.of(58), new Date(),
+        SimpleMailboxMessage message = new SimpleMailboxMessage(TestMessageId.of(58), new Date(),
             size, bodyStartOctet, new SharedByteArrayInputStream(content), new Flags(), new PropertyBuilder(),
-            mailboxId);
+            mailbox.getMailboxId());
+        MessageMetaData messageMetaData = mapperFactory.createMessageMapper(null).add(mailbox, message);
+        message.setUid(messageMetaData.getUid());
+        return message;
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[10/16] james-project git commit: MAILBOX-357 ListeningMessageSearchIndex should not rely on implementation details to retrieve the mailbox

Posted by bt...@apache.org.
MAILBOX-357 ListeningMessageSearchIndex should not rely on implementation details to retrieve the mailbox

We should rather read it from the mailbox store


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

Branch: refs/heads/master
Commit: 13f6aff3adabf942a5beacf33c46369f51573b7a
Parents: 0748989
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:17:35 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../search/ListeningMessageSearchIndex.java     | 28 ++++++++++----------
 1 file changed, 14 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/13f6aff3/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
index 104291e..e02b736 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
@@ -30,7 +30,6 @@ import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MessageRange;
 import org.apache.james.mailbox.model.UpdatedFlags;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
-import org.apache.james.mailbox.store.event.EventFactory;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
@@ -63,34 +62,35 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
     public void event(Event event) {
         try {
             MailboxSession session = mailboxManager.createSystemSession(event.getUser().asString());
-            if (event instanceof MessageEvent) {
-                if (event instanceof EventFactory.AddedImpl) {
-                    EventFactory.AddedImpl added = (EventFactory.AddedImpl) event;
-                    Mailbox mailbox = added.getMailbox();
+            if (event instanceof MailboxEvent) {
+                MailboxEvent mailboxEvent = (MailboxEvent) event;
+                Mailbox mailbox = factory.getMailboxMapper(session).findMailboxById(mailboxEvent.getMailboxId());
+
+                if (event instanceof Added) {
+                    Added added = (Added) event;
 
                     for (MessageUid uid : added.getUids()) {
                         retrieveMailboxMessage(session, mailbox, uid)
                             .ifPresent(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
                     }
-                } else if (event instanceof EventFactory.ExpungedImpl) {
-                    EventFactory.ExpungedImpl expunged = (EventFactory.ExpungedImpl) event;
+                } else if (event instanceof Expunged) {
+                    Expunged expunged = (Expunged) event;
                     try {
-                        delete(session, expunged.getMailbox(), expunged.getUids());
+                        delete(session, mailbox, expunged.getUids());
                     } catch (MailboxException e) {
-                        LOGGER.error("Unable to deleted messages {} from index for mailbox {}", expunged.getUids(), expunged.getMailbox(), e);
+                        LOGGER.error("Unable to deleted messages {} from index for mailbox {}", expunged.getUids(), mailbox, e);
                     }
-                } else if (event instanceof EventFactory.FlagsUpdatedImpl) {
-                    EventFactory.FlagsUpdatedImpl flagsUpdated = (EventFactory.FlagsUpdatedImpl) event;
-                    Mailbox mailbox = flagsUpdated.getMailbox();
+                } else if (event instanceof FlagsUpdated) {
+                    FlagsUpdated flagsUpdated = (FlagsUpdated) event;
 
                     try {
                         update(session, mailbox, flagsUpdated.getUpdatedFlags());
                     } catch (MailboxException e) {
                         LOGGER.error("Unable to update flags in index for mailbox {}", mailbox, e);
                     }
+                } else if (event instanceof MailboxDeletion) {
+                    deleteAll(session, mailbox);
                 }
-            } else if (event instanceof EventFactory.MailboxDeletionImpl) {
-                deleteAll(session, ((EventFactory.MailboxDeletionImpl) event).getMailbox());
             }
         } catch (MailboxException e) {
             LOGGER.error("Unable to update index", e);


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[15/16] james-project git commit: MAILBOX-357 Use method extraction to make ListeningMessageSearchIndex more readable

Posted by bt...@apache.org.
MAILBOX-357 Use method extraction to make ListeningMessageSearchIndex more readable


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

Branch: refs/heads/master
Commit: 0e01d39cbd476bdf8c1b82c4cd5ba11cd974f1b7
Parents: 205f817
Author: Benoit Tellier <bt...@linagora.com>
Authored: Tue Dec 11 10:09:05 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../search/ListeningMessageSearchIndex.java     | 69 ++++++++++++--------
 1 file changed, 40 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0e01d39c/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
index e11dc86..7de8fcc 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
@@ -61,42 +61,53 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
     @Override
     public void event(Event event) {
         try {
-            MailboxSession session = mailboxManager.createSystemSession(event.getUser().asString());
             if (event instanceof MailboxEvent) {
-                MailboxEvent mailboxEvent = (MailboxEvent) event;
-                Mailbox mailbox = factory.getMailboxMapper(session).findMailboxById(mailboxEvent.getMailboxId());
-
-                if (event instanceof Added) {
-                    Added added = (Added) event;
-
-                    added.getUids()
-                        .stream()
-                        .flatMap(uid -> retrieveMailboxMessage(session, mailbox, uid))
-                        .forEach(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
-                } else if (event instanceof Expunged) {
-                    Expunged expunged = (Expunged) event;
-                    try {
-                        delete(session, mailbox, expunged.getUids());
-                    } catch (MailboxException e) {
-                        LOGGER.error("Unable to deleted messages {} from index for mailbox {}", expunged.getUids(), mailbox, e);
-                    }
-                } else if (event instanceof FlagsUpdated) {
-                    FlagsUpdated flagsUpdated = (FlagsUpdated) event;
-
-                    try {
-                        update(session, mailbox, flagsUpdated.getUpdatedFlags());
-                    } catch (MailboxException e) {
-                        LOGGER.error("Unable to update flags in index for mailbox {}", mailbox, e);
-                    }
-                } else if (event instanceof MailboxDeletion) {
-                    deleteAll(session, mailbox);
-                }
+                handleMailboxEvent(event,
+                    mailboxManager.createSystemSession(event.getUser().asString()),
+                    (MailboxEvent) event);
             }
         } catch (MailboxException e) {
             LOGGER.error("Unable to update index", e);
         }
     }
 
+    private void handleMailboxEvent(Event event, MailboxSession session, MailboxEvent mailboxEvent) throws MailboxException {
+        Mailbox mailbox = factory.getMailboxMapper(session).findMailboxById(mailboxEvent.getMailboxId());
+
+        if (event instanceof Added) {
+            handleAdded(session, mailbox, (Added) event);
+        } else if (event instanceof Expunged) {
+            handleExpunged(session, mailbox, (Expunged) event);
+        } else if (event instanceof FlagsUpdated) {
+            handleFlagsUpdated(session, mailbox, (FlagsUpdated) event);
+        } else if (event instanceof MailboxDeletion) {
+            deleteAll(session, mailbox);
+        }
+    }
+
+    private void handleFlagsUpdated(MailboxSession session, Mailbox mailbox, FlagsUpdated flagsUpdated) {
+        try {
+            update(session, mailbox, flagsUpdated.getUpdatedFlags());
+        } catch (MailboxException e) {
+            LOGGER.error("Unable to update flags in index for mailbox {}", mailbox, e);
+        }
+    }
+
+    private void handleExpunged(MailboxSession session, Mailbox mailbox, Expunged expunged) {
+        try {
+            delete(session, mailbox, expunged.getUids());
+        } catch (MailboxException e) {
+            LOGGER.error("Unable to deleted messages {} from index for mailbox {}", expunged.getUids(), mailbox, e);
+        }
+    }
+
+    private void handleAdded(MailboxSession session, Mailbox mailbox, Added added) {
+        added.getUids()
+            .stream()
+            .flatMap(uid -> retrieveMailboxMessage(session, mailbox, uid))
+            .forEach(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
+    }
+
     private Stream<MailboxMessage> retrieveMailboxMessage(MailboxSession session, Mailbox mailbox, MessageUid uid) {
         try {
             return Stream.of(factory.getMessageMapper(session)


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[02/16] james-project git commit: MAILBOX-357 SpamAssassinListener should not rely on AddedImpl

Posted by bt...@apache.org.
MAILBOX-357 SpamAssassinListener should not rely on AddedImpl


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

Branch: refs/heads/master
Commit: 371310927977afb47740ff56bc869dc5e2f1e6a8
Parents: a8a5b01
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:13:11 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:57 2018 +0700

----------------------------------------------------------------------
 .../spamassassin/SpamAssassinListener.java      | 76 ++++++++++++++------
 .../spamassassin/SpamAssassinListenerTest.java  |  2 +-
 2 files changed, 54 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/37131092/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
index 74443c9..8ac45e1 100644
--- 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
@@ -20,19 +20,27 @@ package org.apache.james.mailbox.spamassassin;
 
 import java.io.InputStream;
 import java.util.List;
+import java.util.stream.Stream;
 
 import javax.inject.Inject;
 
 import org.apache.james.mailbox.Event;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.Role;
 import org.apache.james.mailbox.SystemMailboxesProvider;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MailboxId;
-import org.apache.james.mailbox.store.event.EventFactory;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.event.MessageMoveEvent;
 import org.apache.james.mailbox.store.event.SpamEventListener;
+import org.apache.james.mailbox.store.mail.MessageMapper;
+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.Message;
+import org.apache.james.util.streams.Iterators;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,15 +52,20 @@ import com.google.common.collect.ImmutableList;
 public class SpamAssassinListener implements SpamEventListener {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(SpamAssassinListener.class);
+    private static final int LIMIT = 1;
 
     private final SpamAssassin spamAssassin;
     private final SystemMailboxesProvider systemMailboxesProvider;
+    private final MailboxManager mailboxManager;
+    private final MailboxSessionMapperFactory mapperFactory;
     private final ExecutionMode executionMode;
 
     @Inject
-    SpamAssassinListener(SpamAssassin spamAssassin, SystemMailboxesProvider systemMailboxesProvider, ExecutionMode executionMode) {
+    SpamAssassinListener(SpamAssassin spamAssassin, SystemMailboxesProvider systemMailboxesProvider, MailboxManager mailboxManager, MailboxSessionMapperFactory mapperFactory, ExecutionMode executionMode) {
         this.spamAssassin = spamAssassin;
         this.systemMailboxesProvider = systemMailboxesProvider;
+        this.mailboxManager = mailboxManager;
+        this.mapperFactory = mapperFactory;
         this.executionMode = executionMode;
     }
 
@@ -68,32 +81,49 @@ public class SpamAssassinListener implements SpamEventListener {
 
     @Override
     public void event(Event event) {
-        if (event instanceof MessageMoveEvent) {
-            MessageMoveEvent messageMoveEvent = (MessageMoveEvent) event;
-            if (isMessageMovedToSpamMailbox(messageMoveEvent)) {
-                LOGGER.debug("Spam event detected");
-                ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
-                spamAssassin.learnSpam(messages, event.getUser());
+        try {
+            MailboxSession session = mailboxManager.createSystemSession(getClass().getCanonicalName());
+            if (event instanceof MessageMoveEvent) {
+                MessageMoveEvent messageMoveEvent = (MessageMoveEvent) event;
+                if (isMessageMovedToSpamMailbox(messageMoveEvent)) {
+                    LOGGER.debug("Spam event detected");
+                    ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
+                    spamAssassin.learnSpam(messages, event.getUser());
+                }
+                if (isMessageMovedOutOfSpamMailbox(messageMoveEvent)) {
+                    ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
+                    spamAssassin.learnHam(messages, event.getUser());
+                }
             }
-            if (isMessageMovedOutOfSpamMailbox(messageMoveEvent)) {
-                ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
-                spamAssassin.learnHam(messages, event.getUser());
+            if (event instanceof Added) {
+                Added addedEvent = (Added) event;
+                if (isAppendedToInbox(addedEvent)) {
+                    Mailbox mailbox = mapperFactory.getMailboxMapper(session).findMailboxById(addedEvent.getMailboxId());
+                    MessageMapper messageMapper = mapperFactory.getMessageMapper(session);
+
+                    List<InputStream> contents = addedEvent.getUids()
+                        .stream()
+                        .flatMap(uid -> retrieveMessage(messageMapper, mailbox, uid))
+                        .map(Throwing.function(MailboxMessage::getFullContent))
+                        .collect(Guavate.toImmutableList());
+                    spamAssassin.learnHam(contents, event.getUser());
+                }
             }
+        } catch (MailboxException e) {
+            LOGGER.warn("Error while processing SPAM listener", e);
         }
-        if (event instanceof EventFactory.AddedImpl) {
-            EventFactory.AddedImpl addedEvent = (EventFactory.AddedImpl) event;
-            if (isAppendedToInbox(addedEvent)) {
-                List<InputStream> contents = addedEvent.getAvailableMessages()
-                    .values()
-                    .stream()
-                    .map(Throwing.function(MailboxMessage::getFullContent))
-                    .collect(Guavate.toImmutableList());
-                spamAssassin.learnHam(contents, event.getUser());
-            }
+    }
+
+    private Stream<MailboxMessage> retrieveMessage(MessageMapper messageMapper, Mailbox mailbox, MessageUid uid) {
+        try {
+            return Iterators.toStream(messageMapper.findInMailbox(mailbox, MessageRange.one(uid), MessageMapper.FetchType.Full, LIMIT));
+        } catch (MailboxException e) {
+            LOGGER.warn("Can not retrieve message {} {}", mailbox.getMailboxId(), uid.asLong(), e);
+            return Stream.empty();
         }
     }
 
-    private boolean isAppendedToInbox(EventFactory.AddedImpl addedEvent) {
+    private boolean isAppendedToInbox(Added addedEvent) {
         try {
             return systemMailboxesProvider.findMailbox(Role.INBOX, addedEvent.getUser())
                 .getId().equals(addedEvent.getMailboxId());
@@ -103,7 +133,7 @@ public class SpamAssassinListener implements SpamEventListener {
         }
     }
 
-    public ImmutableList<InputStream> retrieveMessages(MessageMoveEvent messageMoveEvent) {
+    private ImmutableList<InputStream> retrieveMessages(MessageMoveEvent messageMoveEvent) {
         return messageMoveEvent.getMessages()
             .values()
             .stream()

http://git-wip-us.apache.org/repos/asf/james-project/blob/37131092/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
index 19e7755..bc8aaeb 100644
--- 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
@@ -98,7 +98,7 @@ public class SpamAssassinListenerTest {
         spamCapitalMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "SPAM"), UID_VALIDITY));
         trashMailboxId = mailboxMapper.save(new SimpleMailbox(MailboxPath.forUser(USER, "Trash"), UID_VALIDITY));
 
-        listener = new SpamAssassinListener(spamAssassin, systemMailboxesProvider, MailboxListener.ExecutionMode.SYNCHRONOUS);
+        listener = new SpamAssassinListener(spamAssassin, systemMailboxesProvider, mailboxManager, mapperFactory, MailboxListener.ExecutionMode.SYNCHRONOUS);
     }
 
     @After


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[05/16] james-project git commit: MAILBOX-357 Solve some IntelliJ warnings

Posted by bt...@apache.org.
MAILBOX-357 Solve some  IntelliJ warnings

In ListeningMessageSearchIndex, in ElasticSearchListeningMessageSearchIndexTest
and remove an unused import in ElasticSearchIndexerTest.

These class will be removed later on.


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

Branch: refs/heads/master
Commit: 57a02129f0d9d8c1eeb6d4db8e42874a15e1204c
Parents: e7048f4
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 13:53:40 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:57 2018 +0700

----------------------------------------------------------------------
 ...icSearchListeningMessageSearchIndexTest.java | 20 ++++++++------------
 .../search/ListeningMessageSearchIndex.java     | 16 +++++++---------
 2 files changed, 15 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/57a02129/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
----------------------------------------------------------------------
diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
index 9cba34b..90f012d 100644
--- a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
+++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java
@@ -26,7 +26,6 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import java.io.IOException;
 import java.util.List;
 import java.util.Optional;
 
@@ -60,14 +59,12 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
 public class ElasticSearchListeningMessageSearchIndexTest {
-    
-
-    public static final long MODSEQ = 18L;
-    public static final MessageUid MESSAGE_UID = MessageUid.of(1);
-    public static final TestId MAILBOX_ID = TestId.of(12);
-    public static final String ELASTIC_SEARCH_ID = "12:1";
-    public static final String EXPECTED_JSON_CONTENT = "json content";
-    public static final String USERNAME = "username";
+    private static final long MODSEQ = 18L;
+    private static final MessageUid MESSAGE_UID = MessageUid.of(1);
+    private static final TestId MAILBOX_ID = TestId.of(12);
+    private static final String ELASTIC_SEARCH_ID = "12:1";
+    private static final String EXPECTED_JSON_CONTENT = "json content";
+    private static final String USERNAME = "username";
 
     private ElasticSearchIndexer elasticSearchIndexer;
     private MessageToElasticSearchJson messageToElasticSearchJson;
@@ -76,8 +73,7 @@ public class ElasticSearchListeningMessageSearchIndexTest {
     private List<User> users;
     
     @Before
-    public void setup() throws JsonProcessingException {
-
+    public void setup() {
         MessageMapperFactory mapperFactory = mock(MessageMapperFactory.class);
         messageToElasticSearchJson = mock(MessageToElasticSearchJson.class);
         ElasticSearchSearcher elasticSearchSearcher = mock(ElasticSearchSearcher.class);
@@ -131,7 +127,7 @@ public class ElasticSearchListeningMessageSearchIndexTest {
         verify(elasticSearchIndexer).index(eq(ELASTIC_SEARCH_ID), eq(EXPECTED_JSON_CONTENT));
     }
 
-    private MailboxMessage mockedMessage(MessageUid messageId) throws IOException {
+    private MailboxMessage mockedMessage(MessageUid messageId) {
         MailboxMessage message = mock(MailboxMessage.class);
         when(message.getUid())
             .thenReturn(messageId);

http://git-wip-us.apache.org/repos/asf/james-project/blob/57a02129/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
index aff8454..badfe3f 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
@@ -46,7 +46,7 @@ import org.slf4j.LoggerFactory;
 public abstract class ListeningMessageSearchIndex implements MessageSearchIndex, MailboxListener {
     private static final Logger LOGGER = LoggerFactory.getLogger(ListeningMessageSearchIndex.class);
 
-    public static final int UNLIMITED = -1;
+    private static final int UNLIMITED = -1;
     private final MessageMapperFactory factory;
     private final MailboxManager mailboxManager;
 
@@ -66,13 +66,11 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
             if (event instanceof MessageEvent) {
                 if (event instanceof EventFactory.AddedImpl) {
                     EventFactory.AddedImpl added = (EventFactory.AddedImpl) event;
-                    final Mailbox mailbox = added.getMailbox();
+                    Mailbox mailbox = added.getMailbox();
 
-                    for (final MessageUid next : (Iterable<MessageUid>) added.getUids()) {
-                        Optional<MailboxMessage> mailboxMessage = retrieveMailboxMessage(session, added, mailbox, next);
-                        if (mailboxMessage.isPresent()) {
-                            addMessage(session, mailbox, mailboxMessage.get());
-                        }
+                    for (MessageUid uid : added.getUids()) {
+                        retrieveMailboxMessage(session, added, mailbox, uid)
+                            .ifPresent(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
                     }
                 } else if (event instanceof EventFactory.ExpungedImpl) {
                     EventFactory.ExpungedImpl expunged = (EventFactory.ExpungedImpl) event;
@@ -83,7 +81,7 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
                     }
                 } else if (event instanceof EventFactory.FlagsUpdatedImpl) {
                     EventFactory.FlagsUpdatedImpl flagsUpdated = (EventFactory.FlagsUpdatedImpl) event;
-                    final Mailbox mailbox = flagsUpdated.getMailbox();
+                    Mailbox mailbox = flagsUpdated.getMailbox();
 
                     try {
                         update(session, mailbox, flagsUpdated.getUpdatedFlags());
@@ -115,7 +113,7 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
         }
     }
 
-    private void addMessage(final MailboxSession session, final Mailbox mailbox, MailboxMessage message) {
+    private void addMessage(MailboxSession session, Mailbox mailbox, MailboxMessage message) {
         try {
             add(session, mailbox, message);
         } catch (MailboxException e) {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[14/16] james-project git commit: MAILBOX-357 ListeningSearchIndex should not rely on "available messages"

Posted by bt...@apache.org.
MAILBOX-357 ListeningSearchIndex should not rely on "available messages"

This method is not part of the mailbox-api and messages will not be carried along
in distributed events.

AddedImpl no longer need to carry along cached messages.


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

Branch: refs/heads/master
Commit: 074898961aa1847639f1a583ec693cc799773cbf
Parents: 8d2db2c
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:00:13 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../mailbox/store/StoreMessageManager.java      |   4 +-
 .../james/mailbox/store/event/EventFactory.java |  16 +-
 .../store/event/MailboxEventDispatcher.java     |   8 +-
 .../search/ListeningMessageSearchIndex.java     | 319 +++++++++----------
 .../processor/base/SelectedMailboxImplTest.java |   3 +-
 5 files changed, 169 insertions(+), 181 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/07489896/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
index fc5492f..434c948 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
@@ -739,7 +739,7 @@ public class StoreMessageManager implements org.apache.james.mailbox.MessageMana
         for (MailboxMessage message : originalRows.getEntriesSeen()) {
             messagesMap.put(message.getUid(), immutableMailboxMessageFactory.from(to.getMailboxEntity().getMailboxId(), message));
         }
-        dispatcher.added(session, copiedUids, to.getMailboxEntity(), messagesMap.build());
+        dispatcher.added(session, copiedUids, to.getMailboxEntity());
         dispatcher.moved(session,
             MessageMoves.builder()
                 .previousMailboxIds(getMailboxEntity().getMailboxId())
@@ -759,7 +759,7 @@ public class StoreMessageManager implements org.apache.james.mailbox.MessageMana
         for (MailboxMessage message : originalRows.getEntriesSeen()) {
             messagesMap.put(message.getUid(), immutableMailboxMessageFactory.from(to.getMailboxEntity().getMailboxId(), message));
         }
-        dispatcher.added(session, moveUids, to.getMailboxEntity(), messagesMap.build());
+        dispatcher.added(session, moveUids, to.getMailboxEntity());
         dispatcher.expunged(session, collectMetadata(moveResult.getOriginalMessages()), getMailboxEntity());
         dispatcher.moved(session,
             MessageMoves.builder()

http://git-wip-us.apache.org/repos/asf/james-project/blob/07489896/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
index dd47954..1fd51e5 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
@@ -51,14 +51,12 @@ public class EventFactory {
 
     public final class AddedImpl extends MailboxListener.Added implements MailboxAware {
         private final Map<MessageUid, MessageMetaData> added;
-        private final Map<MessageUid, MailboxMessage> availableMessages;
         private final Mailbox mailbox;
 
-        public AddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, SortedMap<MessageUid, MessageMetaData> uids, Map<MessageUid, MailboxMessage> availableMessages) {
+        public AddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, SortedMap<MessageUid, MessageMetaData> uids) {
             super(sessionId, user, new StoreMailboxPath(mailbox), mailbox.getMailboxId());
             this.added = ImmutableMap.copyOf(uids);
             this.mailbox = mailbox;
-            this.availableMessages = ImmutableMap.copyOf(availableMessages);
         }
 
         @Override
@@ -75,10 +73,6 @@ public class EventFactory {
         public Mailbox getMailbox() {
             return mailbox;
         }
-
-        public Map<MessageUid, MailboxMessage> getAvailableMessages() {
-            return availableMessages;
-        }
     }
 
     public final class ExpungedImpl extends MailboxListener.Expunged implements MailboxAware {
@@ -193,12 +187,12 @@ public class EventFactory {
         }
     }
 
-    public MailboxListener.Added added(MailboxSession session, SortedMap<MessageUid, MessageMetaData> uids, Mailbox mailbox, Map<MessageUid, MailboxMessage> cachedMessages) {
-        return added(session.getSessionId(), session.getUser().getCoreUser(), uids, mailbox, cachedMessages);
+    public MailboxListener.Added added(MailboxSession session, SortedMap<MessageUid, MessageMetaData> uids, Mailbox mailbox) {
+        return added(session.getSessionId(), session.getUser().getCoreUser(), uids, mailbox);
     }
 
-    public MailboxListener.Added added(MailboxSession.SessionId sessionId, User user, SortedMap<MessageUid, MessageMetaData> uids, Mailbox mailbox, Map<MessageUid, MailboxMessage> cachedMessages) {
-        return new AddedImpl(sessionId, user, mailbox, uids, cachedMessages);
+    public MailboxListener.Added added(MailboxSession.SessionId sessionId, User user, SortedMap<MessageUid, MessageMetaData> uids, Mailbox mailbox) {
+        return new AddedImpl(sessionId, user, mailbox, uids);
     }
 
     public MailboxListener.Expunged expunged(MailboxSession session,  Map<MessageUid, MessageMetaData> uids, Mailbox mailbox) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/07489896/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
index 206e8b7..2d6afd9 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxEventDispatcher.java
@@ -81,8 +81,8 @@ public class MailboxEventDispatcher {
      * @param uids Sorted map with uids and message meta data
      * @param mailbox The mailbox
      */
-    public void added(MailboxSession session, SortedMap<MessageUid, MessageMetaData> uids, Mailbox mailbox, Map<MessageUid, MailboxMessage> cachedMessages) {
-        listener.event(eventFactory.added(session, uids, mailbox, cachedMessages));
+    public void added(MailboxSession session, SortedMap<MessageUid, MessageMetaData> uids, Mailbox mailbox) {
+        listener.event(eventFactory.added(session, uids, mailbox));
     }
 
     public void added(MailboxSession session, Mailbox mailbox, MailboxMessage mailboxMessage) {
@@ -90,14 +90,14 @@ public class MailboxEventDispatcher {
         SortedMap<MessageUid, MessageMetaData> metaDataMap = ImmutableSortedMap.<MessageUid, MessageMetaData>naturalOrder()
                 .put(messageMetaData.getUid(), messageMetaData)
                 .build();
-        added(session, metaDataMap, mailbox, ImmutableMap.of(mailboxMessage.getUid(), mailboxMessage));
+        added(session, metaDataMap, mailbox);
     }
 
     public void added(MailboxSession session, MessageMetaData messageMetaData, Mailbox mailbox) {
         SortedMap<MessageUid, MessageMetaData> metaDataMap = ImmutableSortedMap.<MessageUid, MessageMetaData>naturalOrder()
             .put(messageMetaData.getUid(), messageMetaData)
             .build();
-        added(session, metaDataMap, mailbox, ImmutableMap.<MessageUid, MailboxMessage>of());
+        added(session, metaDataMap, mailbox);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/07489896/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
index 01a0d21..104291e 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
@@ -1,162 +1,157 @@
-/****************************************************************
- * 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.store.search;
-
-import java.util.List;
-import java.util.Optional;
-
-import org.apache.james.mailbox.Event;
-import org.apache.james.mailbox.MailboxListener;
-import org.apache.james.mailbox.MailboxManager;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MessageUid;
-import org.apache.james.mailbox.exception.MailboxException;
-import org.apache.james.mailbox.model.MessageRange;
-import org.apache.james.mailbox.model.UpdatedFlags;
-import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
-import org.apache.james.mailbox.store.event.EventFactory;
-import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
-import org.apache.james.mailbox.store.mail.model.Mailbox;
-import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * {@link MessageSearchIndex} which needs to get registered as global {@link MailboxListener} and so get
- * notified about message changes. This will then allow to update the underlying index.
- * 
- *
- */
-public abstract class ListeningMessageSearchIndex implements MessageSearchIndex, MailboxListener {
-    private static final Logger LOGGER = LoggerFactory.getLogger(ListeningMessageSearchIndex.class);
-
-    private static final int UNLIMITED = -1;
-    private final MailboxSessionMapperFactory factory;
-    private final MailboxManager mailboxManager;
-
-    public ListeningMessageSearchIndex(MailboxSessionMapperFactory factory, MailboxManager mailboxManager) {
-        this.factory = factory;
-        this.mailboxManager = mailboxManager;
-    }
-
-    /**
-     * Process the {@link org.apache.james.mailbox.Event} and update the index if
-     * something relevant is received
-     */
-    @Override
-    public void event(Event event) {
-        try {
-            MailboxSession session = mailboxManager.createSystemSession(event.getUser().asString());
-            if (event instanceof MessageEvent) {
-                if (event instanceof EventFactory.AddedImpl) {
-                    EventFactory.AddedImpl added = (EventFactory.AddedImpl) event;
-                    Mailbox mailbox = added.getMailbox();
-
-                    for (MessageUid uid : added.getUids()) {
-                        retrieveMailboxMessage(session, added, mailbox, uid)
-                            .ifPresent(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
-                    }
-                } else if (event instanceof EventFactory.ExpungedImpl) {
-                    EventFactory.ExpungedImpl expunged = (EventFactory.ExpungedImpl) event;
-                    try {
-                        delete(session, expunged.getMailbox(), expunged.getUids());
-                    } catch (MailboxException e) {
-                        LOGGER.error("Unable to deleted messages {} from index for mailbox {}", expunged.getUids(), expunged.getMailbox(), e);
-                    }
-                } else if (event instanceof EventFactory.FlagsUpdatedImpl) {
-                    EventFactory.FlagsUpdatedImpl flagsUpdated = (EventFactory.FlagsUpdatedImpl) event;
-                    Mailbox mailbox = flagsUpdated.getMailbox();
-
-                    try {
-                        update(session, mailbox, flagsUpdated.getUpdatedFlags());
-                    } catch (MailboxException e) {
-                        LOGGER.error("Unable to update flags in index for mailbox {}", mailbox, e);
-                    }
-                }
-            } else if (event instanceof EventFactory.MailboxDeletionImpl) {
-                deleteAll(session, ((EventFactory.MailboxDeletionImpl) event).getMailbox());
-            }
-        } catch (MailboxException e) {
-            LOGGER.error("Unable to update index", e);
-        }
-    }
-
-    private Optional<MailboxMessage> retrieveMailboxMessage(MailboxSession session, EventFactory.AddedImpl added, Mailbox mailbox, MessageUid next) {
-        Optional<MailboxMessage> firstChoice = Optional.ofNullable(added.getAvailableMessages().get(next));
-        if (firstChoice.isPresent()) {
-            return firstChoice;
-        } else {
-            try {
-                return Optional.of(factory.getMessageMapper(session)
-                    .findInMailbox(mailbox, MessageRange.one(next), FetchType.Full, UNLIMITED)
-                    .next());
-            } catch (Exception e) {
-                LOGGER.error("Could not retrieve message {} in mailbox {}", next, mailbox.getMailboxId().serialize(), e);
-                return Optional.empty();
-            }
-        }
-    }
-
-    private void addMessage(MailboxSession session, Mailbox mailbox, MailboxMessage message) {
-        try {
-            add(session, mailbox, message);
-        } catch (MailboxException e) {
-            LOGGER.error("Unable to index message {} for mailbox {}", message.getUid(), mailbox, e);
-        }
-    }
-
-    /**
-     * Add the {@link MailboxMessage} for the given {@link Mailbox} to the index
-     *
-     * @param session The mailbox session performing the message addition
-     * @param mailbox mailbox on which the message addition was performed
-     * @param message The added message
-     * @throws MailboxException
-     */
-    public abstract void add(MailboxSession session, Mailbox mailbox, MailboxMessage message) throws MailboxException;
-
-    /**
-     * Delete the concerned UIDs for the given {@link Mailbox} from the index
-     *
-     * @param session The mailbox session performing the expunge
-     * @param mailbox mailbox on which the expunge was performed
-     * @param expungedUids UIDS to be deleted
-     * @throws MailboxException
-     */
-    public abstract void delete(MailboxSession session, Mailbox mailbox, List<MessageUid> expungedUids) throws MailboxException;
-
-    /**
-     * Delete the messages contained in the given {@link Mailbox} from the index
-     *
-     * @param session The mailbox session performing the expunge
-     * @param mailbox mailbox on which the expunge was performed
-     * @throws MailboxException
-     */
-    public abstract void deleteAll(MailboxSession session, Mailbox mailbox) throws MailboxException;
-    
-    /**
-     * Update the messages concerned by the updated flags list for the given {@link Mailbox}
-     *
-     * @param session session that performed the update
-     * @param mailbox mailbox containing the updated messages
-     * @param updatedFlagsList list of flags that were updated
-     * @throws MailboxException
-     */
-    public abstract void update(MailboxSession session, Mailbox mailbox, List<UpdatedFlags> updatedFlagsList) throws MailboxException;
-}
+/****************************************************************
+ * 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.store.search;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.james.mailbox.Event;
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.model.UpdatedFlags;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
+import org.apache.james.mailbox.store.event.EventFactory;
+import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link MessageSearchIndex} which needs to get registered as global {@link MailboxListener} and so get
+ * notified about message changes. This will then allow to update the underlying index.
+ * 
+ *
+ */
+public abstract class ListeningMessageSearchIndex implements MessageSearchIndex, MailboxListener {
+    private static final Logger LOGGER = LoggerFactory.getLogger(ListeningMessageSearchIndex.class);
+
+    private static final int UNLIMITED = -1;
+    private final MailboxSessionMapperFactory factory;
+    private final MailboxManager mailboxManager;
+
+    public ListeningMessageSearchIndex(MailboxSessionMapperFactory factory, MailboxManager mailboxManager) {
+        this.factory = factory;
+        this.mailboxManager = mailboxManager;
+    }
+
+    /**
+     * Process the {@link org.apache.james.mailbox.Event} and update the index if
+     * something relevant is received
+     */
+    @Override
+    public void event(Event event) {
+        try {
+            MailboxSession session = mailboxManager.createSystemSession(event.getUser().asString());
+            if (event instanceof MessageEvent) {
+                if (event instanceof EventFactory.AddedImpl) {
+                    EventFactory.AddedImpl added = (EventFactory.AddedImpl) event;
+                    Mailbox mailbox = added.getMailbox();
+
+                    for (MessageUid uid : added.getUids()) {
+                        retrieveMailboxMessage(session, mailbox, uid)
+                            .ifPresent(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
+                    }
+                } else if (event instanceof EventFactory.ExpungedImpl) {
+                    EventFactory.ExpungedImpl expunged = (EventFactory.ExpungedImpl) event;
+                    try {
+                        delete(session, expunged.getMailbox(), expunged.getUids());
+                    } catch (MailboxException e) {
+                        LOGGER.error("Unable to deleted messages {} from index for mailbox {}", expunged.getUids(), expunged.getMailbox(), e);
+                    }
+                } else if (event instanceof EventFactory.FlagsUpdatedImpl) {
+                    EventFactory.FlagsUpdatedImpl flagsUpdated = (EventFactory.FlagsUpdatedImpl) event;
+                    Mailbox mailbox = flagsUpdated.getMailbox();
+
+                    try {
+                        update(session, mailbox, flagsUpdated.getUpdatedFlags());
+                    } catch (MailboxException e) {
+                        LOGGER.error("Unable to update flags in index for mailbox {}", mailbox, e);
+                    }
+                }
+            } else if (event instanceof EventFactory.MailboxDeletionImpl) {
+                deleteAll(session, ((EventFactory.MailboxDeletionImpl) event).getMailbox());
+            }
+        } catch (MailboxException e) {
+            LOGGER.error("Unable to update index", e);
+        }
+    }
+
+    private Optional<MailboxMessage> retrieveMailboxMessage(MailboxSession session, Mailbox mailbox, MessageUid uid) {
+        try {
+            return Optional.of(factory.getMessageMapper(session)
+                .findInMailbox(mailbox, MessageRange.one(uid), FetchType.Full, UNLIMITED)
+                .next());
+        } catch (Exception e) {
+            LOGGER.error("Could not retrieve message {} in mailbox {}", uid.asLong(), mailbox.getMailboxId().serialize(), e);
+            return Optional.empty();
+        }
+    }
+
+    private void addMessage(MailboxSession session, Mailbox mailbox, MailboxMessage message) {
+        try {
+            add(session, mailbox, message);
+        } catch (MailboxException e) {
+            LOGGER.error("Unable to index message {} for mailbox {}", message.getUid(), mailbox, e);
+        }
+    }
+
+    /**
+     * Add the {@link MailboxMessage} for the given {@link Mailbox} to the index
+     *
+     * @param session The mailbox session performing the message addition
+     * @param mailbox mailbox on which the message addition was performed
+     * @param message The added message
+     * @throws MailboxException
+     */
+    public abstract void add(MailboxSession session, Mailbox mailbox, MailboxMessage message) throws MailboxException;
+
+    /**
+     * Delete the concerned UIDs for the given {@link Mailbox} from the index
+     *
+     * @param session The mailbox session performing the expunge
+     * @param mailbox mailbox on which the expunge was performed
+     * @param expungedUids UIDS to be deleted
+     * @throws MailboxException
+     */
+    public abstract void delete(MailboxSession session, Mailbox mailbox, List<MessageUid> expungedUids) throws MailboxException;
+
+    /**
+     * Delete the messages contained in the given {@link Mailbox} from the index
+     *
+     * @param session The mailbox session performing the expunge
+     * @param mailbox mailbox on which the expunge was performed
+     * @throws MailboxException
+     */
+    public abstract void deleteAll(MailboxSession session, Mailbox mailbox) throws MailboxException;
+    
+    /**
+     * Update the messages concerned by the updated flags list for the given {@link Mailbox}
+     *
+     * @param session session that performed the update
+     * @param mailbox mailbox containing the updated messages
+     * @param updatedFlagsList list of flags that were updated
+     * @throws MailboxException
+     */
+    public abstract void update(MailboxSession session, Mailbox mailbox, List<UpdatedFlags> updatedFlagsList) throws MailboxException;
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/07489896/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java
----------------------------------------------------------------------
diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java
index dfb1797..7973ab2 100644
--- a/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java
+++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java
@@ -63,7 +63,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
 
 
 public class SelectedMailboxImplTest {
@@ -171,6 +170,6 @@ public class SelectedMailboxImplTest {
         TreeMap<MessageUid, MessageMetaData> result = new TreeMap<>();
         result.put(EMITTED_EVENT_UID, new SimpleMessageMetaData(EMITTED_EVENT_UID, MOD_SEQ, new Flags(), SIZE, new Date(), new DefaultMessageId()));
         mailboxListener.event(new EventFactory().added(MailboxSession.SessionId.of(random.nextLong()),
-            mock(User.class), result, mailbox, ImmutableMap.of()));
+            mock(User.class), result, mailbox));
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[03/16] james-project git commit: MAILBOX-357 SetMessagesMethodTest should rely on event API

Posted by bt...@apache.org.
MAILBOX-357 SetMessagesMethodTest should rely on event API

Specific implementation details are never accessed


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

Branch: refs/heads/master
Commit: a8a5b01e12a4ef2cb212d1546503810145c4c908
Parents: c8d9080
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:29:24 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:57 2018 +0700

----------------------------------------------------------------------
 .../james/jmap/methods/integration/SetMessagesMethodTest.java   | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/a8a5b01e/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
index ff955e3..67d7624 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesMethodTest.java
@@ -96,7 +96,6 @@ import org.apache.james.mailbox.model.SerializableQuotaValue;
 import org.apache.james.mailbox.probe.ACLProbe;
 import org.apache.james.mailbox.probe.MailboxProbe;
 import org.apache.james.mailbox.probe.QuotaProbe;
-import org.apache.james.mailbox.store.event.EventFactory;
 import org.apache.james.modules.ACLProbeImpl;
 import org.apache.james.modules.MailboxProbeImpl;
 import org.apache.james.modules.QuotaProbesImpl;
@@ -2338,10 +2337,10 @@ public abstract class SetMessagesMethodTest {
     }
 
     private boolean isAddedToOutboxEvent(String messageId, Event event, String outboxId) {
-        if (!(event instanceof EventFactory.AddedImpl)) {
+        if (!(event instanceof MailboxListener.Added)) {
             return false;
         }
-        EventFactory.AddedImpl added = (EventFactory.AddedImpl) event;
+        MailboxListener.Added added = (MailboxListener.Added) event;
         return added.getMailboxId().serialize().equals(outboxId)
             && added.getUids().size() == 1
             && added.getMetaData(added.getUids().get(0)).getMessageId().serialize().equals(messageId);


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[11/16] james-project git commit: MAILBOX-357 Get rid of the EventFactory#MailboxAware API

Posted by bt...@apache.org.
MAILBOX-357 Get rid of the EventFactory#MailboxAware API


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

Branch: refs/heads/master
Commit: 6d75890a86ba3b351ba86e8ed4ac0c9a67a5a2d7
Parents: 13f6aff
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:25:24 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../james/mailbox/store/event/EventFactory.java | 69 ++------------------
 1 file changed, 6 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6d75890a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
index 1fd51e5..9a372ac 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
@@ -45,18 +45,12 @@ import com.google.common.collect.ImmutableMap;
 
 public class EventFactory {
 
-    public interface MailboxAware {
-        Mailbox getMailbox();
-    }
-
-    public final class AddedImpl extends MailboxListener.Added implements MailboxAware {
+    public final class AddedImpl extends MailboxListener.Added {
         private final Map<MessageUid, MessageMetaData> added;
-        private final Mailbox mailbox;
 
         public AddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, SortedMap<MessageUid, MessageMetaData> uids) {
             super(sessionId, user, new StoreMailboxPath(mailbox), mailbox.getMailboxId());
             this.added = ImmutableMap.copyOf(uids);
-            this.mailbox = mailbox;
         }
 
         @Override
@@ -68,21 +62,14 @@ public class EventFactory {
         public MessageMetaData getMetaData(MessageUid uid) {
             return added.get(uid);
         }
-
-        @Override
-        public Mailbox getMailbox() {
-            return mailbox;
-        }
     }
 
-    public final class ExpungedImpl extends MailboxListener.Expunged implements MailboxAware {
+    public final class ExpungedImpl extends MailboxListener.Expunged {
         private final Map<MessageUid, MessageMetaData> uids;
-        private final Mailbox mailbox;
 
         public ExpungedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox,  Map<MessageUid, MessageMetaData> uids) {
             super(sessionId, user,  new StoreMailboxPath(mailbox), mailbox.getMailboxId());
             this.uids = ImmutableMap.copyOf(uids);
-            this.mailbox = mailbox;
         }
 
         @Override
@@ -94,25 +81,16 @@ public class EventFactory {
         public MessageMetaData getMetaData(MessageUid uid) {
             return uids.get(uid);
         }
-
-        @Override
-        public Mailbox getMailbox() {
-            return mailbox;
-        }
     }
 
-    public final class FlagsUpdatedImpl extends MailboxListener.FlagsUpdated implements MailboxAware {
+    public final class FlagsUpdatedImpl extends MailboxListener.FlagsUpdated {
         private final List<MessageUid> uids;
-
-        private final Mailbox mailbox;
-
         private final List<UpdatedFlags> uFlags;
 
         public FlagsUpdatedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, List<MessageUid> uids, List<UpdatedFlags> uFlags) {
             super(sessionId, user, new StoreMailboxPath(mailbox), mailbox.getMailboxId());
             this.uids = ImmutableList.copyOf(uids);
             this.uFlags = ImmutableList.copyOf(uFlags);
-            this.mailbox = mailbox;
         }
 
         @Override
@@ -124,67 +102,32 @@ public class EventFactory {
         public List<UpdatedFlags> getUpdatedFlags() {
             return uFlags;
         }
-
-        @Override
-        public Mailbox getMailbox() {
-            return mailbox;
-        }
-
     }
 
-    public final class MailboxDeletionImpl extends MailboxListener.MailboxDeletion implements MailboxAware {
-        private final Mailbox mailbox;
-
+    public final class MailboxDeletionImpl extends MailboxListener.MailboxDeletion {
         public MailboxDeletionImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, QuotaRoot quotaRoot, QuotaCount deletedMessageCount, QuotaSize totalDeletedSize) {
             super(sessionId, user, new StoreMailboxPath(mailbox), quotaRoot, deletedMessageCount, totalDeletedSize, mailbox.getMailboxId());
-            this.mailbox = mailbox;
         }
-
-
-        @Override
-        public Mailbox getMailbox() {
-            return mailbox;
-        }
-
     }
 
-    public final class MailboxAddedImpl extends MailboxListener.MailboxAdded implements MailboxAware {
-
-        private final Mailbox mailbox;
-
+    public final class MailboxAddedImpl extends MailboxListener.MailboxAdded {
         public MailboxAddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox) {
             super(sessionId, user,  new StoreMailboxPath(mailbox), mailbox.getMailboxId());
-            this.mailbox = mailbox;
         }
-
-
-        @Override
-        public Mailbox getMailbox() {
-            return mailbox;
-        }
-
     }
 
-    public final class MailboxRenamedEventImpl extends MailboxListener.MailboxRenamed implements MailboxAware {
-
+    public final class MailboxRenamedEventImpl extends MailboxListener.MailboxRenamed {
         private final MailboxPath newPath;
-        private final Mailbox newMailbox;
 
         public MailboxRenamedEventImpl(MailboxSession.SessionId sessionId, User user, MailboxPath oldPath, Mailbox newMailbox) {
             super(sessionId, user, oldPath, newMailbox.getMailboxId());
             this.newPath = new StoreMailboxPath(newMailbox);
-            this.newMailbox = newMailbox;
         }
 
         @Override
         public MailboxPath getNewPath() {
             return newPath;
         }
-
-        @Override
-        public Mailbox getMailbox() {
-            return newMailbox;
-        }
     }
 
     public MailboxListener.Added added(MailboxSession session, SortedMap<MessageUid, MessageMetaData> uids, Mailbox mailbox) {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[12/16] james-project git commit: MAILBOX-357 Group messages by ranges when retrieving them in MailboxListener

Posted by bt...@apache.org.
MAILBOX-357 Group messages by ranges when retrieving them in MailboxListener


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

Branch: refs/heads/master
Commit: 8051692fecc111962e4e733b5cad87f6125fffe6
Parents: 2dab033
Author: Benoit Tellier <bt...@linagora.com>
Authored: Wed Dec 12 09:12:03 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../james/mailbox/spamassassin/SpamAssassinListener.java | 11 +++++------
 .../store/search/ListeningMessageSearchIndex.java        | 10 +++++-----
 2 files changed, 10 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8051692f/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
index 0c2ea77..3c5e937 100644
--- 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
@@ -27,7 +27,6 @@ import javax.inject.Inject;
 import org.apache.james.mailbox.Event;
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.Role;
 import org.apache.james.mailbox.SystemMailboxesProvider;
 import org.apache.james.mailbox.exception.MailboxException;
@@ -99,9 +98,9 @@ public class SpamAssassinListener implements SpamEventListener {
             Mailbox mailbox = mapperFactory.getMailboxMapper(session).findMailboxById(addedEvent.getMailboxId());
             MessageMapper messageMapper = mapperFactory.getMessageMapper(session);
 
-            List<InputStream> contents = addedEvent.getUids()
+            List<InputStream> contents = MessageRange.toRanges(addedEvent.getUids())
                 .stream()
-                .flatMap(uid -> retrieveMessage(messageMapper, mailbox, uid))
+                .flatMap(range -> retrieveMessages(messageMapper, mailbox, range))
                 .map(Throwing.function(MailboxMessage::getFullContent))
                 .collect(Guavate.toImmutableList());
             spamAssassin.learnHam(contents, event.getUser());
@@ -120,11 +119,11 @@ public class SpamAssassinListener implements SpamEventListener {
         }
     }
 
-    private Stream<MailboxMessage> retrieveMessage(MessageMapper messageMapper, Mailbox mailbox, MessageUid uid) {
+    private Stream<MailboxMessage> retrieveMessages(MessageMapper messageMapper, Mailbox mailbox, MessageRange range) {
         try {
-            return Iterators.toStream(messageMapper.findInMailbox(mailbox, MessageRange.one(uid), MessageMapper.FetchType.Full, LIMIT));
+            return Iterators.toStream(messageMapper.findInMailbox(mailbox, range, MessageMapper.FetchType.Full, LIMIT));
         } catch (MailboxException e) {
-            LOGGER.warn("Can not retrieve message {} {}", mailbox.getMailboxId(), uid.asLong(), e);
+            LOGGER.warn("Can not retrieve message {} {}", mailbox.getMailboxId(), range.toString(), e);
             return Stream.empty();
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/8051692f/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
index 7de8fcc..3adaf38 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
@@ -102,19 +102,19 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
     }
 
     private void handleAdded(MailboxSession session, Mailbox mailbox, Added added) {
-        added.getUids()
+        MessageRange.toRanges(added.getUids())
             .stream()
-            .flatMap(uid -> retrieveMailboxMessage(session, mailbox, uid))
+            .flatMap(range -> retrieveMailboxMessages(session, mailbox, range))
             .forEach(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
     }
 
-    private Stream<MailboxMessage> retrieveMailboxMessage(MailboxSession session, Mailbox mailbox, MessageUid uid) {
+    private Stream<MailboxMessage> retrieveMailboxMessages(MailboxSession session, Mailbox mailbox, MessageRange range) {
         try {
             return Stream.of(factory.getMessageMapper(session)
-                .findInMailbox(mailbox, MessageRange.one(uid), FetchType.Full, UNLIMITED)
+                .findInMailbox(mailbox, range, FetchType.Full, UNLIMITED)
                 .next());
         } catch (Exception e) {
-            LOGGER.error("Could not retrieve message {} in mailbox {}", uid.asLong(), mailbox.getMailboxId().serialize(), e);
+            LOGGER.error("Could not retrieve message {} in mailbox {}", range.toString(), mailbox.getMailboxId().serialize(), e);
             return Stream.empty();
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[13/16] james-project git commit: MAILBOX-357 Improve SpamAssassinListener with method extraction

Posted by bt...@apache.org.
MAILBOX-357 Improve SpamAssassinListener with method extraction


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

Branch: refs/heads/master
Commit: f1cf245245b7c190ddd3743cef0b65c36a2f47b2
Parents: 0e01d39
Author: Benoit Tellier <bt...@linagora.com>
Authored: Tue Dec 11 10:14:05 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../spamassassin/SpamAssassinListener.java      | 50 +++++++++++---------
 1 file changed, 28 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/f1cf2452/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
index 8ac45e1..0c2ea77 100644
--- 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
@@ -84,36 +84,42 @@ public class SpamAssassinListener implements SpamEventListener {
         try {
             MailboxSession session = mailboxManager.createSystemSession(getClass().getCanonicalName());
             if (event instanceof MessageMoveEvent) {
-                MessageMoveEvent messageMoveEvent = (MessageMoveEvent) event;
-                if (isMessageMovedToSpamMailbox(messageMoveEvent)) {
-                    LOGGER.debug("Spam event detected");
-                    ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
-                    spamAssassin.learnSpam(messages, event.getUser());
-                }
-                if (isMessageMovedOutOfSpamMailbox(messageMoveEvent)) {
-                    ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
-                    spamAssassin.learnHam(messages, event.getUser());
-                }
+                handleMessageMove(event, (MessageMoveEvent) event);
             }
             if (event instanceof Added) {
-                Added addedEvent = (Added) event;
-                if (isAppendedToInbox(addedEvent)) {
-                    Mailbox mailbox = mapperFactory.getMailboxMapper(session).findMailboxById(addedEvent.getMailboxId());
-                    MessageMapper messageMapper = mapperFactory.getMessageMapper(session);
-
-                    List<InputStream> contents = addedEvent.getUids()
-                        .stream()
-                        .flatMap(uid -> retrieveMessage(messageMapper, mailbox, uid))
-                        .map(Throwing.function(MailboxMessage::getFullContent))
-                        .collect(Guavate.toImmutableList());
-                    spamAssassin.learnHam(contents, event.getUser());
-                }
+                handleAdded(event, session, (Added) event);
             }
         } catch (MailboxException e) {
             LOGGER.warn("Error while processing SPAM listener", e);
         }
     }
 
+    private void handleAdded(Event event, MailboxSession session, Added addedEvent) throws MailboxException {
+        if (isAppendedToInbox(addedEvent)) {
+            Mailbox mailbox = mapperFactory.getMailboxMapper(session).findMailboxById(addedEvent.getMailboxId());
+            MessageMapper messageMapper = mapperFactory.getMessageMapper(session);
+
+            List<InputStream> contents = addedEvent.getUids()
+                .stream()
+                .flatMap(uid -> retrieveMessage(messageMapper, mailbox, uid))
+                .map(Throwing.function(MailboxMessage::getFullContent))
+                .collect(Guavate.toImmutableList());
+            spamAssassin.learnHam(contents, event.getUser());
+        }
+    }
+
+    private void handleMessageMove(Event event, MessageMoveEvent messageMoveEvent) {
+        if (isMessageMovedToSpamMailbox(messageMoveEvent)) {
+            LOGGER.debug("Spam event detected");
+            ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
+            spamAssassin.learnSpam(messages, event.getUser());
+        }
+        if (isMessageMovedOutOfSpamMailbox(messageMoveEvent)) {
+            ImmutableList<InputStream> messages = retrieveMessages(messageMoveEvent);
+            spamAssassin.learnHam(messages, event.getUser());
+        }
+    }
+
     private Stream<MailboxMessage> retrieveMessage(MessageMapper messageMapper, Mailbox mailbox, MessageUid uid) {
         try {
             return Iterators.toStream(messageMapper.findInMailbox(mailbox, MessageRange.one(uid), MessageMapper.FetchType.Full, LIMIT));


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[04/16] james-project git commit: MAILBOX-357 MailboxAnnotationListener can read MailboxId directly

Posted by bt...@apache.org.
MAILBOX-357 MailboxAnnotationListener can read MailboxId directly


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

Branch: refs/heads/master
Commit: c8d90800d7dbf71321c3bdf6f86aad9e7009043d
Parents: 57a0212
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:22:42 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:57 2018 +0700

----------------------------------------------------------------------
 .../james/mailbox/store/event/MailboxAnnotationListener.java     | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c8d90800/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxAnnotationListener.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxAnnotationListener.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxAnnotationListener.java
index e016361..b64dd6d 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxAnnotationListener.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/MailboxAnnotationListener.java
@@ -52,11 +52,11 @@ public class MailboxAnnotationListener implements MailboxListener {
 
     @Override
     public void event(Event event) {
-        if (event instanceof EventFactory.MailboxDeletionImpl) {
+        if (event instanceof MailboxDeletion) {
             try {
                 MailboxSession mailboxSession = mailboxManager.createSystemSession(event.getUser().asString());
                 AnnotationMapper annotationMapper = mailboxSessionMapperFactory.getAnnotationMapper(mailboxSession);
-                MailboxId mailboxId = ((EventFactory.MailboxDeletionImpl) event).getMailbox().getMailboxId();
+                MailboxId mailboxId = ((MailboxDeletion) event).getMailboxId();
 
                 deleteRelatedAnnotations(mailboxId, annotationMapper);
             } catch (MailboxException e) {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[06/16] james-project git commit: MAILBOX-357 EventFactory events should be private

Posted by bt...@apache.org.
MAILBOX-357 EventFactory events should be private


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

Branch: refs/heads/master
Commit: 2ef26a793652e6eb9aafe0167f22874fbd64c789
Parents: 2fa3183
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:39:15 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../james/mailbox/store/event/EventFactory.java | 24 ++++++++++----------
 1 file changed, 12 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2ef26a79/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
index 9a372ac..7df4a95 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/event/EventFactory.java
@@ -45,10 +45,10 @@ import com.google.common.collect.ImmutableMap;
 
 public class EventFactory {
 
-    public final class AddedImpl extends MailboxListener.Added {
+    private final class AddedImpl extends MailboxListener.Added {
         private final Map<MessageUid, MessageMetaData> added;
 
-        public AddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, SortedMap<MessageUid, MessageMetaData> uids) {
+        AddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, SortedMap<MessageUid, MessageMetaData> uids) {
             super(sessionId, user, new StoreMailboxPath(mailbox), mailbox.getMailboxId());
             this.added = ImmutableMap.copyOf(uids);
         }
@@ -64,10 +64,10 @@ public class EventFactory {
         }
     }
 
-    public final class ExpungedImpl extends MailboxListener.Expunged {
+    private final class ExpungedImpl extends MailboxListener.Expunged {
         private final Map<MessageUid, MessageMetaData> uids;
 
-        public ExpungedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox,  Map<MessageUid, MessageMetaData> uids) {
+        ExpungedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, Map<MessageUid, MessageMetaData> uids) {
             super(sessionId, user,  new StoreMailboxPath(mailbox), mailbox.getMailboxId());
             this.uids = ImmutableMap.copyOf(uids);
         }
@@ -83,11 +83,11 @@ public class EventFactory {
         }
     }
 
-    public final class FlagsUpdatedImpl extends MailboxListener.FlagsUpdated {
+    private final class FlagsUpdatedImpl extends MailboxListener.FlagsUpdated {
         private final List<MessageUid> uids;
         private final List<UpdatedFlags> uFlags;
 
-        public FlagsUpdatedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, List<MessageUid> uids, List<UpdatedFlags> uFlags) {
+        FlagsUpdatedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, List<MessageUid> uids, List<UpdatedFlags> uFlags) {
             super(sessionId, user, new StoreMailboxPath(mailbox), mailbox.getMailboxId());
             this.uids = ImmutableList.copyOf(uids);
             this.uFlags = ImmutableList.copyOf(uFlags);
@@ -104,22 +104,22 @@ public class EventFactory {
         }
     }
 
-    public final class MailboxDeletionImpl extends MailboxListener.MailboxDeletion {
-        public MailboxDeletionImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, QuotaRoot quotaRoot, QuotaCount deletedMessageCount, QuotaSize totalDeletedSize) {
+    private final class MailboxDeletionImpl extends MailboxListener.MailboxDeletion {
+        MailboxDeletionImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox, QuotaRoot quotaRoot, QuotaCount deletedMessageCount, QuotaSize totalDeletedSize) {
             super(sessionId, user, new StoreMailboxPath(mailbox), quotaRoot, deletedMessageCount, totalDeletedSize, mailbox.getMailboxId());
         }
     }
 
-    public final class MailboxAddedImpl extends MailboxListener.MailboxAdded {
-        public MailboxAddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox) {
+    private final class MailboxAddedImpl extends MailboxListener.MailboxAdded {
+        MailboxAddedImpl(MailboxSession.SessionId sessionId, User user, Mailbox mailbox) {
             super(sessionId, user,  new StoreMailboxPath(mailbox), mailbox.getMailboxId());
         }
     }
 
-    public final class MailboxRenamedEventImpl extends MailboxListener.MailboxRenamed {
+    private final class MailboxRenamedEventImpl extends MailboxListener.MailboxRenamed {
         private final MailboxPath newPath;
 
-        public MailboxRenamedEventImpl(MailboxSession.SessionId sessionId, User user, MailboxPath oldPath, Mailbox newMailbox) {
+        MailboxRenamedEventImpl(MailboxSession.SessionId sessionId, User user, MailboxPath oldPath, Mailbox newMailbox) {
             super(sessionId, user, oldPath, newMailbox.getMailboxId());
             this.newPath = new StoreMailboxPath(newMailbox);
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[07/16] james-project git commit: MAILBOX-357 SpamAssassinListenerTest should use the Event factory

Posted by bt...@apache.org.
MAILBOX-357 SpamAssassinListenerTest should use the Event factory

This avoids a direct reference to the specific "store" objects


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

Branch: refs/heads/master
Commit: 2fa3183eec80e949bea31ea1491c57d490c721c5
Parents: 6d75890
Author: Benoit Tellier <bt...@linagora.com>
Authored: Mon Dec 10 14:37:02 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../spamassassin/SpamAssassinListenerTest.java  | 23 ++++++++------------
 1 file changed, 9 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2fa3183e/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
index bc8aaeb..dc1d1d0 100644
--- 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
@@ -48,7 +48,6 @@ import org.apache.james.mailbox.store.SimpleMessageMetaData;
 import org.apache.james.mailbox.store.StoreMailboxManager;
 import org.apache.james.mailbox.store.SystemMailboxesProviderImpl;
 import org.apache.james.mailbox.store.event.EventFactory;
-import org.apache.james.mailbox.store.event.EventFactory.AddedImpl;
 import org.apache.james.mailbox.store.event.MessageMoveEvent;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
@@ -249,13 +248,11 @@ public class SpamAssassinListenerTest {
     @Test
     public void eventShouldCallSpamAssassinHamLearningWhenTheMessageIsAddedInInbox() {
         SimpleMailboxMessage message = createMessage(inboxId);
-        EventFactory eventFactory = new EventFactory();
-        AddedImpl addedEvent = eventFactory.new AddedImpl(
-                MAILBOX_SESSION.getSessionId(),
-                MAILBOX_SESSION.getUser().getCoreUser(),
-                inbox,
+
+        MailboxListener.Added addedEvent = new EventFactory().added(
+                MAILBOX_SESSION,
                 ImmutableSortedMap.of(MessageUid.of(45), new SimpleMessageMetaData(message)),
-                ImmutableMap.of(MessageUid.of(45), message));
+                inbox);
 
         listener.event(addedEvent);
 
@@ -265,13 +262,11 @@ public class SpamAssassinListenerTest {
     @Test
     public void eventShouldNotCallSpamAssassinHamLearningWhenTheMessageIsAddedInAMailboxOtherThanInbox() {
         SimpleMailboxMessage message = createMessage(mailboxId1);
-        EventFactory eventFactory = new EventFactory();
-        AddedImpl addedEvent = eventFactory.new AddedImpl(
-                MAILBOX_SESSION.getSessionId(),
-                MAILBOX_SESSION.getUser().getCoreUser(),
-                mailbox1,
-                ImmutableSortedMap.of(MessageUid.of(45), new SimpleMessageMetaData(message)),
-                ImmutableMap.of(MessageUid.of(45), message));
+
+        MailboxListener.Added addedEvent = new EventFactory().added(
+            MAILBOX_SESSION,
+            ImmutableSortedMap.of(MessageUid.of(45), new SimpleMessageMetaData(message)),
+            inbox);
 
         listener.event(addedEvent);
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[08/16] james-project git commit: MAILBOX-357 Use functional paradigm in ListeningSearchIndex

Posted by bt...@apache.org.
MAILBOX-357 Use functional paradigm in ListeningSearchIndex


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

Branch: refs/heads/master
Commit: 205f8178a6f84b0cefa08c127e4b43d51d0a900d
Parents: 2ef26a7
Author: Benoit Tellier <bt...@linagora.com>
Authored: Tue Dec 11 09:02:09 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../store/search/ListeningMessageSearchIndex.java   | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/205f8178/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
index e02b736..e11dc86 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
@@ -19,7 +19,7 @@
 package org.apache.james.mailbox.store.search;
 
 import java.util.List;
-import java.util.Optional;
+import java.util.stream.Stream;
 
 import org.apache.james.mailbox.Event;
 import org.apache.james.mailbox.MailboxListener;
@@ -69,10 +69,10 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
                 if (event instanceof Added) {
                     Added added = (Added) event;
 
-                    for (MessageUid uid : added.getUids()) {
-                        retrieveMailboxMessage(session, mailbox, uid)
-                            .ifPresent(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
-                    }
+                    added.getUids()
+                        .stream()
+                        .flatMap(uid -> retrieveMailboxMessage(session, mailbox, uid))
+                        .forEach(mailboxMessage -> addMessage(session, mailbox, mailboxMessage));
                 } else if (event instanceof Expunged) {
                     Expunged expunged = (Expunged) event;
                     try {
@@ -97,14 +97,14 @@ public abstract class ListeningMessageSearchIndex implements MessageSearchIndex,
         }
     }
 
-    private Optional<MailboxMessage> retrieveMailboxMessage(MailboxSession session, Mailbox mailbox, MessageUid uid) {
+    private Stream<MailboxMessage> retrieveMailboxMessage(MailboxSession session, Mailbox mailbox, MessageUid uid) {
         try {
-            return Optional.of(factory.getMessageMapper(session)
+            return Stream.of(factory.getMessageMapper(session)
                 .findInMailbox(mailbox, MessageRange.one(uid), FetchType.Full, UNLIMITED)
                 .next());
         } catch (Exception e) {
             LOGGER.error("Could not retrieve message {} in mailbox {}", uid.asLong(), mailbox.getMailboxId().serialize(), e);
-            return Optional.empty();
+            return Stream.empty();
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[16/16] james-project git commit: MAILBOX-357 Correct ElasticSearch host system

Posted by bt...@apache.org.
MAILBOX-357 Correct ElasticSearch host system

This host system was using two distinct Mapper factories. This causes some
 failures while retrieving mailboxes in the ElasticSearch indexer


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

Branch: refs/heads/master
Commit: 2dab033fba415345411b0cdffe869f67d6800097
Parents: d972ed5
Author: Benoit Tellier <bt...@linagora.com>
Authored: Tue Dec 11 14:26:04 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Wed Dec 12 17:50:58 2018 +0700

----------------------------------------------------------------------
 .../elasticsearch/host/ElasticSearchHostSystem.java | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2dab033f/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java
----------------------------------------------------------------------
diff --git a/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java b/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java
index ac854a4..971c99b 100644
--- a/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java
+++ b/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java
@@ -49,7 +49,6 @@ import org.apache.james.mailbox.elasticsearch.query.QueryConverter;
 import org.apache.james.mailbox.elasticsearch.search.ElasticSearchSearcher;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.inmemory.InMemoryId;
-import org.apache.james.mailbox.inmemory.InMemoryMailboxSessionMapperFactory;
 import org.apache.james.mailbox.inmemory.InMemoryMessageId;
 import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
 import org.apache.james.mailbox.mock.MockMailboxSession;
@@ -95,16 +94,15 @@ public class ElasticSearchHostSystem extends JamesImapHostSystem {
             new TestingClientProvider(embeddedElasticSearch.getNode()).get(),
                 ElasticSearchConfiguration.DEFAULT_CONFIGURATION);
 
-        InMemoryMailboxSessionMapperFactory factory = new InMemoryMailboxSessionMapperFactory();
         InMemoryMessageId.Factory messageIdFactory = new InMemoryMessageId.Factory();
-        this.mailboxManager = new InMemoryIntegrationResources()
+        mailboxManager = new InMemoryIntegrationResources()
             .createMailboxManager(new SimpleGroupMembershipResolver(),
                 authenticator,
                 authorizator);
 
         ThreadFactory threadFactory = NamedThreadFactory.withClassName(getClass());
         ElasticSearchListeningMessageSearchIndex searchIndex = new ElasticSearchListeningMessageSearchIndex(
-            factory,
+            mailboxManager.getMapperFactory(),
             new ElasticSearchIndexer(client,
                 Executors.newSingleThreadExecutor(threadFactory),
                 MailboxElasticSearchConstants.DEFAULT_MAILBOX_WRITE_ALIAS,
@@ -117,14 +115,14 @@ public class ElasticSearchHostSystem extends JamesImapHostSystem {
             new MessageToElasticSearchJson(new DefaultTextExtractor(), ZoneId.systemDefault(), IndexAttachments.YES),
             mailboxManager);
 
-        this.mailboxManager.setMessageSearchIndex(searchIndex);
-        this.mailboxManager.addGlobalListener(searchIndex, new MockMailboxSession("admin"));
+        mailboxManager.setMessageSearchIndex(searchIndex);
+        mailboxManager.addGlobalListener(searchIndex, new MockMailboxSession("admin"));
 
         ImapProcessor defaultImapProcessorFactory =
-            DefaultImapProcessorFactory.createDefaultProcessor(this.mailboxManager,
-                new StoreSubscriptionManager(factory),
+            DefaultImapProcessorFactory.createDefaultProcessor(mailboxManager,
+                new StoreSubscriptionManager(mailboxManager.getMapperFactory()),
                 new NoQuotaManager(),
-                new DefaultUserQuotaRootResolver(mailboxManager, factory),
+                new DefaultUserQuotaRootResolver(mailboxManager, mailboxManager.getMapperFactory()),
                 new DefaultMetricFactory());
         configure(new DefaultImapDecoderFactory().buildImapDecoder(),
             new DefaultImapEncoderFactory().buildImapEncoder(),


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org