1 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 ; 25 import org.xml.sax.SAXException ; 26 27 import javax.mail.AuthenticationFailedException ; 28 import javax.mail.Folder ; 29 import javax.mail.Message ; 30 import javax.mail.Session ; 31 import javax.mail.Store ; 32 import java.io.IOException ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 36 58 public class IMAPGenerator extends AbstractGenerator { 59 60 static final String URI = "http://apache.org/cocoon/imap/1.0/"; 61 static final String PREFIX = "imap"; 62 63 private String host; 64 private String user; 65 private String pass; 66 67 private Properties props = new Properties (); 68 private Message message[]; 69 70 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 71 throws ProcessingException, SAXException , IOException { 72 73 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 , ProcessingException { 88 89 try { 90 Session sess = Session.getDefaultInstance(this.props, null); 91 Store 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 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 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 afe) { 147 throw new ProcessingException("Failed to authenticate with the IMAP server."); 148 } catch (Exception e) { 149 throw new ProcessingException(e.toString()); 151 } 152 } 153 154 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 name, Attributes attr) 169 throws SAXException { 170 super.contentHandler.startElement(URI, name, PREFIX + ":" + name, attr); 171 } 172 173 private void end(String name) 174 throws SAXException { 175 super.contentHandler.endElement(URI, name, PREFIX + ":" + name); 176 } 177 178 private void data(String data) 179 throws SAXException { 180 super.contentHandler.characters( data.toCharArray(), 0, data.length() ); 181 } 182 183 private void log(String msg) { 184 getLogger().debug(msg); 185 } 186 } 187 | Popular Tags |