KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > IMAPGenerator


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

16 package org.apache.cocoon.generation;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19
20 import org.apache.cocoon.ProcessingException;
21 import org.apache.cocoon.environment.SourceResolver;
22 import org.apache.cocoon.xml.XMLUtils;
23
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import javax.mail.AuthenticationFailedException JavaDoc;
28 import javax.mail.Folder JavaDoc;
29 import javax.mail.Message JavaDoc;
30 import javax.mail.Session JavaDoc;
31 import javax.mail.Store JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 /**
37  * Generates an XML listing of messages from an IMAP mail server.
38  *
39  * <p>You <b>must</b> configure this generator with "host", "user", and "pass" parameters
40  * which specifies the mail server host, the user to login as, and the password to use,
41  * respectively. Beware that these passwords will be sent cleartext since the Generator
42  * does not use an SSL-enabled IMAP connection.</p>
43  *
44  * <p>Also beware that storing sensitive data, (such as mail usernames and passwords) can
45  * be very dangerous, so please be very careful in the method by which you send the user
46  * and password parameters to the generator.</p>
47  *
48  * Instructions: get the JavaMail API jar from http://java.sun.com/products/javamail/, and
49  * the JAF activation.jar from http://java.sun.com/beans/glasgow/jaf.html. Put mail.jar
50  * and activation.jar in xml-cocoon2/lib/local/, and recompile. These jars could actually be
51  * moved to lib/optional and added to jars.xml in the future.
52  *
53  * <br>TODO Refactor all of this to use the MailCommandManager, etc...
54  *
55  * @author <a HREF="mailto:tony@apache.org">Tony Collen</a>
56  * @version $Id: IMAPGenerator.java 164808 2005-04-26 16:07:03Z vgritsenko $
57  */

58 public class IMAPGenerator extends AbstractGenerator {
59
60     static final String JavaDoc URI = "http://apache.org/cocoon/imap/1.0/";
61     static final String JavaDoc PREFIX = "imap";
62
63     private String JavaDoc host;
64     private String JavaDoc user;
65     private String JavaDoc pass;
66
67     private Properties JavaDoc props = new Properties JavaDoc();
68     private Message JavaDoc message[];
69
70     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
71     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
72
73         // TODO: the default values should be something else...
74
this.host = par.getParameter("host", "none");
75         this.user = par.getParameter("user", "none");
76         this.pass = par.getParameter("pass", "none");
77
78         if (this.host.equals("none") ||
79             this.user.equals("none") ||
80             this.pass.equals("none")) {
81
82             throw new ProcessingException("You must configure this generator with host, user, and pass parameters.");
83         }
84     }
85
86     public void generate()
87     throws SAXException JavaDoc, ProcessingException {
88
89         try {
90             Session JavaDoc sess = Session.getDefaultInstance(this.props, null);
91             Store JavaDoc st = sess.getStore("imap");
92
93             log("Connecting to IMAP server @ " + this.host);
94             st.connect(this.host, this.user, this.pass);
95
96             log("Attempting to open default folder");
97             Folder JavaDoc f = st.getFolder("inbox");
98
99             f.open(Folder.READ_WRITE);
100
101             log("Downloading message list from folder");
102             this.message = f.getMessages();
103
104             int i = 0;
105
106             log("Starting XML generation");
107             this.contentHandler.startDocument();
108             this.contentHandler.startPrefixMapping(PREFIX, URI);
109
110             start("imap", XMLUtils.EMPTY_ATTRIBUTES);
111             start("messages", XMLUtils.EMPTY_ATTRIBUTES);
112
113             for (i = 0; i < this.message.length; i++) {
114                 // Loop through the messages and output XML.
115
// TODO: actually use the attributes...
116

117                 start("msg", XMLUtils.EMPTY_ATTRIBUTES);
118
119                 start("subject", XMLUtils.EMPTY_ATTRIBUTES);
120                 data(this.message[i].getSubject());
121                 end("subject");
122
123                 start("from", XMLUtils.EMPTY_ATTRIBUTES);
124                 data(this.message[i].getFrom()[0].toString());
125                 end("from");
126
127                 start("sentDate", XMLUtils.EMPTY_ATTRIBUTES);
128                 data(this.message[i].getSentDate().toString());
129                 end("sentDate");
130
131                 start("num", XMLUtils.EMPTY_ATTRIBUTES);
132                 data(Integer.toString(this.message[i].getMessageNumber()));
133                 end("num");
134
135                 end("msg");
136             }
137
138             end("messages");
139             end("imap");
140
141             this.contentHandler.endPrefixMapping(PREFIX);
142             this.contentHandler.endDocument();
143
144             log("Finished generating XML");
145
146         } catch (AuthenticationFailedException JavaDoc afe) {
147             throw new ProcessingException("Failed to authenticate with the IMAP server.");
148         } catch (Exception JavaDoc e) {
149             // TODO: be more specific when catching this exception...
150
throw new ProcessingException(e.toString());
151         }
152     }
153
154     /**
155      * Recycle the generator by removing references
156      */

157     public void recycle() {
158         this.host = null;
159         this.user = null;
160         this.pass = null;
161
162         this.props = null;
163         this.message = null;
164
165         super.recycle();
166     }
167
168     private void start(String JavaDoc name, Attributes JavaDoc attr)
169     throws SAXException JavaDoc {
170         super.contentHandler.startElement(URI, name, PREFIX + ":" + name, attr);
171     }
172
173     private void end(String JavaDoc name)
174     throws SAXException JavaDoc {
175         super.contentHandler.endElement(URI, name, PREFIX + ":" + name);
176     }
177
178     private void data(String JavaDoc data)
179     throws SAXException JavaDoc {
180         super.contentHandler.characters( data.toCharArray(), 0, data.length() );
181     }
182
183     private void log(String JavaDoc msg) {
184         getLogger().debug(msg);
185     }
186 }
187
Popular Tags