KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > UIDMimeMessage


1 package net.suberic.pooka;
2 import javax.mail.*;
3 import javax.mail.internet.*;
4 import net.suberic.pooka.FolderInfo;
5 import net.suberic.pooka.Pooka;
6 import javax.activation.DataHandler JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11
12 /**
13  * A wrapper around a MimeMessage which can either work in real or
14  * disconnected (cached) mode.
15  */

16
17 public class UIDMimeMessage extends MimeMessage {
18
19   long uid;
20   UIDFolderInfo parent;
21
22   public UIDMimeMessage(UIDFolderInfo parentFolderInfo, long newUid) {
23     super(Pooka.getDefaultSession());
24     uid = newUid;
25     parent = parentFolderInfo;
26     saved=true;
27     modified=false;
28   }
29
30   public int getSize() throws MessagingException {
31     try {
32       return getMessage().getSize();
33     } catch (FolderClosedException fce) {
34       int status = parent.getStatus();
35       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
36         try {
37           parent.openFolder(Folder.READ_WRITE);
38         } catch (MessagingException me) {
39           throw fce;
40         }
41
42         return getMessage().getSize();
43
44       } else {
45         throw fce;
46       }
47     }
48   }
49
50   protected InputStream JavaDoc getContentStream() throws MessagingException {
51     // sigh. this is pretty much taken from the javamail source.
52

53     try {
54       InputStream JavaDoc handlerStream = getInputStream();
55       InternetHeaders tmpHeaders = new InternetHeaders(handlerStream);
56
57       byte[] buf;
58
59       int len;
60       int size = 1024;
61
62       if (handlerStream instanceof ByteArrayInputStream JavaDoc) {
63         size = handlerStream.available();
64         buf = new byte[size];
65         len = handlerStream.read(buf, 0, size);
66       }
67       else {
68         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
69         buf = new byte[size];
70         while ((len = handlerStream.read(buf, 0, size)) != -1)
71           bos.write(buf, 0, len);
72         buf = bos.toByteArray();
73       }
74
75       return new ByteArrayInputStream JavaDoc(buf);
76     } catch (java.io.IOException JavaDoc ioe) {
77       throw new MessagingException("Error getting Content Stream", ioe);
78     }
79
80     //throw new MessagingException("No getting the content stream! Bad code!");
81
}
82
83   public synchronized DataHandler JavaDoc getDataHandler()
84     throws MessagingException {
85     try {
86       return getMessage().getDataHandler();
87     } catch (FolderClosedException fce) {
88       int status = parent.getStatus();
89       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
90         try {
91           parent.openFolder(Folder.READ_WRITE);
92         } catch (MessagingException me) {
93           throw fce;
94         }
95
96         return getMessage().getDataHandler();
97       } else {
98         throw fce;
99       }
100     }
101   }
102
103   public String JavaDoc[] getHeader(String JavaDoc name)
104     throws MessagingException {
105     try {
106       return getMessage().getHeader(name);
107     } catch (FolderClosedException fce) {
108       int status = parent.getStatus();
109       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
110         try {
111           parent.openFolder(Folder.READ_WRITE);
112         } catch (MessagingException me) {
113           throw fce;
114         }
115
116         return getMessage().getHeader(name);
117
118       } else {
119         throw fce;
120       }
121     }
122   }
123
124   public String JavaDoc getHeader(String JavaDoc name, String JavaDoc delimiter)
125     throws MessagingException {
126     try {
127       return getMessage().getHeader(name, delimiter);
128     } catch (FolderClosedException fce) {
129       int status = parent.getStatus();
130       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
131         try {
132           parent.openFolder(Folder.READ_WRITE);
133         } catch (MessagingException me) {
134           throw fce;
135         }
136
137         return getMessage().getHeader(name, delimiter);
138
139       } else {
140         throw fce;
141       }
142     }
143   }
144
145   public void setHeader(String JavaDoc name, String JavaDoc value)
146     throws MessagingException {
147     try {
148       getMessage().setHeader(name, value);
149     } catch (FolderClosedException fce) {
150       int status = parent.getStatus();
151       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
152         try {
153           parent.openFolder(Folder.READ_WRITE);
154         } catch (MessagingException me) {
155           throw fce;
156         }
157
158         getMessage().setHeader(name, value);
159
160       } else {
161         throw fce;
162       }
163     }
164   }
165
166   public void addHeader(String JavaDoc name, String JavaDoc value)
167     throws MessagingException {
168     try {
169       getMessage().addHeader(name, value);
170     } catch (FolderClosedException fce) {
171       int status = parent.getStatus();
172       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
173         try {
174           parent.openFolder(Folder.READ_WRITE);
175         } catch (MessagingException me) {
176           throw fce;
177         }
178
179         getMessage().addHeader(name, value);
180
181       } else {
182         throw fce;
183       }
184     }
185   }
186
187   public void removeHeader(String JavaDoc name)
188     throws MessagingException {
189     try {
190       getMessage().removeHeader(name);
191     } catch (FolderClosedException fce) {
192       int status = parent.getStatus();
193       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
194         try {
195           parent.openFolder(Folder.READ_WRITE);
196         } catch (MessagingException me) {
197           throw fce;
198         }
199
200         getMessage().removeHeader(name);
201
202       } else {
203         throw fce;
204       }
205     }
206   }
207
208   public Enumeration JavaDoc getAllHeaders() throws MessagingException {
209     try {
210       return getMessage().getAllHeaders();
211     } catch (FolderClosedException fce) {
212       if (Pooka.isDebug())
213         System.out.println("debug: caught FolderClosedException.");
214       int status = parent.getStatus();
215       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
216         if (Pooka.isDebug())
217           System.out.println("debug: folder should be open. trying to re-open folder.");
218         try {
219           parent.openFolder(Folder.READ_WRITE);
220         } catch (MessagingException me) {
221           throw fce;
222         }
223
224         return getMessage().getAllHeaders();
225
226       } else {
227         throw fce;
228       }
229     }
230   }
231
232   public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] names)
233     throws MessagingException {
234     try {
235       return getMessage().getMatchingHeaders(names);
236     } catch (FolderClosedException fce) {
237       int status = parent.getStatus();
238       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
239         try {
240           parent.openFolder(Folder.READ_WRITE);
241         } catch (MessagingException me) {
242           throw fce;
243         }
244
245         return getMessage().getMatchingHeaders(names);
246
247       } else {
248         throw fce;
249       }
250     }
251   }
252
253
254   /**
255    * Return non-matching headers from this Message as an
256    * Enumeration of Header objects. This implementation
257    * obtains the header from the <code>headers</code> InternetHeaders object.
258    *
259    * @exception MessagingException
260    */

