KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > core > MimeMessageWrapper


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.core;
19
20 import javax.activation.DataHandler JavaDoc;
21 import javax.mail.Address JavaDoc;
22 import javax.mail.Flags JavaDoc;
23 import javax.mail.Header JavaDoc;
24 import javax.mail.Message JavaDoc;
25 import javax.mail.MessagingException JavaDoc;
26 import javax.mail.Multipart JavaDoc;
27 import javax.mail.Session JavaDoc;
28 import javax.mail.internet.InternetAddress JavaDoc;
29 import javax.mail.internet.InternetHeaders JavaDoc;
30 import javax.mail.internet.MimeMessage JavaDoc;
31 import javax.mail.internet.MimeUtility JavaDoc;
32 import javax.mail.internet.NewsAddress JavaDoc;
33 import java.io.BufferedWriter JavaDoc;
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.LineNumberReader JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.io.OutputStreamWriter JavaDoc;
41 import java.io.PrintWriter JavaDoc;
42 import java.io.SequenceInputStream JavaDoc;
43 import java.io.UnsupportedEncodingException JavaDoc;
44 import java.text.ParseException JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.Enumeration JavaDoc;
48
49 import org.apache.james.util.InternetPrintWriter;
50 import org.apache.james.util.RFC2822Headers;
51 import org.apache.james.util.RFC822DateFormat;
52
53 import org.apache.avalon.framework.activity.Disposable;
54 import org.apache.avalon.excalibur.io.IOUtil;
55
56 /**
57  * This object wraps a MimeMessage, only loading the underlying MimeMessage
58  * object when needed. Also tracks if changes were made to reduce
59  * unnecessary saves.
60  */

