private boolean isMultipart(Entity entity) { return entity.isMultipart() && entity.getBody() instanceof Multipart; }
private MessageContent parseFirstFoundMultipart(Multipart multipart) throws IOException { ThrowingFunction<Entity, MessageContent> parseMultipart = firstPart -> parseMultipart(firstPart, (Multipart)firstPart.getBody()); return multipart.getBodyParts() .stream() .filter(part -> part.getBody() instanceof Multipart) .findFirst() .map(Throwing.function(parseMultipart).sneakyThrow()) .orElse(MessageContent.empty()); }
private MessageContent parseFirstFoundMultipart(Multipart multipart) throws IOException { ThrowingFunction<Entity, MessageContent> parseMultipart = firstPart -> parseMultipart(firstPart, (Multipart)firstPart.getBody()); return multipart.getBodyParts() .stream() .filter(part -> part.getBody() instanceof Multipart) .findFirst() .map(Throwing.function(parseMultipart).sneakyThrow()) .orElse(MessageContent.empty()); }
private Stream<MessageContent> extractContentIfReadable(Entity entity) throws IOException { if (TEXT_HTML.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofHtmlOnly(asString((TextBody)entity.getBody()))); } if (TEXT_PLAIN.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofTextOnly(asString((TextBody)entity.getBody()))); } if (entity.isMultipart() && entity.getBody() instanceof Multipart) { MessageContent innerMultipartContent = parseMultipart(entity, (Multipart)entity.getBody()); if (!innerMultipartContent.isEmpty()) { return Stream.of(innerMultipartContent); } } return Stream.empty(); }
private MessageContent parseFirstFoundMultipart(Multipart multipart) throws IOException { ThrowingFunction<Entity, MessageContent> parseMultipart = firstPart -> parseMultipart(firstPart, (Multipart)firstPart.getBody()); return multipart.getBodyParts() .stream() .filter(part -> part.getBody() instanceof Multipart) .findFirst() .map(Throwing.function(parseMultipart).sneakyThrow()) .orElse(MessageContent.empty()); }
private Stream<MessageContent> extractContentIfReadable(Entity entity) throws IOException { if (TEXT_HTML.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofHtmlOnly(asString((TextBody)entity.getBody()))); } if (TEXT_PLAIN.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofTextOnly(asString((TextBody)entity.getBody()))); } if (entity.isMultipart() && entity.getBody() instanceof Multipart) { MessageContent innerMultipartContent = parseMultipart(entity, (Multipart)entity.getBody()); if (!innerMultipartContent.isEmpty()) { return Stream.of(innerMultipartContent); } } return Stream.empty(); }
private Stream<MessageContent> extractContentIfReadable(Entity entity) throws IOException { if (TEXT_HTML.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofHtmlOnly(asString((TextBody)entity.getBody()))); } if (TEXT_PLAIN.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofTextOnly(asString((TextBody)entity.getBody()))); } if (entity.isMultipart() && entity.getBody() instanceof Multipart) { MessageContent innerMultipartContent = parseMultipart(entity, (Multipart)entity.getBody()); if (!innerMultipartContent.isEmpty()) { return Stream.of(innerMultipartContent); } } return Stream.empty(); }
private Optional<MDNReport> parseReport(Entity report) { LOGGER.debug("Parsing report"); try { return new MDNReportParser().parse(((SingleBody)report.getBody()).getInputStream(), report.getCharset()); } catch (IOException e) { LOGGER.error("unable to parse MESSAGE_DISPOSITION_NOTIFICATION part", e); return Optional.empty(); } }
private Stream<MessageAttachment> listAttachments(Entity entity, Context context) { if (isMultipart(entity)) { return listAttachments((Multipart) entity.getBody(), Context.fromEntity(entity)); } if (isAttachment(entity, context)) { try { return Stream.of(retrieveAttachment(entity)); } catch (IllegalStateException e) { LOGGER.warn("The attachment is not well-formed", e); } catch (IOException e) { LOGGER.warn("There is an error when retrieving attachment", e); } } return Stream.empty(); }
/** * Write the specified <code>Entity</code> to the specified * <code>OutputStream</code>. * * @param entity * the <code>Entity</code> to write. * @param out * the OutputStream to write to. * @throws IOException * if an I/O error occurs. */ public void writeEntity(Entity entity, OutputStream out) throws IOException { final Header header = entity.getHeader(); if (header == null) throw new IllegalArgumentException("Missing header"); writeHeader(header, out); final Body body = entity.getBody(); if (body == null) throw new IllegalArgumentException("Missing body"); boolean binaryBody = body instanceof BinaryBody; OutputStream encOut = encodeStream(out, entity .getContentTransferEncoding(), binaryBody); writeBody(body, encOut); // close if wrapped (base64 or quoted-printable) if (encOut != out) encOut.close(); }
Body body = entity.getBody();
/** * Creates a new <code>BodyPart</code> from the specified * <code>Entity</code>. The <code>BodyPart</code> instance is initialized * with copies of header and body of the specified <code>Entity</code>. * The parent entity of the new body part is <code>null</code>. * * @param other * body part to copy. * @throws UnsupportedOperationException * if <code>other</code> contains a {@link SingleBody} that * does not support the {@link SingleBody#copy() copy()} * operation. * @throws IllegalArgumentException * if <code>other</code> contains a <code>Body</code> that * is neither a {@link Message}, {@link Multipart} or * {@link SingleBody}. */ public BodyPart copy(Entity other) { BodyPart copy = new BodyPart(); if (other.getHeader() != null) { copy.setHeader(copy(other.getHeader())); } if (other.getBody() != null) { copy.setBody(copy(other.getBody())); } return copy; }
/** Process a multipart body part */ private boolean processMultipart(JCas jCas, Multipart mp, String sourceUri) throws IOException { boolean doneBody = false; for (Entity e : mp.getBodyParts()) { if (e.getFilename() != null) { // Part has a filename, and is therefore an attachment String extension = FilenameUtils.getExtension(e.getFilename()).toLowerCase(); if (ignoreExtensionsList.contains(extension)) { getMonitor().info("Skipping attachment {}", e.getFilename()); continue; } attachments.put(sourceUri + "/" + e.getFilename(), e.getBody()); } else if (!doneBody) { // Part has no filename, and we've not already processed a part to use as a body processBody(jCas, e.getBody(), sourceUri); // Add metadata for (Field f : e.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } doneBody = true; } } return doneBody; }
/** Process a multipart body part */ private boolean processMultipart(JCas jCas, Multipart mp, String sourceUri) throws IOException { boolean doneBody = false; for (Entity e : mp.getBodyParts()) { if (e.getFilename() != null) { // Part has a filename, and is therefore an attachment String extension = FilenameUtils.getExtension(e.getFilename()).toLowerCase(); if (ignoreExtensionsList.contains(extension)) { getMonitor().info("Skipping attachment {}", e.getFilename()); continue; } attachments.put(sourceUri + "/" + e.getFilename(), e.getBody()); } else if (!doneBody) { // Part has no filename, and we've not already processed a part to use as a body processBody(jCas, e.getBody(), sourceUri); // Add metadata for (Field f : e.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } doneBody = true; } } return doneBody; }
private MessageAttachment retrieveAttachment(Entity entity) throws IOException { Optional<ContentTypeField> contentTypeField = getContentTypeField(entity); Optional<ContentDispositionField> contentDispositionField = getContentDispositionField(entity); Optional<String> contentType = contentType(contentTypeField); Optional<String> name = name(contentTypeField, contentDispositionField); Optional<Cid> cid = cid(readHeader(entity, CONTENT_ID, ContentIdField.class)); boolean isInline = isInline(readHeader(entity, CONTENT_DISPOSITION, ContentDispositionField.class)) && cid.isPresent(); return MessageAttachment.builder() .attachment(Attachment.builder() .bytes(getBytes(entity.getBody())) .type(contentType.orElse(DEFAULT_CONTENT_TYPE)) .build()) .name(name.orElse(null)) .cid(cid.orElse(null)) .isInline(isInline) .build(); }
final Body otherBody = otherPart.getBody(); if (otherBody != null) { Body body = null;
assertThat(typedResult.getBodyParts()).hasSize(2); Entity mainBodyPart = typedResult.getBodyParts().get(0); assertThat(mainBodyPart.getBody()).isInstanceOf(Multipart.class); assertThat(mainBodyPart.isMultipart()).isTrue(); assertThat(mainBodyPart.getMimeType()).isEqualTo("multipart/alternative"); assertThat(((Multipart)mainBodyPart.getBody()).getBodyParts()).hasSize(2); Entity textPart = ((Multipart)mainBodyPart.getBody()).getBodyParts().get(0); Entity htmlPart = ((Multipart)mainBodyPart.getBody()).getBodyParts().get(1); assertThat(textPart.getBody()).isEqualToComparingOnlyGivenFields(expectedTextBody, "content"); assertThat(htmlPart.getBody()).isEqualToComparingOnlyGivenFields(expectedHtmlBody, "content"); assertThat(attachmentPart.getBody()).isEqualToComparingOnlyGivenFields(expectedAttachmentBody, "content"); assertThat(attachmentPart.getDispositionType()).isEqualTo("inline"); assertThat(attachmentPart.getMimeType()).isEqualTo(expectedMimeType);
assertThat(typedResult.getBodyParts()).hasSize(2); Entity attachmentPart = typedResult.getBodyParts().get(1); assertThat(attachmentPart.getBody()).isEqualToComparingOnlyGivenFields(expectedBody, "content"); assertThat(attachmentPart.getDispositionType()).isEqualTo("inline"); assertThat(attachmentPart.getMimeType()).isEqualTo(expectedMimeType);