You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jv...@apache.org on 2001/07/10 02:38:40 UTC

cvs commit: jakarta-turbine/src/adapter/org/apache/turbine/util/velocity VelocityEmail.java VelocityHtmlEmail.java

jvanzyl     01/07/09 17:38:40

  Added:       src/adapter/org/apache/turbine/util/velocity
                        VelocityEmail.java VelocityHtmlEmail.java
  Log:
  - adapter code
  
  Revision  Changes    Path
  1.1                  jakarta-turbine/src/adapter/org/apache/turbine/util/velocity/VelocityEmail.java
  
  Index: VelocityEmail.java
  ===================================================================
  package org.apache.turbine.util.velocity;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.turbine.services.velocity.TurbineVelocity;
  
  import org.apache.turbine.util.Log;
  import org.apache.turbine.util.StringUtils;
  import org.apache.turbine.util.mail.Email;
  import org.apache.turbine.util.mail.SimpleEmail;
  
  import org.apache.velocity.context.Context;
  
  /**
   * This is a simple class for sending email from within Velocity.
   * Essentially, the body of the email is processed with a 
   * Velocity Context object.
   * The beauty of this is that you can send email from within your
   * Velocity template or from your business logic in your Java code.
   * The body of the email is just a Velocity template so you can use
   * all the template functionality of Velocity within your emails!
   *
   * <p>Example Usage (This all needs to be on one line in your
   * template):
   *
   * <p>Setup your context:
   *
   * <p>context.put ("VelocityEmail", new VelocityEmail() );
   *
   * <p>Then, in your template:
   *
   * <pre>
   * $VelocityEmail.setTo("Jon Stevens", "jon@latchkey.com")
   *     .setFrom("Mom", "mom@mom.com").setSubject("Eat dinner")
   *     .setTemplate("email/momEmail.vm")
   *     .setContext($context)
   * </pre>
   *
   * The email/momEmail.wm template will then be parsed with the
   * Context that was defined with setContext().
   *
   * <p>If you want to use this class from within your Java code all you
   * have to do is something like this:
   *
   * <pre>
   * VelocityEmail ve = new VelocityEmail();
   * ve.setTo("Jon Stevens", "jon@latchkey.com");
   * ve.setFrom("Mom", "mom@mom.com").setSubject("Eat dinner");
   * ve.setContext(context);
   * ve.setTemplate("email/momEmail.vm")
   * ve.send();
   * </pre>
   *
   * <p>(Note that when used within a Velocity template, the send method
   * will be called for you when Velocity tries to convert the
   * VelocityEmail to a string by calling toString()).</p>
   *
   * <p>If you need your email to be word-wrapped, you can add the 
   * following call to those above:
   *
   * <pre>
   * ve.setWordWrap (60);
   * </pre>
   *
   * <p>This class is just a wrapper around the SimpleEmail class.
   * Thus, it uses the JavaMail API and also depends on having the
   * mail.server property set in the TurbineResources.properties file.
   * If you want to use this class outside of Turbine for general
   * processing that is also possible by making sure to set the path to
   * the TurbineResources.properties.  See the
   * TurbineConfig class for more information.</p>
   *
   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
   * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
   * @version $Id: VelocityEmail.java,v 1.1 2001/07/10 00:38:39 jvanzyl Exp $
   */
  public class VelocityEmail
  {
      /** The to name field. */
      private String toName = null;
  
      /** The to email field. */
      private String toEmail = null;
  
      /** The from name field. */
      private String fromName = null;
  
      /** The from email field. */
      private String fromEmail = null;
  
      /** The subject of the message. */
      private String subject = null;
  
      /** The column to word-wrap at.  <code>0</code> indicates no wrap. */
      private int wordWrap = 0;
  
      /**
       * The template to process, relative to WM's template
       * directory.
       */
      private String template = null;
  
      /**
       * A Context
       */
      private Context context = null;
  
      /**
       * Constructor
       */
      public VelocityEmail ()
      {
      }
  
      /**
       * Constructor
       */
      public VelocityEmail (Context context)
      {
          this.context = context;
      }
  
      /**
       * To: name, email
       *
       * @param to A String with the TO name.
       * @param email A String with the TO email.
       * @return A VelocityEmail (self).
       */
      public VelocityEmail setTo(String to,
                                 String email)
      {
          this.toName = to;
          this.toEmail = email;
          return (this);
      }
  
      /**
       * From: name, email.
       *
       * @param from A String with the FROM name.
       * @param email A String with the FROM email.
       * @return A VelocityEmail (self).
       */
      public VelocityEmail setFrom(String from,
                                   String email)
      {
          this.fromName = from;
          this.fromEmail = email;
          return (this);
      }
  
      /**
       * Subject.
       *
       * @param subject A String with the subject.
       * @return A VelocityEmail (self).
       */
      public VelocityEmail setSubject(String subject)
      {
          this.subject = subject;
          return (this);
      }
  
      /**
       * Velocity template to execute. Path is relative to the Velocity
       * templates directory.
       *
       * @param template A String with the template.
       * @return A VelocityEmail (self).
       */
      public VelocityEmail setTemplate(String template)
      {
          this.template = template;
          return (this);
      }
  
      /**
       * Set the column at which long lines of text should be word-
       * wrapped. Setting to zero turns off word-wrap (default).
       *
       * NOTE: don't use tabs in your email template document,
       * or your word-wrapping will be off for the lines with tabs
       * in them.
       *
       * @param wordWrap The column at which to wrap long lines.
       * @return A VelocityEmail (self).
       */
      public VelocityEmail setWordWrap(int wordWrap)
      {
          this.wordWrap = wordWrap;
          return (this);
      }
  
      /**
       * Set the context object that will be merged with the 
       * template.
       *
       * @param context A Velocity context object.
       * @return A VelocityEmail (self).
       */
      public VelocityEmail setContext(Context context)
      {
          this.context = context;
          return (this);
      }
  
      /**
       * Get the context object that will be merged with the 
       * template.
       *
       * @return A Context (self).
       */
      public Context getContext()
      {
          return this.context;
      }
  
      /**
       * This method sends the email.
       */
      public void send()
          throws Exception
      {
          // Process the template.
          String body = TurbineVelocity.handleRequest(context,template);
  
          // If the caller desires word-wrapping, do it here
          if (wordWrap > 0)
          {
              body = StringUtils.wrapText (body,
                                           System.getProperty("line.separator"),
                                           wordWrap);
          }
  
          SimpleEmail se = new SimpleEmail();
          se.setFrom(fromEmail, fromName);
          se.addTo(toEmail, toName);
          se.setSubject(subject);
          se.setMsg(body);
          se.send();
      }
  
      /**
       * The method toString() calls send() for ease of use within a
       * Velocity template (see example usage above).
       *
       * @return An empty string.
       */
      public String toString()
      {
          try
          {
              send();
          }
          catch (Exception e)
          {
              Log.error ("VelocityEmail error", e);
          }
          return "";
      }
  }
  
  
  
  1.1                  jakarta-turbine/src/adapter/org/apache/turbine/util/velocity/VelocityHtmlEmail.java
  
  Index: VelocityHtmlEmail.java
  ===================================================================
  package org.apache.turbine.util.velocity;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.net.URL;
  import java.util.Hashtable;
  
  import javax.mail.MessagingException;
  
  import org.apache.turbine.services.velocity.TurbineVelocity;
  import org.apache.turbine.services.velocity.VelocityService;
  
  import org.apache.turbine.util.Log;
  import org.apache.turbine.RunData;
  import org.apache.turbine.util.mail.HtmlEmail;
  import org.apache.turbine.util.mail.MultiPartEmail;
  import org.apache.turbine.util.template.TemplateInfo;
  
  import org.apache.velocity.context.Context;
  
  /**
   * This is a simple class for sending html email from within Velocity.
   * Essentially, the bodies (text and html) of the email are a Velocity
   * Context objects.  The beauty of this is that you can send email
   * from within your Velocity template or from your business logic in
   * your Java code.  The body of the email is just a Velocity template
   * so you can use all the template functionality of Velocity within
   * your emails!
   *
   * <p>This class allows you to send HTML email with embedded content
   * and/or with attachments.  You can access the VelocityHtmlEmail
   * instance within your templates trough the <code>$mail</code>
   * Velocity variable.
   * <p><code>VelocityHtmlEmail	myEmail= new VelocityHtmlEmail(context);<br>
   *                              context.put("mail", theMessage);</code>
   *
   *
   * <p>The templates should be located under your Template turbine
   * directory.
   *
   * <p>This class extends the HtmlEmail class.  Thus, it uses the
   * JavaMail API and also depends on having the mail.server property
   * set in the TurbineResources.properties file.  If you want to use
   * this class outside of Turbine for general processing that is also
   * possible by making sure to set the path to the
   * TurbineResources.properties.  See the
   * TurbineResourceService.setPropertiesFileName() method for more
   * information.
   *
   * <p>This class is basically a conversion of the WebMacroHtmlEmail
   * written by Regis Koenig
   *
   * @author <a href="mailto:A.Schild@aarboard.ch">Andre Schild</a>
   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
   * @version $Id: VelocityHtmlEmail.java,v 1.1 2001/07/10 00:38:39 jvanzyl Exp $
   */
  public class VelocityHtmlEmail
      extends HtmlEmail
  {
      /**
       * The html template to process, relative to VM's template
       * directory.
       */
      private String htmlTemplate = null;
  
      /**
       * A Context object which stores the information
       * needed to construct the email.
       */
      private Context context = null;
  
      /**
       * A Context object which stores the information
       * needed to construct the email.
       */
      private RunData data = null;
  
      /**
       * The text template to process, relative to VM's template
       * directory.
       */
      private String textTemplate = null;
  
      /** The map of embedded files. */
      private Hashtable embmap = null;
  
      /**
       * Constructor, sets the RunData object.
       *
       * @param data A Turbine RunData object.
       * @exception MessagingException.
       * @deprecated Please use the Context constructor instead.
       */
      public VelocityHtmlEmail(RunData data)
          throws MessagingException
      {
          super.init();
          this.data = data;
          embmap = new Hashtable();
      }
  
      /**
       * Constructor, sets the Context object.
       *
       * @param data A Velocity Context object.
       * @exception MessagingException.
       */
      public VelocityHtmlEmail(Context context)
          throws MessagingException
      {
          super.init();
          this.context = context;
          embmap = new Hashtable();
      }
  
      /**
       * Set the HTML template for the mail.  This is the Webmacro
       * template to execute for the HTML part.  Path is relative to the
       * VM templates directory.
       *
       * @param template A String.
       * @return A VelocityHtmlEmail (self).
       */
      public VelocityHtmlEmail setHtmlTemplate(String template)
      {
          this.htmlTemplate = template;
          return this;
      }
  
      /**
       * Set the text template for the mail.  This is the Velocity
       * template to execute for the text part.  Path is relative to the
       * VM templates directory
       *
       * @param template A String.
       * @return A VelocityHtmlEmail (self).
       */
      public VelocityHtmlEmail setTextTemplate(String template)
      {
          this.textTemplate = template;
          return this;
      }
  
      /**
       * Actually send the mail.
       *
       * @exception MessagingException.
       */
      public void send()
          throws MessagingException
      {
          if (data != null)
          {
              context = getContext(data);
          }
          context.put("mail",this);
  
          String htmlbody = "";
          String textbody = "";
  
          // Process the templates.
          try
          {
              if( htmlTemplate != null )
                  htmlbody = TurbineVelocity.handleRequest(context,
                                                           htmlTemplate);
              if( textTemplate != null )
                  textbody = TurbineVelocity.handleRequest(context,
                                                           textTemplate);
          }
          catch( Exception e)
          {
              throw new MessagingException("Cannot parse template", e);
          }
  
          setHtmlMsg(htmlbody);
          setTextMsg(textbody);
  
          super.send();
      }
  
      /**
       * Embed a file in the mail.  The file can be referenced through
       * its Content-ID.  This function also registers the CID in an
       * internal map, so the embedded file can be referenced more than
       * once by using the getCid() function.  This may be useful in a
       * template.
       *
       * <p>Example of template:
       *
       * <code><pre width="80">
       * &lt;html&gt;
       * &lt;!-- $mail.embed("http://server/border.gif","border.gif"); --&gt;
       * &lt;img src=$mail.getCid("border.gif")&gt;
       * &lt;p&gt;This is your content
       * &lt;img src=$mail.getCid("border.gif")&gt;
       * &lt;/html&gt;
       * </pre></code>
       *
       * @param surl A String.
       * @param name A String.
       * @return A String with the cid of the embedded file.
       * @exception MessagingException.
       * @see HtmlEmail#embed(URL surl, String name) embed.
       */
      public String embed(String surl,
                          String name)
          throws MessagingException
      {
          String cid ="";
          try
          {
              URL url = new URL(surl);
              cid = super.embed(url, name);
              embmap.put(name,cid);
          }
          catch( Exception e )
          {
              Log.error("cannot embed "+surl+": ", e);
          }
          return cid;
      }
  
      /**
       * Get the cid of an embedded file.
       *
       * @param filename A String.
       * @return A String with the cid of the embedded file.
       * @see #embed(String surl, String name) embed.
       */
      public String getCid(String filename)
      {
          String cid = (String)embmap.get(filename);
          return "cid:"+cid;
      }
  
      /**
       * Return the Context needed by Velocity.
       *
       * @param data A Turbine RunData object.
       * @return A Context.
       */
      private static Context getContext(RunData data)
      {
          // Attempt to get it from the RunData first.  If it doesn't
          // exist, create it and then stuff it into the RunData.
          Context vc = (Context)data.getTemplateInfo()
              .getTemplateContext(VelocityService.CONTEXT);
          if (vc == null)
          {
              vc = TurbineVelocity.getContext(data);
              data.getTemplateInfo()
                  .setTemplateContext(VelocityService.CONTEXT,
                                      vc);
          }
          return vc;
      }
  }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org