JAMES-1818 Use AttachmentManager in creation processor
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b7fe5dfb Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b7fe5dfb Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b7fe5dfb Branch: refs/heads/master Commit: b7fe5dfb3d5fa5e0c08295059564666f4a739b8a Parents: 49b3ba3 Author: Raphael Ouazana <raphael.ouaz...@linagora.com> Authored: Thu Aug 18 17:02:50 2016 +0200 Committer: Raphael Ouazana <raphael.ouaz...@linagora.com> Committed: Mon Aug 29 15:15:43 2016 +0200 ---------------------------------------------------------------------- .../methods/SetMessagesCreationProcessor.java | 45 +++++++++++--------- .../SetMessagesCreationProcessorTest.java | 26 +++++------ 2 files changed, 35 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/b7fe5dfb/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java index 5d1b1e6..ce8538d 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java @@ -26,7 +26,6 @@ import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; -import java.util.stream.Stream; import javax.inject.Inject; import javax.mail.Flags; @@ -57,6 +56,7 @@ import org.apache.james.jmap.send.MailMetadata; import org.apache.james.jmap.send.MailSpool; import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.lifecycle.api.LifecycleUtil; +import org.apache.james.mailbox.AttachmentManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.exception.AttachmentNotFoundException; import org.apache.james.mailbox.exception.MailboxException; @@ -65,8 +65,6 @@ import org.apache.james.mailbox.model.AttachmentId; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.store.MailboxSessionMapperFactory; -import org.apache.james.mailbox.store.mail.AttachmentMapper; -import org.apache.james.mailbox.store.mail.AttachmentMapperFactory; 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; @@ -78,6 +76,9 @@ import org.apache.mailet.Mail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.github.fge.lambdas.Throwing; +import com.github.fge.lambdas.functions.ThrowingFunction; +import com.github.fge.lambdas.predicates.ThrowingPredicate; import com.github.steveash.guavate.Guavate; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; @@ -93,7 +94,7 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor { private final MailFactory mailFactory; private final MessageFactory messageFactory; private final SystemMailboxesProvider systemMailboxesProvider; - private AttachmentMapperFactory attachmentMapperFactory; + private final AttachmentManager attachmentManager; @VisibleForTesting @Inject @@ -103,14 +104,14 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor { MailFactory mailFactory, MessageFactory messageFactory, SystemMailboxesProvider systemMailboxesProvider, - AttachmentMapperFactory attachmentMapperFactory) { + AttachmentManager attachmentManager) { this.mailboxSessionMapperFactory = mailboxSessionMapperFactory; this.mimeMessageConverter = mimeMessageConverter; this.mailSpool = mailSpool; this.mailFactory = mailFactory; this.messageFactory = messageFactory; this.systemMailboxesProvider = systemMailboxesProvider; - this.attachmentMapperFactory = attachmentMapperFactory; + this.attachmentManager = attachmentManager; } @Override @@ -197,24 +198,26 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor { @VisibleForTesting void assertAttachmentsExist(CreationMessageEntry entry, MailboxSession session) throws AttachmentsNotFoundException, MailboxException { List<Attachment> attachments = entry.getValue().getAttachments(); if (!attachments.isEmpty()) { - AttachmentMapper attachmentMapper = attachmentMapperFactory.getAttachmentMapper(session); - List<BlobId> notFounds = listAttachmentsNotFound(attachments, attachmentMapper); + List<BlobId> notFounds = listAttachmentsNotFound(attachments, session); if (!notFounds.isEmpty()) { throw new AttachmentsNotFoundException(notFounds); } } } - private List<BlobId> listAttachmentsNotFound(List<Attachment> attachments, AttachmentMapper attachmentMapper) { + private List<BlobId> listAttachmentsNotFound(List<Attachment> attachments, MailboxSession session) throws MailboxException { + ThrowingPredicate<Attachment> notExists = attachment -> { + try { + attachmentManager.getAttachment(getAttachmentId(attachment), session); + return false; + } catch (AttachmentNotFoundException e) { + return true; + } + }; return attachments.stream() - .flatMap(attachment -> { - try { - attachmentMapper.getAttachment(getAttachmentId(attachment)); - return Stream.of(); - } catch (AttachmentNotFoundException e) { - return Stream.of(attachment.getBlobId()); - } - }).collect(Guavate.toImmutableList()); + .filter(Throwing.predicate(notExists).sneakyThrow()) + .map(Attachment::getBlobId) + .collect(Guavate.toImmutableList()); } private AttachmentId getAttachmentId(Attachment attachment) { @@ -318,16 +321,16 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor { } private ImmutableList<MessageAttachment> getMessageAttachments(MailboxSession session, ImmutableList<Attachment> attachments) throws MailboxException { - AttachmentMapper attachmentMapper = attachmentMapperFactory.getAttachmentMapper(session); + ThrowingFunction<Attachment, MessageAttachment> toMessageAttachment = att -> messageAttachment(session, att); return attachments.stream() - .map(att -> messageAttachment(attachmentMapper, att)) + .map(Throwing.function(toMessageAttachment).sneakyThrow()) .collect(Guavate.toImmutableList()); } - private MessageAttachment messageAttachment(AttachmentMapper attachmentMapper, Attachment attachment) { + private MessageAttachment messageAttachment(MailboxSession session, Attachment attachment) throws MailboxException { try { return MessageAttachment.builder() - .attachment(attachmentMapper.getAttachment(AttachmentId.from(attachment.getBlobId().getRawValue()))) + .attachment(attachmentManager.getAttachment(AttachmentId.from(attachment.getBlobId().getRawValue()), session)) .name(attachment.getName().orElse(null)) .cid(attachment.getCid().map(Cid::from).orElse(null)) .isInline(attachment.isIsInline()) http://git-wip-us.apache.org/repos/asf/james-project/blob/b7fe5dfb/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java index f8cc675..f81ed55 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java @@ -57,6 +57,7 @@ import org.apache.james.jmap.send.MailSpool; import org.apache.james.jmap.utils.HtmlTextExtractor; import org.apache.james.jmap.utils.MailboxBasedHtmlTextExtractor; import org.apache.james.jmap.utils.SystemMailboxesProvider; +import org.apache.james.mailbox.AttachmentManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.exception.AttachmentNotFoundException; import org.apache.james.mailbox.exception.MailboxException; @@ -66,8 +67,6 @@ import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.store.MailboxSessionMapperFactory; import org.apache.james.mailbox.store.TestId; import org.apache.james.mailbox.store.extractor.DefaultTextExtractor; -import org.apache.james.mailbox.store.mail.AttachmentMapper; -import org.apache.james.mailbox.store.mail.AttachmentMapperFactory; 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; @@ -142,8 +141,7 @@ public class SetMessagesCreationProcessorTest { private SystemMailboxesProvider fakeSystemMailboxesProvider; private MockMailboxSession session; private MIMEMessageConverter mimeMessageConverter; - private AttachmentMapper mockedAttachmentMapper; - private AttachmentMapperFactory mockedAttachmentMapperFactory; + private AttachmentManager mockedAttachmentManager; private SetMessagesCreationProcessor sut; @Before @@ -154,15 +152,13 @@ public class SetMessagesCreationProcessorTest { .thenReturn(mockMapper); mockedMailSpool = mock(MailSpool.class); mockedMailFactory = mock(MailFactory.class); - mockedAttachmentMapperFactory = mock(AttachmentMapperFactory.class); - mockedAttachmentMapper = mock(AttachmentMapper.class); - when(mockedAttachmentMapperFactory.getAttachmentMapper(any(MailboxSession.class))).thenReturn(mockedAttachmentMapper); + mockedAttachmentManager = mock(AttachmentManager.class); fakeSystemMailboxesProvider = new TestSystemMailboxesProvider(() -> optionalOutbox, () -> optionalDrafts); session = new MockMailboxSession(USER); mimeMessageConverter = new MIMEMessageConverter(); sut = new SetMessagesCreationProcessor( - stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory, messageFactory, fakeSystemMailboxesProvider, mockedAttachmentMapperFactory); + stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory, messageFactory, fakeSystemMailboxesProvider, mockedAttachmentManager); } @Test @@ -184,7 +180,7 @@ public class SetMessagesCreationProcessorTest { .thenReturn(stubMapper); sut = new SetMessagesCreationProcessor( - mockSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory, messageFactory, fakeSystemMailboxesProvider, mockedAttachmentMapperFactory) { + mockSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory, messageFactory, fakeSystemMailboxesProvider, mockedAttachmentManager) { @Override protected MessageWithId createMessageInOutboxAndSend(ValueWithId.CreationMessageEntry createdEntry, MailboxSession session, Mailbox outbox, Function<Long, MessageId> buildMessageIdFromUid) { return new MessageWithId(createdEntry.getCreationId(), FAKE_OUTBOX_MESSAGE); @@ -204,7 +200,7 @@ public class SetMessagesCreationProcessorTest { // Given TestSystemMailboxesProvider doNotProvideOutbox = new TestSystemMailboxesProvider(Optional::empty, () -> optionalDrafts); SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor( - stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory, messageFactory, doNotProvideOutbox, mockedAttachmentMapperFactory); + stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory, messageFactory, doNotProvideOutbox, mockedAttachmentManager); // When SetMessagesResponse actual = sut.process(createMessageInOutbox, session); @@ -282,10 +278,10 @@ public class SetMessagesCreationProcessorTest { @Test - public void assertAttachmentsExistShouldThrowWhenUnknownBlobId() throws AttachmentNotFoundException { + public void assertAttachmentsExistShouldThrowWhenUnknownBlobId() throws MailboxException { BlobId unknownBlobId = BlobId.of("unknownBlobId"); AttachmentId unknownAttachmentId = AttachmentId.from(unknownBlobId.getRawValue()); - when(mockedAttachmentMapper.getAttachment(unknownAttachmentId)).thenThrow(new AttachmentNotFoundException(unknownBlobId.getRawValue())); + when(mockedAttachmentManager.getAttachment(unknownAttachmentId, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId.getRawValue())); assertThatThrownBy(() -> sut.assertAttachmentsExist( new CreationMessageEntry( @@ -299,14 +295,14 @@ public class SetMessagesCreationProcessorTest { } @Test - public void assertAttachmentsExistShouldThrowWhenUnknownBlobIds() throws AttachmentNotFoundException { + public void assertAttachmentsExistShouldThrowWhenUnknownBlobIds() throws MailboxException { BlobId unknownBlobId1 = BlobId.of("unknownBlobId1"); BlobId unknownBlobId2 = BlobId.of("unknownBlobId2"); AttachmentId unknownAttachmentId1 = AttachmentId.from(unknownBlobId1.getRawValue()); AttachmentId unknownAttachmentId2 = AttachmentId.from(unknownBlobId2.getRawValue()); - when(mockedAttachmentMapper.getAttachment(unknownAttachmentId1)).thenThrow(new AttachmentNotFoundException(unknownBlobId1.getRawValue())); - when(mockedAttachmentMapper.getAttachment(unknownAttachmentId2)).thenThrow(new AttachmentNotFoundException(unknownBlobId2.getRawValue())); + when(mockedAttachmentManager.getAttachment(unknownAttachmentId1, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId1.getRawValue())); + when(mockedAttachmentManager.getAttachment(unknownAttachmentId2, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId2.getRawValue())); assertThatThrownBy(() -> sut.assertAttachmentsExist( new CreationMessageEntry( --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org