Hello, anyone interested in reviewing this class and (hopefully) check it in ? Andre. package org.apache.turbine.util.velocity; /* * Copyright (c) 1997-2000 The Java Apache Project. 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. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine", * "Apache Turbine", "Turbine Project", "Apache Turbine Project" and * "Java Apache Project" must not be used to endorse or promote products * derived from this software without prior written permission. * * 5. Products derived from this software may not be called "Apache JServ" * nor may "Apache" nor "Apache JServ" appear in their names without * prior written permission of the Java Apache Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "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 JAVA APACHE PROJECT 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 Java Apache Group. For more information * on the Java Apache Project and the Apache JServ Servlet Engine project, * please see <http://java.apache.org/>. * */ // Java Stuff // import java.util.Map; import java.util.Hashtable; import java.net.URL; // Javax Stuff import javax.mail.MessagingException; // Turbine Stuff import org.apache.turbine.services.*; import org.apache.turbine.services.velocity.*; import org.apache.turbine.util.*; import org.apache.turbine.util.mail.*; import org.apache.turbine.util.db.Criteria; import org.apache.velocity.Context; // Velocity Stuff //import org.velocity.*; //import org.velocity.servlet.*; /** * 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(data);<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:unknown">Andre Schild</a> * @version $Id: VelocityHtmlEmail.java,v 0.1 2000/12/06 12:00:00 mcnally Exp $ */ public class VelocityHtmlEmail extends HtmlEmail { /** * The html template to process, relative to VM's template * directory. */ private String htmlTemplate = null; /** * The text template to process, relative to VM's template * directory. */ private String textTemplate = null; /** The cached rundata object. */ private RunData data = null; /** The map of embedded files. */ private Hashtable embmap = null; /** * Constructor, sets the RunData object. * * @param data A Turbine RunData object. * @exception MessagingException. */ public VelocityHtmlEmail(RunData data) throws MessagingException { super.init(); this.data = data; 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 { Context 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"> * <html> * <!-- $mail.embed("http://server/border.gif","border.gif"); --> * <img src=$mail.getCid("border.gif")> * <p>This is your content * <img src=$mail.getCid("border.gif")> * </html> * </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 final 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 subscribe: [EMAIL PROTECTED] To unsubscribe: [EMAIL PROTECTED] Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/> Problems?: [EMAIL PROTECTED]