61 public class MimeMessageWrapper
62     extends MimeMessage JavaDoc
63     implements Disposable {
64
65     /**
66      * Can provide an input stream to the data
67      */

68     MimeMessageSource source = null;
69     /**
70      * The Internet headers in memory
71      */

72     MailHeaders headers = null;
73     /**
74      * The mime message in memory
75      */

76     MimeMessage JavaDoc message = null;
77     /**
78      * Record whether a change was made to this message
79      */

80     boolean modified = false;
81     /**
82      * How to format a mail date
83      */

84     RFC822DateFormat mailDateFormat = new RFC822DateFormat();
85
86     /**
87      * A constructor that instantiates a MimeMessageWrapper based on
88      * a MimeMessageSource
89      *
90      * @param source the MimeMessageSource
91      */

92     public MimeMessageWrapper(MimeMessageSource source) {
93         super(Session.getDefaultInstance(System.getProperties(), null));
94         this.source = source;
95     }
96
97     /**
98      * Returns the source ID of the MimeMessageSource that is supplying this
99      * with data.
100      * @see MimeMessageSource
101      */

102     public String JavaDoc getSourceId() {
103         return source.getSourceId();
104     }
105
106     /**
107      * Load the message headers from the internal source.
108      *
109      * @throws MessagingException if an error is encountered while
110      * loading the headers
111      */

112     private synchronized void loadHeaders() throws MessagingException JavaDoc {
113         if (headers != null) {
114             //Another thread has already loaded these headers
115
return;
116         }
117         try {
118             InputStream JavaDoc in = source.getInputStream();
119             try {
120                 headers = new MailHeaders(in);
121             } finally {
122                 IOUtil.shutdownStream(in);
123             }
124         } catch (IOException JavaDoc ioe) {
125             throw new MessagingException JavaDoc("Unable to parse headers from stream: " + ioe.getMessage(), ioe);
126         }
127     }
128
129     /**
130      * Load the complete MimeMessage from the internal source.
131      *
132      * @throws MessagingException if an error is encountered while
133      * loading the message
134      */

135     private synchronized void loadMessage() throws MessagingException JavaDoc {
136         if (message != null) {
137             //Another thread has already loaded this message
138
return;
139         }
140         InputStream JavaDoc in = null;
141         try {
142             in = source.getInputStream();
143             headers = new MailHeaders(in);
144
145             ByteArrayInputStream JavaDoc headersIn
146                     = new ByteArrayInputStream JavaDoc(headers.toByteArray());
147             in = new SequenceInputStream JavaDoc(headersIn, in);
148
149             message = new MimeMessage JavaDoc(session, in);
150         } catch (IOException JavaDoc ioe) {
151             throw new MessagingException JavaDoc("Unable to parse stream: " + ioe.getMessage(), ioe);
152         } finally {
153             IOUtil.shutdownStream(in);
154         }
155     }
156
157     /**
158      * Internal implementation to get InternetAddress headers
159      */

160     private Address JavaDoc[] getAddressHeader(String JavaDoc name) throws MessagingException JavaDoc {
161         String JavaDoc addr = getHeader(name, ",");
162         if (addr == null) {
163             return null;
164         } else {
165             return InternetAddress.parse(addr);
166         }
167     }
168
169
170     /**
171      * Internal implementation to find headers
172      */

173     private String JavaDoc getHeaderName(Message.RecipientType JavaDoc recipienttype) throws MessagingException JavaDoc {
174         String JavaDoc s;
175         if (recipienttype == Message.RecipientType.TO) {
176             s = RFC2822Headers.TO;
177         } else if (recipienttype == Message.RecipientType.CC) {
178             s = RFC2822Headers.CC;
179         } else if (recipienttype == Message.RecipientType.BCC) {
180             s = RFC2822Headers.BCC;
181         } else if (recipienttype == RecipientType.NEWSGROUPS) {
182             s = "Newsgroups";
183         } else {
184             throw new MessagingException JavaDoc("Invalid Recipient Type");
185         }
186         return s;
187     }
188
189
190     /**
191      * Get whether the message has been modified.
192      *
193      * @return whether the message has been modified
194      */

195     public boolean isModified() {
196         return modified;
197     }
198
199     /**
200      * Rewritten for optimization purposes
201      */

202     public void writeTo(OutputStream JavaDoc os) throws IOException JavaDoc, MessagingException JavaDoc {
203         if (message == null || !isModified()) {
204             // We do not want to instantiate the message... just read from source
205
// and write to this outputstream
206
InputStream JavaDoc in = source.getInputStream();
207             try {
208                 copyStream(in, os);
209             } finally {
210                 IOUtil.shutdownStream(in);
211             }
212         } else {
213             writeTo(os, os);
214         }
215     }
216
217     /**
218      * Rewritten for optimization purposes
219      */

220     public void writeTo(OutputStream JavaDoc os, String JavaDoc[] ignoreList) throws IOException JavaDoc, MessagingException JavaDoc {
221         writeTo(os, os, ignoreList);
222     }
223
224     /**
225      * Write
226      */

227     public void writeTo(OutputStream JavaDoc headerOs, OutputStream JavaDoc bodyOs) throws IOException JavaDoc, MessagingException JavaDoc {
228         writeTo(headerOs, bodyOs, new String JavaDoc[0]);
229     }
230
231     public void writeTo(OutputStream JavaDoc headerOs, OutputStream JavaDoc bodyOs, String JavaDoc[] ignoreList) throws IOException JavaDoc, MessagingException JavaDoc {
232         if (message == null || !isModified()) {
233             //We do not want to instantiate the message... just read from source
234
// and write to this outputstream
235

236             //First handle the headers
237
InputStream JavaDoc in = source.getInputStream();
238             try {
239                 InternetHeaders JavaDoc headers = new InternetHeaders JavaDoc(in);
240                 PrintWriter JavaDoc pos = new InternetPrintWriter(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(headerOs), 512), true);
241                 for (Enumeration JavaDoc e = headers.getNonMatchingHeaderLines(ignoreList); e.hasMoreElements(); ) {
242                     String JavaDoc header = (String JavaDoc)e.nextElement();
243                     pos.println(header);
244                 }
245                 pos.println();
246                 pos.flush();
247                 copyStream(in, bodyOs);
248             } finally {
249                 IOUtil.shutdownStream(in);
250             }
251         } else {
252             writeTo(message, headerOs, bodyOs, ignoreList);
253         }
254     }
255
256     /**
257      * Convenience method to take any MimeMessage and write the headers and body to two
258      * different output streams
259      */

