KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > IMAPBodyPart


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)IMAPBodyPart.java 1.17 06/03/24
24  *
25  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.imap;
29
30 import java.io.*;
31
32 import java.util.Enumeration JavaDoc;
33 import javax.mail.*;
34 import javax.mail.internet.*;
35 import javax.activation.*;
36
37 import com.sun.mail.util.*;
38 import com.sun.mail.iap.*;
39 import com.sun.mail.imap.protocol.*;
40
41 /**
42  * This class
43  *
44  * @version 1.4, 97/12/09
45  * @author John Mani
46  */

47
48 public class IMAPBodyPart extends MimeBodyPart {
49     private IMAPMessage message;
50     private BODYSTRUCTURE bs;
51     private String JavaDoc sectionId;
52
53     // processed values ..
54
private String JavaDoc type;
55     private String JavaDoc description;
56
57     private boolean headersLoaded = false;
58
59     protected IMAPBodyPart(BODYSTRUCTURE bs, String JavaDoc sid, IMAPMessage message) {
60     super();
61     this.bs = bs;
62     this.sectionId = sid;
63     this.message = message;
64     // generate content-type
65
ContentType ct = new ContentType(bs.type, bs.subtype, bs.cParams);
66     type = ct.toString();
67     }
68
69     /* Override this method to make it a no-op, rather than throw
70      * an IllegalWriteException. This will permit IMAPBodyParts to
71      * be inserted in newly crafted MimeMessages, especially when
72      * forwarding or replying to messages.
73      */

74     protected void updateHeaders() {
75     return;
76     }
77
78     public int getSize() throws MessagingException {
79     return bs.size;
80     }
81
82     public int getLineCount() throws MessagingException {
83     return bs.lines;
84     }
85
86     public String JavaDoc getContentType() throws MessagingException {
87     return type;
88     }
89
90     public String JavaDoc getDisposition() throws MessagingException {
91     return bs.disposition;
92     }
93
94     public void setDisposition(String JavaDoc disposition) throws MessagingException {
95     throw new IllegalWriteException("IMAPBodyPart is read-only");
96     }
97
98     public String JavaDoc getEncoding() throws MessagingException {
99     return bs.encoding;
100     }
101
102     public String JavaDoc getContentID() throws MessagingException {
103     return bs.id;
104     }
105
106     public String JavaDoc getContentMD5() throws MessagingException {
107     return bs.md5;
108     }
109
110     public void setContentMD5(String JavaDoc md5) throws MessagingException {
111     throw new IllegalWriteException("IMAPBodyPart is read-only");
112     }
113
114     public String JavaDoc getDescription() throws MessagingException {
115     if (description != null) // cached value ?
116
return description;
117
118     if (bs.description == null)
119         return null;
120     
121     try {
122         description = MimeUtility.decodeText(bs.description);
123     } catch (UnsupportedEncodingException ex) {
124         description = bs.description;
125     }
126
127     return description;
128     }
129
130     public void setDescription(String JavaDoc description, String JavaDoc charset)
131             throws MessagingException {
132     throw new IllegalWriteException("IMAPBodyPart is read-only");
133     }
134
135     public String JavaDoc getFileName() throws MessagingException {
136     String JavaDoc filename = null;
137     if (bs.dParams != null)
138         filename = bs.dParams.get("filename");
139     if (filename == null && bs.cParams != null)
140         filename = bs.cParams.get("name");
141     return filename;
142     }
143
144     public void setFileName(String JavaDoc filename) throws MessagingException {
145     throw new IllegalWriteException("IMAPBodyPart is read-only");
146     }
147
148     protected InputStream getContentStream() throws MessagingException {
149     InputStream is = null;
150     boolean pk = message.getPeek(); // acquire outisde of message cache lock
151

152         // Acquire MessageCacheLock, to freeze seqnum.
153
synchronized(message.getMessageCacheLock()) {
154         IMAPProtocol p = message.getProtocol();
155         if (p.isREV1() && (message.getFetchBlockSize() != -1))
156         return new IMAPInputStream(message, sectionId, bs.size, pk);
157
158         // Else, vanila IMAP4, no partial fetch
159

160         // Check whether this message is expunged
161
message.checkExpunged();
162
163         int seqnum = message.getSequenceNumber();
164         try {
165         BODY b;
166         if (message.getPeek())
167             b = p.peekBody(seqnum, sectionId);
168         else
169             b = p.fetchBody(seqnum, sectionId);
170         if (b != null)
171             is = b.getByteArrayInputStream();
172         } catch (ConnectionException cex) {
173         throw new FolderClosedException(
174             message.getFolder(), cex.getMessage());
175         } catch (ProtocolException pex) {
176         throw new MessagingException(pex.getMessage(), pex);
177         }
178     }
179
180     if (is == null)
181         throw new MessagingException("No content");
182     else
183         return is;
184     }
185         
186     public synchronized DataHandler getDataHandler()
187         throws MessagingException {
188     if (dh == null) {
189        if (bs.isMulti())
190         dh = new DataHandler(
191             new IMAPMultipartDataSource(
192                 this, bs.bodies, sectionId, message)
193              );
194         else if (bs.isNested() && message.getProtocol().isREV1())
195         dh = new DataHandler(
196             new IMAPNestedMessage(message,
197                           bs.bodies[0],
198                           bs.envelope,
199                           sectionId),
200             type
201              );
202     }
203
204     return super.getDataHandler();
205     }
206
207     public void setDataHandler(DataHandler content) throws MessagingException {
208     throw new IllegalWriteException("IMAPBodyPart is read-only");
209     }
210
211     public void setContent(Object JavaDoc o, String JavaDoc type) throws MessagingException {
212     throw new IllegalWriteException("IMAPBodyPart is read-only");
213     }
214
215     public void setContent(Multipart mp) throws MessagingException {
216     throw new IllegalWriteException("IMAPBodyPart is read-only");
217     }
218
219     public String JavaDoc[] getHeader(String JavaDoc name) throws MessagingException {
220     loadHeaders();
221     return super.getHeader(name);
222     }
223
224     public void setHeader(String JavaDoc name, String JavaDoc value)
225         throws MessagingException {
226     throw new IllegalWriteException("IMAPBodyPart is read-only");
227     }
228
229     public void addHeader(String JavaDoc name, String JavaDoc value)
230         throws MessagingException {
231     throw new IllegalWriteException("IMAPBodyPart is read-only");
232     }
233
234     public void removeHeader(String JavaDoc name) throws MessagingException {
235     throw new IllegalWriteException("IMAPBodyPart is read-only");
236     }
237
238     public Enumeration JavaDoc getAllHeaders() throws MessagingException {
239     loadHeaders();
240     return super.getAllHeaders();
241     }
242
243     public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] names)
244         throws MessagingException {
245     loadHeaders();
246     return super.getMatchingHeaders(names);
247     }
248
249     public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] names)
250         throws MessagingException {
251     loadHeaders();
252     return super.getNonMatchingHeaders(names);
253     }
254
255     public void addHeaderLine(String JavaDoc line) throws MessagingException {
256     throw new IllegalWriteException("IMAPBodyPart is read-only");
257     }
258
259     public Enumeration JavaDoc getAllHeaderLines() throws MessagingException {
260     loadHeaders();
261     return super.getAllHeaderLines();
262     }
263
264     public Enumeration JavaDoc getMatchingHeaderLines(String JavaDoc[] names)
265         throws MessagingException {
266     loadHeaders();
267     return super.getMatchingHeaderLines(names);
268     }
269
270     public Enumeration JavaDoc getNonMatchingHeaderLines(String JavaDoc[] names)
271         throws MessagingException {
272     loadHeaders();
273     return super.getNonMatchingHeaderLines(names);
274     }
275
276     private synchronized void loadHeaders() throws MessagingException {
277     if (headersLoaded)
278         return;
279
280     if (headers == null)
281         headers = new InternetHeaders();
282
283     // load headers
284
IMAPProtocol p = message.getProtocol();
285     if (p.isREV1()) {
286         BODY b = null;
287
288         // Acquire MessageCacheLock, to freeze seqnum.
289
synchronized(message.getMessageCacheLock()) {
290         // fetch again, in the unlikely event folder has been closed
291
p = message.getProtocol();
292
293         // Check whether this message got expunged
294
message.checkExpunged();
295
296         int seqnum = message.getSequenceNumber();
297         try {
298             b = p.peekBody(seqnum, sectionId + ".MIME");
299         } catch (ConnectionException cex) {
300             throw new FolderClosedException(
301                 message.getFolder(), cex.getMessage());
302         } catch (ProtocolException pex) {
303             throw new MessagingException(pex.getMessage(), pex);
304         }
305         }
306
307         if (b == null)
308         throw new MessagingException("Failed to fetch headers");
309
310         ByteArrayInputStream bis = b.getByteArrayInputStream();
311         if (bis == null)
312         throw new MessagingException("Failed to fetch headers");
313
314         headers.load(bis);
315
316     } else {
317
318         // RFC 1730 does not provide for fetching BodyPart headers
319
// So, just dump the RFC1730 BODYSTRUCTURE into the headerStore
320

321         // Content-Type
322
headers.addHeader("Content-Type", type);
323         // Content-Transfer-Encoding
324
headers.addHeader("Content-Transfer-Encoding", bs.encoding);
325         // Content-Description
326
if (bs.description != null)
327         headers.addHeader("Content-Description", bs.description);
328         // Content-ID
329
if (bs.id != null)
330         headers.addHeader("Content-ID", bs.id);
331         // Content-MD5
332
if (bs.md5 != null)
333         headers.addHeader("Content-MD5", bs.md5);
334     }
335     headersLoaded = true;
336     }
337 }
338
Popular Tags