View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.maildir.util;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.OutputStream;
18  import java.util.Date;
19  import java.util.Enumeration;
20  import javax.activation.DataHandler;
21  import javax.mail.Address;
22  import javax.mail.Folder;
23  import javax.mail.Message;
24  import javax.mail.MessagingException;
25  import javax.mail.Multipart;
26  import javax.mail.internet.InternetHeaders;
27  import javax.mail.internet.MimeMessage;
28  import javax.mail.search.SearchTerm;
29  
30  
31  /**
32   * This message implementation keeps input stream received through
33   * <code>parse</code> method and calls superclass <code>parse</code> on demand only.
34   *
35   * @author Daniel Sendula
36   */
37  public class LazyParsingMessage extends MessageBase {
38  
39      /** Flag to show is message parsed or not */
40      protected boolean parsed;
41  
42      /** Cached input stream */
43      protected InputStream inputStream;
44  
45      /**
46       * Constructor
47       * @param folder folder
48       * @param msgnum message number
49       * @throws MessagingException
50       */
51      protected LazyParsingMessage(Folder folder, int msgnum) throws MessagingException {
52          super(folder, msgnum);
53      }
54  
55      /**
56       * Constructor
57       * @param message message
58       * @throws MessagingException
59       */
60      public LazyParsingMessage(MimeMessage message) throws MessagingException {
61          super(message);
62      }
63  
64      /**
65       * Returns <code>true</code> if is parsed
66       * @return <code>true</code> if is parsed
67       */
68      protected boolean isParsed() {
69          return parsed;
70      }
71  
72      /**
73       * Stores input stream for later invoking of superclass' parse method
74       * @param is input stream
75       * @throws MessagingException
76       */
77      protected void parse(InputStream is) throws MessagingException {
78          inputStream = is;
79      }
80  
81      /**
82       * Parses message. It calls superclass' parse method.
83       *
84       * @throws MessagingException
85       */
86      protected synchronized void parseImpl() throws MessagingException {
87          if (!parsed) {
88              super.parse(inputStream);
89              parsed = true;
90          }
91      }
92  
93      /**
94       * Returns headers
95       * @param inputStream input stream
96       * @return fixed internet headers
97       * @throws MessagingException
98       */
99      protected InternetHeaders createInternetHeaders(InputStream inputStream) throws MessagingException {
100         return new InternetHeadersImpl(inputStream);
101     }
102 
103     // ----------------------------------------------------------------------------------
104 
105     /**
106      * Adds from address
107      * @param addresses array of addresses
108      * @throws MessagingException
109      */
110     public void addFrom(Address[] addresses) throws MessagingException {
111         if (!parsed) { parseImpl(); }
112         super.addFrom(addresses);
113     }
114 
115     /**
116      * Adds new header
117      * @param name header name
118      * @param value value
119      * @throws MessagingException
120      */
121     public void addHeader(String name, String value) throws MessagingException {
122         if (!parsed) { parseImpl(); }
123         super.addHeader(name, value);
124     }
125 
126     /**
127      * Adds header line
128      * @param line header line
129      * @throws MessagingException
130      */
131     public void addHeaderLine(String line) throws MessagingException {
132         if (!parsed) { parseImpl(); }
133         super.addHeaderLine(line);
134     }
135 
136     /**
137      * Adds recipients
138      * @param type recipient type (see {@link javax.mail.Message.RecipientType})
139      * @param addresses addresses
140      * @throws MessagingException
141      */
142     public void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException {
143         if (!parsed) { parseImpl(); }
144         super.addRecipients(type, addresses);
145     }
146 
147     /**
148      * Adds recipients
149      * @param type recipient type (see {@link javax.mail.Message.RecipientType})
150      * @param addresses addresses
151      * @throws MessagingException
152      */
153     public void addRecipients(Message.RecipientType type, String addresses) throws MessagingException {
154         if (!parsed) { parseImpl(); }
155         super.addRecipients(type, addresses);
156     }
157 
158     /**
159      * Returns header lines
160      * @return enumeration
161      * @throws MessagingException
162      */
163     public Enumeration<?> getAllHeaderLines() throws MessagingException {
164         if (!parsed) { parseImpl(); }
165         return super.getAllHeaderLines();
166     }
167 
168     /**
169      * Returns headers
170      * @return enumeration
171      * @throws MessagingException
172      */
173     public Enumeration<?> getAllHeaders() throws MessagingException {
174         if (!parsed) { parseImpl(); }
175         return super.getAllHeaders();
176     }
177 
178     /**
179      * Returns all recipients
180      * @return array of addresses
181      * @throws MessagingException
182      */
183     public Address[] getAllRecipients() throws MessagingException {
184         if (!parsed) { parseImpl(); }
185         return super.getAllRecipients();
186     }
187 
188     /**
189      * Returns content
190      * @return content
191      * @throws IOException
192      * @throws MessagingException
193      */
194     public Object getContent() throws IOException, MessagingException {
195         if (!parsed) { parseImpl(); }
196         return super.getContent();
197     }
198 
199     /**
200      * Returns content id
201      * @return content id
202      * @throws MessagingException
203      */
204     public String getContentID() throws MessagingException {
205         if (!parsed) { parseImpl(); }
206         return super.getContentID();
207     }
208 
209     /**
210      * Returns content language
211      * @return content language
212      * @throws MessagingException
213      */
214     public String[] getContentLanguage() throws MessagingException {
215         if (!parsed) { parseImpl(); }
216         return super.getContentLanguage();
217     }
218 
219     /**
220      * Returns content md3
221      * @return content md
222      * @throws MessagingException
223      */
224     public String  getContentMD5() throws MessagingException {
225         if (!parsed) { parseImpl(); }
226         return super.getContentMD5();
227     }
228 
229     /**
230      * Returns content stream
231      * @return content stream
232      * @throws MessagingException
233      */
234     protected InputStream getContentStream() throws MessagingException {
235         if (!parsed) { parseImpl(); }
236         return super.getContentStream();
237     }
238 
239     /**
240      * Returns content type
241      * @return content type
242      * @throws MessagingException
243      */
244     public String getContentType() throws MessagingException {
245         if (!parsed) { parseImpl(); }
246         return super.getContentType();
247     }
248 
249     /**
250      * Returns data handler
251      * @return data handler
252      * @throws MessagingException
253      */
254     public DataHandler getDataHandler() throws MessagingException {
255         if (!parsed) { parseImpl(); }
256         return super.getDataHandler();
257     }
258 
259     /**
260      * Returns description
261      * @return description
262      * @throws MessagingException
263      */
264     public String getDescription() throws MessagingException {
265         if (!parsed) { parseImpl(); }
266         return super.getDescription();
267     }
268 
269     /**
270      * Returns disposition
271      * @return disposition
272      * @throws MessagingException
273      */
274     public String getDisposition() throws MessagingException {
275         if (!parsed) { parseImpl(); }
276         return super.getDisposition();
277     }
278 
279     /**
280      * Returns encoding
281      * @return encoding
282      * @throws MessagingException
283      */
284     public String getEncoding() throws MessagingException {
285         if (!parsed) { parseImpl(); }
286         return super.getEncoding();
287     }
288 
289     /**
290      * Returns file name
291      * @return file name
292      * @throws MessagingException
293      */
294     public String getFileName() throws MessagingException {
295         if (!parsed) { parseImpl(); }
296         return super.getFileName();
297     }
298 
299     /**
300      * Returns from
301      * @return array of from addresses
302      * @throws MessagingException
303      */
304     public Address[] getFrom() throws MessagingException {
305         if (!parsed) { parseImpl(); }
306         return super.getFrom();
307     }
308 
309     /**
310      * Returns header
311      * @param name name of header
312      * @return array of header values
313      * @throws MessagingException
314      */
315     public String[] getHeader(String name) throws MessagingException {
316         if (!parsed) { parseImpl(); }
317         return super.getHeader(name);
318     }
319 
320     /**
321      * Returns header
322      * @param name name
323      * @param delimiter delimiter
324      * @return header
325      * @throws MessagingException
326      */
327     public String getHeader(String name, String delimiter) throws MessagingException {
328         if (!parsed) { parseImpl(); }
329         return super.getHeader(name, delimiter);
330     }
331 
332     /**
333      * Returns input stream
334      * @return input stream
335      * @throws IOException
336      * @throws MessagingException
337      */
338     public InputStream getInputStream() throws IOException, MessagingException {
339         if (!parsed) { parseImpl(); }
340         return super.getInputStream();
341     }
342 
343     /**
344      * Returns line count
345      * @return line count
346      * @throws MessagingException
347      */
348     public int getLineCount() throws MessagingException {
349         if (!parsed) { parseImpl(); }
350         return super.getLineCount();
351     }
352 
353     /**
354      * Returns matching header lines
355      * @param names array of names
356      * @return enumeration
357      * @throws MessagingException
358      */
359     public Enumeration<?> getMatchingHeaderLines(String[] names) throws MessagingException {
360         if (!parsed) { parseImpl(); }
361         return super.getMatchingHeaderLines(names);
362     }
363 
364     /**
365      * Returns matching headers
366      * @param names header names
367      * @return enumeration
368      * @throws MessagingException
369      */
370     public Enumeration<?> getMatchingHeaders(String[] names) throws MessagingException {
371         if (!parsed) { parseImpl(); }
372         return super.getMatchingHeaders(names);
373     }
374 
375     /**
376      * Returns message id
377      * @return message id
378      * @throws MessagingException
379      */
380     public String getMessageID() throws MessagingException {
381         if (!parsed) { parseImpl(); }
382         return super.getMessageID();
383     }
384 
385     /**
386      * Returns non matching header lines
387      * @param names array of names
388      * @return enumeration
389      * @throws MessagingException
390      */
391     public Enumeration<?> getNonMatchingHeaderLines(String[] names) throws MessagingException {
392         if (!parsed) { parseImpl(); }
393         return super.getNonMatchingHeaderLines(names);
394     }
395 
396     /**
397      * Returns non matching headers
398      * @param names header names
399      * @return enumeration
400      * @throws MessagingException
401      */
402     public Enumeration<?> getNonMatchingHeaders(String[] names) throws MessagingException {
403         if (!parsed) { parseImpl(); }
404         return super.getNonMatchingHeaders(names);
405     }
406 
407     /**
408      * Returns raw input stream
409      * @return raw input stream
410      * @throws MessagingException
411      */
412     public InputStream getRawInputStream() throws MessagingException {
413         if (!parsed) { parseImpl(); }
414         return super.getRawInputStream();
415     }
416 
417     /**
418      * Returns recipients
419      * @param type recipitents' type
420      * @return array of recipients
421      * @throws MessagingException
422      */
423     public Address[] getRecipients(Message.RecipientType type) throws MessagingException {
424         if (!parsed) { parseImpl(); }
425         return super.getRecipients(type);
426     }
427 
428     /**
429      * Returns reply to
430      * @return array of recipients
431      * @throws MessagingException
432      */
433     public Address[] getReplyTo() throws MessagingException {
434         if (!parsed) { parseImpl(); }
435         return super.getReplyTo();
436     }
437 
438     /**
439      * Returns sender
440      * @return sender
441      * @throws MessagingException
442      */
443     public Address getSender() throws MessagingException {
444         if (!parsed) { parseImpl(); }
445         return super.getSender();
446     }
447 
448     /**
449      * Returns sent date
450      * @return sent date
451      * @throws MessagingException
452      */
453     public Date getSentDate() throws MessagingException {
454         if (!parsed) { parseImpl(); }
455         return super.getSentDate();
456     }
457 
458     /**
459      * Returns size
460      * @return size
461      * @throws MessagingException
462      */
463     public int getSize() throws MessagingException {
464         if (!parsed) { parseImpl(); }
465         return super.getSize();
466     }
467 
468     /**
469      * Returns subject
470      * @return subject
471      * @throws MessagingException
472      */
473     public String getSubject() throws MessagingException {
474         if (!parsed) { parseImpl(); }
475         return super.getSubject();
476     }
477 
478     /**
479      * Returns <code>true</code> if is of supplied mime type
480      * @param mimeType mime type to be checked
481      * @return <code>true</code> if is of supplied mime type
482      * @throws MessagingException
483      */
484     public boolean isMimeType(String mimeType) throws MessagingException {
485         if (!parsed) { parseImpl(); }
486         return super.isMimeType(mimeType);
487     }
488 
489     /**
490      * Removes header
491      * @param name header's name
492      * @throws MessagingException
493      */
494     public void removeHeader(String name) throws MessagingException {
495         if (!parsed) { parseImpl(); }
496         super.removeHeader(name);
497     }
498 
499     /**
500      * Makes reply message
501      * @param replyToAll should it reply to all
502      * @return new message
503      * @throws MessagingException
504      */
505     public Message reply(boolean replyToAll) throws MessagingException {
506         if (!parsed) { parseImpl(); }
507         return super.reply(replyToAll);
508     }
509 
510     /**
511      * Saves changes in message
512      * @throws MessagingException
513      */
514     public void saveChanges() throws MessagingException {
515         if (!parsed) { parseImpl(); }
516         super.saveChanges();
517     }
518 
519     /**
520      * Sets contnet as multipart
521      * @param mp multipart content
522      * @throws MessagingException
523      */
524     public void setContent(Multipart mp) throws MessagingException {
525         if (!parsed) { parseImpl(); }
526         super.setContent(mp);
527     }
528 
529     /**
530      * Sets content
531      * @param o content object
532      * @param type mime type
533      * @throws MessagingException
534      */
535     public void setContent(Object o, String type) throws MessagingException {
536         if (!parsed) { parseImpl(); }
537         super.setContent(o, type);
538     }
539 
540     /**
541      * Sets content id
542      * @param cid content id
543      * @throws MessagingException
544      */
545     public void setContentID(String cid) throws MessagingException {
546         if (!parsed) { parseImpl(); }
547         super.setContentID(cid);
548     }
549 
550     /**
551      * Sets languages
552      * @param languages array of language strings
553      * @throws MessagingException
554      */
555     public void setContentLanguage(String[] languages) throws MessagingException {
556         if (!parsed) { parseImpl(); }
557         super.setContentLanguage(languages);
558     }
559 
560     /**
561      * Sets content md5
562      * @param md5 content md5
563      * @throws MessagingException
564      */
565     public void setContentMD5(String md5) throws MessagingException {
566         if (!parsed) { parseImpl(); }
567         super.setContentMD5(md5);
568     }
569 
570     /**
571      * Sets data handler
572      * @param dh data handler
573      * @throws MessagingException
574      */
575     public void setDataHandler(DataHandler dh) throws MessagingException {
576         if (!parsed) { parseImpl(); }
577         super.setDataHandler(dh);
578     }
579 
580     /**
581      * Sets description
582      * @param description description
583      * @throws MessagingException
584      */
585     public void setDescription(String description) throws MessagingException {
586         if (!parsed) { parseImpl(); }
587         super.setDescription(description);
588     }
589 
590     /**
591      * Sets description
592      * @param description description
593      * @param charset character set
594      * @throws MessagingException
595      */
596     public void setDescription(String description, String charset) throws MessagingException {
597         if (!parsed) { parseImpl(); }
598         super.setDescription(description, charset);
599     }
600 
601     /**
602      * Sets disposition
603      * @param disposition content disposition
604      * @throws MessagingException
605      */
606     public void setDisposition(String disposition) throws MessagingException {
607         if (!parsed) { parseImpl(); }
608         super.setDisposition(disposition);
609     }
610 
611     /**
612      * Sets file name
613      * @param filename file name
614      * @throws MessagingException
615      */
616     public void setFileName(String filename) throws MessagingException {
617         if (!parsed) { parseImpl(); }
618         super.setFileName(filename);
619     }
620 
621     /**
622      * Sets from
623      * @throws MessagingException
624      */
625     public void setFrom() throws MessagingException {
626         if (!parsed) { parseImpl(); }
627         super.setFrom();
628     }
629 
630     /**
631      * Sets from
632      * @param address from address
633      * @throws MessagingException
634      */
635     public void setFrom(Address address) throws MessagingException {
636         if (!parsed) { parseImpl(); }
637         super.setFrom(address);
638     }
639 
640     /**
641      * Set header
642      * @param name header name
643      * @param value header value
644      * @throws MessagingException
645      */
646     public void setHeader(String name, String value) throws MessagingException {
647         if (!parsed) { parseImpl(); }
648         super.setHeader(name, value);
649     }
650 
651     /**
652      * Sets recipients
653      * @param type recipients' type
654      * @param addresses addresses
655      * @throws MessagingException
656      */
657     public void setRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException {
658         if (!parsed) { parseImpl(); }
659         super.setRecipients(type, addresses);
660     }
661 
662     /**
663      * Sets recipients
664      * @param type recipients' type
665      * @param addresses addresses
666      * @throws MessagingException
667      */
668     public void setRecipients(Message.RecipientType type, String addresses) throws MessagingException {
669         if (!parsed) { parseImpl(); }
670         super.setRecipients(type, addresses);
671     }
672 
673     /**
674      * Sets reply to address
675      * @param addresses addresses
676      * @throws MessagingException
677      */
678     public void setReplyTo(Address[] addresses) throws MessagingException {
679         if (!parsed) { parseImpl(); }
680         super.setReplyTo(addresses);
681     }
682 
683     /**
684      * Sets sender's address
685      * @param address sender's address
686      * @throws MessagingException
687      */
688     public void setSender(Address address) throws MessagingException {
689         if (!parsed) { parseImpl(); }
690         super.setSender(address);
691     }
692 
693     /**
694      * Sets sent date
695      * @param d date
696      * @throws MessagingException
697      */
698     public void setSentDate(Date d) throws MessagingException {
699         if (!parsed) { parseImpl(); }
700         super.setSentDate(d);
701     }
702 
703     /**
704      * Sets subject
705      * @param subject subject
706      * @throws MessagingException
707      */
708     public void setSubject(String subject) throws MessagingException {
709         if (!parsed) { parseImpl(); }
710         super.setSubject(subject);
711     }
712 
713     /**
714      * Sets subject
715      * @param subject subject
716      * @param charset character set
717      * @throws MessagingException
718      */
719     public void setSubject(String subject, String charset) throws MessagingException {
720         if (!parsed) { parseImpl(); }
721         super.setSubject(subject, charset);
722     }
723 
724     /**
725      * Sets body as text
726      * @param text body text
727      * @throws MessagingException
728      */
729     public void setText(String text) throws MessagingException {
730         if (!parsed) { parseImpl(); }
731         super.setText(text);
732     }
733 
734     /**
735      * Sets body as text
736      * @param text body text
737      * @param charset character set
738      * @throws MessagingException
739      */
740     public void setText(String text, String charset) throws MessagingException {
741         if (!parsed) { parseImpl(); }
742         super.setText(text, charset);
743     }
744 
745     /**
746      * Updates headers
747      * @throws MessagingException
748      */
749     protected void updateHeaders() throws MessagingException {
750         if (!parsed) { parseImpl(); }
751         super.updateHeaders();
752     }
753 
754     /**
755      * Writes content of the message to output stream
756      * @param os output stream
757      * @throws IOException
758      * @throws MessagingException
759      */
760     public void writeTo(OutputStream os) throws IOException, MessagingException {
761         if (!parsed) { parseImpl(); }
762         super.writeTo(os);
763     }
764 
765     /**
766      * Writes content of the message to output stream ignoring supplied headers
767      * @param os output stream
768      * @param ignoreList array of headers to be ignored
769      * @throws IOException
770      * @throws MessagingException
771      */
772     public void writeTo(OutputStream os, String[] ignoreList) throws IOException, MessagingException {
773         if (!parsed) { parseImpl(); }
774         super.writeTo(os, ignoreList);
775     }
776 
777     /**
778      * Adds new recipient to the message
779      * @param type recipient type
780      * @param address address
781      * @throws MessagingException
782      */
783     public void addRecipient(Message.RecipientType type, Address address) throws MessagingException {
784         if (!parsed) { parseImpl(); }
785         super.addRecipient(type, address);
786     }
787 
788     /**
789      * Matches message
790      * @param term term to be used for matching
791      * @return <code>true</code> if matched
792      * @throws MessagingException
793      */
794     public boolean match(SearchTerm term) throws MessagingException {
795         if (!parsed) { parseImpl(); }
796         return super.match(term);
797     }
798 
799     /**
800      * Sets recipient to the message
801      * @param type recipient type
802      * @param address address
803      * @throws MessagingException
804      */
805     public void setRecipient(Message.RecipientType type, Address address) throws MessagingException {
806         if (!parsed) { parseImpl(); }
807         super.setRecipient(type, address);
808     }
809 
810     /**
811      * Returns <code>true</code> if message is expunged
812      * @return <code>true</code> if message is expunged
813      */
814     public boolean isExpunged() {
815         return super.isExpunged();
816     }
817 
818 }