org.apache.james.mailbox.model.Attachment.getBytes()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(100)

本文整理了Java中org.apache.james.mailbox.model.Attachment.getBytes()方法的一些代码示例,展示了Attachment.getBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Attachment.getBytes()方法的具体详情如下:
包路径:org.apache.james.mailbox.model.Attachment
类名称:Attachment
方法名:getBytes

Attachment.getBytes介绍

[英]Be careful the returned array is not a copy of the attachment byte array. Mutating it will mutate the attachment!
[中]请注意,返回的数组不是附件字节数组的副本。变异它会变异附件!

代码示例

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

public CompletableFuture<Void> storeAttachmentAsync(Attachment attachment, MessageId ownerMessageId) {
  return blobStore.save(attachment.getBytes())
    .thenApply(blobId -> CassandraAttachmentDAOV2.from(attachment, blobId))
    .thenCompose(daoAttachment -> storeAttachmentWithIndex(daoAttachment, ownerMessageId));
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Override
public void storeAttachmentForOwner(Attachment attachment, Username owner) throws MailboxException {
  ownerDAO.addOwner(attachment.getAttachmentId(), owner)
    .thenCompose(any -> blobStore.save(attachment.getBytes()))
    .thenApply(blobId -> CassandraAttachmentDAOV2.from(attachment, blobId))
    .thenCompose(attachmentDAOV2::storeAttachment)
    .join();
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

private Result migrateAttachment(Attachment attachment) {
    try {
      blobStore.save(attachment.getBytes())
        .thenApply(blobId -> CassandraAttachmentDAOV2.from(attachment, blobId))
        .thenCompose(attachmentDAOV2::storeAttachment)
        .thenCompose(any -> attachmentDAOV1.deleteAttachment(attachment.getAttachmentId()))
        .join();
      return Result.COMPLETED;
    } catch (Exception e) {
      LOGGER.error("Error while performing attachmentDAO V2 migration", e);
      return Result.PARTIAL;
    }
  }
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Test
void runShouldReturnPartialWhenSavingAttachmentV2Fail() {
  CassandraAttachmentDAO attachmentDAO = mock(CassandraAttachmentDAO.class);
  CassandraAttachmentDAOV2 attachmentDAOV2 = mock(CassandraAttachmentDAOV2.class);
  CassandraBlobsDAO blobsDAO = mock(CassandraBlobsDAO.class);
  migration = new AttachmentV2Migration(attachmentDAO, attachmentDAOV2, blobsDAO);
  when(attachmentDAO.retrieveAll()).thenReturn(Stream.of(
    attachment1,
    attachment2));
  when(blobsDAO.save(attachment1.getBytes()))
    .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment1.getBytes())));
  when(blobsDAO.save(attachment2.getBytes()))
    .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment2.getBytes())));
  when(attachmentDAOV2.storeAttachment(any())).thenThrow(new RuntimeException());
  assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL);
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Test
void runShouldReturnPartialWhenAtLeastOneAttachmentMigrationFails() {
  CassandraAttachmentDAO attachmentDAO = mock(CassandraAttachmentDAO.class);
  CassandraAttachmentDAOV2 attachmentDAOV2 = mock(CassandraAttachmentDAOV2.class);
  CassandraBlobsDAO blobsDAO = mock(CassandraBlobsDAO.class);
  migration = new AttachmentV2Migration(attachmentDAO, attachmentDAOV2, blobsDAO);
  when(attachmentDAO.retrieveAll()).thenReturn(Stream.of(
    attachment1,
    attachment2));
  when(blobsDAO.save(attachment1.getBytes()))
    .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment1.getBytes())));
  when(blobsDAO.save(attachment2.getBytes()))
    .thenThrow(new RuntimeException());
  when(attachmentDAOV2.storeAttachment(any())).thenReturn(CompletableFuture.completedFuture(null));
  when(attachmentDAO.deleteAttachment(any())).thenReturn(CompletableFuture.completedFuture(null));
  assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL);
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Test
void runShouldReturnPartialWhenDeleteV1AttachmentFail() {
  CassandraAttachmentDAO attachmentDAO = mock(CassandraAttachmentDAO.class);
  CassandraAttachmentDAOV2 attachmentDAOV2 = mock(CassandraAttachmentDAOV2.class);
  CassandraBlobsDAO blobsDAO = mock(CassandraBlobsDAO.class);
  migration = new AttachmentV2Migration(attachmentDAO, attachmentDAOV2, blobsDAO);
  when(attachmentDAO.retrieveAll()).thenReturn(Stream.of(
    attachment1,
    attachment2));
  when(blobsDAO.save(attachment1.getBytes()))
    .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment1.getBytes())));
  when(blobsDAO.save(attachment2.getBytes()))
    .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment2.getBytes())));
  when(attachmentDAOV2.storeAttachment(any())).thenReturn(CompletableFuture.completedFuture(null));
  when(attachmentDAO.deleteAttachment(any())).thenThrow(new RuntimeException());
  assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL);
}

