KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > nntp > NNTPMessage


1 /*
2  * NNTPMessage.java
3  * Copyright (C) 2002 dog <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.nntp;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 import javax.mail.FetchProfile JavaDoc;
30 import javax.mail.Flags JavaDoc;
31 import javax.mail.MessagingException JavaDoc;
32 import javax.mail.internet.InternetHeaders JavaDoc;
33 import javax.mail.internet.MimeMessage JavaDoc;
34
35 /**
36  * A JavaMail MIME message delegate for an NNTP article.
37  *
38  * @author <a HREF='mailto:dog@gnu.org'>Chris Burdess</a>
39  * @version 2.0
40  */

41 public final class NNTPMessage
42   extends MimeMessage JavaDoc
43 {
44
45   String JavaDoc messageId;
46   
47   NNTPMessage(NNTPFolder folder,
48       int msgnum,
49       String JavaDoc messageId)
50   {
51     super(folder, msgnum);
52     this.messageId = messageId;
53     headers = null;
54     // Set SEEN state
55
flags = folder.getPermanentFlags();
56     if (folder.isSeen(msgnum))
57       flags.add(Flags.Flag.SEEN);
58   }
59
60   public String JavaDoc getMessageId()
61   {
62     return messageId;
63   }
64
65   void requestHeaders()
66     throws MessagingException JavaDoc
67   {
68     FetchProfile JavaDoc fp = new FetchProfile JavaDoc();
69     fp.add(FetchProfile.Item.ENVELOPE);
70     NNTPMessage[] messages = new NNTPMessage[1];
71     messages[0] = this;
72     folder.fetch(messages, fp);
73   }
74
75   /*
76    * Called by NNTPFolder
77    */

78   void updateHeaders(InputStream JavaDoc in)
79     throws MessagingException JavaDoc, IOException JavaDoc
80   {
81     headers = new InternetHeaders JavaDoc(in);
82   }
83
84   void requestContent()
85     throws MessagingException JavaDoc
86   {
87     FetchProfile JavaDoc fp = new FetchProfile JavaDoc();
88     fp.add(FetchProfile.Item.CONTENT_INFO);
89     NNTPMessage[] messages = new NNTPMessage[1];
90     messages[0] = this;
91     folder.fetch(messages, fp);
92   }
93
94   /*
95    * Called by NNTPFolder
96    */

97   void updateContent(byte[] content)
98   {
99     this.content = content;
100   }
101
102   // -- Header retrieval --
103

104   public String JavaDoc[] getHeader(String JavaDoc name)
105     throws MessagingException JavaDoc
106   {
107     if (headers==null)
108       requestHeaders();
109     return super.getHeader(name);
110   }
111   
112   public String JavaDoc getHeader(String JavaDoc name, String JavaDoc delimiter)
113     throws MessagingException JavaDoc
114   {
115     if (headers==null)
116       requestHeaders();
117     return super.getHeader(name, delimiter);
118   }
119   
120   public Enumeration JavaDoc getAllHeaders()
121     throws MessagingException JavaDoc
122   {
123     if (headers==null)
124       requestHeaders();
125     return super.getAllHeaders();
126   }
127   
128   public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] names)
129     throws MessagingException JavaDoc
130   {
131     if (headers==null)
132       requestHeaders();
133     return super.getMatchingHeaders(names);
134   }
135   
136   public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] names)
137     throws MessagingException JavaDoc
138   {
139     if (headers==null)
140       requestHeaders();
141     return super.getNonMatchingHeaders(names);
142   }
143   
144   public Enumeration JavaDoc getAllHeaderLines()
145     throws MessagingException JavaDoc
146   {
147     if (headers==null)
148       requestHeaders();
149     return super.getAllHeaderLines();
150   }
151   
152   public Enumeration JavaDoc getMatchingHeaderLines(String JavaDoc[] names)
153     throws MessagingException JavaDoc
154   {
155     if (headers==null)
156       requestHeaders();
157     return super.getMatchingHeaderLines(names);
158   }
159   
160   public Enumeration JavaDoc getNonMatchingHeaderLines(String JavaDoc[] names)
161     throws MessagingException JavaDoc
162   {
163     if (headers==null)
164       requestHeaders();
165     return super.getNonMatchingHeaderLines(names);
166   }
167   
168   
169   // setHeader / addHeader / removeHeader
170

171   // -- Content retrieval --
172

173   public int getSize()
174     throws MessagingException JavaDoc
175   {
176     if (content==null)
177       requestContent();
178     return super.getSize();
179   }
180
181   public int getLineCount()
182     throws MessagingException JavaDoc
183   {
184     String JavaDoc value = getHeader("Lines", ",");
185     if (value!=null)
186     {
187       try
188       {
189         return Integer.parseInt(value.trim());
190       }
191       catch (NumberFormatException JavaDoc e)
192       {
193       }
194     }
195     return -1;
196   }
197
198   public InputStream JavaDoc getContentStream()
199     throws MessagingException JavaDoc
200   {
201     if (content==null)
202       requestContent();
203     return super.getContentStream();
204   }
205
206   // setContent(Object o, tring type), setContent(Multpart)
207

208   public void saveChanges()
209     throws MessagingException JavaDoc
210   {
211     if (headers==null)
212       requestHeaders();
213     if (content==null)
214       requestContent();
215   }
216
217   // -- Update SEEN flag if necessary --
218

219   public void setFlags(Flags JavaDoc flag, boolean set)
220     throws MessagingException JavaDoc
221   {
222     if (flag.contains(Flags.Flag.SEEN))
223       ((NNTPFolder)folder).setSeen(msgnum, set);
224     super.setFlags(flag, set);
225   }
226   
227 }
228
Popular Tags