Search in sources :

Example 1 with HMEFMessage

use of org.apache.poi.hmef.HMEFMessage in project alfresco-repository by Alfresco.

the class AttachmentsExtractor method createAttachment.

/**
 * Create an attachment given a mime part
 *
 * @param messageFile the file containing the message
 * @param attachmentsFolderRef where to put the attachment
 * @param part the mime part
 * @throws MessagingException
 * @throws IOException
 */
private void createAttachment(NodeRef messageFile, NodeRef attachmentsFolderRef, Part part) throws MessagingException, IOException {
    String fileName = part.getFileName();
    if (fileName == null || fileName.isEmpty()) {
        fileName = "unnamed";
    }
    try {
        fileName = MimeUtility.decodeText(fileName);
    } catch (UnsupportedEncodingException e) {
        if (logger.isWarnEnabled()) {
            logger.warn("Cannot decode file name '" + fileName + "'", e);
        }
    }
    ContentType contentType = new ContentType(part.getContentType());
    if (contentType.getBaseType().equalsIgnoreCase("application/ms-tnef")) {
        // The content is TNEF
        HMEFMessage hmef = new HMEFMessage(part.getInputStream());
        // hmef.getBody();
        List<org.apache.poi.hmef.Attachment> attachments = hmef.getAttachments();
        for (org.apache.poi.hmef.Attachment attachment : attachments) {
            String subName = attachment.getLongFilename();
            NodeRef attachmentNode = fileFolderService.searchSimple(attachmentsFolderRef, subName);
            if (attachmentNode == null) {
                /*
                     * If the node with the given name does not already exist Create the content node to contain the attachment
                     */
                FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, subName, ContentModel.TYPE_CONTENT);
                attachmentNode = createdFile.getNodeRef();
                serviceRegistry.getNodeService().createAssociation(messageFile, attachmentNode, ImapModel.ASSOC_IMAP_ATTACHMENT);
                byte[] bytes = attachment.getContents();
                ContentWriter writer = fileFolderService.getWriter(attachmentNode);
                // TODO ENCODING - attachment.getAttribute(TNEFProperty.);
                String extension = attachment.getExtension();
                String mimetype = mimetypeService.getMimetype(extension);
                if (mimetype != null) {
                    writer.setMimetype(mimetype);
                }
                OutputStream os = writer.getContentOutputStream();
                ByteArrayInputStream is = new ByteArrayInputStream(bytes);
                FileCopyUtils.copy(is, os);
            }
        }
    } else {
        // not TNEF
        NodeRef attachmentFile = fileFolderService.searchSimple(attachmentsFolderRef, fileName);
        // And another one behaviour which will overwrite the content of the existing file. It is performance preferable.
        if (attachmentFile == null) {
            FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, fileName, ContentModel.TYPE_CONTENT);
            nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT);
            attachmentFile = createdFile.getNodeRef();
        } else {
            String newFileName = imapService.generateUniqueFilename(attachmentsFolderRef, fileName);
            FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, newFileName, ContentModel.TYPE_CONTENT);
            nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT);
            attachmentFile = createdFile.getNodeRef();
        }
        nodeService.setProperty(attachmentFile, ContentModel.PROP_DESCRIPTION, nodeService.getProperty(messageFile, ContentModel.PROP_NAME));
        ContentWriter writer = fileFolderService.getWriter(attachmentFile);
        writer.setMimetype(contentType.getBaseType());
        OutputStream os = writer.getContentOutputStream();
        FileCopyUtils.copy(part.getInputStream(), os);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) OutputStream(java.io.OutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HMEFMessage(org.apache.poi.hmef.HMEFMessage) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileInfo(org.alfresco.service.cmr.model.FileInfo) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 2 with HMEFMessage

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

the class HMEFFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    HMEFMessage msg = new HMEFMessage(stream);
    // list all properties
    StringBuilder props = new StringBuilder();
    for (MAPIAttribute att : msg.getMessageMAPIAttributes()) {
        props.append(att.getType()).append(": ").append(MAPIStringAttribute.getAsString(att)).append("\n");
    }
    // there are two test-files that have no body...
    if (!msg.getSubject().equals("Testing TNEF Message") && !msg.getSubject().equals("TNEF test message with attachments")) {
        assertNotNull("Had: " + msg.getBody() + ", " + msg.getSubject() + ", " + msg.getAttachments() + ": " + props, msg.getBody());
    }
    assertNotNull("Had: " + msg.getBody() + ", " + msg.getSubject() + ", " + msg.getAttachments() + ": " + props, msg.getSubject());
}
Also used : HMEFMessage(org.apache.poi.hmef.HMEFMessage) MAPIAttribute(org.apache.poi.hmef.attribute.MAPIAttribute)

Example 3 with HMEFMessage

use of org.apache.poi.hmef.HMEFMessage 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)

Example 4 with HMEFMessage

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

the class TestMAPIAttributes method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    stream = _samples.openResourceAsStream("quick-winmail.dat");
    quick = new HMEFMessage(stream);
}
Also used : HMEFMessage(org.apache.poi.hmef.HMEFMessage)

Example 5 with HMEFMessage

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

the class TestTNEFAttributes method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    quick = new HMEFMessage(_samples.openResourceAsStream("quick-winmail.dat"));
}
Also used : HMEFMessage(org.apache.poi.hmef.HMEFMessage)

Aggregations

HMEFMessage (org.apache.poi.hmef.HMEFMessage)6 OutputStream (java.io.OutputStream)2 Attachment (org.apache.poi.hmef.Attachment)2 MAPIAttribute (org.apache.poi.hmef.attribute.MAPIAttribute)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ContentType (javax.mail.internet.ContentType)1 FileInfo (org.alfresco.service.cmr.model.FileInfo)1 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 FlowFileHandlingException (org.apache.nifi.processor.exception.FlowFileHandlingException)1 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)1