260     public static void writeTo(MimeMessage JavaDoc message, OutputStream JavaDoc headerOs, OutputStream JavaDoc bodyOs) throws IOException JavaDoc, MessagingException JavaDoc {
261         writeTo(message, headerOs, bodyOs, null);
262     }
263
264     /**
265      * Convenience method to take any MimeMessage and write the headers and body to two
266      * different output streams, with an ignore list
267      */

268     public static void writeTo(MimeMessage JavaDoc message, OutputStream JavaDoc headerOs, OutputStream JavaDoc bodyOs, String JavaDoc[] ignoreList) throws IOException JavaDoc, MessagingException JavaDoc {
269         if (message instanceof MimeMessageWrapper) {
270             MimeMessageWrapper wrapper = (MimeMessageWrapper)message;
271             wrapper.writeTo(headerOs, bodyOs, ignoreList);
272         } else {
273             if(message.getMessageID() == null) {
274                 message.saveChanges();
275             }
276
277             //Write the headers (minus ignored ones)
278
Enumeration JavaDoc headers = message.getNonMatchingHeaderLines(ignoreList);
279             PrintWriter JavaDoc hos = new InternetPrintWriter(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(headerOs), 512), true);
280             while (headers.hasMoreElements()) {
281                 hos.println((String JavaDoc)headers.nextElement());
282             }
283             // Print header/data separator
284
hos.println();
285             hos.flush();
286
287             InputStream JavaDoc bis = null;
288             OutputStream JavaDoc bos = null;
289             // Write the body to the output stream
290

291             /*
292             try {
293                 bis = message.getRawInputStream();
294                 bos = bodyOs;
295             } catch(javax.mail.MessagingException me) {
296                 // we may get a "No content" exception
297                 // if that happens, try it the hard way
298
299                 // Why, you ask? In JavaMail v1.3, when you initially
300                 // create a message using MimeMessage APIs, there is no
301                 // raw content available. getInputStream() works, but
302                 // getRawInputStream() throws an exception.
303
304                 bos = MimeUtility.encode(bodyOs, message.getEncoding());
305                 bis = message.getInputStream();
306             }
307             */

308
309             try {
310                 // Get the message as a stream. This will encode
311
// objects as necessary, and we have some input from
312
// decoding an re-encoding the stream. I'd prefer the
313
// raw stream, but see
314
bos = MimeUtility.encode(bodyOs, message.getEncoding());
315                 bis = message.getInputStream();
316             } catch(javax.activation.UnsupportedDataTypeException JavaDoc udte) {
317                 /* If we get an UnsupportedDataTypeException try using
318                  * the raw input stream as a "best attempt" at rendering
319                  * a message.
320                  *
321                  * WARNING: JavaMail v1.3 getRawInputStream() returns
322                  * INVALID (unchanged) content for a changed message.
323                  * getInputStream() works properly, but in this case
324                  * has failed due to a missing DataHandler.
325                  *
326                  * MimeMessage.getRawInputStream() may throw a "no
327                  * content" MessagingException. In JavaMail v1.3, when
328                  * you initially create a message using MimeMessage
329                  * APIs, there is no raw content available.
330                  * getInputStream() works, but getRawInputStream()
331                  * throws an exception. If we catch that exception,
332                  * throw the UDTE. It should mean that someone has
333                  * locally constructed a message part for which JavaMail
334                  * doesn't have a DataHandler.
335                 */

336
337                 try {
338                     bis = message.getRawInputStream();
339                     bos = bodyOs;
340                 } catch(javax.mail.MessagingException JavaDoc _) {
341                     throw udte;
342                 }
343             }
344             catch(javax.mail.MessagingException JavaDoc me) {
345                 /* This could be another kind of MessagingException
346                  * thrown by MimeMessage.getInputStream(), such as a
347                  * javax.mail.internet.ParseException.
348                  *
349                  * The ParseException is precisely one of the reasons
350                  * why the getRawInputStream() method exists, so that we
351                  * can continue to stream the content, even if we cannot
352                  * handle it. Again, if we get an exception, we throw
353                  * the one that caused us to call getRawInputStream().
354                  */

355                 try {
356                     bis = message.getRawInputStream();
357                     bos = bodyOs;
358                 } catch(javax.mail.MessagingException JavaDoc _) {
359                     throw me;
360                 }
361             }
362
363             try {
364                 copyStream(bis, bos);
365             }
366             finally {
367                 IOUtil.shutdownStream(bis);
368             }
369         }
370     }
371
372     /**
373      * Various reader methods
374      */

