public BlobNotFoundException(BlobId blobId) { super("Could not retrieve " + blobId.asString()); this.blobId = blobId; }
@Override public BlobId toBlobId(MessageId messageId) { return BlobId.fromString(messageId.serialize()); }
public static BlobId fromString(String raw) { Preconditions.checkArgument(!Strings.isNullOrEmpty(raw)); return new BlobId(raw); }
@Test public void asStringShouldReturnUnderlyingId() { assertThat(BlobId.fromString("abc").asString()) .isEqualTo("abc"); }
public Blob toBlob() { return Blob.builder() .id(BlobId.fromBytes(bytes)) .payload(bytes) .contentType(type) .build(); }
private boolean attachmentExists(MailboxSession mailboxSession, String blobId) throws MailboxException { try { blobManager.retrieve(BlobId.fromString(blobId), mailboxSession); return true; } catch (BlobNotFoundException e) { return false; } }
private Optional<MessageId> retrieveMessageId(BlobId blobId) { try { return Optional.of(messageIdFactory.fromString(blobId.asString())); } catch (IllegalArgumentException e) { return Optional.empty(); } }
@Test public void toBlobShouldGenerateTheAttachmentBlob() { byte[] bytes = "mystream".getBytes(CHARSET); String content = "content"; Attachment attachment = Attachment.builder() .bytes(bytes) .type(content) .build(); Blob expected = Blob.builder() .id(BlobId.fromBytes(bytes)) .contentType(content) .payload(bytes) .build(); assertThat(attachment.toBlob()).isEqualTo(expected); } }
public static BlobId fromBytes(byte[] bytes) { Preconditions.checkNotNull(bytes); return new BlobId(Hashing.sha256().hashBytes(bytes).toString()); }
@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); } }
public static AttachmentId from(BlobId blobId) { return new AttachmentId(blobId.asString()); }
@Test public void fromStringShouldThrowOnNull() { assertThatThrownBy(() -> BlobId.fromString(null)) .isInstanceOf(IllegalArgumentException.class); }
public static BlobId of(org.apache.james.mailbox.model.BlobId blobId) { return new BlobId(blobId.asString()); }
@Test public void fromStringShouldThrowOnEmpty() { assertThatThrownBy(() -> BlobId.fromString("")) .isInstanceOf(IllegalArgumentException.class); }
@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);