代码示例来源:origin: apache/james-project

@Test
public void getByteShouldReturnByteArrayRepresentingTheAttachment() {
  String input = "mystream";
  Attachment attachment = Attachment.builder()
    .bytes(input.getBytes(CHARSET))
    .type("content")
    .build();
  byte[] bytes = attachment.getBytes();
  assertThat(new String(bytes, CHARSET)).isEqualTo(input);
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Test
void migrationShouldMoveAttachmentsToV2() throws Exception {
  attachmentDAO.storeAttachment(attachment1).join();
  attachmentDAO.storeAttachment(attachment2).join();
  migration.run();
  assertThat(attachmentDAOV2.getAttachment(ATTACHMENT_ID).join())
    .contains(CassandraAttachmentDAOV2.from(attachment1, BLOB_ID_FACTORY.forPayload(attachment1.getBytes())));
  assertThat(attachmentDAOV2.getAttachment(ATTACHMENT_ID_2).join())
    .contains(CassandraAttachmentDAOV2.from(attachment2, BLOB_ID_FACTORY.forPayload(attachment2.getBytes())));
  assertThat(blobsDAO.readBytes(BLOB_ID_FACTORY.forPayload(attachment1.getBytes())).join())
    .isEqualTo(attachment1.getBytes());
  assertThat(blobsDAO.readBytes(BLOB_ID_FACTORY.forPayload(attachment2.getBytes())).join())
    .isEqualTo(attachment2.getBytes());
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

public CompletableFuture<Void> storeAttachment(Attachment attachment) throws IOException {
  return cassandraAsyncExecutor.executeVoid(
    insertStatement.bind()
      .setString(ID, attachment.getAttachmentId().getId())
      .setLong(SIZE, attachment.getSize())
      .setString(TYPE, attachment.getType())
      .setBytes(PAYLOAD, ByteBuffer.wrap(attachment.getBytes())));
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Test
void getAttachmentsShouldReturnV2WhenV2AndV1() throws Exception {
  Attachment attachment = Attachment.builder()
    .attachmentId(ATTACHMENT_ID_1)
    .type("application/json")
    .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8))
    .build();
  Attachment otherAttachment = Attachment.builder()
    .attachmentId(ATTACHMENT_ID_1)
    .type("application/json")
    .bytes("{\"property\":`\"different\"}".getBytes(StandardCharsets.UTF_8))
    .build();
  BlobId blobId = blobsDAO.save(attachment.getBytes()).join();
  attachmentDAOV2.storeAttachment(CassandraAttachmentDAOV2.from(attachment, blobId)).join();
  attachmentDAO.storeAttachment(otherAttachment).join();
  assertThat(attachmentMapper.getAttachments(ImmutableList.of(ATTACHMENT_ID_1)))
    .containsExactly(attachment);
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Test
  void getAttachmentsShouldCombineElementsFromV1AndV2() throws Exception {
    Attachment attachment = Attachment.builder()
      .attachmentId(ATTACHMENT_ID_1)
      .type("application/json")
      .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8))
      .build();
    Attachment otherAttachment = Attachment.builder()
      .attachmentId(ATTACHMENT_ID_2)
      .type("application/json")
      .bytes("{\"property\":`\"different\"}".getBytes(StandardCharsets.UTF_8))
      .build();

    BlobId blobId = blobsDAO.save(attachment.getBytes()).join();
    attachmentDAOV2.storeAttachment(CassandraAttachmentDAOV2.from(attachment, blobId)).join();
    attachmentDAO.storeAttachment(otherAttachment).join();

    assertThat(attachmentMapper.getAttachments(ImmutableList.of(ATTACHMENT_ID_1, ATTACHMENT_ID_2)))
      .containsExactly(attachment, otherAttachment);
  }
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

@Test
void getAttachmentShouldReturnV2WhenPresentInV1AndV2() throws Exception {
  Attachment attachment = Attachment.builder()
    .attachmentId(ATTACHMENT_ID_1)
    .type("application/json")
    .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8))
    .build();
  Attachment otherAttachment = Attachment.builder()
    .attachmentId(ATTACHMENT_ID_1)
    .type("application/json")
    .bytes("{\"property\":`\"different\"}".getBytes(StandardCharsets.UTF_8))
    .build();
  BlobId blobId = blobsDAO.save(attachment.getBytes()).join();
  attachmentDAOV2.storeAttachment(CassandraAttachmentDAOV2.from(attachment, blobId)).join();
  attachmentDAO.storeAttachment(otherAttachment).join();
  assertThat(attachmentMapper.getAttachment(ATTACHMENT_ID_1))
    .isEqualTo(attachment);
}

相关文章