375     public Address JavaDoc[] getFrom() throws MessagingException JavaDoc {
376         if (headers == null) {
377             loadHeaders();
378         }
379         Address JavaDoc from[] = getAddressHeader(RFC2822Headers.FROM);
380         if(from == null) {
381             from = getAddressHeader(RFC2822Headers.SENDER);
382         }
383         return from;
384     }
385
386     public Address JavaDoc[] getRecipients(Message.RecipientType JavaDoc type) throws MessagingException JavaDoc {
387         if (headers == null) {
388             loadHeaders();
389         }
390         if (type == RecipientType.NEWSGROUPS) {
391             String JavaDoc s = headers.getHeader("Newsgroups", ",");
392             if(s == null) {
393                 return null;
394             } else {
395                 return NewsAddress.parse(s);
396             }
397         } else {
398             return getAddressHeader(getHeaderName(type));
399         }
400     }
401
402     public Address JavaDoc[] getAllRecipients() throws MessagingException JavaDoc {
403         if (headers == null) {
404             loadHeaders();
405         }
406         Address JavaDoc toAddresses[] = getRecipients(RecipientType.TO);
407         Address JavaDoc ccAddresses[] = getRecipients(RecipientType.CC);
408         Address JavaDoc bccAddresses[] = getRecipients(RecipientType.BCC);
409         Address JavaDoc newsAddresses[] = getRecipients(RecipientType.NEWSGROUPS);
410         if(ccAddresses == null && bccAddresses == null && newsAddresses == null) {
411             return toAddresses;
412         }
413         int i = (toAddresses == null ? 0 : toAddresses.length)
414                 + (ccAddresses == null ? 0 : ccAddresses.length)
415                 + (bccAddresses == null ? 0 : bccAddresses.length)
416                 + (newsAddresses == null ? 0 : newsAddresses.length);
417         Address JavaDoc allAddresses[] = new Address JavaDoc[i];
418         int j = 0;
419         if (toAddresses != null) {
420             System.arraycopy(toAddresses, 0, allAddresses, j, toAddresses.length);
421             j += toAddresses.length;
422         }
423         if(ccAddresses != null) {
424             System.arraycopy(ccAddresses, 0, allAddresses, j, ccAddresses.length);
425             j += ccAddresses.length;
426         }
427         if(bccAddresses != null) {
428             System.arraycopy(bccAddresses, 0, allAddresses, j, bccAddresses.length);
429             j += bccAddresses.length;
430         }
431         if(newsAddresses != null) {
432             System.arraycopy(newsAddresses, 0, allAddresses, j, newsAddresses.length);
433             j += newsAddresses.length;
434         }
435         return allAddresses;
436     }
437
438     public Address JavaDoc[] getReplyTo() throws MessagingException JavaDoc {
439         if (headers == null) {
440             loadHeaders();
441         }
442         Address JavaDoc replyTo[] = getAddressHeader(RFC2822Headers.REPLY_TO);
443         if(replyTo == null) {
444             replyTo = getFrom();
445         }
446         return replyTo;
447     }
448
449     public String JavaDoc getSubject() throws MessagingException JavaDoc {
450         if (headers == null) {
451             loadHeaders();
452         }
453         String JavaDoc subject = getHeader(RFC2822Headers.SUBJECT, null);
454         if (subject == null) {
455             return null;
456         }
457         try {
458             return MimeUtility.decodeText(subject);
459         } catch(UnsupportedEncodingException JavaDoc _ex) {
460             return subject;
461         }
462     }
463
464     public Date JavaDoc getSentDate() throws MessagingException JavaDoc {
465         if (headers == null) {
466             loadHeaders();
467         }
468         String JavaDoc header = getHeader(RFC2822Headers.DATE, null);
469         if(header != null) {
470             try {
471                 return mailDateFormat.parse(header);
472             } catch(ParseException JavaDoc _ex) {
473                 return null;
474             }
475         } else {
476             return null;
477         }
478     }
479
480     /**
481      * We do not attempt to define the received date, although in theory this is the last
482      * most date in the Received: headers. For now we return null, which means we are
483      * not implementing it.
484      */

