KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > MailFolder


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util;
37
38 import java.util.*;
39 import java.io.*;
40
41 import javax.mail.*;
42 import javax.mail.internet.*;
43 import javax.activation.*;
44
45 import com.micronova.util.*;
46
47 /** MailFolder using NestedMap. */
48
49 public class MailFolder
50 {
51     // control keys
52

53     public static final String JavaDoc HEADER = "header";
54     public static final String JavaDoc HEADERMAP = "headerMap";
55     public static final String JavaDoc CONTENT = "content";
56     public static final String JavaDoc MAXCONTENTSIZE = "maxContentSize";
57     public static final String JavaDoc MAXPARTSIZE = "maxPartSize";
58     public static final String JavaDoc CONTENTASSTRING = "contentAsString";
59
60     // field names
61

62     public static final String JavaDoc RECEIVEDDATE = "receivedDate";
63     public static final String JavaDoc MESSAGENUMBER = "messageNumber";
64     public static final String JavaDoc SUBJECT = "subject";
65     public static final String JavaDoc EXCEPTION = "exception";
66     public static final String JavaDoc SENTDATE = "sentDate";
67     public static final String JavaDoc TO = "to";
68     public static final String JavaDoc CC = "cc";
69     public static final String JavaDoc BCC = "bcc";
70     public static final String JavaDoc REPLYTO = "replyTo";
71     public static final String JavaDoc FROM = "from";
72     public static final String JavaDoc FLAGS = "flags";
73     public static final String JavaDoc NAME = "name";
74     public static final String JavaDoc VALUE = "value";
75     public static final String JavaDoc FULLNAME = "fullName";
76     public static final String JavaDoc URLNAME = "urlName";
77     public static final String JavaDoc MESSAGECOUNT = "messageCount";
78     public static final String JavaDoc ADDRESS = "address";
79     public static final String JavaDoc PERSONAL = "personal";
80     public static final String JavaDoc DESCRIPTION = "description";
81     public static final String JavaDoc DISPOSITION = "disposition";
82     public static final String JavaDoc FILENAME = "fileName";
83     public static final String JavaDoc LINECOUNT = "lineCount";
84     public static final String JavaDoc SIZE = "size";
85     public static final String JavaDoc CONTENTTYPE = "contentType";
86     public static final String JavaDoc TYPE = "type";
87     public static final String JavaDoc PARTNAME = "partName";
88
89     public static final String JavaDoc PART = "_part";
90     public static final String JavaDoc FOLDER = "_folder";
91
92     public static final int[] ALLMESSAGES = new int[0];
93
94     /** set InternetAddress */
95
96     private static void setAddress(NestedMap map, InternetAddress address)
97     {
98         map.put(ADDRESS, address.getAddress());
99         map.put(PERSONAL, address.getPersonal());
100     }
101
102     /** set address */
103
104     private static void setAddresses(NestedMap map, String JavaDoc name, Address[] addresses) throws Exception JavaDoc
105     {
106         if (addresses != null)
107         {
108             NestedMap addressListMap = new NestedMap();
109
110             map.put(name, addressListMap);
111
112             List list = addressListMap.getSubList();
113
114             for (int i = 0; i < addresses.length; i ++)
115             {
116                 InternetAddress address = (InternetAddress)addresses[i];
117
118                 if (address != null)
119                 {
120                     list.add(address);
121                 }
122             }
123         }
124     }
125
126     public static String JavaDoc makePartMap(NestedMap partMap, Part part, Map controlMap) throws Exception JavaDoc
127     {
128         String JavaDoc name = null;
129
130         partMap.put(PART, part);
131
132         partMap.put(DESCRIPTION, part.getDescription());
133         partMap.put(FILENAME, part.getFileName());
134         partMap.put(LINECOUNT, new Long JavaDoc(part.getLineCount()));
135         partMap.put(SIZE, new Long JavaDoc(part.getSize()));
136         partMap.put(CONTENTTYPE, part.getContentType());
137         partMap.put(TYPE, NetUtil.parseMime(part.getContentType()));
138
139         String JavaDoc[] dispositions = part.getHeader("Content-Disposition");
140
141         if (dispositions != null)
142         {
143             for (int i = 0; i < dispositions.length; i ++)
144             {
145                 NestedMap dispositionMap = (NestedMap)NetUtil.parseMime(dispositions[i]);
146
147                 partMap.put(DISPOSITION, dispositionMap);
148
149                 name = (String JavaDoc)dispositionMap.get("@parameter.name");
150             }
151         }
152
153         if (controlMap != null)
154         {
155             if (!TypeUtil.isFalse(controlMap.get(HEADER)))
156             {
157                 NestedMap headerMap = new NestedMap();
158                 
159                 partMap.put(HEADER, headerMap);
160                 
161                 boolean isHeaderMap = (!TypeUtil.isFalse(controlMap.get(HEADERMAP)));
162                 
163                 List headerList = headerMap.getSubList();
164                 
165                 Enumeration enumHeaders = part.getAllHeaders();
166                 
167                 while (enumHeaders.hasMoreElements())
168                 {
169                     Header header = (Header)enumHeaders.nextElement();
170                     
171                     NestedMap headerElementMap = new NestedMap();
172                     
173                     String JavaDoc headerName = header.getName();
174                     String JavaDoc headerValue = header.getValue();
175
176                     headerElementMap.put(NAME, headerName);
177                     headerElementMap.put(VALUE, headerValue);
178                     
179                     headerList.add(headerElementMap);
180                     
181                     if (isHeaderMap)
182                     {
183                         if ((headerName != null) && (headerValue != null))
184                         {
185                             headerMap.put(headerName, headerValue);
186                         }
187                     }
188                 }
189             }
190
191             if (!TypeUtil.isFalse(controlMap.get(CONTENT)))
192             {
193                 Integer JavaDoc iMaxSize = TypeUtil.isInteger(controlMap.get(MAXPARTSIZE));
194
195                 int maxSize = 0;
196                 
197                 if (iMaxSize != null)
198                 {
199                     maxSize = iMaxSize.intValue();
200                 }
201
202                 if ((maxSize <= 0) || (part.getSize() < maxSize))
203                 {
204                     Object JavaDoc content = part.getContent();
205
206                     if (content instanceof Multipart)
207                     {
208                         Multipart multipart = (Multipart)content;
209                         List partMapList = partMap.getSubList();
210                         
211                         for (int i = 0; i < multipart.getCount(); i ++)
212                         {
213                             NestedMap subPartMap = new NestedMap();
214                             
215                             String JavaDoc partName = makePartMap(subPartMap, multipart.getBodyPart(i), controlMap);
216                             
217                             partMapList.add(subPartMap);
218                             
219                             if (partName != null)
220                             {
221                                 NestedMap partNameMap = partMap.getSubMap(PARTNAME);
222                                 partNameMap.put(partName, subPartMap);
223                             }
224                         }
225                     }
226                     else
227                     {
228                         int partSize = part.getSize();
229
230                         Integer JavaDoc iMaxContentSize = TypeUtil.isInteger(controlMap.get(MAXCONTENTSIZE));
231
232                         int maxContentSize = 0;
233                         
234                         if (iMaxContentSize != null)
235                         {
236                             maxContentSize = iMaxContentSize.intValue();
237                         }
238
239                         if ((maxContentSize <= 0) || (partSize < maxContentSize))
240                         {
241                             if (partSize > 0)
242                             {
243                                 if (TypeUtil.isTrue(controlMap.get(CONTENTASSTRING)))
244                                 {
245                                     content = TypeUtil.isString(content);
246                                 }
247
248                                 partMap.put(CONTENT, content);
249                             }
250                         }
251                     }
252                 }
253             }
254         }
255
256         return name;
257     }
258
259     /** reads given messages. If messageNumbers is null, then no messages are retrieved. If messageNumbers equal to ALLMESSAGES, then all messages are retrieved. */
260
261     public static Map readMessages(Folder folder, int[] messageNumbers, Map controlMap) throws Exception JavaDoc
262     {
263         Message[] messages = null;
264
265         if (messageNumbers == ALLMESSAGES)
266         {
267             messages = folder.getMessages();
268         }
269         else if (messageNumbers != null)
270         {
271             messages = folder.getMessages(messageNumbers);
272         }
273
274         return makeFolderMap(folder, messages, controlMap);
275     }
276     
277     /** creates a message map */
278         
279     private static Map makeMessageMap(Message message, Map controlMap)
280     {
281         NestedMap map = new NestedMap();
282
283         int messageNumber = message.getMessageNumber();
284
285         map.put(MESSAGENUMBER, new Long JavaDoc(messageNumber));
286
287         try
288         {
289             if (controlMap != null)
290             {
291                 String JavaDoc subject = message.getSubject();
292
293                 if (subject != null)
294                 {
295                     map.put(SUBJECT, subject);
296                 }
297                 
298                 Date sentDate = message.getSentDate();
299                 
300                 if (sentDate != null)
301                 {
302                     map.put(SENTDATE, sentDate);
303                 }
304                 
305                 Date receivedDate = message.getReceivedDate();
306                 
307                 if (receivedDate != null)
308                 {
309                     map.put(RECEIVEDDATE, receivedDate);
310                 }
311                 
312                 setAddresses(map, TO, message.getRecipients(Message.RecipientType.TO));
313                 setAddresses(map, CC, message.getRecipients(Message.RecipientType.CC));
314                 setAddresses(map, BCC, message.getRecipients(Message.RecipientType.BCC));
315             
316                 setAddresses(map, REPLYTO, message.getReplyTo());
317                 
318                 setAddresses(map, FROM, message.getFrom());
319
320                 makePartMap(map, message, controlMap);
321             }
322         }
323         catch (Exception JavaDoc e)
324         {
325             map.put(EXCEPTION, e);
326         }
327
328         return map;
329     }
330
331     private static Map makeFolderMap(Folder folder, Message[] messages, Map controlMap) throws Exception JavaDoc
332     {
333         NestedMap map = new NestedMap();
334
335         map.put(FOLDER, folder);
336
337         map.put(NAME, folder.getName());
338         map.put(FULLNAME, folder.getFullName());
339         map.put(URLNAME, folder.getURLName());
340         map.put(MESSAGECOUNT, new Integer JavaDoc(folder.getMessageCount()));
341
342         if (messages != null)
343         {
344             List list = map.getSubList();
345
346             for (int i = 0; i < messages.length; i ++)
347             {
348                 Message message = messages[i];
349
350                 Map messageMap = makeMessageMap(message, controlMap);
351
352                 list.add(messageMap);
353             }
354         }
355
356         return map;
357     }
358
359     /** delete messages */
360
361     public static void deleteMessages(Folder folder, int[] messageNumbers) throws Exception JavaDoc
362     {
363         if (messageNumbers != null)
364         {
365             Message[] messages = folder.getMessages(messageNumbers);
366
367             for (int i = messages.length; --i >= 0;)
368             {
369                 Message message = messages[i];
370
371                 message.setFlag(Flags.Flag.DELETED, true);
372             }
373
374             // try to expunge if supported
375

376             try
377             {
378                 folder.expunge();
379             }
380             catch (MethodNotSupportedException e)
381             {
382             }
383         }
384     }
385
386     /** decode multipart stream using given header */
387
388     public static NestedMap decodeMultipart(InputStream inputStream, String JavaDoc header, NestedMap controlMap) throws Exception JavaDoc
389     {
390         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
391
392         buffer.append("Content-Type:" + header + "\r\n\r\n");
393
394         ByteArrayInputStream headerInputStream = new ByteArrayInputStream(buffer.toString().getBytes("ISO-8859-1"));
395
396         SequenceInputStream sequenceInputStream = new SequenceInputStream(headerInputStream, inputStream);
397
398         MimeMessage mimeMessage = new MimeMessage(null, sequenceInputStream);
399
400         NestedMap map = new NestedMap();
401                 
402         makePartMap(map, mimeMessage, new NestedMap(controlMap));
403                 
404         return map;
405     }
406 }
407
Popular Tags