261   public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] names)
262     throws MessagingException {
263     try {
264       return getMessage().getNonMatchingHeaders(names);
265     } catch (FolderClosedException fce) {
266       int status = parent.getStatus();
267       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
268         try {
269           parent.openFolder(Folder.READ_WRITE);
270         } catch (MessagingException me) {
271           throw fce;
272         }
273
274         return getMessage().getNonMatchingHeaders(names);
275
276       } else {
277         throw fce;
278       }
279     }
280   }
281
282   /**
283    * Add a raw RFC 822 header-line.
284    *
285    * @exceptionIllegalWriteException if the underlying
286    *implementation does not support modification
287    * @exceptionIllegalStateException if this message is
288    *obtained from a READ_ONLY folder.
289    * @exception MessagingException
290    */

291   public void addHeaderLine(String JavaDoc line) throws MessagingException {
292     try {
293       getMessage().addHeaderLine(line);
294     } catch (FolderClosedException fce) {
295       int status = parent.getStatus();
296       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
297         try {
298           parent.openFolder(Folder.READ_WRITE);
299         } catch (MessagingException me) {
300           throw fce;
301         }
302
303         getMessage().addHeaderLine(line);
304
305       } else {
306         throw fce;
307       }
308     }
309   }
310
311   /**
312    * Get all header lines as an Enumeration of Strings. A Header
313    * line is a raw RFC 822 header-line, containing both the "name"
314    * and "value" field.
315    *
316    * @exception MessagingException
317    */