485     public Date JavaDoc getReceivedDate() throws MessagingException JavaDoc {
486         if (headers == null) {
487             loadHeaders();
488         }
489         return null;
490     }
491
492     /**
493      * This is the MimeMessage implementation - this should return ONLY the
494      * body, not the entire message (should not count headers). Will have
495      * to parse the message.
496      */

497     public int getSize() throws MessagingException JavaDoc {
498         if (message == null) {
499             loadMessage();
500         }
501         return message.getSize();
502     }
503
504     /**
505      * Corrects JavaMail 1.1 version which always returns -1.
506      * Only corrected for content less than 5000 bytes,
507      * to avoid memory hogging.
508      */

509     public int getLineCount() throws MessagingException JavaDoc {
510             InputStream JavaDoc in=null;
511         try{
512             in = getContentStream();
513         }catch(Exception JavaDoc e){
514             return -1;
515         }
516         if (in == null) {
517             return -1;
518         }
519         //Wrap input stream in LineNumberReader
520
//Not sure what encoding to use really...
521
try {
522             LineNumberReader JavaDoc counter = new LineNumberReader JavaDoc(new InputStreamReader JavaDoc(in, getEncoding()));
523             //Read through all the data
524
char[] block = new char[4096];
525             while (counter.read(block) > -1) {
526                 //Just keep reading
527
}
528             return counter.getLineNumber();
529         } catch (IOException JavaDoc ioe) {
530             return -1;
531         } finally {
532             IOUtil.shutdownStream(in);
533         }
534     }
535
536     /**
537      * Returns size of message, ie headers and content. Current implementation
538      * actually returns number of characters in headers plus number of bytes
539      * in the internal content byte array.
540      */

