Search in sources :

Example 1 with Attachment

use of org.apache.poi.hmef.Attachment in project poi by apache.

the class HMEFContentsExtractor method extractAttachments.

/**
     * Extracts all the message attachments to the supplied directory
     */
public void extractAttachments(File dir) throws IOException {
    int count = 0;
    for (Attachment att : message.getAttachments()) {
        count++;
        // Decide what to call it
        String filename = att.getLongFilename();
        if (filename == null || filename.length() == 0) {
            filename = att.getFilename();
        }
        if (filename == null || filename.length() == 0) {
            filename = "attachment" + count;
            if (att.getExtension() != null) {
                filename += att.getExtension();
            }
        }
        // Save it
        File file = new File(dir, filename);
        OutputStream fout = new FileOutputStream(file);
        try {
            fout.write(att.getContents());
        } finally {
            fout.close();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Attachment(org.apache.poi.hmef.Attachment) File(java.io.File)

Example 2 with Attachment

use of org.apache.poi.hmef.Attachment in project tika by apache.

the class TNEFParser method parse.

/**
     * Extracts properties and text from an MS Document input stream
     */
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    // We work by recursing, so get the appropriate bits
    EmbeddedDocumentExtractor embeddedExtractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
    // Ask POI to process the file for us
    HMEFMessage msg = new HMEFMessage(stream);
    // Set the message subject if known
    String subject = msg.getSubject();
    if (subject != null && subject.length() > 0) {
        // TODO: Move to title in Tika 2.0
        metadata.set(TikaCoreProperties.TRANSITION_SUBJECT_TO_DC_TITLE, subject);
    }
    // Recurse into the message body RTF
    MAPIAttribute attr = msg.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
    if (attr != null && attr instanceof MAPIRtfAttribute) {
        MAPIRtfAttribute rtf = (MAPIRtfAttribute) attr;
        handleEmbedded("message.rtf", "application/rtf", rtf.getData(), embeddedExtractor, handler);
    }
    // Recurse into each attachment in turn
    for (Attachment attachment : msg.getAttachments()) {
        String name = attachment.getLongFilename();
        if (name == null || name.length() == 0) {
            name = attachment.getFilename();
        }
        if (name == null || name.length() == 0) {
            String ext = attachment.getExtension();
            if (ext != null) {
                name = "unknown" + ext;
            }
        }
        handleEmbedded(name, null, attachment.getContents(), embeddedExtractor, handler);
    }
}
Also used : HMEFMessage(org.apache.poi.hmef.HMEFMessage) MAPIRtfAttribute(org.apache.poi.hmef.attribute.MAPIRtfAttribute) EmbeddedDocumentExtractor(org.apache.tika.extractor.EmbeddedDocumentExtractor) MAPIAttribute(org.apache.poi.hmef.attribute.MAPIAttribute) Attachment(org.apache.poi.hmef.Attachment)

Aggregations

Attachment (org.apache.poi.hmef.Attachment)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HMEFMessage (org.apache.poi.hmef.HMEFMessage)1 MAPIAttribute (org.apache.poi.hmef.attribute.MAPIAttribute)1 MAPIRtfAttribute (org.apache.poi.hmef.attribute.MAPIRtfAttribute)1 EmbeddedDocumentExtractor (org.apache.tika.extractor.EmbeddedDocumentExtractor)1