318   public Enumeration JavaDoc getAllHeaderLines() throws MessagingException {
319     try {
320       return getMessage().getAllHeaderLines();
321     } catch (FolderClosedException fce) {
322       int status = parent.getStatus();
323       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
324         try {
325           parent.openFolder(Folder.READ_WRITE);
326         } catch (MessagingException me) {
327           throw fce;
328         }
329
330         return getMessage().getAllHeaderLines();
331
332       } else {
333         throw fce;
334       }
335     }
336   }
337
338   /**
339    * Get matching header lines as an Enumeration of Strings.
340    * A Header line is a raw RFC 822 header-line, containing both
341    * the "name" and "value" field.
342    *
343    * @exception MessagingException
344    */

345   public Enumeration JavaDoc getMatchingHeaderLines(String JavaDoc[] names)
346     throws MessagingException {
347     try {
348       return getMessage().getMatchingHeaderLines(names);
349     } catch (FolderClosedException fce) {
350       int status = parent.getStatus();
351       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
352         try {
353           parent.openFolder(Folder.READ_WRITE);
354         } catch (MessagingException me) {
355           throw fce;
356         }
357
358         return getMessage().getMatchingHeaderLines(names);
359
360       } else {
361         throw fce;
362       }
363     }
364   }
365
366   /**
367    * Get non-matching header lines as an Enumeration of Strings.
368    * A Header line is a raw RFC 822 header-line, containing both
369    * the "name" and "value" field.
370    *
371    * @exception MessagingException
372    */

373   public Enumeration JavaDoc getNonMatchingHeaderLines(String JavaDoc[] names)
374     throws MessagingException {
375     try {
376       return getMessage().getNonMatchingHeaderLines(names);
377     } catch (FolderClosedException fce) {
378       int status = parent.getStatus();
379       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
380         try {
381           parent.openFolder(Folder.READ_WRITE);
382         } catch (MessagingException me) {
383           throw fce;
384         }
385
386         return getMessage().getNonMatchingHeaderLines(names);
387
388       } else {
389         throw fce;
390       }
391     }
392   }
393
394   /**
395    * Return a <code>Flags</code> object containing the flags for
396    * this message. <p>
397    *
398    * Note that a clone of the internal Flags object is returned, so
399    * modifying the returned Flags object will not affect the flags
400    * of this message.
401    *
402    * @return Flags object containing the flags for this message
403    * @exception MessagingException
404    * @see javax.mail.Flags
405    */

406   public synchronized Flags getFlags() throws MessagingException {
407     try {
408       Message m = getMessage();
409       if (m != null)
410         return m.getFlags();
411       else
412         throw new MessageRemovedException();
413     } catch (FolderClosedException fce) {
414       int status = parent.getStatus();
415       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
416         try {
417           parent.openFolder(Folder.READ_WRITE);
418         } catch (MessagingException me) {
419           throw fce;
420         }
421
422         return getMessage().getFlags();
423
424       } else {
425         throw fce;
426       }
427     }
428   }
429
430   /**
431    * Check whether the flag specified in the <code>flag</code>
432    * argument is set in this message. <p>
433    *
434    * This implementation checks this message's internal
435    * <code>flags</code> object.
436    *
437    * @param flagthe flag
438    * @returnvalue of the specified flag for this message
439    * @see javax.mail.Flags.Flag
440    * @seejavax.mail.Flags.Flag#ANSWERED
441    * @seejavax.mail.Flags.Flag#DELETED
442    * @seejavax.mail.Flags.Flag#DRAFT
443    * @seejavax.mail.Flags.Flag#FLAGGED
444    * @seejavax.mail.Flags.Flag#RECENT
445    * @seejavax.mail.Flags.Flag#SEEN
446    * @exception MessagingException
447    */