541     public long getMessageSize() throws MessagingException JavaDoc {
542         try {
543             return source.getMessageSize();
544         } catch (IOException JavaDoc ioe) {
545             throw new MessagingException JavaDoc("Error retrieving message size", ioe);
546         }
547     }
548
549     public String JavaDoc getContentType() throws MessagingException JavaDoc {
550         if (headers == null) {
551             loadHeaders();
552         }
553         String JavaDoc value = getHeader(RFC2822Headers.CONTENT_TYPE, null);
554         if (value == null) {
555             return "text/plain";
556         } else {
557             return value;
558         }
559     }
560
561     public boolean isMimeType(String JavaDoc mimeType) throws MessagingException JavaDoc {
562         if (message == null) {
563             loadMessage();
564         }
565         return message.isMimeType(mimeType);
566     }
567
568     public String JavaDoc getDisposition() throws MessagingException JavaDoc {
569         if (message == null) {
570             loadMessage();
571         }
572         return message.getDisposition();
573     }
574
575     public String JavaDoc getEncoding() throws MessagingException JavaDoc {
576         if (message == null) {
577             loadMessage();
578         }
579         return message.getEncoding();
580     }
581
582     public String JavaDoc getContentID() throws MessagingException JavaDoc {
583         if (headers == null) {
584             loadHeaders();
585         }
586         return getHeader("Content-Id", null);
587     }
588
589     public String JavaDoc getContentMD5() throws MessagingException JavaDoc {
590         if (headers == null) {
591             loadHeaders();
592         }
593         return getHeader("Content-MD5", null);
594     }
595
596     public String JavaDoc getDescription() throws MessagingException JavaDoc {
597         if (message == null) {
598             loadMessage();
599         }
600         return message.getDescription();
601     }
602
603     public String JavaDoc[] getContentLanguage() throws MessagingException JavaDoc {
604         if (message == null) {
605             loadMessage();
606         }
607         return message.getContentLanguage();
608     }
609
610     public String JavaDoc getMessageID() throws MessagingException JavaDoc {
611         if (headers == null) {
612             loadHeaders();
613         }
614         return getHeader(RFC2822Headers.MESSAGE_ID, null);
615     }
616
617     public String JavaDoc getFileName() throws MessagingException JavaDoc {
618         if (message == null) {
619             loadMessage();
620         }
621         return message.getFileName();
622     }
623
624     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, MessagingException JavaDoc {
625         if (message == null) {
626             //This is incorrect... supposed to return a decoded inputstream of
627
// the message body
628
//return source.getInputStream();
629
loadMessage();
630             return message.getInputStream();
631         } else {
632             return message.getInputStream();
633         }
634     }
635
636     public DataHandler JavaDoc getDataHandler() throws MessagingException JavaDoc {
637         if (message == null) {
638             loadMessage();
639         }
640         return message.getDataHandler();
641     }
642
643     public Object JavaDoc getContent() throws IOException JavaDoc, MessagingException JavaDoc {
644         if (message == null) {
645             loadMessage();
646         }
647         return message.getContent();
648     }
649
650     public String JavaDoc[] getHeader(String JavaDoc name) throws MessagingException JavaDoc {
651         if (headers == null) {
652             loadHeaders();
653         }
654         return headers.getHeader(name);
655     }
656
657     public String JavaDoc getHeader(String JavaDoc name, String JavaDoc delimiter) throws MessagingException JavaDoc {
658         if (headers == null) {
659             loadHeaders();
660         }
661         return headers.getHeader(name, delimiter);
662     }
663
664     public Enumeration JavaDoc getAllHeaders() throws MessagingException JavaDoc {
665         if (headers == null) {
666             loadHeaders();
667         }
668         return headers.getAllHeaders();
669     }
670
671     public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] names) throws MessagingException JavaDoc {
672         if (headers == null) {
673             loadHeaders();
674         }
675         return headers.getMatchingHeaders(names);
676     }
677
678     public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] names) throws MessagingException JavaDoc {
679         if (headers == null) {
680             loadHeaders();
681         }
682         return headers.getNonMatchingHeaders(names);
683     }
684
685     public Enumeration JavaDoc getAllHeaderLines() throws MessagingException JavaDoc {
686         if (headers == null) {
687             loadHeaders();
688         }
689         return headers.getAllHeaderLines();
690     }
691
692     public Enumeration JavaDoc getMatchingHeaderLines(String JavaDoc[] names) throws MessagingException JavaDoc {
693         if (headers == null) {
694             loadHeaders();
695         }
696         return headers.getMatchingHeaderLines(names);
697     }
698
699     public Enumeration JavaDoc getNonMatchingHeaderLines(String JavaDoc[] names) throws MessagingException JavaDoc {
700         if (headers == null) {
701             loadHeaders();
702         }
703         return headers.getNonMatchingHeaderLines(names);
704     }
705
706     public Flags JavaDoc getFlags() throws MessagingException JavaDoc {
707         if (message == null) {
708             loadMessage();
709         }
710         return message.getFlags();
711     }
712
713     public boolean isSet(Flags.Flag JavaDoc flag) throws MessagingException JavaDoc {
714         if (message == null) {
715             loadMessage();
716         }
717         return message.isSet(flag);
718     }
719
720
721     /**
722      * Writes content only, ie not headers, to the specified OutputStream.
723      *
724      * @param outs the OutputStream to which the content is written
725      */

726     public void writeContentTo(OutputStream JavaDoc outs)
727             throws java.io.IOException JavaDoc, MessagingException JavaDoc {
728         if (message == null) {
729             loadMessage();
730         }
731         InputStream JavaDoc in = getContentStream();
732         try {
733             copyStream(in, outs);
734         } finally {
735             IOUtil.shutdownStream(in);
736         }
737     }
738
739     /**
740      * Convenience method to copy streams
741      */

