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 ad...@apache.org on 2017/09/13 10:57:35 UTC

[09/24] james-project git commit: JAMES-2143 Introduce StoreBlobManager

JAMES-2143 Introduce StoreBlobManager


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

Branch: refs/heads/master
Commit: a688e343ae9ec54d3fc442ec9a991dd06f7a7238
Parents: 6487016
Author: benwa <bt...@linagora.com>
Authored: Mon Sep 11 16:12:46 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Wed Sep 13 10:19:53 2017 +0200

----------------------------------------------------------------------
 .../james/mailbox/store/StoreBlobManager.java   | 109 +++++++++
 .../mailbox/store/StoreBlobManagerTest.java     | 224 +++++++++++++++++++
 2 files changed, 333 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/a688e343/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreBlobManager.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreBlobManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreBlobManager.java
new file mode 100644
index 0000000..e1474e3
--- /dev/null
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreBlobManager.java
@@ -0,0 +1,109 @@
+/****************************************************************
+ * 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;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.mailbox.AttachmentManager;
+import org.apache.james.mailbox.BlobManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageIdManager;
+import org.apache.james.mailbox.exception.AttachmentNotFoundException;
+import org.apache.james.mailbox.exception.BlobNotFoundException;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.Attachment;
+import org.apache.james.mailbox.model.AttachmentId;
+import org.apache.james.mailbox.model.Blob;
+import org.apache.james.mailbox.model.BlobId;
+import org.apache.james.mailbox.model.FetchGroupImpl;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.model.MessageResult;
+
+import com.google.common.base.Throwables;
+import com.google.common.collect.ImmutableList;
+
+public class StoreBlobManager implements BlobManager {
+    public static final String MESSAGE_RFC822_CONTENT_TYPE = "message/rfc822";
+    private final AttachmentManager attachmentManager;
+    private final MessageIdManager messageIdManager;
+    private final MessageId.Factory messageIdFactory;
+
+    public StoreBlobManager(AttachmentManager attachmentManager, MessageIdManager messageIdManager, MessageId.Factory messageIdFactory) {
+        this.attachmentManager = attachmentManager;
+        this.messageIdManager = messageIdManager;
+        this.messageIdFactory = messageIdFactory;
+    }
+
+    @Override
+    public Blob retrieve(BlobId blobId, MailboxSession mailboxSession) throws MailboxException, BlobNotFoundException {
+        return getBlobFromAttachment(blobId, mailboxSession)
+            .orElseGet(() -> getBlobFromMessage(blobId, mailboxSession)
+                .orElseThrow(() -> new BlobNotFoundException(blobId)));
+    }
+
+    private Optional<Blob> getBlobFromAttachment(BlobId blobId, MailboxSession mailboxSession) throws MailboxException {
+        try {
+            Attachment attachment = attachmentManager.getAttachment(
+                AttachmentId.from(blobId.asString()),
+                mailboxSession);
+            return Optional.of(attachment.toBlob());
+        } catch (AttachmentNotFoundException e) {
+            return Optional.empty();
+        }
+    }
+
+    private Optional<Blob> getBlobFromMessage(BlobId blobId, MailboxSession mailboxSession) {
+        return retrieveMessageId(blobId)
+            .flatMap(messageId -> fromMessageId(blobId, mailboxSession, messageId));
+    }
+
+    private Optional<MessageId> retrieveMessageId(BlobId blobId) {
+        try {
+            return Optional.of(messageIdFactory.fromString(blobId.asString()));
+        } catch (Exception e) {
+            return Optional.empty();
+        }
+    }
+
+    private Optional<Blob> fromMessageId(BlobId blobId, MailboxSession mailboxSession, MessageId messageId)  {
+        try {
+            return messageIdManager.getMessages(ImmutableList.of(messageId), FetchGroupImpl.FULL_CONTENT, mailboxSession)
+                .stream()
+                .findFirst()
+                .map(messageResult -> toBlob(blobId, messageResult));
+        } catch (MailboxException e) {
+            throw Throwables.propagate(e);
+        }
+    }
+
+    private Blob toBlob(BlobId blobId, MessageResult messageResult) {
+        try {
+            return Blob.builder()
+                .id(blobId)
+                .contentType(MESSAGE_RFC822_CONTENT_TYPE)
+                .payload(IOUtils.toByteArray(messageResult.getFullContent().getInputStream()))
+                .build();
+        } catch (IOException | MailboxException e) {
+            throw Throwables.propagate(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/a688e343/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreBlobManagerTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreBlobManagerTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreBlobManagerTest.java
new file mode 100644
index 0000000..366bc76
--- /dev/null
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreBlobManagerTest.java
@@ -0,0 +1,224 @@
+/****************************************************************
+ * 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;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.james.mailbox.AttachmentManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageIdManager;
+import org.apache.james.mailbox.exception.AttachmentNotFoundException;
+import org.apache.james.mailbox.exception.BlobNotFoundException;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.Attachment;
+import org.apache.james.mailbox.model.AttachmentId;
+import org.apache.james.mailbox.model.Blob;
+import org.apache.james.mailbox.model.BlobId;
+import org.apache.james.mailbox.model.Content;
+import org.apache.james.mailbox.model.FetchGroupImpl;
+import org.apache.james.mailbox.model.MessageResult;
+import org.apache.james.mailbox.model.TestMessageId;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class StoreBlobManagerTest {
+    public static final String ID = "abc";
+    public static final AttachmentId ATTACHMENT_ID = AttachmentId.from(ID);
+    public static final String CONTENT_TYPE = "text/plain";
+    public static final byte[] BYTES = "abc".getBytes(StandardCharsets.UTF_8);
+    public static final TestMessageId MESSAGE_ID = TestMessageId.of(125);
+    public static final BlobId BLOB_ID_ATTACHMENT = BlobId.fromString(ID);
+    public static final BlobId BLOB_ID_MESSAGE = BlobId.fromString(MESSAGE_ID.serialize());
+    private StoreBlobManager blobManager;
+
+    private AttachmentManager attachmentManager;
+    private MessageIdManager messageIdManager;
+    private MailboxSession session;
+
+    @Before
+    public void setUp() {
+        attachmentManager = mock(AttachmentManager.class);
+        messageIdManager = mock(MessageIdManager.class);
+        session = mock(MailboxSession.class);
+
+        blobManager = new StoreBlobManager(attachmentManager, messageIdManager, new TestMessageId.Factory());
+    }
+
+    @Test
+    public void retrieveShouldReturnBlobWhenAttachment() throws Exception {
+        when(attachmentManager.getAttachment(ATTACHMENT_ID, session))
+            .thenReturn(Attachment.builder()
+                .attachmentId(ATTACHMENT_ID)
+                .bytes(BYTES)
+                .type(CONTENT_TYPE)
+                .build());
+
+        assertThat(blobManager.retrieve(BLOB_ID_ATTACHMENT, session))
+            .isEqualTo(Blob.builder()
+                .id(BLOB_ID_ATTACHMENT)
+                .contentType(CONTENT_TYPE)
+                .payload(BYTES)
+                .build());
+    }
+
+    @Test
+    public void retrieveShouldThrowWhenNotFound() throws Exception {
+        when(attachmentManager.getAttachment(ATTACHMENT_ID, session))
+            .thenThrow(new AttachmentNotFoundException(ID));
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenReturn(ImmutableList.of());
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_ATTACHMENT, session))
+            .isInstanceOf(BlobNotFoundException.class);
+    }
+
+    @Test
+    public void retrieveShouldReturnBlobWhenMessage() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new AttachmentNotFoundException(ID));
+
+        MessageResult messageResult = mock(MessageResult.class);
+        Content content = mock(Content.class);
+        when(content.getInputStream()).thenReturn(new ByteArrayInputStream(BYTES));
+        when(messageResult.getFullContent()).thenReturn(content);
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenReturn(ImmutableList.of(messageResult));
+
+        assertThat(blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isEqualTo(Blob.builder()
+                .id(BLOB_ID_MESSAGE)
+                .contentType(StoreBlobManager.MESSAGE_RFC822_CONTENT_TYPE)
+                .payload(BYTES)
+                .build());
+    }
+
+    @Test
+    public void retrieveShouldThrowOnMailboxExceptionWhenRetrievingAttachment() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new MailboxException());
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(MailboxException.class);
+    }
+
+    @Test
+    public void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingAttachment() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new RuntimeException());
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(RuntimeException.class);
+    }
+
+    @Test
+    public void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingMessage() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new AttachmentNotFoundException(ID));
+
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenThrow(new RuntimeException());
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(RuntimeException.class);
+    }
+
+    @Test
+    public void retrieveShouldThrowOnMailboxExceptionWhenRetrievingMessage() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new AttachmentNotFoundException(ID));
+
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenThrow(new MailboxException());
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(RuntimeException.class);
+    }
+
+    @Test
+    public void retrieveShouldThrowOnMailboxExceptionWhenRetrievingMessageContent() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new AttachmentNotFoundException(ID));
+
+        MessageResult messageResult = mock(MessageResult.class);
+        when(messageResult.getFullContent()).thenThrow(new MailboxException());
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenReturn(ImmutableList.of(messageResult));
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(RuntimeException.class);
+    }
+
+    @Test
+    public void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingMessageContent() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new AttachmentNotFoundException(ID));
+
+        MessageResult messageResult = mock(MessageResult.class);
+        when(messageResult.getFullContent()).thenThrow(new RuntimeException());
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenReturn(ImmutableList.of(messageResult));
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(RuntimeException.class);
+    }
+
+    @Test
+    public void retrieveShouldThrowOnIOExceptionWhenRetrievingMessageContentInputStream() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new AttachmentNotFoundException(ID));
+
+        MessageResult messageResult = mock(MessageResult.class);
+        Content content = mock(Content.class);
+        when(content.getInputStream()).thenThrow(new IOException());
+        when(messageResult.getFullContent()).thenReturn(content);
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenReturn(ImmutableList.of(messageResult));
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(RuntimeException.class);
+    }
+
+    @Test
+    public void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingMessageContentInputStream() throws Exception {
+        when(attachmentManager.getAttachment(any(), any()))
+            .thenThrow(new AttachmentNotFoundException(ID));
+
+        MessageResult messageResult = mock(MessageResult.class);
+        Content content = mock(Content.class);
+        when(content.getInputStream()).thenThrow(new RuntimeException());
+        when(messageResult.getFullContent()).thenReturn(content);
+        when(messageIdManager.getMessages(ImmutableList.of(MESSAGE_ID), FetchGroupImpl.FULL_CONTENT, session))
+            .thenReturn(ImmutableList.of(messageResult));
+
+        assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session))
+            .isInstanceOf(RuntimeException.class);
+    }
+
+}


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