private Stream<String> toAttachmentContent(Attachment attachment) { try { return OptionalUtils.toStream( textExtractor .extractContent( attachment.getStream(), attachment.getType()) .getTextualContent()); } catch (Exception e) { LOGGER.error("Error while parsing attachment content", e); return Stream.of(); } }
@Test public void streamShouldBeConsumedMoreThanOneTime() throws Exception { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); attachment.getStream(); InputStream stream = attachment.getStream(); assertThat(stream).isNotNull(); assertThat(IOUtils.toString(stream, CHARSET)).isEqualTo(input); }
@Test public void streamShouldBeConsumedOneTime() throws Exception { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); InputStream stream = attachment.getStream(); assertThat(stream).isNotNull(); assertThat(IOUtils.toString(stream, CHARSET)).isEqualTo(input); }
private BodyPart attachmentBodyPart(MessageAttachment att) throws IOException { BodyPartBuilder builder = BodyPartBuilder.create() .use(bodyFactory) .setBody(new BasicBodyFactory().binaryBody(ByteStreams.toByteArray(att.getAttachment().getStream()))) .setField(contentTypeField(att)) .setField(contentDispositionField(att.isInline())) .setContentTransferEncoding(BASE64); contentId(builder, att); return builder.build(); }
private String uploadTextAttachment(Attachment attachment) throws IOException { return with() .header("Authorization", accessToken.serialize()) .contentType(attachment.getType()) .body(new String(IOUtils.toByteArray(attachment.getStream()), StandardCharsets.UTF_8)) .post("/upload") .then() .extract() .body() .jsonPath() .getString("blobId"); }
private String uploadAttachment(Attachment attachment) throws IOException { return with() .header("Authorization", accessToken.serialize()) .contentType(attachment.getType()) .body(attachment.getStream()) .post("/upload") .then() .extract() .body() .jsonPath() .getString("blobId"); }