private boolean attachmentExists(MailboxSession mailboxSession, String blobId) throws MailboxException { try { blobManager.retrieve(BlobId.fromString(blobId), mailboxSession); return true; } catch (BlobNotFoundException e) { return false; } }
@Override public BlobId toBlobId(MessageId messageId) { return BlobId.fromString(messageId.serialize()); }
@VisibleForTesting void download(MailboxSession mailboxSession, DownloadPath downloadPath, HttpServletResponse resp) { String blobId = downloadPath.getBlobId(); try { Blob blob = blobManager.retrieve(BlobId.fromString(blobId), mailboxSession); addContentDispositionHeader(downloadPath.getName(), resp); resp.setHeader("Content-Length", String.valueOf(blob.getSize())); resp.setStatus(SC_OK); IOUtils.copy(blob.getStream(), resp.getOutputStream()); } catch (BlobNotFoundException e) { LOGGER.info("Attachment '{}' not found", blobId, e); resp.setStatus(SC_NOT_FOUND); } catch (MailboxException | IOException e) { LOGGER.error("Error while downloading", e); resp.setStatus(SC_INTERNAL_SERVER_ERROR); } }
@Test public void fromStringShouldThrowOnEmpty() { assertThatThrownBy(() -> BlobId.fromString("")) .isInstanceOf(IllegalArgumentException.class); }
@Test public void fromStringShouldThrowOnNull() { assertThatThrownBy(() -> BlobId.fromString(null)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void asStringShouldReturnUnderlyingId() { assertThat(BlobId.fromString("abc").asString()) .isEqualTo("abc"); }
@Test public void fromBlobIdShouldWork() { String expectedId = "f07e5a815613c5abeddc4b682247a4c42d8a95df"; AttachmentId attachmentId = AttachmentId.from(BlobId.fromString(expectedId)); assertThat(attachmentId.getId()).isEqualTo(expectedId); }
@Before public void setUp() { HtmlTextExtractor htmlTextExtractor = new JsoupHtmlTextExtractor(); MessagePreviewGenerator messagePreview = new MessagePreviewGenerator(); MessageContentExtractor messageContentExtractor = new MessageContentExtractor(); BlobManager blobManager = mock(BlobManager.class); when(blobManager.toBlobId(any(MessageId.class))).thenReturn(org.apache.james.mailbox.model.BlobId.fromString("blobId")); messageFactory = new MessageFactory(blobManager, messagePreview, messageContentExtractor, htmlTextExtractor); }
when(messagePreview.compute(any())).thenReturn("text preview"); BlobManager blobManager = mock(BlobManager.class); when(blobManager.toBlobId(any(MessageId.class))).thenReturn(org.apache.james.mailbox.model.BlobId.fromString("fake")); messageFactory = new MessageFactory(blobManager, messagePreview, messageContentExtractor, htmlTextExtractor); mockedMailSpool = mock(MailSpool.class);
when(blobManager.toBlobId(any(MessageId.class))).thenReturn(BlobId.fromString("fake")); MessageFactory messageFactory = new MessageFactory(blobManager, messagePreview, messageContentExtractor, htmlTextExtractor); jmapMessage = messageFactory.fromMetaDataWithContent(message);
MessageContentExtractor messageContentExtractor = new MessageContentExtractor(); BlobManager blobManager = mock(BlobManager.class); when(blobManager.toBlobId(any(MessageId.class))).thenReturn(BlobId.fromString("fake")); MessageFactory messageFactory = new MessageFactory(blobManager, messagePreview, messageContentExtractor, htmlTextExtractor); InMemoryIntegrationResources inMemoryIntegrationResources = new InMemoryIntegrationResources();