cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject cvs commit: cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/generation
Date Fri, 17 Oct 2003 18:11:47 GMT
tony        2003/10/17 11:11:47

  Added:       src/blocks/mail/java/org/apache/cocoon/generation
  initial imapgenerator import
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/mail/java/org/apache/cocoon/generation/
  package org.apache.cocoon.generation;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.cocoon.xml.AbstractXMLProducer;
  import org.xml.sax.helpers.AttributesImpl;
  import org.xml.sax.SAXException;
  import java.util.Map;
  import java.util.Date;
  import java.util.Properties;
  import javax.mail.Folder;
  import javax.mail.Message;
  import javax.mail.Session;
  import javax.mail.Store;
  import javax.mail.URLName;
  import javax.mail.AuthenticationFailedException;
   * Generates an XML listing of messages from an IMAP mail server.
   * <p>You <b>must</b> configure this generator with "host", "user", and
"pass" parameters
   * which specifies the mail server host, the user to login as, and the password to use,
   * respectively.  Beware that these passwords will be sent cleartext since the Generator
   * does not use an SSL-enabled IMAP connection.</p>
   * <p>Also beware that storing sensitive data, (such as mail usernames and passwords)
   * be very dangerous, so please be very careful in the method by which you send the user
   * and password parameters to the generator.</p>
   * Instructions: get the JavaMail API jar from, and
   * the JAF activation.jar from  Put mail.jar

   * and activation.jar in xml-cocoon2/lib/local/, and recompile.  These jars could actually
   * moved to lib/optional and added to jars.xml in the future.
   * @todo. Refactor all of this to use the MailCommandManager, etc...
   * @author <a href="">Tony Collen</a>
  public class IMAPGenerator extends AbstractGenerator {
      static final String URI = "";
      static final String PREFIX = "imap";
      private String host;
      private String user;
      private String pass;
      private Properties props = new Properties();
      private Message message[] = null;
      private Date messageSentDate = null;
      public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
          throws ProcessingException, SAXException, IOException {
          // TODO: the default values should be something else...
 = par.getParameter("host", "none");
          this.user = par.getParameter("user", "none");
          this.pass = par.getParameter("pass", "none");
          if ("none") || 
              this.user.equals("none") || 
              this.pass.equals("none")) {
              throw new ProcessingException("You must configure this generator with host,
user, and pass parameters.");
      public void generate()
      throws SAXException, ProcessingException {
          try {
              Session sess = Session.getDefaultInstance(this.props, null);
              Store st = sess.getStore("imap");
              AttributesImpl attr = new AttributesImpl();
              log("Connecting to IMAP server @ " +;
              st.connect(, this.user, this.pass);
              log("Attempting to open default folder");
              Folder f = st.getFolder("inbox");
              log("Downloading message list from folder");
              this.message = f.getMessages();
              int i = 0;
              log("Starting XML generation");
              this.contentHandler.startPrefixMapping(PREFIX, URI);
              start("imap", attr);
              start("messages", attr);
              for (i = 0; i < this.message.length; i++) {
              // loop through the messages and output XML.
              // TODO: actually use the attributes...
              start("msg", attr);
              start("subject", attr);
              data( this.message[i].getSubject() );
              start("from", attr);
              data( this.message[i].getFrom()[0].toString() );
              start("sentDate", attr);
              data( this.message[i].getSentDate().toString() );
              start("num", attr);
              data( Integer.toString( this.message[i].getMessageNumber() ) );
              log("Finished generating XML");
          } catch (AuthenticationFailedException afe) {
              throw new ProcessingException("Failed to authenticate with the IMAP server.");
          } catch (Exception e) {
              // TODO: be more specific when catching this exception...
              throw new ProcessingException( e.toString() );
       * Recycle the generator by removing references
      public void recycle() {
 = null;
          this.user = null;
          this.pass = null;
          this.props = null;
          this.message = null;
          this.messageSentDate = null;
      private void start(String name, AttributesImpl attr)
      throws SAXException 
          super.contentHandler.startElement(URI, name, PREFIX + ":" + name, attr);
      private void end(String name) 
      throws SAXException
          super.contentHandler.endElement(URI, name, PREFIX + ":" + name);
      private void data(String data) 
      throws SAXException
          super.contentHandler.characters( data.toCharArray(), 0, data.length() );
      private void log(String msg)
          if (this.getLogger().isDebugEnabled()) {

View raw message