KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > imap4 > IMAPMessage


1 /*
2  * IMAPMessage.java
3  * Copyright (C) 2003 Chris Burdess <dog@gnu.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * You also have permission to link it with the Sun Microsystems, Inc.
11  * JavaMail(tm) extension and run that combination.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22
23 package gnu.mail.providers.imap4;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import javax.activation.DataHandler JavaDoc;
35 import javax.mail.Flags JavaDoc;
36 import javax.mail.Folder JavaDoc;
37 import javax.mail.Message JavaDoc;
38 import javax.mail.MessagingException JavaDoc;
39 import javax.mail.internet.InternetHeaders JavaDoc;
40 import javax.mail.internet.MimeMessage JavaDoc;
41
42 import gnu.mail.providers.ReadOnlyMessage;
43
44 /**
45  * The message class implementing the IMAP4 mail protocol.
46  *
47  * @author <a HREF='mailto:dog@gnu.org'>Chris Burdess</a>
48  * @version 1.0
49  */

50 public class IMAPMessage extends ReadOnlyMessage implements IMAPConstants
51 {
52
53   protected static final String JavaDoc FETCH_HEADERS = "BODY.PEEK[HEADER]";
54   protected static final String JavaDoc FETCH_CONTENT = "BODY.PEEK[]";
55
56   protected IMAPMessage(IMAPFolder folder, InputStream JavaDoc in, int msgnum)
57     throws MessagingException JavaDoc
58   {
59     super(folder, in, msgnum);
60     flags = null;
61   }
62
63   protected IMAPMessage(IMAPFolder folder, int msgnum)
64     throws MessagingException JavaDoc
65   {
66     super(folder, msgnum);
67     flags = null;
68   }
69
70   /**
71    * Fetches the flags fo this message.
72    */

73   protected void fetchFlags()
74     throws MessagingException JavaDoc
75   {
76     String JavaDoc[] commands = new String JavaDoc[] { FLAGS };
77     fetch(commands);
78   }
79
80   /**
81    * Fetches the message header.
82    */

83   protected void fetchHeaders()
84     throws MessagingException JavaDoc
85   {
86     String JavaDoc[] commands = new String JavaDoc[] { FETCH_HEADERS };
87     fetch(commands);
88   }
89
90   /**
91    * Fetches the message body.
92    */

93   protected void fetchContent()
94     throws MessagingException JavaDoc
95   {
96     String JavaDoc[] commands = new String JavaDoc[] { FETCH_CONTENT };
97     fetch(commands);
98   }
99
100   /**
101    * Generic fetch routine.
102    */

103   protected void fetch(String JavaDoc[] commands)
104     throws MessagingException JavaDoc
105   {
106     try
107     {
108       IMAPStore store = (IMAPStore)folder.getStore();
109       // Select folder
110
if (!folder.isOpen())
111         folder.open(Folder.READ_WRITE);
112       int[] messages = new int[] { msgnum };
113       MessageStatus[] ms = store.connection.fetch(messages, commands);
114       for (int i=0; i<ms.length; i++)
115       {
116         if (ms[i].getMessageNumber()==msgnum)
117           update(ms[i]);
118       }
119     }
120     catch (IOException JavaDoc e)
121     {
122       throw new MessagingException JavaDoc(e.getMessage(), e);
123     }
124   }
125
126   /**
127    * Updates this message using the specified message status object.
128    */

129   protected void update(MessageStatus status)
130     throws MessagingException JavaDoc
131   {
132     for (Iterator JavaDoc i = status.keySet().iterator(); i.hasNext(); )
133     {
134       String JavaDoc key = (String JavaDoc)i.next();
135       if (key==FLAGS)
136       {
137         List JavaDoc fl = (List JavaDoc)status.get(key);
138         flags = new Flags JavaDoc();
139         for (Iterator JavaDoc j = fl.iterator(); j.hasNext(); )
140         {
141           Object JavaDoc f = j.next();
142           if (f==FLAG_ANSWERED)
143             flags.add(Flags.Flag.ANSWERED);
144           else if (f==FLAG_DELETED)
145             flags.add(Flags.Flag.DELETED);
146           else if (f==FLAG_DRAFT)
147             flags.add(Flags.Flag.DRAFT);
148           else if (f==FLAG_FLAGGED)
149             flags.add(Flags.Flag.FLAGGED);
150           else if (f==FLAG_RECENT)
151             flags.add(Flags.Flag.RECENT);
152           else if (f==FLAG_SEEN)
153             flags.add(Flags.Flag.SEEN);
154           else if (f instanceof String JavaDoc)
155             flags.add((String JavaDoc)f);
156         }
157       }
158       else if (key==BODYHEADER)
159       {
160         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(status.getContent());
161         headers = createInternetHeaders(in);
162       }
163       else if (key==BODY)
164       {
165         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(status.getContent());
166         parse(in);
167       }
168       else
169         throw new MessagingException JavaDoc("Unknown message status key: "+key);
170     }
171     
172   }
173
174   // -- Content access --
175

176   /**
177    * Returns a data handler for this message's content.
178    */

179   public DataHandler JavaDoc getDataHandler()
180     throws MessagingException JavaDoc
181   {
182     if (content==null)
183       fetchContent();
184     return super.getDataHandler();
185   }
186
187   /**
188    * Returns the raw content stream.
189    */

190   protected InputStream JavaDoc getContentStream()
191     throws MessagingException JavaDoc
192   {
193     if (content==null)
194       fetchContent();
195     return super.getContentStream();
196   }
197
198   // -- Header access --
199

200   /**
201    * Returns the specified header field.
202    */

203   public String JavaDoc[] getHeader(String JavaDoc name)
204     throws MessagingException JavaDoc
205   {
206     if (headers==null)
207       fetchHeaders();
208     return super.getHeader(name);
209   }
210
211   /**
212    * Returns the specified header field.
213    */

214   public String JavaDoc getHeader(String JavaDoc name, String JavaDoc delimiter)
215     throws MessagingException JavaDoc
216   {
217     if (headers==null)
218       fetchHeaders();
219     return super.getHeader(name, delimiter);
220   }
221
222   public Enumeration JavaDoc getAllHeaders()
223     throws MessagingException JavaDoc
224   {
225     if (headers==null)
226       fetchHeaders();
227     return super.getAllHeaders();
228   }
229
230   public Enumeration JavaDoc getAllHeaderLines()
231     throws MessagingException JavaDoc
232   {
233     if (headers==null)
234       fetchHeaders();
235     return super.getAllHeaderLines();
236   }
237
238   public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] names)
239     throws MessagingException JavaDoc
240   {
241     if (headers==null)
242       fetchHeaders();
243     return super.getMatchingHeaders(names);
244   }
245
246   public Enumeration JavaDoc getMatchingHeaderLines(String JavaDoc[] names)
247     throws MessagingException JavaDoc
248   {
249     if (headers==null)
250       fetchHeaders();
251     return super.getMatchingHeaderLines(names);
252   }
253
254   public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] names)
255     throws MessagingException JavaDoc
256   {
257     if (headers==null)
258       fetchHeaders();
259     return super.getNonMatchingHeaders(names);
260   }
261
262   public Enumeration JavaDoc getNonMatchingHeaderLines(String JavaDoc[] names)
263     throws MessagingException JavaDoc
264   {
265     if (headers==null)
266       fetchHeaders();
267     return super.getNonMatchingHeaderLines(names);
268   }
269
270   // -- Flags access --
271

