james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject james-project git commit: JAMES-1936 JMAP GetMessages should retrieve all messages at once
Date Fri, 10 Feb 2017 15:46:41 GMT
Repository: james-project
Updated Branches:
  refs/heads/master a4d8c8e00 -> d9a6b0f18


JAMES-1936 JMAP GetMessages should retrieve all messages at once


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

Branch: refs/heads/master
Commit: d9a6b0f18c75ea940ff5705d58419c869418f881
Parents: a4d8c8e
Author: Benoit Tellier <btellier@linagora.com>
Authored: Fri Feb 10 16:20:29 2017 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Fri Feb 10 16:30:52 2017 +0700

----------------------------------------------------------------------
 .../james/jmap/methods/GetMessagesMethod.java   | 69 ++++++++++----------
 1 file changed, 34 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d9a6b0f1/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
index 404e404..8361598 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
@@ -19,7 +19,7 @@
 
 package org.apache.james.jmap.methods;
 
-import java.util.List;
+import java.util.Collection;
 import java.util.Optional;
 import java.util.function.Function;
 import java.util.stream.Stream;
@@ -38,9 +38,8 @@ import org.apache.james.jmap.model.MessageProperties;
 import org.apache.james.jmap.model.MessageProperties.HeaderProperty;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageIdManager;
+import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.FetchGroupImpl;
-import org.apache.james.mailbox.model.MailboxId;
-import org.apache.james.mailbox.model.MessageId;
 import org.apache.james.mailbox.model.MessageResult;
 
 import com.fasterxml.jackson.databind.ser.PropertyFilter;
@@ -50,7 +49,7 @@ import com.github.fge.lambdas.functions.ThrowingFunction;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
+import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableSet;
 
 public class GetMessagesMethod implements Method {
@@ -107,41 +106,41 @@ public class GetMessagesMethod implements Method {
 
     private GetMessagesResponse getMessagesResponse(MailboxSession mailboxSession, GetMessagesRequest
getMessagesRequest) {
         getMessagesRequest.getAccountId().ifPresent((input) -> notImplemented());
-        
-        Function<MessageId, Stream<MetaDataWithContent>> loadMessages = loadMessage(mailboxSession);
-        Function<MetaDataWithContent, Message> convertToJmapMessage = Throwing.function(messageFactory::fromMetaDataWithContent).sneakyThrow();
-        
-        List<Message> result = getMessagesRequest.getIds().stream()
-            .flatMap(loadMessages)
-            .map(convertToJmapMessage)
-            .collect(Guavate.toImmutableList());
 
-        return GetMessagesResponse.builder().messages(result).expectedMessageIds(getMessagesRequest.getIds()).build();
+        try {
+            return GetMessagesResponse.builder()
+                .messages(
+                    messageIdManager.getMessages(getMessagesRequest.getIds(), FetchGroupImpl.FULL_CONTENT,
mailboxSession)
+                        .stream()
+                        .collect(Guavate.toImmutableListMultimap(MessageResult::getMessageId))
+                        .asMap()
+                        .values()
+                        .stream()
+                        .filter(collection -> !collection.isEmpty())
+                        .map(Throwing.function(toMetaDataWithContent()).sneakyThrow())
+                        .map(Throwing.function(messageFactory::fromMetaDataWithContent).sneakyThrow())
+                        .collect(Guavate.toImmutableList()))
+                .expectedMessageIds(getMessagesRequest.getIds())
+                .build();
+        } catch (MailboxException e) {
+            throw Throwables.propagate(e);
+        }
     }
 
-    private static void notImplemented() {
-        throw new NotImplementedException();
-    }
-
-    private Function<MessageId, Stream<MetaDataWithContent>> loadMessage(MailboxSession
mailboxSession) {
-        ThrowingFunction<MessageId, Stream<MetaDataWithContent>> toMetaDataWithContentStream
=
-            (MessageId messageId) -> {
-                ImmutableList<MessageResult> messageResults = messageIdManager.getMessages(ImmutableList.of(messageId),
FetchGroupImpl.FULL_CONTENT, mailboxSession)
-                    .stream()
-                    .collect(Guavate.toImmutableList());
-                List<MailboxId> mailboxIds = messageResults.stream()
+    private ThrowingFunction<Collection<MessageResult>, MetaDataWithContent>
toMetaDataWithContent() {
+        return messageResults -> {
+            MessageResult firstMessageResult = messageResults.iterator().next();
+            return MetaDataWithContent.builderFromMessageResult(firstMessageResult)
+                .messageId(firstMessageResult.getMessageId())
+                .mailboxIds(messageResults.stream()
                     .map(MessageResult::getMailboxId)
                     .distinct()
-                    .collect(Guavate.toImmutableList());
-                return messageResults.stream()
-                    .findFirst()
-                    .map(Stream::of)
-                    .orElse(Stream.of())
-                    .map(Throwing.function(MetaDataWithContent::builderFromMessageResult).sneakyThrow())
-                    .map(builder -> builder.messageId(messageId))
-                    .map(builder -> builder.mailboxIds(mailboxIds))
-                    .map(MetaDataWithContent.Builder::build);
-            };
-        return Throwing.function(toMetaDataWithContentStream).sneakyThrow();
+                    .collect(Guavate.toImmutableList()))
+                .build();
+        };
+    }
+
+    private static void notImplemented() {
+        throw new NotImplementedException();
     }
 }
\ No newline at end of file


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


Mime
View raw message