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
This commit is contained in:
Tim Allison 2017-03-01 15:47:47 +00:00
parent e36313aff1
commit 1ace11526e
7 changed files with 70 additions and 5 deletions

View 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.

View 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("_", "")));
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.