public Blob build() { Preconditions.checkState(blobId != null, "id can not be empty"); Preconditions.checkState(payload != null, "payload can not be empty"); Preconditions.checkState(contentType != null, "contentType can not be empty"); return new Blob(blobId, payload, contentType); } }
@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); } }
private Optional<Blob> getBlobFromMessage(BlobId blobId, MailboxSession mailboxSession) { return retrieveMessageId(blobId) .flatMap(messageId -> loadMessageAsBlob(messageId, mailboxSession)) .map(Throwing.function( blob -> Blob.builder() .id(blobId) .contentType(MESSAGE_RFC822_CONTENT_TYPE) .payload(IOUtils.toByteArray(blob)) .build())); }
public Blob toBlob() { return Blob.builder() .id(BlobId.fromBytes(bytes)) .payload(bytes) .contentType(type) .build(); }
@Test public void buildShouldThrowOnMissingBlobId() { assertThatThrownBy(() -> Blob.builder() .contentType(CONTENT_TYPE) .payload(PAYLOAD) .build()) .isInstanceOf(IllegalStateException.class); }
@Test public void buildShouldThrowOnMissingPayload() { assertThatThrownBy(() -> Blob.builder() .id(ID) .contentType(CONTENT_TYPE) .build()) .isInstanceOf(IllegalStateException.class); } }
@Test public void buildShouldThrowOnMissingContentType() { assertThatThrownBy(() -> Blob.builder() .id(ID) .payload(PAYLOAD) .build()) .isInstanceOf(IllegalStateException.class); }
@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); } }