KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > spam > command > CommandHelper


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo
13
// Stich.
14
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15
//
16
//All Rights Reserved.
17
package org.columba.mail.spam.command;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.SequenceInputStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.columba.mail.config.AccountItem;
27 import org.columba.mail.config.MailConfig;
28 import org.columba.mail.folder.IMailbox;
29 import org.columba.ristretto.coder.Base64DecoderInputStream;
30 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
31 import org.columba.ristretto.message.Header;
32 import org.columba.ristretto.message.MimeHeader;
33 import org.columba.ristretto.message.MimePart;
34 import org.columba.ristretto.message.MimeTree;
35
36 /**
37  * Helper class provides methods for preparing email messages before getting
38  * passed along to the spam filter.
39  *
40  * @author fdietz
41  */

42 public final class CommandHelper {
43
44     /**
45      * Return bodypart of message as inputstream.
46      * <p>
47      * Note, that this depends on wether the user prefers HTML or text messages.
48      * <p>
49      * Bodypart is decoded if necessary.
50      *
51      * @param folder
52      * selected folder containing the message
53      * @param uid
54      * ID of message
55      * @return inputstream of message bodypart
56      * @throws Exception
57      */

58     public static InputStream JavaDoc getBodyPart(IMailbox folder, Object JavaDoc uid)
59             throws Exception JavaDoc {
60         MimeTree mimePartTree = folder.getMimePartTree(uid);
61
62         List JavaDoc l = mimePartTree.getLeafsWithContentType(mimePartTree
63                 .getRootMimeNode(), "text");
64         if (l.size() > 1) {
65             Vector JavaDoc streamList = new Vector JavaDoc();
66             Iterator JavaDoc it = l.iterator();
67             while (it.hasNext()) {
68                 MimePart mp = (MimePart) it.next();
69
70                 InputStream JavaDoc s = getBodyPartStream(folder, uid, mp);
71                 streamList.add(s);
72             }
73
74             SequenceInputStream JavaDoc stream = new SequenceInputStream JavaDoc(streamList
75                     .elements());
76             return stream;
77         } else if (l.size() == 1) {
78             MimePart mp = (MimePart) l.get(0);
79             InputStream JavaDoc s = getBodyPartStream(folder, uid, mp);
80             return s;
81         } else {
82             return new ByteArrayInputStream JavaDoc(new byte[0]);
83
84         }
85
86     }
87
88     /**
89      * Get inputstream of bodypart.
90      * <p>
91      * Additionally decode inputstream.
92      *
93      * @param folder
94      * selected folder
95      * @param uid
96      * selected message UID
97      * @param mp
98      * selected Mimepart
99      * @return decoded inputstream
100      * @throws Exception
101      */

102     private static InputStream JavaDoc getBodyPartStream(IMailbox folder, Object JavaDoc uid,
103             MimePart mp) throws Exception JavaDoc {
104         InputStream JavaDoc bodyStream = folder.getMimePartBodyStream(uid, mp
105                 .getAddress());
106
107         int encoding = mp.getHeader().getContentTransferEncoding();
108
109         switch (encoding) {
110         case MimeHeader.QUOTED_PRINTABLE: {
111             bodyStream = new QuotedPrintableDecoderInputStream(bodyStream);
112
113             break;
114         }
115
116         case MimeHeader.BASE64: {
117             bodyStream = new Base64DecoderInputStream(bodyStream);
118
119             break;
120         }
121         }
122
123         return bodyStream;
124     }
125
126     /**
127      * Retrieve account this message is associated to.
128      *
129      * @param folder
130      * selected folder
131      * @param uid
132      * selected message
133      * @return account item
134      * @throws Exception
135      */

136     public static AccountItem retrieveAccountItem(IMailbox folder, Object JavaDoc uid)
137             throws Exception JavaDoc {
138         AccountItem item = null;
139
140         Object JavaDoc accountUid = folder.getAttribute(uid, "columba.accountuid");
141         if (accountUid != null) {
142             // try to get account using the account ID
143
item = MailConfig.getInstance().getAccountList().uidGet(
144                     ((Integer JavaDoc) accountUid).intValue());
145
146         }
147
148         if (item == null) {
149             // try to get the account using the email address
150
Header header = folder.getHeaderFields(uid, new String JavaDoc[] { "To" });
151
152             item = MailConfig.getInstance().getAccountList().getAccount(
153                     header.get("To"));
154
155         }
156
157         if (item == null) {
158             // use default account as fallback
159

160             item = MailConfig.getInstance().getAccountList()
161                     .getDefaultAccount();
162         }
163
164         return item;
165     }
166 }
Popular Tags