1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.geronimo.javamail.store.imap;
19
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.util.Enumeration;
23
24 import javax.activation.DataHandler;
25
26 import javax.mail.IllegalWriteException;
27 import javax.mail.MessagingException;
28 import javax.mail.Multipart;
29
30 import javax.mail.internet.MimeBodyPart;
31 import javax.mail.internet.MimeUtility;
32
33 import org.apache.geronimo.javamail.store.imap.connection.IMAPBodyStructure;
34 import org.apache.geronimo.javamail.store.imap.connection.IMAPConnection;
35
36
37 public class IMAPMimeBodyPart extends MimeBodyPart {
38
39 protected IMAPMessage message;
40
41 protected IMAPBodyStructure bodyStructure;
42
43
44
45 protected String section;
46
47 boolean headersLoaded = false;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public IMAPMimeBodyPart(IMAPMessage message, IMAPBodyStructure bodyStructure, String section) {
64 super();
65 this.message = message;
66 this.bodyStructure = bodyStructure;
67 this.section = section;
68 }
69
70
71
72
73
74
75
76
77 public int getSize() throws MessagingException {
78 return bodyStructure.bodySize;
79 }
80
81
82
83
84
85
86
87
88 public int getLineCount() throws MessagingException {
89 return bodyStructure.lines;
90 }
91
92
93
94
95
96
97
98 public String getContentType() throws MessagingException {
99 return bodyStructure.mimeType.toString();
100 }
101
102
103
104
105
106
107
108
109
110
111 public boolean isMimeType(String type) throws MessagingException {
112 return bodyStructure.mimeType.match(type);
113 }
114
115
116
117
118
119
120
121
122 public String getDisposition() throws MessagingException {
123 return bodyStructure.disposition.getDisposition();
124 }
125
126
127
128
129
130
131
132
133
134
135 public void setDisposition(String disposition) throws MessagingException {
136 throw new IllegalWriteException("IMAP message parts are read-only");
137 }
138
139 public String getEncoding() throws MessagingException {
140 return bodyStructure.transferEncoding;
141 }
142
143 public String getContentID() throws MessagingException {
144 return bodyStructure.contentID;
145 }
146
147 public void setContentID(String id) throws MessagingException {
148 throw new IllegalWriteException("IMAP message parts are read-only");
149 }
150
151 public String getContentMD5() throws MessagingException {
152 return bodyStructure.md5Hash;
153 }
154
155 public void setContentMD5(String id) throws MessagingException {
156 throw new IllegalWriteException("IMAP message parts are read-only");
157 }
158
159 public String getDescription() throws MessagingException {
160 String description = bodyStructure.contentDescription;
161 if (description != null) {
162 try {
163
164 return MimeUtility.decodeText(MimeUtility.unfold(description));
165 } catch (UnsupportedEncodingException e) {
166
167 }
168 }
169
170 return description;
171 }
172
173 public void setDescription(String d, String charset) throws MessagingException {
174 throw new IllegalWriteException("IMAP message parts are read-only");
175 }
176
177 public String getFileName() throws MessagingException {
178 String filename = bodyStructure.disposition.getParameter("filename");
179 if (filename == null) {
180 filename = bodyStructure.mimeType.getParameter("name");
181 }
182 return filename;
183 }
184
185 public void setFileName(String name) throws MessagingException {
186 throw new IllegalWriteException("IMAP message parts are read-only");
187 }
188
189 protected InputStream getContentStream() throws MessagingException {
190
191
192 if (content == null) {
193
194 message.checkValidity();
195
196 loadContent();
197 }
198
199
200 return super.getContentStream();
201 }
202
203
204
205
206
207
208
209
210
211 public synchronized DataHandler getDataHandler() throws MessagingException {
212 if (dh == null) {
213
214 if (bodyStructure.isMultipart()) {
215 dh = new DataHandler(new IMAPMultipartDataSource(message, this, section, bodyStructure));
216 return dh;
217 }
218 else if (bodyStructure.isAttachedMessage()) {
219 dh = new DataHandler(new IMAPAttachedMessage(message, section, bodyStructure.nestedEnvelope,
220 bodyStructure.nestedBody), bodyStructure.mimeType.toString());
221 return dh;
222 }
223 }
224
225
226 return super.getDataHandler();
227 }
228
229 public void setDataHandler(DataHandler content) throws MessagingException {
230 throw new IllegalWriteException("IMAP body parts are read-only");
231 }
232
233 public void setContent(Object o, String type) throws MessagingException {
234 throw new IllegalWriteException("IMAP body parts are read-only");
235 }
236
237 public void setContent(Multipart mp) throws MessagingException {
238 throw new IllegalWriteException("IMAP body parts are read-only");
239 }
240
241
242
243
244
245
246
247
248 public String[] getHeader(String name) throws MessagingException {
249 loadHeaders();
250 return headers.getHeader(name);
251 }
252
253 public String getHeader(String name, String delimiter) throws MessagingException {
254 loadHeaders();
255 return headers.getHeader(name, delimiter);
256 }
257
258 public Enumeration getAllHeaders() throws MessagingException {
259 loadHeaders();
260 return headers.getAllHeaders();
261 }
262
263 public Enumeration getMatchingHeaders(String[] names) throws MessagingException {
264 loadHeaders();
265 return headers.getMatchingHeaders(names);
266 }
267
268 public Enumeration getNonMatchingHeaders(String[] names) throws MessagingException {
269 loadHeaders();
270 return headers.getNonMatchingHeaders(names);
271 }
272
273 public Enumeration getAllHeaderLines() throws MessagingException {
274 loadHeaders();
275 return headers.getAllHeaderLines();
276 }
277
278 public Enumeration getMatchingHeaderLines(String[] names) throws MessagingException {
279 loadHeaders();
280 return headers.getMatchingHeaderLines(names);
281 }
282
283 public Enumeration getNonMatchingHeaderLines(String[] names) throws MessagingException {
284 loadHeaders();
285 return headers.getNonMatchingHeaderLines(names);
286 }
287
288
289
290 public void addHeader(String name, String value) throws MessagingException {
291 throw new IllegalWriteException("IMAP messages are read-only");
292 }
293
294 public void setHeader(String name, String value) throws MessagingException {
295 throw new IllegalWriteException("IMAP messages are read-only");
296 }
297
298
299 public void removeHeader(String name) throws MessagingException {
300 throw new IllegalWriteException("IMAP messages are read-only");
301 }
302
303 public void addHeaderLine(String line) throws MessagingException {
304 throw new IllegalWriteException("IMAP messages are read-only");
305 }
306
307
308
309
310
311
312
313 protected synchronized void loadHeaders() throws MessagingException {
314
315 if (headers != null) {
316 return;
317 }
318
319 IMAPConnection connection = message.getConnection();
320 try {
321
322 headers = connection.fetchHeaders(message.getSequenceNumber(), section);
323 } finally {
324 message.releaseConnection(connection);
325 }
326
327 }
328
329
330
331
332
333
334
335 protected void loadContent() throws MessagingException {
336
337 if (content != null) {
338 return;
339 }
340
341 IMAPConnection connection = message.getConnection();
342 try {
343
344 content = connection.fetchContent(message.getSequenceNumber(), section);
345 } finally {
346 message.releaseConnection(connection);
347 }
348 }
349 }
350