KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > pop3 > POP3Message


1 /*
2  * POP3Message.java
3  * Copyright (C) 1999, 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.pop3;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import javax.activation.DataHandler JavaDoc;
30 import javax.mail.MessagingException JavaDoc;
31 import javax.mail.Session JavaDoc;
32 import javax.mail.internet.InternetHeaders JavaDoc;
33 import javax.mail.internet.MimeMessage JavaDoc;
34
35 import gnu.mail.providers.ReadOnlyMessage;
36
37 /**
38  * The message class implementing the POP3 mail protocol.
39  *
40  * @author <a HREF='mailto:dog@gnu.org'>Chris Burdess</a>
41  * @author <a HREF='mailto:nferrier@tapsellferrier.co.uk'>Nic Ferrier</a>
42  * @version 1.2
43  */

44 public final class POP3Message
45   // if we inherit from ReadOnlyMessage, setting the DELETE flag causes an exception, but
46
// extending MimeMessage means we don't get the readonly protection for other operations
47
// todo: inherit from ReadOnlyMessage and override methods as needed
48
extends MimeMessage JavaDoc
49 {
50
51   /*
52    * The size of this message, if predetermined.
53    */

54   int size;
55
56   /**
57    * Create a POP3Message.
58    */

59   POP3Message(POP3Folder folder, InputStream JavaDoc in, int msgnum)
60     throws MessagingException JavaDoc
61   {
62     super(folder, in, msgnum);
63     setFrame (size);
64   }
65
66   /**
67    * Create a POP3Message.
68    * This is called by the POP3Store.
69    *
70    * <p>The constructor is smart about what data it retrieves. If the stream
71    * contains no data then the object is left in a state where the headers and
72    * the content must be read. If the constructor manages to read the headers
73    * it will acknowledge their receipt in the internal state of the object,
74    * the content will be read in if the user causes it to be necessary.
75    * Finally, the constructor may see the whole content on the stream in which
76    * case it reads in everything.</p>
77    */

78   POP3Message(POP3Folder folder, int msgnum, int size)
79     throws MessagingException JavaDoc
80   {
81     super (folder, msgnum);
82     setFrame (size);
83   }
84
85   /**
86    * Create a POP3Message.
87    * This is called by the POP3Store.
88    *
89    * <p>The constructor is smart about what data it retrieves. If the stream
90    * contains no data then the object is left in a state where the headers and
91    * the content must be read. If the constructor manages to read the headers
92    * it will acknowledge their receipt in the internal state of the object,
93    * the content will be read in if the user causes it to be necessary.
94    * Finally, the constructor may see the whole content on the stream in which
95    * case it reads in everything.</p>
96    */

97   POP3Message(POP3Folder folder, InputStream JavaDoc in, int msgnum, int size)
98     throws MessagingException JavaDoc
99   {
100     this (folder, in, msgnum);
101     setFrame (size);
102   }
103   
104   private void setFrame (int size)
105   {
106     this.size = size;
107     POP3Headers h = (POP3Headers) headers;
108     if (headers!=null && h.isEmpty())
109       headers = null;
110     if (content!=null && content.length==0)
111       content = null;
112   }
113
114   // -- Content --
115

116   /**
117    * Retrieves the content of the message.
118    * This uses the POP3Store to do the retrieval.
119    */

120   void fetchContent()
121     throws MessagingException JavaDoc
122   {
123     // we can call in.available since it returns
124
POP3Store str = (POP3Store)folder.getStore();
125     parse(str.popRETR(msgnum));
126   }
127
128   /**
129    * Causes the content to be read in.
130    */

131   public DataHandler JavaDoc getDataHandler()
132     throws MessagingException JavaDoc
133   {
134     if (content==null)
135       fetchContent();
136     return super.getDataHandler();
137   }
138
139   /**
140    * Causes the content to be read in.
141    */

142   protected InputStream JavaDoc getContentStream()
143     throws MessagingException JavaDoc
144   {
145     if (content==null)
146       fetchContent();
147     return super.getContentStream();
148   }
149
150   /**
151    * Gets the size of the message.
152    * Uses the cached size if it's available to us.
153    */

154   public int getSize()
155     throws MessagingException JavaDoc
156   {
157     if (size>-1)
158       return size;
159     if (content==null)
160       fetchContent();
161     return super.getSize();
162   }
163
164   // -- Headers --
165

166   /**
167    * Causes the headers to be read.
168    */

169   void fetchHeaders()
170     throws MessagingException JavaDoc
171   {
172     POP3Store str = (POP3Store)folder.getStore();
173     headers = createInternetHeaders(str.popTOP(msgnum));
174   }
175
176   /**
177    * Causes the headers to be read.
178    */

179   public String JavaDoc[] getHeader(String JavaDoc name)
180     throws MessagingException JavaDoc
181   {
182     if (headers==null)
183       fetchHeaders();
184     return super.getHeader(name);
185   }
186   
187   /**
188    * Causes the headers to be read.
189    */

190   public String JavaDoc getHeader(String JavaDoc name, String JavaDoc delimiter)
191     throws MessagingException JavaDoc
192   {
193     if (headers==null)
194       fetchHeaders();
195     return super.getHeader(name, delimiter);
196   }
197
198   /**
199    * Causes the headers to be read.
200    */

201   public Enumeration JavaDoc getAllHeaders()
202     throws MessagingException JavaDoc
203   {
204     if (headers==null)
205       fetchHeaders();
206     return super.getAllHeaders();
207   }
208
209   /**
210    * Causes the headers to be read.
211    */

212   public Enumeration JavaDoc getAllHeaderLines()
213     throws MessagingException JavaDoc
214   {
215     if (headers==null)
216       fetchHeaders();
217     return super.getAllHeaderLines();
218   }
219
220   /**
221    * Causes the headers to be read.
222    */

223   public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] names)
224     throws MessagingException JavaDoc
225   {
226     if (headers==null)
227       fetchHeaders();
228     return super.getMatchingHeaders(names);
229   }
230
231   /**
232    * Causes the headers to be read.
233    */

234   public Enumeration JavaDoc getMatchingHeaderLines(String JavaDoc[] names)
235     throws MessagingException JavaDoc
236   {
237     if (headers==null)
238       fetchHeaders();
239     return super.getMatchingHeaderLines(names);
240   }
241
242   /**
243    * Causes the headers to be read.
244    */

245   public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] names)
246     throws MessagingException JavaDoc
247   {
248     if (headers==null)
249       fetchHeaders();
250     return super.getNonMatchingHeaders(names);
251   }
252
253   /**
254    * Causes the headers to be read.
255    */

256   public Enumeration JavaDoc getNonMatchingHeaderLines(String JavaDoc[] names)
257     throws MessagingException JavaDoc
258   {
259     if (headers==null)
260       fetchHeaders();
261     return super.getNonMatchingHeaderLines(names);
262   }
263
264   protected InternetHeaders JavaDoc createInternetHeaders(InputStream JavaDoc is)
265     throws MessagingException JavaDoc
266   {
267     return new POP3Headers(is);
268   }
269
270   // -- Utility --
271

272   public void writeTo(OutputStream JavaDoc msgStream)
273     throws IOException JavaDoc, MessagingException JavaDoc
274   {
275     if (content==null)
276       fetchContent();
277     super.writeTo(msgStream);
278   }
279
280   public void writeTo(OutputStream JavaDoc msgStream, String JavaDoc[] ignoreList)
281     throws IOException JavaDoc, MessagingException JavaDoc
282   {
283     if (content==null)
284       fetchContent();
285     super.writeTo(msgStream, ignoreList);
286   }
287
288 }
289
Popular Tags