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