You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2021/01/05 04:55:54 UTC

[james-project] 15/24: JAMES-3485 MessageAppender should group attachment reads too

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 7a7afdd1278d44d9c45187e5935df7ebb01569fe
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Dec 25 22:49:19 2020 +0700

    JAMES-3485 MessageAppender should group attachment reads too
---
 .../james/jmap/draft/methods/MessageAppender.java  | 28 +++++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MessageAppender.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MessageAppender.java
index 611f612..81eb6d8 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MessageAppender.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/MessageAppender.java
@@ -22,6 +22,7 @@ package org.apache.james.jmap.draft.methods;
 import java.io.IOException;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 
 import javax.inject.Inject;
@@ -39,15 +40,16 @@ import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageIdManager;
 import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.MessageManager.AppendResult;
-import org.apache.james.mailbox.exception.AttachmentNotFoundException;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.AttachmentId;
+import org.apache.james.mailbox.model.AttachmentMetadata;
 import org.apache.james.mailbox.model.Cid;
 import org.apache.james.mailbox.model.ComposedMessageId;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MessageAttachmentMetadata;
 import org.apache.james.mime4j.dom.Message;
 import org.apache.james.mime4j.message.DefaultMessageWriter;
+import org.apache.james.util.OptionalUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -147,24 +149,32 @@ public class MessageAppender {
     }
 
     private ImmutableList<MessageAttachmentMetadata> getMessageAttachments(MailboxSession session, ImmutableList<Attachment> attachments) throws MailboxException {
-        ThrowingFunction<Attachment, Optional<MessageAttachmentMetadata>> toMessageAttachment = att -> messageAttachment(session, att);
+        Map<AttachmentId, AttachmentMetadata> attachmentsById = attachmentManager.getAttachments(attachments.stream()
+            .map(attachment -> AttachmentId.from(attachment.getBlobId().getRawValue()))
+            .collect(Guavate.toImmutableList()), session)
+            .stream()
+            .collect(Guavate.toImmutableMap(AttachmentMetadata::getAttachmentId));
+
+        ThrowingFunction<Attachment, Optional<MessageAttachmentMetadata>> toMessageAttachment = att -> messageAttachment(att, attachmentsById);
+
+
         return attachments.stream()
             .map(Throwing.function(toMessageAttachment).sneakyThrow())
             .flatMap(Optional::stream)
             .collect(Guavate.toImmutableList());
     }
 
-    private Optional<MessageAttachmentMetadata> messageAttachment(MailboxSession session, Attachment attachment) throws MailboxException {
+    private Optional<MessageAttachmentMetadata> messageAttachment(Attachment attachment, Map<AttachmentId, AttachmentMetadata> attachmentsById) throws MailboxException {
         try {
-            return Optional.of(MessageAttachmentMetadata.builder()
-                .attachment(attachmentManager.getAttachment(AttachmentId.from(attachment.getBlobId().getRawValue()), session))
+            AttachmentId attachmentId = AttachmentId.from(attachment.getBlobId().getRawValue());
+            return OptionalUtils.executeIfEmpty(Optional.ofNullable(attachmentsById.get(attachmentId))
+                .map(attachmentMetadata -> MessageAttachmentMetadata.builder()
+                    .attachment(attachmentMetadata)
                 .name(attachment.getName().orElse(null))
                 .cid(attachment.getCid().map(Cid::from).orElse(null))
                 .isInline(attachment.isIsInline())
-                .build());
-        } catch (AttachmentNotFoundException e) {
-            LOGGER.error(String.format("Attachment %s not found", attachment.getBlobId()), e);
-            return Optional.empty();
+                .build()),
+                () -> LOGGER.error(String.format("Attachment %s not found", attachment.getBlobId())));
         } catch (IllegalStateException e) {
             LOGGER.error(String.format("Attachment %s is not well-formed", attachment.getBlobId()), e);
             return Optional.empty();


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