Search in sources :

Example 11 with MAPIMessage

use of org.apache.poi.hsmf.MAPIMessage in project poi by apache.

the class HSMFFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    MAPIMessage mapi = new MAPIMessage(stream);
    assertNotNull(mapi.getAttachmentFiles());
    assertNotNull(mapi.getDisplayBCC());
    assertNotNull(mapi.getMessageDate());
    AttachmentChunks[] attachments = mapi.getAttachmentFiles();
    for (AttachmentChunks attachment : attachments) {
        DirectoryChunk chunkDirectory = attachment.getAttachmentDirectory();
        if (chunkDirectory != null) {
            MAPIMessage attachmentMSG = chunkDirectory.getAsEmbededMessage();
            assertNotNull(attachmentMSG);
            String body = attachmentMSG.getTextBody();
            assertNotNull(body);
        }
    }
    /* => Writing isn't yet supported...
		// write out the file
		File file = TempFile.createTempFile("StressTest", ".msg");
		writeToFile(mapi, file);
		
		MAPIMessage read = new MAPIMessage(file.getAbsolutePath());
		assertNotNull(read.getAttachmentFiles());
		assertNotNull(read.getDisplayBCC());
		assertNotNull(read.getMessageDate());
		*/
    // writing is not yet supported... handlePOIDocument(mapi);
    mapi.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) DirectoryChunk(org.apache.poi.hsmf.datatypes.DirectoryChunk) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks)

Example 12 with MAPIMessage

use of org.apache.poi.hsmf.MAPIMessage in project poi by apache.

the class OutlookTextExtactor method getText.

/**
    * Outputs something a little like a RFC822 email
    */