742     private static void copyStream(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
743         // TODO: This is really a bad way to do this sort of thing. A shared buffer to
744
// allow simultaneous read/writes would be a substantial improvement
745
byte[] block = new byte[1024];
746         int read = 0;
747         while ((read = in.read(block)) > -1) {
748             out.write(block, 0, read);
749         }
750         out.flush();
751     }
752
753     /*
754      * Various writer methods
755      */

756
757     public void setFrom(Address JavaDoc address) throws MessagingException JavaDoc {
758         if (message == null) {
759             loadMessage();
760         }
761         modified = true;
762         message.setFrom(address);
763     }
764
765     public void setFrom() throws MessagingException JavaDoc {
766         if (message == null) {
767             loadMessage();
768         }
769         modified = true;
770         message.setFrom();
771     }
772
773     public void addFrom(Address JavaDoc[] addresses) throws MessagingException JavaDoc {
774         if (message == null) {
775             loadMessage();
776         }
777         modified = true;
778         message.addFrom(addresses);
779     }
780
781     public void setRecipients(Message.RecipientType JavaDoc type, Address JavaDoc[] addresses) throws MessagingException JavaDoc {
782         if (message == null) {
783             loadMessage();
784         }
785         modified = true;
786         message.setRecipients(type, addresses);
787     }
788
789     public void addRecipients(Message.RecipientType JavaDoc type, Address JavaDoc[] addresses) throws MessagingException JavaDoc {
790         if (message == null) {
791             loadMessage();
792         }
793         modified = true;
794         message.addRecipients(type, addresses);
795     }
796
797     public void setReplyTo(Address JavaDoc[] addresses) throws MessagingException JavaDoc {
798         if (message == null) {
799             loadMessage();
800         }
801         modified = true;
802         message.setReplyTo(addresses);
803     }
804
805     public void setSubject(String JavaDoc subject) throws MessagingException JavaDoc {
806         if (message == null) {
807             loadMessage();
808         }
809         modified = true;
810         headers.setHeader(RFC2822Headers.SUBJECT, subject);
811         message.setSubject(subject);
812     }
813
814     public void setSubject(String JavaDoc subject, String JavaDoc charset) throws MessagingException JavaDoc {
815         if (message == null) {
816             loadMessage();
817         }
818         modified = true;
819         // is this correct?
820
try {
821             headers.setHeader(RFC2822Headers.SUBJECT, new String JavaDoc(subject.getBytes(charset)));
822         }
823         catch (java.io.UnsupportedEncodingException JavaDoc _) { /* TODO */ }
824         message.setSubject(subject, charset);
825     }
826
827     public void setSentDate(Date JavaDoc d) throws MessagingException JavaDoc {
828         if (message == null) {
829             loadMessage();
830         }
831         modified = true;
832         headers.setHeader(RFC2822Headers.DATE, mailDateFormat.format(d));
833         message.setSentDate(d);
834     }
835
836     public void setDisposition(String JavaDoc disposition) throws MessagingException JavaDoc {
837         if (message == null) {
838             loadMessage();
839         }
840         modified = true;
841         message.setDisposition(disposition);
842     }
843
844     public void setContentID(String JavaDoc cid) throws MessagingException JavaDoc {
845         if (message == null) {
846             loadMessage();
847         }
848         modified = true;
849         message.setContentID(cid);
850     }
851
852     public void setContentMD5(String JavaDoc md5) throws MessagingException JavaDoc {
853         if (message == null) {
854             loadMessage();
855         }
856         modified = true;
857         message.setContentMD5(md5);
858     }
859
860     public void setDescription(String JavaDoc description) throws MessagingException JavaDoc {
861         if (message == null) {
862             loadMessage();
863         }
864         modified = true;
865         message.setDescription(description);
866     }
867
868     public void setDescription(String JavaDoc description, String JavaDoc charset) throws MessagingException JavaDoc {
869         if (message == null) {
870             loadMessage();
871         }
872         modified = true;
873         message.setDescription(description, charset);
874     }
875
876     public void setContentLanguage(String JavaDoc[] languages) throws MessagingException JavaDoc {
877         if (message == null) {
878             loadMessage();
879         }
880         modified = true;
881         message.setContentLanguage(languages);
882     }
883
884     public void setFileName(String JavaDoc filename) throws MessagingException JavaDoc {
885         if (message == null) {
886             loadMessage();
887         }
888         modified = true;
889         message.setFileName(filename);
890     }
891
892     public void setDataHandler(DataHandler JavaDoc dh) throws MessagingException JavaDoc {
893         if (message == null) {
894             loadMessage();
895         }
896         modified = true;
897         message.setDataHandler(dh);
898     }
899
900     public void setContent(Object JavaDoc o, String JavaDoc type) throws MessagingException JavaDoc {
901         if (message == null) {
902             loadMessage();
903         }
904         modified = true;
905         message.setContent(o, type);
906     }
907
908     public void setText(String JavaDoc text) throws MessagingException JavaDoc {
909         if (message == null) {
910             loadMessage();
911         }
912         modified = true;
913         message.setText(text);
914     }
915
916     public void setText(String JavaDoc text, String JavaDoc charset) throws MessagingException JavaDoc {
917         if (message == null) {
918             loadMessage();
919         }
920         modified = true;
921         message.setText(text, charset);
922     }
923
924     public void setContent(Multipart JavaDoc mp) throws MessagingException JavaDoc {
925         if (message == null) {
926             loadMessage();
927         }
928         modified = true;
929         message.setContent(mp);
930     }
931
932     public Message JavaDoc reply(boolean replyToAll) throws MessagingException JavaDoc {
933         if (message == null) {
934             loadMessage();
935         }
936         modified = true;
937         return message.reply(replyToAll);
938     }
939
940     public void setHeader(String JavaDoc name, String JavaDoc value) throws MessagingException JavaDoc {
941         if (message == null) {
942             loadMessage();
943         }
944         modified = true;
945         headers.setHeader(name, value);
946         message.setHeader(name, value);
947     }
948
949     public void addHeader(String JavaDoc name, String JavaDoc value) throws MessagingException JavaDoc {
950         if (message == null) {
951             loadMessage();
952         }
953         modified = true;
954         headers.addHeader(name, value);
955         message.addHeader(name, value);
956     }
957
958     public void removeHeader(String JavaDoc name) throws MessagingException JavaDoc {
959         if (message == null) {
960             loadMessage();
961         }
962         modified = true;
963         headers.removeHeader(name);
964         message.removeHeader(name);
965     }
966
967     public void addHeaderLine(String JavaDoc line) throws MessagingException JavaDoc {
968         if (message == null) {
969             loadMessage();
970         }
971         headers.addHeaderLine(line);
972         message.addHeaderLine(line);
973     }
974
975     public void setFlags(Flags JavaDoc flag, boolean set) throws MessagingException JavaDoc {
976         if (message == null) {
977             loadMessage();
978         }
979         modified = true;
980         message.setFlags(flag, set);
981     }
982
983     public void saveChanges() throws MessagingException JavaDoc {
984         if (message == null) {
985             loadMessage();
986         }
987         modified = true;
988         message.saveChanges();
989     }
990
991     /*
992      * Since JavaMail 1.2
993      */

994     public InputStream JavaDoc getRawInputStream() throws MessagingException JavaDoc {
995         if (message == null) {
996             loadMessage();
997         }
998         return message.getRawInputStream();
999     }
1000
1001    public void addRecipients(Message.RecipientType JavaDoc type, String JavaDoc addresses) throws MessagingException JavaDoc {
1002        if (message == null) {
1003            loadMessage();
1004        }
1005        modified = true;
1006        message.addRecipients(type, addresses);
1007    }
1008
1009    public void setRecipients(Message.RecipientType JavaDoc type, String JavaDoc addresses) throws MessagingException JavaDoc {
1010        if (message == null) {
1011            loadMessage();
1012        }
1013        modified = true;
1014        message.setRecipients(type, addresses);
1015    }
1016
1017    /**
1018     * @see org.apache.avalon.framework.activity.Disposable#dispose()
1019     */

1020    public void dispose() {
1021        if (source instanceof Disposable) {
1022            ((Disposable)source).dispose();
1023        }
1024    }
1025}
1026
Popular Tags