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 boolean isInlinedWithoutCid(Entity part) { return Objects.equals(part.getDispositionType(), MimeMessage.INLINE) && part.getHeader().getField(CONTENT_ID) == null; }
/** * Disposes of the BodyParts of this Multipart. Note that the dispose call * does not get forwarded to the parent entity of this Multipart. * * @see org.apache.james.mime4j.dom.Disposable#dispose() */ public void dispose() { for (Entity bodyPart : bodyParts) { bodyPart.dispose(); } }
private boolean isMultipart(Entity entity) { return entity.isMultipart() && entity.getBody() instanceof Multipart; }
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(attachmentPart.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID);
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); assertThat(attachmentPart.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID); assertThat(attachmentPart.getContentTransferEncoding()).isEqualTo("base64");
/** 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; }
node.add(createNode(entity.getHeader())); Body body = entity.getBody(); type += " (" + entity.getMimeType() + ")"; node.add(new DefaultMutableTreeNode(new ObjectWrapper(type, body)));
/** * 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; }
private boolean isDispositionNotification(Entity entity) { return entity .getMimeType() .startsWith(MESSAGE_DISPOSITION_NOTIFICATION); }
/** * 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(); }
private ContentTypeField getContentType(Multipart multipart) { Entity parent = multipart.getParent(); if (parent == null) throw new IllegalArgumentException( "Missing parent entity in multipart"); Header header = parent.getHeader(); if (header == null) throw new IllegalArgumentException( "Missing header in parent entity"); ContentTypeField contentType = (ContentTypeField) header .getField(FieldName.CONTENT_TYPE); if (contentType == null) throw new IllegalArgumentException( "Content-Type field not specified"); return contentType; }
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 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 boolean isNotAttachment(Entity part) { return part.getDispositionType() == null; }
/** 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 static boolean isMultipartAlternative(Entity entity) { return entity.getMimeType().equalsIgnoreCase(MULTIPART_ALTERNATIVE); }
private <T extends ParsedField> Optional<T> readHeader(Entity entity, String headerName, Class<T> clazz) { return castField(entity.getHeader().getField(headerName), clazz); }
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()); }