KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > cache > CachingMimeMessage


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

17
18 public class CachingMimeMessage extends UIDMimeMessage {
19   
20   public CachingMimeMessage(CachingFolderInfo parentFolderInfo, long newUid) {
21     super(parentFolderInfo, newUid);
22   }
23   
24   public int getSize() throws MessagingException {
25     return getCache().getSize(getUID());
26     /*
27       try {
28       if (getContent() != null) {
29       //return getContent().length;
30       return 1;
31       }
32       else
33       return -1;
34       } catch (java.io.IOException ioe) {
35       throw new MessagingException(ioe.getMessage(), ioe);
36       }
37     */

38   }
39   
40   public synchronized DataHandler JavaDoc getDataHandler()
41     throws MessagingException {
42     return getCache().getDataHandler(getUID(), getUIDValidity(), ! getCacheHeadersOnly());
43   }
44   
45   public String JavaDoc[] getHeader(String JavaDoc name)
46     throws MessagingException {
47     return getHeaders().getHeader(name);
48   }
49   
50   public String JavaDoc getHeader(String JavaDoc name, String JavaDoc delimiter)
51     throws MessagingException {
52     return getHeaders().getHeader(name, delimiter);
53   }
54   
55   public void setHeader(String JavaDoc name, String JavaDoc value)
56     throws MessagingException {
57     throw new IllegalWriteException(Pooka.getProperty("error.cache.illegalWrite", "Cannot write to an existing message."));
58   }
59   
60   public void addHeader(String JavaDoc name, String JavaDoc value)
61     throws MessagingException {
62     throw new IllegalWriteException(Pooka.getProperty("error.cache.illegalWrite", "Cannot write to an existing message."));
63   }
64   
65   public void removeHeader(String JavaDoc name)
66     throws MessagingException {
67     throw new IllegalWriteException(Pooka.getProperty("error.cache.illegalWrite", "Cannot write to an existing message."));
68   }
69   
70   public Enumeration JavaDoc getAllHeaders() throws MessagingException {
71     return getHeaders().getAllHeaders();
72   }
73   
74   public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] names)
75     throws MessagingException {
76     return getHeaders().getMatchingHeaders(names);
77   }
78   
79   
80   /**
81    * Return non-matching headers from this Message as an
82    * Enumeration of Header objects. This implementation
83    * obtains the header from the <code>headers</code> InternetHeaders object.
84    *
85    * @exception MessagingException
86    */

87   public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] names)
88     throws MessagingException {
89     return getHeaders().getNonMatchingHeaders(names);
90   }
91   
92   /**
93    * Add a raw RFC 822 header-line.
94    *
95    * @exception IllegalWriteException if the underlying
96    * implementation does not support modification
97    * @exception IllegalStateException if this message is
98    * obtained from a READ_ONLY folder.
99    * @exception MessagingException
100    */

101   public void addHeaderLine(String JavaDoc line) throws MessagingException {
102     throw new IllegalWriteException(Pooka.getProperty("error.cache.illegalWrite", "Cannot write to an existing message."));
103   }
104   
105   /**
106    * Get all header lines as an Enumeration of Strings. A Header
107    * line is a raw RFC 822 header-line, containing both the "name"
108    * and "value" field.
109    *
110    * @exception MessagingException
111    */

112   public Enumeration JavaDoc getAllHeaderLines() throws MessagingException {
113     return getHeaders().getAllHeaderLines();
114   }
115   
116   /**
117    * Get matching header lines as an Enumeration of Strings.
118    * A Header line is a raw RFC 822 header-line, containing both
119    * the "name" and "value" field.
120    *
121    * @exception MessagingException
122    */

123   public Enumeration JavaDoc getMatchingHeaderLines(String JavaDoc[] names)
124     throws MessagingException {
125     return getHeaders().getMatchingHeaderLines(names);
126   }
127   
128   /**
129    * Get non-matching header lines as an Enumeration of Strings.
130    * A Header line is a raw RFC 822 header-line, containing both
131    * the "name" and "value" field.
132    *
133    * @exception MessagingException
134    */