448   public synchronized boolean isSet(Flags.Flag flag)
449     throws MessagingException {
450     try {
451       return getFlags().contains(flag);
452     } catch (FolderClosedException fce) {
453       int status = parent.getStatus();
454       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
455         try {
456           parent.openFolder(Folder.READ_WRITE);
457         } catch (MessagingException me) {
458           throw fce;
459         }
460
461         return getFlags().contains(flag);
462
463       } else {
464         throw fce;
465       }
466     }
467   }
468
469   /**
470    * Set the flags for this message. <p>
471    *
472    * This implementation modifies the <code>flags</code> field.
473    *
474    * @exceptionIllegalWriteException if the underlying
475    *implementation does not support modification
476    * @exceptionIllegalStateException if this message is
477    *obtained from a READ_ONLY folder.
478    * @exception MessagingException
479    */

480   public synchronized void setFlags(Flags flag, boolean set)
481     throws MessagingException {
482     try {
483       getMessage().setFlags(flag, set);
484     } catch (FolderClosedException fce) {
485       int status = parent.getStatus();
486       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
487         try {
488           parent.openFolder(Folder.READ_WRITE);
489         } catch (MessagingException me) {
490           throw fce;
491         }
492
493         getMessage().setFlags(flag, set);
494
495       } else {
496         throw fce;
497       }
498     }
499   }
500
501   /**
502    * Checks whether this message is expunged. All other methods except
503    * <code>getMessageNumber()</code> are invalid on an expunged
504    * Message object. <p>
505    *
506    * Messages that are expunged due to an explict <code>expunge()</code>
507    * request on the containing Folder are removed from the Folder
508    * immediately. Messages that are externally expunged by another source
509    * are marked "expunged" and return true for the isExpunged() method,
510    * but they are not removed from the Folder until an explicit
511    * <code>expunge()</code> is done on the Folder. <p>
512    *
513    * See the description of <code>expunge()</code> for more details on
514    * expunge handling.
515    *
516    * @seeFolder#expunge
517    */

518   public boolean isExpunged() {
519     try {
520       try {
521         return (getMessage() == null);
522       } catch (FolderClosedException fce) {
523         int status = parent.getStatus();
524         if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
525           try {
526             parent.openFolder(Folder.READ_WRITE);
527           } catch (MessagingException me) {
528             throw fce;
529           }
530
531           return (getMessage() == null);
532
533         } else {
534           throw fce;
535         }
536       }
537     } catch (MessagingException me) {
538       return false;
539     }
540   }
541
542   public void writeTo(java.io.OutputStream JavaDoc os, java.lang.String JavaDoc[] ignoreList)
543     throws java.io.IOException JavaDoc, MessagingException {
544     try {
545       getMessage().writeTo(os, ignoreList);
546     } catch (FolderClosedException fce) {
547       int status = parent.getStatus();
548       if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) {
549         try {
550           parent.openFolder(Folder.READ_WRITE);
551         } catch (MessagingException me) {
552           throw fce;
553         }
554
555         getMessage().writeTo(os, ignoreList);
556       } else {
557         throw fce;
558       }
559     }
560   }
561
562   public long getUID() {
563     return uid;
564   }
565
566   public long getUIDValidity() {
567     return parent.getUIDValidity();
568   }
569
570   public MimeMessage getMessage() throws MessagingException {
571     MimeMessage returnValue = parent.getRealMessageById(uid);
572     if (returnValue == null) {
573       throw new MessageRemovedException("Message with UID " + uid + " does not exist in Folder " + parent.getFolderID());
574     } else {
575       return returnValue;
576     }
577   }
578
579   public UIDFolderInfo getParent() {
580     return parent;
581   }
582
583   /**
584    * Overrides equals to make it so that any two MimeMessages who are
585    * from the same folder and have the same UID are considered equal.
586    */

587   public boolean equals(Object JavaDoc o) {
588     if (o instanceof Message) {
589       try {
590         UIDMimeMessage uidMsg = parent.getUIDMimeMessage((Message) o);
591         if (uidMsg != null)
592           if (uidMsg.getUID() == getUID())
593             return true;
594           else
595             return false;
596       } catch (MessagingException me) {
597         return false;
598       }
599     }
600
601     return false;
602   }
603 }
604
605
606
607
608
Popular Tags