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 boolean isInlinedWithoutCid(Entity part) { return Objects.equals(part.getDispositionType(), MimeMessage.INLINE) && part.getHeader().getField(CONTENT_ID) == null; }
private boolean isInlinedWithoutCid(Entity part) { return Objects.equals(part.getDispositionType(), MimeMessage.INLINE) && part.getHeader().getField(CONTENT_ID) == null; }
private boolean isInlinedWithoutCid(Entity part) { return Objects.equals(part.getDispositionType(), MimeMessage.INLINE) && part.getHeader().getField(CONTENT_ID) == null; }
private <T extends ParsedField> Optional<T> readHeader(Entity entity, String headerName, Class<T> clazz) { return castField(entity.getHeader().getField(headerName), clazz); }
private Optional<ContentTypeField> getContentTypeField(Entity entity) { return castField(entity.getHeader().getField(CONTENT_TYPE), ContentTypeField.class); }
private Optional<ContentDispositionField> getContentDispositionField(Entity entity) { return castField(entity.getHeader().getField(CONTENT_DISPOSITION), ContentDispositionField.class); }
/** * 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(); }
public String getMimeType() { ContentTypeField childType = getContentTypeField(); Entity parent = getParent(); ContentTypeField parentType = parent != null ? (ContentTypeField) (parent).getHeader().getField(FieldName.CONTENT_TYPE) : null; return calcMimeType(childType, parentType); }
private String getNameParameterValue(Entity attachmentPart) { return ((ContentTypeField) attachmentPart.getHeader().getField("Content-Type")).getParameter("name"); } }
node.add(createNode(entity.getHeader()));
/** * 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; }
/** Process a single body part */ private boolean processBody(JCas jCas, Body body, String sourceUri) throws IOException { if (body instanceof TextBody) { // Process plain text body processTextBody(jCas, (TextBody) body); // Add fields from parent for (Field f : body.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } // Set up document annotation - this is done by the content extractor in other cases DocumentAnnotation doc = UimaSupport.getDocumentAnnotation(jCas); doc.setSourceUri(sourceUri); doc.setTimestamp(System.currentTimeMillis()); } else if (body instanceof BinaryBody) { processBinaryBody(jCas, (BinaryBody) body, sourceUri); } else if (body instanceof Multipart) { // Multipart message, so recurse Multipart mp = (Multipart) body; return processMultipart(jCas, mp, sourceUri); } else { // No body processed return false; } return true; }
/** Process a single body part */ private boolean processBody(JCas jCas, Body body, String sourceUri) throws IOException { if (body instanceof TextBody) { // Process plain text body processTextBody(jCas, (TextBody) body); // Add fields from parent for (Field f : body.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } // Set up document annotation - this is done by the content extractor in other cases DocumentAnnotation doc = UimaSupport.getDocumentAnnotation(jCas); doc.setSourceUri(sourceUri); doc.setTimestamp(System.currentTimeMillis()); } else if (body instanceof BinaryBody) { processBinaryBody(jCas, (BinaryBody) body, sourceUri); } else if (body instanceof Multipart) { // Multipart message, so recurse Multipart mp = (Multipart) body; return processMultipart(jCas, mp, sourceUri); } else { // No body processed return false; } return true; }
for (Entity otherPart: otherParts) { BodyPart bodyPart = new BodyPart(); Header otherHeader = otherPart.getHeader(); if (otherHeader != null) { HeaderImpl header = new HeaderImpl();
assertThat(attachmentPart.getDispositionType()).isEqualTo("inline"); assertThat(attachmentPart.getMimeType()).isEqualTo(expectedMimeType); assertThat(attachmentPart.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID); assertThat(attachmentPart.getContentTransferEncoding()).isEqualTo("base64");
assertThat(attachmentPart.getDispositionType()).isEqualTo("inline"); assertThat(attachmentPart.getMimeType()).isEqualTo(expectedMimeType); assertThat(attachmentPart.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID);