From: Tim Allison Date: Wed, 1 Mar 2017 15:47:47 +0000 (+0000) Subject: Bug 60795 -- add enum for message class in MAPIMessage; deprecate getMessageClass() X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5b801a9dc608a7bb2f5a1275c4e0ef705cfd73c3;p=poi.git Bug 60795 -- add enum for message class in MAPIMessage; deprecate getMessageClass() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1784978 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java b/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java index a3d2152bbb..c44627656a 100644 --- a/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java +++ b/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java @@ -49,7 +49,6 @@ import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; import org.apache.poi.hsmf.parsers.POIFSChunkParser; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.CodePageUtil; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; @@ -63,6 +62,21 @@ import org.apache.poi.util.POILogger; * [MS-OXCMSG]: Message and Attachment Object Protocol Specification */ public class MAPIMessage extends POIReadOnlyDocument { + + /** + * A MAPI file can be an email (NOTE) or a number of other types + */ + public enum MESSAGE_CLASS { + APPOINTMENT, + CONTACT, + NOTE, + POST, + STICKY_NOTE, + TASK, + UNKNOWN, + UNSPECIFIED + } + /** For logging problems we spot with the file */ private POILogger logger = POILogFactory.getLogger(MAPIMessage.class); @@ -528,11 +542,42 @@ public class MAPIMessage extends POIReadOnlyDocument { * For emails the class will be IPM.Note * * @throws ChunkNotFoundException + * @deprecated use {@link #getMessageClassEnum()} */ public String getMessageClass() throws ChunkNotFoundException { return getStringFromChunk(mainChunks.getMessageClass()); } + /** + * Gets the message class of the parsed Outlook Message. + * (Yes, you can use this to determine if a message is a calendar + * item, note, or actual outlook Message) + * For emails the class will be IPM.Note + * + * @throws ChunkNotFoundException + */ + public MESSAGE_CLASS getMessageClassEnum() throws ChunkNotFoundException { + String mc = getMessageClass(); + if (mc == null || mc.trim().length() == 0) { + return MESSAGE_CLASS.UNSPECIFIED; + } else if (mc.equalsIgnoreCase("IPM.Note")) { + return MESSAGE_CLASS.NOTE; + } else if (mc.equalsIgnoreCase("IPM.Contact")) { + return MESSAGE_CLASS.CONTACT; + } else if (mc.equalsIgnoreCase("IPM.Appointment")) { + return MESSAGE_CLASS.APPOINTMENT; + } else if (mc.equalsIgnoreCase("IPM.StickyNote")) { + return MESSAGE_CLASS.STICKY_NOTE; + } else if (mc.equalsIgnoreCase("IPM.Task")) { + return MESSAGE_CLASS.TASK; + } else if (mc.equalsIgnoreCase("IPM.Post")) { + return MESSAGE_CLASS.POST; + } else { + logger.log(POILogger.WARN, "I don't recognize message class '"+mc+"'. " + + "Please open an issue on POI's bugzilla"); + return MESSAGE_CLASS.UNKNOWN; + } + } /** * Gets the date that the message was accepted by the * server on. diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/TestSimpleFileRead.java b/src/scratchpad/testcases/org/apache/poi/hsmf/TestSimpleFileRead.java index bdfff10788..cc902fae34 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/TestSimpleFileRead.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/TestSimpleFileRead.java @@ -19,11 +19,10 @@ package org.apache.poi.hsmf; import java.io.IOException; -import org.apache.poi.hsmf.MAPIMessage; -import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; -import org.apache.poi.POIDataSamples; - import junit.framework.TestCase; +import org.apache.poi.POIDataSamples; +import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; +import org.junit.Test; /** * Tests to verify that we can read a simple msg file, that is in plain/text @@ -134,4 +133,25 @@ public final class TestSimpleFileRead extends TestCase { String obtained = mapiMessage.getMessageClass(); TestCase.assertEquals("IPM.Note", obtained); } + + /** + * Check the various message classes + * + * @throws Exception + */ + @Test + public void testReadMessageClass2() throws Exception { + TestCase.assertEquals( + MAPIMessage.MESSAGE_CLASS.NOTE, mapiMessage.getMessageClassEnum()); + + for (String messageClass : new String[]{ + "Appointment", "Contact", "Post", "StickyNote", "Task" + }) { + MAPIMessage.MESSAGE_CLASS mc = new MAPIMessage( + POIDataSamples.getHSMFInstance().getFile("msgClass"+messageClass+".msg") + ).getMessageClassEnum(); + assertTrue( mc + " but expected " + messageClass, + messageClass.equalsIgnoreCase(mc.toString().replaceAll("_", ""))); + } + } } diff --git a/test-data/hsmf/msgClassAppointment.msg b/test-data/hsmf/msgClassAppointment.msg new file mode 100644 index 0000000000..c124d31d89 Binary files /dev/null and b/test-data/hsmf/msgClassAppointment.msg differ diff --git a/test-data/hsmf/msgClassContact.msg b/test-data/hsmf/msgClassContact.msg new file mode 100644 index 0000000000..d925f3d702 Binary files /dev/null and b/test-data/hsmf/msgClassContact.msg differ diff --git a/test-data/hsmf/msgClassPost.msg b/test-data/hsmf/msgClassPost.msg new file mode 100644 index 0000000000..3dffd09e7f Binary files /dev/null and b/test-data/hsmf/msgClassPost.msg differ diff --git a/test-data/hsmf/msgClassStickyNote.msg b/test-data/hsmf/msgClassStickyNote.msg new file mode 100644 index 0000000000..13873a1e44 Binary files /dev/null and b/test-data/hsmf/msgClassStickyNote.msg differ diff --git a/test-data/hsmf/msgClassTask.msg b/test-data/hsmf/msgClassTask.msg new file mode 100644 index 0000000000..a2ac9f8c0a Binary files /dev/null and b/test-data/hsmf/msgClassTask.msg differ