]> source.dussan.org Git - poi.git/commitdiff
Bug 60795 -- add enum for message class in MAPIMessage; deprecate getMessageClass()
authorTim Allison <tallison@apache.org>
Wed, 1 Mar 2017 15:47:47 +0000 (15:47 +0000)
committerTim Allison <tallison@apache.org>
Wed, 1 Mar 2017 15:47:47 +0000 (15:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1784978 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java
src/scratchpad/testcases/org/apache/poi/hsmf/TestSimpleFileRead.java
test-data/hsmf/msgClassAppointment.msg [new file with mode: 0644]
test-data/hsmf/msgClassContact.msg [new file with mode: 0644]
test-data/hsmf/msgClassPost.msg [new file with mode: 0644]
test-data/hsmf/msgClassStickyNote.msg [new file with mode: 0644]
test-data/hsmf/msgClassTask.msg [new file with mode: 0644]

index a3d2152bbb7f5f0cd49593e370200d630a6d448d..c44627656ad7c2d3ebb5c42f16ef96659bb4af00 100644 (file)
@@ -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.
index bdfff107886ee787792b0f79e34c5732c809c4cf..cc902fae349956827958cc672dab0394f8b28659 100644 (file)
@@ -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 (file)
index 0000000..c124d31
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 (file)
index 0000000..d925f3d
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 (file)
index 0000000..3dffd09
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 (file)
index 0000000..13873a1
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 (file)
index 0000000..a2ac9f8
Binary files /dev/null and b/test-data/hsmf/msgClassTask.msg differ