001: /*
002: * Created on 21/08/2006 22:14:04
003: */
004: package net.jforum.api.integration.mail.pop;
005:
006: import java.io.BufferedReader;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.io.InputStreamReader;
010: import java.util.Date;
011: import java.util.Enumeration;
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import javax.mail.Header;
016: import javax.mail.Message;
017: import javax.mail.MessagingException;
018: import javax.mail.Multipart;
019: import javax.mail.Part;
020: import javax.mail.internet.InternetAddress;
021:
022: import net.jforum.exceptions.MailException;
023:
024: /**
025: * Represents a pop message.
026: * @author Rafael Steil
027: * @version $Id: POPMessage.java,v 1.6 2006/10/09 00:54:08 rafaelsteil Exp $
028: */
029: public class POPMessage {
030: private static final String IN_REPLY_TO = "In-Reply-To";
031: private static final String REFERENCES = "References";
032:
033: private String subject;
034: private Object message;
035: private String messageContents;
036: private String sender;
037: private String replyTo;
038: private String references;
039: private String inReplyTo;
040: private String contentType;
041: private String listEmail;
042: private Date sendDate;
043: private Map headers;
044:
045: /**
046: * Creates a new instance based on a {@link Message}
047: * @param message the message to convert from.
048: */
049: public POPMessage(Message message) {
050: this .extract(message);
051: }
052:
053: /**
054: * Given a {@link Message}, converts it to our internal format
055: * @param message the message to convert
056: */
057: private void extract(Message message) {
058: try {
059: this .subject = message.getSubject();
060:
061: this .message = message.getContent();
062: this .contentType = message.getContentType();
063: this .sender = ((InternetAddress) message.getFrom()[0])
064: .getAddress();
065: this .listEmail = ((InternetAddress) message
066: .getAllRecipients()[0]).getAddress();
067: this .sendDate = message.getSentDate();
068:
069: if (message.getReplyTo().length > 0) {
070: this .replyTo = ((InternetAddress) message.getReplyTo()[0])
071: .getAddress();
072: } else {
073: this .replyTo = this .sender;
074: }
075:
076: this .headers = new HashMap();
077:
078: for (Enumeration e = message.getAllHeaders(); e
079: .hasMoreElements();) {
080: Header header = (Header) e.nextElement();
081: this .headers.put(header.getName(), header.getValue());
082: }
083:
084: if (this .headers.containsKey(IN_REPLY_TO)) {
085: this .inReplyTo = this .headers.get(IN_REPLY_TO)
086: .toString();
087: }
088:
089: if (this .headers.containsKey(REFERENCES)) {
090: this .references = this .headers.get(REFERENCES)
091: .toString();
092: }
093:
094: this .extractMessageContents(message);
095: } catch (Exception e) {
096:
097: }
098: }
099:
100: private void extractMessageContents(Message m)
101: throws MessagingException {
102: Part messagePart = m;
103:
104: if (this .message instanceof Multipart) {
105: messagePart = ((Multipart) this .message).getBodyPart(0);
106: }
107:
108: if (contentType.startsWith("text/html")
109: || contentType.startsWith("text/plain")) {
110: InputStream is = null;
111: BufferedReader reader = null;
112:
113: try {
114: is = messagePart.getInputStream();
115: is.reset();
116: reader = new BufferedReader(new InputStreamReader(is));
117:
118: StringBuffer sb = new StringBuffer(512);
119: int c = 0;
120: char[] ch = new char[2048];
121:
122: while ((c = reader.read(ch)) != -1) {
123: sb.append(ch, 0, c);
124: }
125:
126: this .messageContents = sb.toString();
127: } catch (IOException e) {
128: throw new MailException(e);
129: } finally {
130: if (reader != null) {
131: try {
132: reader.close();
133: } catch (Exception e) {
134: }
135: }
136: if (is != null) {
137: try {
138: is.close();
139: } catch (Exception e) {
140: }
141: }
142: }
143: }
144: }
145:
146: public String getListEmail() {
147: return this .listEmail;
148: }
149:
150: /**
151: * @return the contentType
152: */
153: public String getContentType() {
154: return this .contentType;
155: }
156:
157: /**
158: * @return the headers
159: */
160: public Map getHeaders() {
161: return this .headers;
162: }
163:
164: /**
165: * @return the inReplyTo
166: */
167: public String getInReplyTo() {
168: return this .inReplyTo;
169: }
170:
171: /**
172: * @return the message
173: */
174: public String getMessage() {
175: return this .messageContents;
176: }
177:
178: /**
179: * @return the references
180: */
181: public String getReferences() {
182: return this .references;
183: }
184:
185: /**
186: * @return the replyTo
187: */
188: public String getReplyTo() {
189: return this .replyTo;
190: }
191:
192: /**
193: * @return the sendDate
194: */
195: public Date getSendDate() {
196: return this .sendDate;
197: }
198:
199: /**
200: * @return the sender
201: */
202: public String getSender() {
203: return this .sender;
204: }
205:
206: /**
207: * @return the subject
208: */
209: public String getSubject() {
210: return this .subject;
211: }
212:
213: /**
214: * @param contentType the contentType to set
215: */
216: public void setContentType(String contentType) {
217: this .contentType = contentType;
218: }
219:
220: /**
221: * @param headers the headers to set
222: */
223: public void setHeaders(Map headers) {
224: this .headers = headers;
225: }
226:
227: /**
228: * @param inReplyTo the inReplyTo to set
229: */
230: public void setInReplyTo(String inReplyTo) {
231: this .inReplyTo = inReplyTo;
232: }
233:
234: /**
235: * @param message the message to set
236: */
237: public void setMessage(Object message) {
238: this .message = message;
239: }
240:
241: /**
242: * @param references the references to set
243: */
244: public void setReferences(String references) {
245: this .references = references;
246: }
247:
248: /**
249: * @param replyTo the replyTo to set
250: */
251: public void setReplyTo(String replyTo) {
252: this .replyTo = replyTo;
253: }
254:
255: /**
256: * @param sendDate the sendDate to set
257: */
258: public void setSendDate(Date sendDate) {
259: this .sendDate = sendDate;
260: }
261:
262: /**
263: * @param sender the sender to set
264: */
265: public void setSender(String sender) {
266: this .sender = sender;
267: }
268:
269: /**
270: * @param subject the subject to set
271: */
272: public void setSubject(String subject) {
273: this .subject = subject;
274: }
275:
276: /**
277: * @see java.lang.Object#toString()
278: */
279: public String toString() {
280: return new StringBuffer().append('[').append(", subject=")
281: .append(this .subject).append(", sender=").append(
282: this .sender).append(", replyTo=").append(
283: this .replyTo).append(", references=").append(
284: this .references).append(", inReplyTo=").append(
285: this .inReplyTo).append(", contentType=")
286: .append(this .contentType).append(", date=").append(
287: this .sendDate).append(", content=").append(
288: this .messageContents).append(", headers=")
289: .append(this .headers).append(']').toString();
290: }
291: }
|