[jboss-cvs] jboss-seam/src/mail/org/jboss/seam/mail/ui ...

Peter Muir peter at bleepbleep.org.uk
Sun Jan 21 09:13:34 EST 2007


  User: pmuir   
  Date: 07/01/21 09:13:34

  Added:       src/mail/org/jboss/seam/mail/ui            UICc.java
                        AddressComponent.java MailComponent.java UIBcc.java
                        RecipientAddressComponent.java UIReplyTo.java
                        UITo.java UIMessage.java UIFrom.java UIBody.java
                        UISubject.java
  Log:
  JBSEAM-626 Initial work on rendering an email message via facelets
  
  Revision  Changes    Path
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UICc.java
  
  Index: UICc.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import javax.mail.Message.RecipientType;
  
  /**
   * JSF component for rendering a Cc
   */
  public class UICc extends RecipientAddressComponent
  {
  
     @Override
     protected RecipientType getRecipientType()
     {
       return RecipientType.CC;
     }
  
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/AddressComponent.java
  
  Index: AddressComponent.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  
  import javax.faces.context.FacesContext;
  import javax.mail.MessagingException;
  import javax.mail.internet.InternetAddress;
  
  public abstract class AddressComponent extends MailComponent
  {
  
     private String name;
     private String address;
  
     public AddressComponent()
     {
        super();
     }
  
     /**
      * get an InternetAddress object based upon name, address
      */
     protected InternetAddress getInternetAddress(FacesContext facesContext) throws MessagingException, IOException
     {
         InternetAddress address = new InternetAddress();
        address.setAddress(getAddress() != null ? getAddress() : encode(facesContext));
        address.setPersonal(getName());
        address.validate();
        return address;
     }
  
     @Override
     public void encodeChildren(FacesContext arg0) throws IOException
     {
       // Disable encoding of children
     }
  
     /**
      * the email address part of the address
      */
     public String getAddress()
     {
        if (address == null)
        {
           return getString("address");
        }
        else
        {
           return address;
        }
     }
  
     public void setAddress(String address)
     {
        this.address = address;
     }
  
     /**
      *  the name part of the address
      */
     public String getName()
     {
        if (address == null)
        {
           return getString("name");
        } 
        else 
        {
           return name;
        }
     }
  
     public void setName(String name)
     {
        this.name = name;
     }
  
  }
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/MailComponent.java
  
  Index: MailComponent.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  import java.io.StringWriter;
  
  import javax.faces.component.UIComponent;
  import javax.faces.component.UIComponentBase;
  import javax.faces.context.FacesContext;
  import javax.faces.context.ResponseWriter;
  import javax.mail.internet.MimeMessage;
  
  import org.jboss.seam.ui.JSF;
  
  /**
   * Abstract base class for mail ui components
   *
   */
  public abstract class MailComponent extends UIComponentBase
  {
  
     private static final String FAMILY = "org.jboss.seam.mail";
  
     @Override
     public String getFamily()
     {
       return FAMILY;
     }
     
     @Override
     public boolean getRendersChildren() {
         return true;
     }
     
     
     protected String encode(FacesContext facesContext) throws IOException {
       return encode(facesContext, this);
     }
     
     /**
      * Encode the children of cmp, writing to a string (rather than the http response object)
      * and return the string
      */
     protected String encode(FacesContext facesContext,UIComponent cmp) throws IOException {
        ResponseWriter response = facesContext.getResponseWriter();
        StringWriter stringWriter = new StringWriter();
        ResponseWriter cachingResponseWriter = response.cloneWithWriter(stringWriter);
        facesContext.setResponseWriter(cachingResponseWriter);
        JSF.renderChildren(facesContext, cmp);
        facesContext.setResponseWriter(response);
        String output = stringWriter.getBuffer().toString();
        return output;
     }
     
     /**
      * look up the tree for mail message
      */
     public MimeMessage findMimeMessage() {
         UIMessage parent = (UIMessage) findParent(this, UIMessage.class);
         if (parent != null) 
         {
             return parent.getMimeMessage();
         }
         else 
         {
             return null;
         }
     }
     
     public MailComponent findParent(UIComponent parent) {
        return findParent(parent, null);
     }
  
     /**
      * find the first parent that is a mail component of a given type
      */
     public MailComponent findParent(UIComponent parent, Class<?> c) {
         if (parent == null) 
         {
             return null;
         }
         
         if (parent instanceof MailComponent) 
         {
             if (c==null || c.isAssignableFrom(parent.getClass())) 
             {
                 return (MailComponent) parent;
             }
         }
  
         return findParent(parent.getParent(),c);
     }
     
     /**
      * Get a valuebinding as a string
      */
     protected String getString(String localName) {
        if (getValue(localName) != null) {
           return getValue(localName).toString();
        } else {
           return null;
        }
     }
     
     /**
      * Get a vauebinding
      */
     protected Object getValue(String localName) {
        if (getValueBinding(localName) == null) {
           return null;
        } else {
           return getValueBinding(localName).getValue(getFacesContext());
        }
     }
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UIBcc.java
  
  Index: UIBcc.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import javax.mail.Message.RecipientType;
  
  /**
   * JSF component for rendering a Bcc
   */
  public class UIBcc extends RecipientAddressComponent
  {
  
     @Override
     protected RecipientType getRecipientType()
     {
       return RecipientType.BCC;
     }
  
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/RecipientAddressComponent.java
  
  Index: RecipientAddressComponent.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  
  import javax.faces.FacesException;
  import javax.faces.context.FacesContext;
  import javax.mail.Message.RecipientType;
  import javax.mail.internet.MimeMessage;
  
  /**
   * Encode a recipient.  Work is done here, subclasses simply need to
   * specify a RecipientType 
   */
  public abstract class RecipientAddressComponent extends AddressComponent
  {
     
     @Override
     public void encodeBegin(FacesContext facesContext) throws IOException
     {
        try
        {
           MimeMessage mimeMessage = findMimeMessage();
           mimeMessage.addRecipient(getRecipientType(), getInternetAddress(facesContext));
        }
        catch (Exception e)
        {
           throw new FacesException(e);
        }
     }
    
     protected abstract RecipientType getRecipientType();
     
     
  
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UIReplyTo.java
  
  Index: UIReplyTo.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  
  import javax.faces.FacesException;
  import javax.faces.context.FacesContext;
  import javax.mail.Address;
  import javax.mail.internet.MimeMessage;
  
  /**
   * JSF component for rendering a Reply-to header
   */
  public class UIReplyTo extends AddressComponent
  {
     
     @Override
     public void encodeBegin(FacesContext facesContext) throws IOException
     {
        try
        {
           MimeMessage mimeMessage = findMimeMessage();
           Address[] replyTo = {getInternetAddress(facesContext)}; 
           mimeMessage.setReplyTo(replyTo);
        }
        catch (Exception e)
        {
          throw new FacesException(e);
        }
     }
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UITo.java
  
  Index: UITo.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import javax.mail.Message.RecipientType;
  
  /**
   * JSF Component for rendering To
   */
  public class UITo extends RecipientAddressComponent
  {
  
     @Override
     protected RecipientType getRecipientType()
     {
       return RecipientType.TO;
     }
  
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UIMessage.java
  
  Index: UIMessage.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  
  import javax.faces.FacesException;
  import javax.faces.context.FacesContext;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.MimeMessage;
  
  import org.jboss.seam.mail.MailSession;
  
  /**
   * JSF component which delimites the start and end of the mail message.  
   */
  // TODO Support Priority,read receipt, encoding
  // TODO Support getting session from JNDI
  public class UIMessage extends MailComponent
  {
     private MimeMessage mimeMessage;
     private Session session;
  
     /**
      * Get the JavaMail Session to use.
      * If not set the default session is used
      */
     public Session getMailSession()
     {
        if (session == null) {
           if (getValue("session") !=  null) {
              session = (Session) getValue("session");
           } else {
              session = MailSession.instance();
           }
        }
        return session;
     }
  
     public void setMailSession(Session session)
     {
        this.session = session;
     }
     
     public MimeMessage getMimeMessage() {
        if (mimeMessage == null) {
           mimeMessage = new MimeMessage(getMailSession());
        }
        return mimeMessage;
     }
     
     @Override
     public void encodeEnd(FacesContext arg0) throws IOException
     {
        super.encodeEnd(arg0);
        try
        {
           Transport.send(getMimeMessage());
        }
        catch (Exception e)
        {
           throw new FacesException(e);
        }
     }
     
     @Override
     public boolean getRendersChildren()
     {
      return false;
     }
  
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UIFrom.java
  
  Index: UIFrom.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  
  import javax.faces.FacesException;
  import javax.faces.context.FacesContext;
  import javax.mail.internet.MimeMessage;
  
  /**
   * JSF Component for rendering a from address
   */
  public class UIFrom extends AddressComponent
  {
     
     @Override
     public void encodeBegin(FacesContext facesContext) throws IOException
     {
        try
        {
           MimeMessage mimeMessage = findMimeMessage();
           mimeMessage.setFrom(getInternetAddress(facesContext));
        }
        catch (Exception e)
        {
          throw new FacesException(e);
        }
     }
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UIBody.java
  
  Index: UIBody.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  
  import javax.faces.FacesException;
  import javax.faces.component.UIComponent;
  import javax.faces.context.FacesContext;
  import javax.mail.BodyPart;
  import javax.mail.Multipart;
  import javax.mail.internet.MimeBodyPart;
  import javax.mail.internet.MimeMessage;
  import javax.mail.internet.MimeMultipart;
  
  /**
   * JSF component for rendering the body
   * Supports plain text, html bodies and setting an alternative
   * (text) part using an alternative facet
   *
   */
  public class UIBody extends MailComponent
  {
     
     public static final String HTML="html";
     public static final String PLAIN = "plain";
     
     private String type = HTML;
     
     @Override
     public void encodeChildren(FacesContext facesContext) throws IOException
     {
       try
       {
          String body = encode(facesContext);
          MimeMessage mimeMessage = findMimeMessage();
          if (PLAIN.equals(type)) 
          {
            mimeMessage.setText(body);
          }
          else if (HTML.equals(type)) 
          {
             UIComponent alternative = getFacet("alternative");
             if (alternative != null)
             {
                BodyPart text = new MimeBodyPart();
                text.setText(encode(facesContext,alternative));
                BodyPart html = new MimeBodyPart();
                html.setContent(body, "text/html");
                Multipart multipart = new MimeMultipart("alternative");
                multipart.addBodyPart(html);
                multipart.addBodyPart(text);
                mimeMessage.setContent(multipart);
             }
             else
             {   
                mimeMessage.setContent(body, "text/html");
             }
          }
        }
        catch (Exception e)
        {
          throw new FacesException(e);
        }
     }
     
     public void setType(String type)
     {
        this.type = type;
     }
     
     /**
      * The type of the body - plain or html
      */
     public String getType()
     {
        if (type == null) 
        {
           return getString("type");
        }
        return type;
     }
  
  }
  
  
  
  1.1      date: 2007/01/21 14:13:34;  author: pmuir;  state: Exp;jboss-seam/src/mail/org/jboss/seam/mail/ui/UISubject.java
  
  Index: UISubject.java
  ===================================================================
  package org.jboss.seam.mail.ui;
  
  import java.io.IOException;
  
  import javax.faces.FacesException;
  import javax.faces.context.FacesContext;
  
  /**
   * JSF component for rendering subject line
   */
  public class UISubject extends MailComponent
  {
     @Override
     public void encodeChildren(FacesContext facesContext) throws IOException
     {
        try
        {
           String subject = encode(facesContext);
           findMimeMessage().setSubject(subject);
        }
        catch (Exception e)
        {
          throw new FacesException(e);
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list