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: package org.apache.cocoon.mail;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Date;
022: import java.util.List;
023:
024: import javax.mail.MessagingException;
025: import javax.mail.internet.AddressException;
026: import javax.mail.internet.InternetAddress;
027: import javax.mail.internet.MimeMessage;
028: import javax.mail.internet.MimeMultipart;
029: import javax.mail.internet.MimePart;
030:
031: /**
032: * Description of the Class
033: *
034: * @author Bernhard Huber
035: * @since 26. Oktober 2002
036: * @version $Id: MimeMessageUtil.java 468424 2006-10-27 15:44:53Z vgritsenko $
037: */
038: public class MimeMessageUtil {
039:
040: /**
041: * Description of the Field
042: */
043: public final static String SENDER_NOT_AVAILABLE = "-not available-";
044:
045: /**
046: * Description of the Field
047: */
048: public final static String NO_SUBJECT = "-none-";
049:
050: /**
051: * Gets the sender attribute of the MimeMessageUtil class
052: *
053: *@param msg Description of the Parameter
054: *@return The sender value
055: */
056: public static String getSender(MimeMessage msg) {
057: String sender = null;
058: try {
059: InternetAddress[] from = (InternetAddress[]) msg.getFrom();
060: if (from != null && from.length > 0) {
061: sender = from[0].getPersonal();
062: if (sender == null) {
063: sender = from[0].getAddress();
064: }
065: }
066: if (sender == null) {
067: sender = SENDER_NOT_AVAILABLE;
068: }
069: } catch (AddressException e) {
070: sender = SENDER_NOT_AVAILABLE;
071: } catch (MessagingException e) {
072: sender = SENDER_NOT_AVAILABLE;
073: }
074: if (sender == null || sender.trim().length() == 0) {
075: sender = SENDER_NOT_AVAILABLE;
076: }
077: return sender;
078: }
079:
080: /**
081: * Gets the senderEmail attribute of the MimeMessageUtil class
082: *
083: *@param msg Description of the Parameter
084: *@return The senderEmail value
085: */
086: public static String getSenderEmail(MimeMessage msg) {
087: String senderEmail = null;
088: try {
089: InternetAddress[] from = (InternetAddress[]) msg.getFrom();
090: if (from != null && from.length > 0) {
091: senderEmail = from[0].getAddress();
092: }
093: } catch (AddressException e) {
094: senderEmail = SENDER_NOT_AVAILABLE;
095: } catch (MessagingException e) {
096: senderEmail = SENDER_NOT_AVAILABLE;
097: }
098: if (senderEmail == null || senderEmail.trim().length() == 0) {
099: senderEmail = SENDER_NOT_AVAILABLE;
100: }
101: return senderEmail;
102: }
103:
104: /**
105: * Gets the subject attribute of the MimeMessageUtil class
106: *
107: *@param msg Description of the Parameter
108: *@return The subject value
109: */
110: public static String getSubject(MimeMessage msg) {
111: String subject = null;
112: try {
113: subject = msg.getSubject();
114: } catch (MessagingException e) {
115: subject = NO_SUBJECT;
116: }
117: if (subject == null || subject.trim().length() == 0) {
118: subject = NO_SUBJECT;
119: }
120: return subject;
121: }
122:
123: /**
124: * Gets the date attribute of the MimeMessageUtil class
125: *
126: *@param msg Description of the Parameter
127: *@return The date value
128: */
129: public static Date getDate(MimeMessage msg) {
130: Date date = null;
131: try {
132: date = msg.getReceivedDate();
133: } catch (MessagingException messagingexception) {
134: /*
135: * empty
136: */
137: }
138: if (date == null) {
139: try {
140: date = msg.getSentDate();
141: } catch (MessagingException messagingexception) {
142: /*
143: * empty
144: */
145: }
146: }
147: return date;
148: }
149:
150: /**
151: * Gets the iD attribute of the MimeMessageUtil class
152: *
153: *@param msg Description of the Parameter
154: *@return The iD value
155: */
156: public static String getID(MimeMessage msg) {
157: String id = null;
158: try {
159: id = msg.getMessageID();
160: } catch (MessagingException messagingexception) {
161: /*
162: * empty
163: */
164: }
165: return id;
166: }
167:
168: /**
169: *
170: *@param part
171: *@param ctPref
172: *@param l
173: */
174: private static void flattenMessageHelper(MimePart part,
175: ContentTypePreference ctPref, List l) {
176: try {
177: if (part.isMimeType("multipart/alternative")) {
178: MimeMultipart mp = (MimeMultipart) part.getContent();
179: MimePart bestPart = null;
180: int ctMax = 0;
181: for (int i = 0; i < mp.getCount(); i++) {
182: MimePart p = (MimePart) mp.getBodyPart(i);
183: int ctPrefN = ctPref.preference(part);
184: if (ctPrefN > ctMax) {
185: ctMax = ctPrefN;
186: bestPart = p;
187: }
188: }
189: if (bestPart != null) {
190: l.add(bestPart);
191: }
192: } else if (part.isMimeType("multipart/*")) {
193: MimeMultipart mp = (MimeMultipart) part.getContent();
194: for (int i = 0; i < mp.getCount(); i++) {
195: flattenMessageHelper((MimePart) mp.getBodyPart(i),
196: ctPref, l);
197: }
198: } else if (part.isMimeType("message/rfc822")) {
199: flattenMessageHelper((MimePart) part.getContent(),
200: ctPref, l);
201: } else if (ctPref.preference(part) > 0) {
202: l.add(part);
203: }
204: } catch (MessagingException e) {
205: /*
206: * empty
207: */
208: } catch (IOException ioexception) {
209: /*
210: * empty
211: */
212: }
213: }
214:
215: /**
216: * Description of the Method
217: *
218: *@param message Description of the Parameter
219: *@param ctPref Description of the Parameter
220: *@return Description of the Return Value
221: */
222: public static MimePart[] flattenMessage(MimeMessage message,
223: ContentTypePreference ctPref) {
224: List parts = new ArrayList();
225: flattenMessageHelper(message, ctPref, parts);
226: return (MimePart[]) parts.toArray(new MimePart[0]);
227: }
228: }
|