KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > mbox > MboxMessage


1 /*
2  * MboxMessage.java
3  * Copyright (C) 1999 dog <dog@dog.net.uk>
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.mbox;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.activation.DataHandler JavaDoc;
28 import javax.mail.*;
29 import javax.mail.internet.*;
30
31 /**
32  * The message class implementing the Mbox mail protocol.
33  *
34  * @author dog@dog.net.uk
35  * @version 2.0
36  */

37 public class MboxMessage
38   extends MimeMessage
39 {
40
41   /**
42    * Status header key.
43    * This keeps the mbox flags.
44    */

45   protected static final String JavaDoc STATUS = "Status";
46   
47   /**
48    * The From_ line associated with this message.
49    * We will preserve this if possible.
50    */

51   protected String JavaDoc fromLine;
52
53   /**
54    * Creates a Mbox message.
55    * This is called by the MboxStore.
56    */

57   protected MboxMessage(MboxFolder folder,
58       String JavaDoc fromLine,
59       InputStream in,
60       int msgnum)
61     throws MessagingException
62   {
63     super(folder, in, msgnum);
64     this.fromLine = fromLine;
65     readStatusHeader();
66   }
67
68   /**
69    * Creates a Mbox message.
70    * This is called by the MboxFolder when appending.
71    * It creates a copy of the specified message for the new folder.
72    */

73   protected MboxMessage(MboxFolder folder,
74       MimeMessage message,
75       int msgnum)
76     throws MessagingException
77   {
78     super(message);
79     this.folder = folder;
80     this.msgnum = msgnum;
81     readStatusHeader();
82   }
83
84   /**
85    * Allow MboxFolder access to set the expunged flag after expunge.
86    */

87   protected void setExpunged(boolean expunged)
88   {
89     super.setExpunged(expunged);
90   }
91     
92   /**
93    * Mbox messages are read-only.
94    */

95   public void setFrom(Address address)
96     throws MessagingException
97   {
98     throw new IllegalWriteException("MboxMessage is read-only");
99   }
100
101   /**
102    * Mbox messages are read-only.
103    */

104   public void addFrom(Address[] address)
105     throws MessagingException
106   {
107     throw new IllegalWriteException("MboxMessage is read-only");
108   }
109
110   /**
111    * Mbox messages are read-only.
112    */

113   public void setRecipients(Message.RecipientType recipienttype,
114       Address[] addresses)
115     throws MessagingException
116   {
117     throw new IllegalWriteException("MboxMessage is read-only");
118   }
119
120   /**
121    * Mbox messages are read-only.
122    */

123   public void addRecipients(Message.RecipientType recipienttype,
124       Address[] addresses)
125     throws MessagingException
126   {
127     throw new IllegalWriteException("MboxMessage is read-only");
128   }
129
130   /**
131    * Mbox messages are read-only.
132    */

133   public void setReplyTo(Address[] addresses)
134     throws MessagingException
135   {
136     throw new IllegalWriteException("MboxMessage is read-only");
137   }
138
139   /**
140    * Mbox messages are read-only.
141    */

142   public void setSubject(String JavaDoc subject, String JavaDoc charset)
143     throws MessagingException
144   {
145     throw new IllegalWriteException("MboxMessage is read-only");
146   }
147
148   /**
149    * Mbox messages are read-only.
150    */

151   public void setSentDate(Date date)
152     throws MessagingException
153   {
154     throw new IllegalWriteException("MboxMessage is read-only");
155   }
156
157   /**
158    * Mbox messages are read-only.
159    */

160   public void setDisposition(String JavaDoc disposition)
161     throws MessagingException
162   {
163     throw new IllegalWriteException("MboxMessage is read-only");
164   }
165
166   /**
167    * Mbox messages are read-only.
168    */

169   public void setContentID(String JavaDoc cid)
170     throws MessagingException
171   {
172     throw new IllegalWriteException("MboxMessage is read-only");
173   }
174
175   /**
176    * Mbox messages are read-only.
177    */

178   public void setContentMD5(String JavaDoc md5)
179     throws MessagingException
180   {
181     throw new IllegalWriteException("MboxMessage is read-only");
182   }
183
184   /**
185    * Mbox messages are read-only.
186    */

187   public void setDescription(String JavaDoc description, String JavaDoc charset)
188     throws MessagingException
189   {
190     throw new IllegalWriteException("MboxMessage is read-only");
191   }
192
193   /**
194    * Mbox messages are read-only.
195    */

196   public void setDataHandler(DataHandler JavaDoc datahandler)
197     throws MessagingException
198   {
199     throw new IllegalWriteException("MboxMessage is read-only");
200   }
201
202   /**
203    * Ok, Mbox messages aren't entirely read-only.
204    */

205   public synchronized void setFlags(Flags flag, boolean set)
206     throws MessagingException
207   {
208     if (set)
209       flags.add(flag);
210     else
211       flags.remove(flag);
212   }
213     
214   /**
215    * Updates the status header from the current flags.
216    */

217   protected void updateHeaders()
218     throws MessagingException
219   {
220     super.updateHeaders();
221
222     String JavaDoc old = getHeader(STATUS, "\n");
223     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
224     boolean seen = flags.contains(Flags.Flag.SEEN);
225     boolean recent = flags.contains(Flags.Flag.RECENT);
226     boolean answered = flags.contains(Flags.Flag.ANSWERED);
227     boolean deleted = flags.contains(Flags.Flag.DELETED);
228     if (seen)
229       buffer.append('R');
230     if (!recent)
231       buffer.append('O');
232     if (answered)
233       buffer.append('A');
234     if (deleted)
235       buffer.append('D');
236     String JavaDoc status = buffer.toString();
237     if (!(status.equals(old)))
238       setHeader(STATUS, status);
239   }
240
241
242   /**
243    * Reads the associated flags from the status header.
244    */

245   private void readStatusHeader()
246     throws MessagingException
247   {
248     String JavaDoc[] currentStatus = this.getHeader(STATUS);
249     if (currentStatus != null && currentStatus.length > 0)
250     {
251       flags = new Flags();
252       if (currentStatus[0].indexOf('R') >= 0)
253         flags.add(Flags.Flag.SEEN);
254       if (currentStatus[0].indexOf('O') < 0)
255         flags.add(Flags.Flag.RECENT);
256       if (currentStatus[0].indexOf('A') >= 0)
257         flags.add(Flags.Flag.ANSWERED);
258       if (currentStatus[0].indexOf('D') >= 0)
259         flags.add(Flags.Flag.DELETED);
260     }
261   }
262
263   // -- Utility methods --
264

265   public boolean equals(Object JavaDoc other)
266   {
267     if (other instanceof MimeMessage)
268     {
269       MimeMessage message = (MimeMessage)other;
270       return (message.getFolder()==getFolder() &&
271           message.getMessageNumber()==getMessageNumber());
272     }
273     return false;
274   }
275
276 }
277
Popular Tags