001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hsmf;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024:
025: import org.apache.poi.hsmf.datatypes.Chunk;
026: import org.apache.poi.hsmf.datatypes.Chunks;
027: import org.apache.poi.hsmf.datatypes.StringChunk;
028: import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
029: import org.apache.poi.hsmf.parsers.POIFSChunkParser;
030: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
031:
032: /**
033: * Reads an Outlook MSG File in and provides hooks into its data structure.
034: *
035: * @author Travis Ferguson
036: */
037: public class MAPIMessage {
038: private POIFSChunkParser chunkParser;
039: private POIFSFileSystem fs;
040:
041: /**
042: * Constructor for creating new files.
043: *
044: */
045: public MAPIMessage() {
046: //TODO make writing possible
047: }
048:
049: /**
050: * Constructor for reading MSG Files from the file system.
051: * @param filename
052: * @throws IOException
053: */
054: public MAPIMessage(String filename) throws IOException {
055: this (new FileInputStream(new File(filename)));
056: }
057:
058: /**
059: * Constructor for reading MSG Files from an input stream.
060: * @param in
061: * @throws IOException
062: */
063: public MAPIMessage(InputStream in) throws IOException {
064: this .fs = new POIFSFileSystem(in);
065: chunkParser = new POIFSChunkParser(this .fs);
066: }
067:
068: /**
069: * Gets a string value based on the passed chunk.
070: * @param chunk
071: * @return
072: * @throws ChunkNotFoundException
073: */
074: public String getStringFromChunk(StringChunk chunk)
075: throws ChunkNotFoundException {
076: Chunk out = this .chunkParser.getDocumentNode(chunk);
077: StringChunk strchunk = (StringChunk) out;
078: return strchunk.toString();
079: }
080:
081: /**
082: * Gets the plain text body of this Outlook Message
083: * @return The string representation of the 'text' version of the body, if available.
084: * @throws IOException
085: * @throws ChunkNotFoundException
086: */
087: public String getTextBody() throws IOException,
088: ChunkNotFoundException {
089: return getStringFromChunk(Chunks.getInstance().textBodyChunk);
090: }
091:
092: /**
093: * Gets the subject line of the Outlook Message
094: * @return
095: * @throws ChunkNotFoundException
096: */
097: public String getSubject() throws ChunkNotFoundException {
098: return getStringFromChunk(Chunks.getInstance().subjectChunk);
099: }
100:
101: /**
102: * Gets the display value of the "TO" line of the outlook message
103: * This is not the actual list of addresses/values that will be sent to if you click Reply in the email.
104: * @return
105: * @throws ChunkNotFoundException
106: */
107: public String getDisplayTo() throws ChunkNotFoundException {
108: return getStringFromChunk(Chunks.getInstance().displayToChunk);
109: }
110:
111: /**
112: * Gets the display value of the "FROM" line of the outlook message
113: * This is not the actual address that was sent from but the formated display of the user name.
114: * @return
115: * @throws ChunkNotFoundException
116: */
117: public String getDisplayFrom() throws ChunkNotFoundException {
118: return getStringFromChunk(Chunks.getInstance().displayFromChunk);
119: }
120:
121: /**
122: * Gets the display value of the "TO" line of the outlook message
123: * This is not the actual list of addresses/values that will be sent to if you click Reply in the email.
124: * @return
125: * @throws ChunkNotFoundException
126: */
127: public String getDisplayCC() throws ChunkNotFoundException {
128: return getStringFromChunk(Chunks.getInstance().displayCCChunk);
129: }
130:
131: /**
132: * Gets the display value of the "TO" line of the outlook message
133: * This is not the actual list of addresses/values that will be sent to if you click Reply in the email.
134: * @return
135: * @throws ChunkNotFoundException
136: */
137: public String getDisplayBCC() throws ChunkNotFoundException {
138: return getStringFromChunk(Chunks.getInstance().displayBCCChunk);
139: }
140:
141: /**
142: * Gets the conversation topic of the parsed Outlook Message.
143: * This is the part of the subject line that is after the RE: and FWD:
144: * @return
145: * @throws ChunkNotFoundException
146: */
147: public String getConversationTopic() throws ChunkNotFoundException {
148: return getStringFromChunk(Chunks.getInstance().conversationTopic);
149: }
150:
151: /**
152: * Gets the message class of the parsed Outlook Message.
153: * (Yes, you can use this to determine if a message is a calendar item, note, or actual outlook Message)
154: * For emails the class will be IPM.Note
155: *
156: * @return
157: * @throws ChunkNotFoundException
158: */
159: public String getMessageClass() throws ChunkNotFoundException {
160: return getStringFromChunk(Chunks.getInstance().messageClass);
161: }
162: }
|