KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > mail > MimeMessageUtil


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.mail;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.mail.MessagingException JavaDoc;
24 import javax.mail.internet.AddressException JavaDoc;
25 import javax.mail.internet.InternetAddress JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import javax.mail.internet.MimeMultipart JavaDoc;
28 import javax.mail.internet.MimePart JavaDoc;
29
30 /**
31  * Description of the Class
32  *
33  * @author Bernhard Huber
34  * @since 26. Oktober 2002
35  * @version CVS $Id: MimeMessageUtil.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public class MimeMessageUtil {
38     /**
39      * Description of the Field
40      */

41     public final static String JavaDoc SENDER_NOT_AVAILABLE = "-not available-";
42     /**
43      * Description of the Field
44      */

45     public final static String JavaDoc NO_SUBJECT = "-none-";
46
47
48     /**
49      * Gets the sender attribute of the MimeMessageUtil class
50      *
51      *@param msg Description of the Parameter
52      *@return The sender value
53      */

54     public static String JavaDoc getSender(MimeMessage JavaDoc msg) {
55         String JavaDoc sender = null;
56         try {
57             InternetAddress JavaDoc[] from = (InternetAddress JavaDoc[]) msg.getFrom();
58             if (from != null && from.length > 0) {
59                 sender = from[0].getPersonal();
60                 if (sender == null) {
61                     sender = from[0].getAddress();
62                 }
63             }
64             if (sender == null) {
65                 sender = SENDER_NOT_AVAILABLE;
66             }
67         } catch (AddressException JavaDoc e) {
68             sender = SENDER_NOT_AVAILABLE;
69         } catch (MessagingException JavaDoc e) {
70             sender = SENDER_NOT_AVAILABLE;
71         }
72         if (sender == null || sender.trim().equals("")) {
73             sender = SENDER_NOT_AVAILABLE;
74         }
75         return sender;
76     }
77
78
79     /**
80      * Gets the senderEmail attribute of the MimeMessageUtil class
81      *
82      *@param msg Description of the Parameter
83      *@return The senderEmail value
84      */

85     public static String JavaDoc getSenderEmail(MimeMessage JavaDoc msg) {
86         String JavaDoc senderEmail = null;
87         try {
88             InternetAddress JavaDoc[] from = (InternetAddress JavaDoc[]) msg.getFrom();
89             if (from != null && from.length > 0) {
90                 senderEmail = from[0].getAddress();
91             }
92         } catch (AddressException JavaDoc e) {
93             senderEmail = SENDER_NOT_AVAILABLE;
94         } catch (MessagingException JavaDoc e) {
95             senderEmail = SENDER_NOT_AVAILABLE;
96         }
97         if (senderEmail == null || senderEmail.trim().equals("")) {
98             senderEmail = SENDER_NOT_AVAILABLE;
99         }
100         return senderEmail;
101     }
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 JavaDoc getSubject(MimeMessage JavaDoc msg) {
111         String JavaDoc subject = null;
112         try {
113             subject = msg.getSubject();
114         } catch (MessagingException JavaDoc e) {
115             subject = NO_SUBJECT;
116         }
117         if (subject == null || subject.trim().equals("")) {
118             subject = NO_SUBJECT;
119         }
120         return subject;
121     }
122
123
124     /**
125      * Gets the date attribute of the MimeMessageUtil class
126      *
127      *@param msg Description of the Parameter
128      *@return The date value
129      */

130     public static Date JavaDoc getDate(MimeMessage JavaDoc msg) {
131         Date JavaDoc date = null;
132         try {
133             date = msg.getReceivedDate();
134         } catch (MessagingException JavaDoc messagingexception) {
135             /*
136              * empty
137              */

138         }
139         if (date == null) {
140             try {
141                 date = msg.getSentDate();
142             } catch (MessagingException JavaDoc messagingexception) {
143                 /*
144                  * empty
145                  */

146             }
147         }
148         return date;
149     }
150
151
152     /**
153      * Gets the iD attribute of the MimeMessageUtil class
154      *
155      *@param msg Description of the Parameter
156      *@return The iD value
157      */

158     public static String JavaDoc getID(MimeMessage JavaDoc msg) {
159         String JavaDoc id = null;
160         try {
161             id = msg.getMessageID();
162         } catch (MessagingException JavaDoc messagingexception) {
163             /*
164              * empty
165              */

166         }
167         return id;
168     }
169
170
171     /**
172      *
173      *@param part
174      *@param ctPref
175      *@param l
176      */

177     private static void flattenMessageHelper
178             (MimePart JavaDoc part, ContentTypePreference ctPref, List JavaDoc l) {
179         try {
180             if (part.isMimeType("multipart/alternative")) {
181                 MimeMultipart JavaDoc mp = (MimeMultipart JavaDoc) part.getContent();
182                 MimePart JavaDoc bestPart = null;
183                 int ctMax = 0;
184                 for (int i = 0; i < mp.getCount(); i++) {
185                     MimePart JavaDoc p = (MimePart JavaDoc) mp.getBodyPart(i);
186                     int ctPrefN = ctPref.preference(part);
187                     if (ctPrefN > ctMax) {
188                         ctMax = ctPrefN;
189                         bestPart = p;
190                     }
191                 }
192                 if (bestPart != null) {
193                     l.add(bestPart);
194                 }
195             } else if (part.isMimeType("multipart/*")) {
196                 MimeMultipart JavaDoc mp = (MimeMultipart JavaDoc) part.getContent();
197                 for (int i = 0; i < mp.getCount(); i++) {
198                     flattenMessageHelper((MimePart JavaDoc) mp.getBodyPart(i),
199                             ctPref, l);
200                 }
201             } else if (part.isMimeType("message/rfc822")) {
202                 flattenMessageHelper((MimePart JavaDoc) part.getContent(), ctPref,
203                         l);
204             } else if (ctPref.preference(part) > 0) {
205                 l.add(part);
206             }
207         } catch (MessagingException JavaDoc e) {
208             /*
209              * empty
210              */

211         } catch (IOException JavaDoc ioexception) {
212             /*
213              * empty
214              */

215         }
216     }
217
218
219     /**
220      * Description of the Method
221      *
222      *@param message Description of the Parameter
223      *@param ctPref Description of the Parameter
224      *@return Description of the Return Value
225      */

226     public static MimePart JavaDoc[] flattenMessage(MimeMessage JavaDoc message,
227             ContentTypePreference ctPref) {
228         List JavaDoc parts = new ArrayList JavaDoc();
229         flattenMessageHelper(message, ctPref, parts);
230         return (MimePart JavaDoc[]) parts.toArray(new MimePart JavaDoc[0]);
231     }
232 }
233
234
Popular Tags