public String getText() {
    MAPIMessage msg = (MAPIMessage) document;
    StringBuffer s = new StringBuffer();
    // See if we can get a suitable encoding for any
    //  non unicode text in the file
    msg.guess7BitEncoding();
    // Off we go
    StringsIterator emails;
    try {
        emails = new StringsIterator(msg.getRecipientEmailAddressList());
    } catch (ChunkNotFoundException e) {
        emails = new StringsIterator(new String[0]);
    }
    try {
        s.append("From: " + msg.getDisplayFrom() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    //  people in To + CC + BCC.
    try {
        handleEmails(s, "To", msg.getDisplayTo(), emails);
    } catch (ChunkNotFoundException e) {
    }
    try {
        handleEmails(s, "CC", msg.getDisplayCC(), emails);
    } catch (ChunkNotFoundException e) {
    }
    try {
        handleEmails(s, "BCC", msg.getDisplayBCC(), emails);
    } catch (ChunkNotFoundException e) {
    }
    // Date - try two ways to find it
    try {
        // First try via the proper chunk
        SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ROOT);
        f.setTimeZone(LocaleUtil.getUserTimeZone());
        s.append("Date: " + f.format(msg.getMessageDate().getTime()) + "\n");
    } catch (ChunkNotFoundException e) {
        try {
            // Failing that try via the raw headers 
            String[] headers = msg.getHeaders();
            for (String header : headers) {
                if (startsWithIgnoreCase(header, "date:")) {
                    s.append("Date:" + header.substring(header.indexOf(':') + 1) + "\n");
                    break;
                }
            }
        } catch (ChunkNotFoundException he) {
        // We can't find the date, sorry...
        }
    }
    try {
        s.append("Subject: " + msg.getSubject() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    // To get the attachments, use ExtractorFactory
    for (AttachmentChunks att : msg.getAttachmentFiles()) {
        StringChunk name = att.getAttachLongFileName();
        if (name == null)
            name = att.getAttachFileName();
        String attName = name == null ? null : name.getValue();
        if (att.getAttachMimeTag() != null && att.getAttachMimeTag().getValue() != null) {
            attName = att.getAttachMimeTag().getValue() + " = " + attName;
        }
        s.append("Attachment: " + attName + "\n");
    }
    try {
        s.append("\n" + msg.getTextBody() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    return s.toString();
}
Also used : ChunkNotFoundException(org.apache.poi.hsmf.exceptions.ChunkNotFoundException) MAPIMessage(org.apache.poi.hsmf.MAPIMessage) SimpleDateFormat(java.text.SimpleDateFormat) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks) StringsIterator(org.apache.poi.util.StringUtil.StringsIterator) StringChunk(org.apache.poi.hsmf.datatypes.StringChunk)

Example 13 with MAPIMessage

use of org.apache.poi.hsmf.MAPIMessage in project poi by apache.

the class TestPOIFSChunkParser method testFindsCore.

@Test
public void testFindsCore() throws IOException, ChunkNotFoundException {
    NPOIFSFileSystem simple = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
    // Check a few core things are present
    simple.getRoot().getEntry((new StringChunk(MAPIProperty.SUBJECT.id, Types.ASCII_STRING)).getEntryName());
    simple.getRoot().getEntry((new StringChunk(MAPIProperty.SENDER_NAME.id, Types.ASCII_STRING)).getEntryName());
    // Now load the file
    MAPIMessage msg = new MAPIMessage(simple);
    assertEquals("Kevin Roast", msg.getDisplayTo());
    assertEquals("Kevin Roast", msg.getDisplayFrom());
    assertEquals("Test the content transformer", msg.getSubject());
    // Check date too
    Calendar calExp = LocaleUtil.getLocaleCalendar(2007, 5, 14, 9, 42, 55);
    Calendar calAct = msg.getMessageDate();
    assertEquals(calExp, calAct);
    msg.close();
    simple.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) Calendar(java.util.Calendar) StringChunk(org.apache.poi.hsmf.datatypes.StringChunk) Test(org.junit.Test)

Example 14 with MAPIMessage

use of org.apache.poi.hsmf.MAPIMessage in project poi by apache.

the class TestPOIFSChunkParser method testFindsNameId.

@Test
public void testFindsNameId() throws IOException {
    NPOIFSFileSystem simple = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
    simple.getRoot().getEntry("__nameid_version1.0");
    ChunkGroup[] groups = POIFSChunkParser.parse(simple.getRoot());
    assertEquals(3, groups.length);
    assertTrue(groups[0] instanceof Chunks);
    assertTrue(groups[1] instanceof RecipientChunks);
    assertTrue(groups[2] instanceof NameIdChunks);
    NameIdChunks nameId = (NameIdChunks) groups[2];
    assertEquals(10, nameId.getAll().length);
    // Now via MAPIMessage
    MAPIMessage msg = new MAPIMessage(simple);
    assertNotNull(msg.getNameIdChunks());
    assertEquals(10, msg.getNameIdChunks().getAll().length);
    msg.close();
    simple.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ChunkGroup(org.apache.poi.hsmf.datatypes.ChunkGroup) Chunks(org.apache.poi.hsmf.datatypes.Chunks) RecipientChunks(org.apache.poi.hsmf.datatypes.RecipientChunks) NameIdChunks(org.apache.poi.hsmf.datatypes.NameIdChunks) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks) RecipientChunks(org.apache.poi.hsmf.datatypes.RecipientChunks) NameIdChunks(org.apache.poi.hsmf.datatypes.NameIdChunks) Test(org.junit.Test)

Example 15 with MAPIMessage

use of org.apache.poi.hsmf.MAPIMessage in project poi by apache.

the class TestPOIFSChunkParser method testFindsRecips.

@Test
public void testFindsRecips() throws IOException, ChunkNotFoundException {
    NPOIFSFileSystem simple = new NPOIFSFileSystem(samples.getFile("quick.msg"), true);
    simple.getRoot().getEntry("__recip_version1.0_#00000000");
    ChunkGroup[] groups = POIFSChunkParser.parse(simple.getRoot());
    assertEquals(3, groups.length);
    assertTrue(groups[0] instanceof Chunks);
    assertTrue(groups[1] instanceof RecipientChunks);
    assertTrue(groups[2] instanceof NameIdChunks);
    RecipientChunks recips = (RecipientChunks) groups[1];
    assertEquals("kevin.roast@alfresco.org", recips.recipientSMTPChunk.getValue());
    assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben", recips.recipientEmailChunk.getValue());
    String search = new String(recips.recipientSearchChunk.getValue(), "ASCII");
    assertEquals("CN=KEVIN.ROAST@BEN\0", search.substring(search.length() - 19));
    // Now via MAPIMessage
    MAPIMessage msg = new MAPIMessage(simple);
    assertNotNull(msg.getRecipientDetailsChunks());
    assertEquals(1, msg.getRecipientDetailsChunks().length);
    assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue());
    assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].getRecipientEmailAddress());
    assertEquals("Kevin Roast", msg.getRecipientDetailsChunks()[0].getRecipientName());
    assertEquals("kevin.roast@alfresco.org", msg.getRecipientEmailAddress());
    // Try both SMTP and EX files for recipient
    assertEquals("EX", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue());
    assertEquals("kevin.roast@alfresco.org", msg.getRecipientDetailsChunks()[0].recipientSMTPChunk.getValue());
    assertEquals("/O=HOSTEDSERVICE2/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=Kevin.roast@ben", msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue());
    msg.close();
    simple.close();
    // Now look at another message
    simple = new NPOIFSFileSystem(samples.getFile("simple_test_msg.msg"), true);
    msg = new MAPIMessage(simple);
    assertNotNull(msg.getRecipientDetailsChunks());
    assertEquals(1, msg.getRecipientDetailsChunks().length);
    assertEquals("SMTP", msg.getRecipientDetailsChunks()[0].deliveryTypeChunk.getValue());
    assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientSMTPChunk);
    assertEquals(null, msg.getRecipientDetailsChunks()[0].recipientNameChunk);
    assertEquals("travis@overwrittenstack.com", msg.getRecipientDetailsChunks()[0].recipientEmailChunk.getValue());
    assertEquals("travis@overwrittenstack.com", msg.getRecipientEmailAddress());
    msg.close();
    simple.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ChunkGroup(org.apache.poi.hsmf.datatypes.ChunkGroup) Chunks(org.apache.poi.hsmf.datatypes.Chunks) RecipientChunks(org.apache.poi.hsmf.datatypes.RecipientChunks) NameIdChunks(org.apache.poi.hsmf.datatypes.NameIdChunks) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks) RecipientChunks(org.apache.poi.hsmf.datatypes.RecipientChunks) NameIdChunks(org.apache.poi.hsmf.datatypes.NameIdChunks) Test(org.junit.Test)

Aggregations

MAPIMessage (org.apache.poi.hsmf.MAPIMessage)19 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)14 Test (org.junit.Test)11 AttachmentChunks (org.apache.poi.hsmf.datatypes.AttachmentChunks)9 ChunkGroup (org.apache.poi.hsmf.datatypes.ChunkGroup)4 Chunks (org.apache.poi.hsmf.datatypes.Chunks)4 NameIdChunks (org.apache.poi.hsmf.datatypes.NameIdChunks)4 RecipientChunks (org.apache.poi.hsmf.datatypes.RecipientChunks)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2 StringChunk (org.apache.poi.hsmf.datatypes.StringChunk)2 ChunkNotFoundException (org.apache.poi.hsmf.exceptions.ChunkNotFoundException)2 OutlookTextExtactor (org.apache.poi.hsmf.extractor.OutlookTextExtactor)2 WordExtractor (org.apache.poi.hwpf.extractor.WordExtractor)2 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)2 Entry (org.apache.poi.poifs.filesystem.Entry)2 FileInputStream (java.io.FileInputStream)1