272   public Flags JavaDoc getFlags()
273     throws MessagingException JavaDoc
274   {
275     if (flags==null)
276       fetchFlags();
277     return super.getFlags();
278   }
279
280   public boolean isSet(Flags.Flag JavaDoc flag)
281     throws MessagingException JavaDoc
282   {
283     if (flags==null)
284       fetchFlags();
285     return super.isSet(flag);
286   }
287
288   /**
289    * Set the specified flags.
290    */

291   public void setFlags(Flags JavaDoc flag, boolean set)
292     throws MessagingException JavaDoc
293   {
294     if (flags==null)
295       fetchFlags();
296     try
297     {
298       if (set)
299         flags.add(flag);
300       else
301         flags.remove(flag);
302       // Create a list of flags to send to the server
303
Flags.Flag JavaDoc[] sflags = flags.getSystemFlags();
304       String JavaDoc[] uflags = flags.getUserFlags();
305       List JavaDoc iflags = new ArrayList JavaDoc(sflags.length+uflags.length);
306       for (int i=0; i<sflags.length; i++)
307       {
308         Flags.Flag JavaDoc f = sflags[i];
309         if (f==Flags.Flag.ANSWERED)
310           iflags.add(FLAG_ANSWERED);
311         else if (f==Flags.Flag.DELETED)
312           iflags.add(FLAG_DELETED);
313         else if (f==Flags.Flag.DRAFT)
314           iflags.add(FLAG_DRAFT);
315         else if (f==Flags.Flag.FLAGGED)
316           iflags.add(FLAG_FLAGGED);
317         else if (f==Flags.Flag.RECENT)
318           iflags.add(FLAG_RECENT);
319         else if (f==Flags.Flag.SEEN)
320           iflags.add(FLAG_SEEN);
321       }
322       iflags.addAll(Arrays.asList(uflags));
323       String JavaDoc[] aflags = new String JavaDoc[iflags.size()];
324       iflags.toArray(aflags);
325       // Perform store
326
IMAPStore store = (IMAPStore)folder.getStore();
327       int[] messages = new int[] { msgnum };
328       MessageStatus[] ms = store.connection.store(messages, FLAGS, aflags);
329       for (int i=0; i<ms.length; i++)
330       {
331         if (ms[i].getMessageNumber()==msgnum)
332           update(ms[i]);
333       }
334     }
335     catch (IOException JavaDoc e)
336     {
337       flags = null; // will be re-read next time
338
throw new MessagingException JavaDoc(e.getMessage(), e);
339     }
340   }
341
342   // -- Utility --
343

344   public void writeTo(OutputStream JavaDoc msgStream)
345     throws IOException JavaDoc, MessagingException JavaDoc
346   {
347     if (content==null)
348       fetchContent();
349     super.writeTo(msgStream);
350   }
351
352   public void writeTo(OutputStream JavaDoc msgStream, String JavaDoc[] ignoreList)
353     throws IOException JavaDoc, MessagingException JavaDoc
354   {
355     if (content==null)
356       fetchContent();
357     super.writeTo(msgStream, ignoreList);
358   }
359
360 }
361
Popular Tags