135   public Enumeration JavaDoc getNonMatchingHeaderLines(String JavaDoc[] names)
136     throws MessagingException {
137     return getHeaders().getNonMatchingHeaderLines(names);
138   }
139
140   /**
141    * Return a <code>Flags</code> object containing the flags for
142    * this message. <p>
143    *
144    * Note that a clone of the internal Flags object is returned, so
145    * modifying the returned Flags object will not affect the flags
146    * of this message.
147    *
148    * @return Flags object containing the flags for this message
149    * @exception MessagingException
150    * @see javax.mail.Flags
151    */

152   public synchronized Flags getFlags() throws MessagingException {
153     try {
154       return (Flags) getCache().getFlags(getUID(), getUIDValidity()).clone();
155     } catch (MessagingException me) {
156       if (me instanceof MessageRemovedException) {
157         return new Flags();
158       } else {
159         throw me;
160       }
161     }
162
163   }
164
165   /**
166    * Check whether the flag specified in the <code>flag</code>
167    * argument is set in this message. <p>
168    *
169    * This implementation checks this message's internal
170    * <code>flags</code> object.
171    *
172    * @param flag the flag
173    * @return value of the specified flag for this message
174    * @see javax.mail.Flags.Flag
175    * @see javax.mail.Flags.Flag#ANSWERED
176    * @see javax.mail.Flags.Flag#DELETED
177    * @see javax.mail.Flags.Flag#DRAFT
178    * @see javax.mail.Flags.Flag#FLAGGED
179    * @see javax.mail.Flags.Flag#RECENT
180    * @see javax.mail.Flags.Flag#SEEN
181    * @exception MessagingException
182    */

183   public synchronized boolean isSet(Flags.Flag flag)
184     throws MessagingException {
185     try {
186       return getFlags().contains(flag);
187     } catch (MessagingException me) {
188       System.out.println("caught exception: " + me);
189       me.printStackTrace();
190       throw me;
191     }
192   }
193   
194   /**
195    * Set the flags for this message. <p>
196    *
197    * This implementation modifies the <code>flags</code> field.
198    *
199    * @exception IllegalWriteException if the underlying
200    * implementation does not support modification
201    * @exception IllegalStateException if this message is
202    * obtained from a READ_ONLY folder.
203    * @exception MessagingException
204    */

205   public synchronized void setFlags(Flags flag, boolean set)
206     throws MessagingException {
207     if (set)
208       getCache().addFlag(getUID(), getUIDValidity(), flag);
209     else
210       getCache().removeFlag(getUID(), getUIDValidity(), flag);
211   }
212
213   public MessageCache getCache() {
214     return ((CachingFolderInfo)getParent()).getCache();
215   }
216
217   public void setExpungedValue(boolean newValue) {
218     expunged=newValue;
219   }
220
221   /**
222    * Returns whether this message is expunged or not.
223    */

224   public boolean isExpunged() {
225     return expunged;
226   }
227
228   public InternetHeaders getHeaders() throws MessagingException {
229     try {
230       return getCache().getHeaders(getUID(), getUIDValidity());
231     } catch (MessagingException me) {
232       if (me instanceof MessageRemovedException) {
233         return new InternetHeaders();
234       } else {
235         throw me;
236       }
237     }
238   }
239
240   public void writeTo(java.io.OutputStream JavaDoc os, String JavaDoc[] ignoreList)
241     throws java.io.IOException JavaDoc, MessagingException {
242     getCache().getMessageRepresentation(getUID(), getUIDValidity(), ! getCacheHeadersOnly()).writeTo(os, ignoreList);
243   }
244  
245   /**
246    * Returns if we're caching only headers.
247    */

248   public boolean getCacheHeadersOnly() {
249     return ((CachingFolderInfo)getParent()).getCacheHeadersOnly();
250   }
251   
252 }
253
254
255
256
257
Popular Tags