private Optional<Blob> getBlobFromAttachment(BlobId blobId, MailboxSession mailboxSession) throws MailboxException { try { AttachmentId attachmentId = AttachmentId.from(blobId); return Optional.of(attachmentManager.getAttachment(attachmentId, mailboxSession).toBlob()); } catch (AttachmentNotFoundException e) { return Optional.empty(); } }
@Override public Attachment getAttachment(AttachmentId attachmentId) throws AttachmentNotFoundException { Preconditions.checkArgument(attachmentId != null); if (!attachmentsById.containsKey(attachmentId)) { throw new AttachmentNotFoundException(attachmentId.getId()); } return attachmentsById.get(attachmentId); }
public CompletableFuture<Void> storeAttachmentForMessageId(AttachmentId attachmentId, MessageId ownerMessageId) { return cassandraAsyncExecutor.executeVoid( insertStatement.bind() .setUUID(ATTACHMENT_ID_AS_UUID, attachmentId.asUUID()) .setString(ATTACHMENT_ID, attachmentId.getId()) .setString(MESSAGE_ID, ownerMessageId.serialize())); } }
public CompletableFuture<Stream<Username>> retrieveOwners(AttachmentId attachmentId) { return executor.execute( selectStatement.bind() .setUUID(ID, attachmentId.asUUID())) .thenApply(cassandraUtils::convertToStream) .thenApply(this::toOwners); }
private AttachmentId attachmentId() { if (attachmentId != null) { return attachmentId; } return AttachmentId.random(); }
public static AttachmentId from(String id) { Preconditions.checkNotNull(id); Preconditions.checkArgument(!id.isEmpty()); return new AttachmentId(id); }
public CompletableFuture<Optional<DAOAttachment>> getAttachment(AttachmentId attachmentId) { Preconditions.checkArgument(attachmentId != null); return cassandraAsyncExecutor.executeSingleRow( selectStatement.bind() .setUUID(ID_AS_UUID, attachmentId.asUUID())) .thenApply(rowOptional -> rowOptional.map(row -> CassandraAttachmentDAOV2.fromRow(row, blobIdFactory))); }
@Test (expected = IllegalStateException.class) public void buildShouldThrowWhenTypeIsNotProvided() { Attachment.builder() .attachmentId(AttachmentId.random()) .bytes("mystream".getBytes(CHARSET)) .build(); }
public static AttachmentId from(BlobId blobId) { return new AttachmentId(blobId.asString()); }
private AttachmentId getAttachmentId(Attachment attachment) { return AttachmentId.from(attachment.getBlobId().getRawValue()); } }
@Override public Attachment getAttachment(AttachmentId attachmentId) throws AttachmentNotFoundException { Preconditions.checkArgument(attachmentId != null); return getAttachmentInternal(attachmentId) .join() .orElseThrow(() -> new AttachmentNotFoundException(attachmentId.getId())); }
public CompletableFuture<Void> storeAttachment(DAOAttachment attachment) { return cassandraAsyncExecutor.executeVoid( insertStatement.bind() .setUUID(ID_AS_UUID, attachment.getAttachmentId().asUUID()) .setString(ID, attachment.getAttachmentId().getId()) .setLong(SIZE, attachment.getSize()) .setString(TYPE, attachment.getType()) .setString(BLOB_ID, attachment.getBlobId().asString())); }
public CompletableFuture<Collection<MessageId>> getOwnerMessageIds(AttachmentId attachmentId) { Preconditions.checkArgument(attachmentId != null); return cassandraAsyncExecutor.execute( selectStatement.bind() .setUUID(ATTACHMENT_ID_AS_UUID, attachmentId.asUUID())) .thenApply(resultSet -> cassandraUtils.convertToStream(resultSet) .map(this::rowToMessageId) .collect(Guavate.toImmutableSet())); }
@Test (expected = IllegalStateException.class) public void buildShouldThrowWhenBytesIsNotProvided() { Attachment.builder() .attachmentId(AttachmentId.random()) .build(); }
public static AttachmentId random() { return new AttachmentId(RANDOM_STRING_GENERATOR.generate(20)); }
@Test public void fromShouldThrowWhenIdIsNull() { String value = null; assertThatThrownBy(() -> AttachmentId.from(value)).isInstanceOf(NullPointerException.class); }