View Javadoc
1 package org.apache.turbine.util.webmacro; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.net.URL; 58 import java.util.Hashtable; 59 import javax.mail.MessagingException; 60 import org.apache.turbine.services.webmacro.TurbineWebMacro; 61 import org.apache.turbine.util.Log; 62 import org.apache.turbine.util.mail.HtmlEmail; 63 import org.webmacro.servlet.WebContext; 64 65 /*** 66 * This is a simple class for sending html email from within WebMacro. 67 * Essentially, the bodies (text and html) of the email are a WebMacro 68 * Context objects. The beauty of this is that you can send email 69 * from within your WebMacro template or from your business logic in 70 * your Java code. The body of the email is just a WebMacro template 71 * so you can use all the template functionality of WebMacro within 72 * your emails! 73 * 74 * <p>This class allows you to send HTML email with embedded content 75 * and/or with attachments. You can access the WebMacroHtmlEmail 76 * instance within your templates trough the <code>$mail</code> 77 * WebMacro variable. 78 * 79 * <p>The templates should be located under your Template turbine 80 * directory. 81 * 82 * <p>This class extends the HtmlEmail class. Thus, it uses the 83 * JavaMail API and also depends on having the mail.server property 84 * set in the TurbineResources.properties file. If you want to use 85 * this class outside of Turbine for general processing that is also 86 * possible by making sure to set the path to the 87 * TurbineResources.properties. See the 88 * TurbineConfig class for more information. 89 * 90 * @author <a href="mailto:unknown">Regis Koenig</a> 91 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 92 * @version $Id: WebMacroHtmlEmail.java,v 1.2 2002/07/11 16:53:19 mpoeschl Exp $ 93 * @deprecated you should use velocity 94 */ 95 public class WebMacroHtmlEmail 96 extends HtmlEmail 97 { 98 /*** 99 * The html template to process, relative to WM's template 100 * directory. 101 */ 102 private String htmlTemplate = null; 103 104 /*** 105 * The text template to process, relative to WM's template 106 * directory. 107 */ 108 private String textTemplate = null; 109 110 /*** The map of embedded files. */ 111 private Hashtable embmap = null; 112 113 /*** 114 * A WebContext 115 */ 116 private WebContext context = null; 117 118 /*** 119 * Constructor, sets the RunData object. 120 * 121 * @param data A Turbine RunData object. 122 * @exception MessagingException. 123 */ 124 public WebMacroHtmlEmail() 125 throws MessagingException 126 { 127 super.init(); 128 embmap = new Hashtable(); 129 } 130 131 /*** 132 * Set the HTML template for the mail. This is the Webmacro 133 * template to execute for the HTML part. Path is relative to the 134 * WM templates directory. 135 * 136 * @param template A String. 137 * @return A WebMacroHtmlEmail (self). 138 */ 139 public WebMacroHtmlEmail setHtmlTemplate(String template) 140 { 141 this.htmlTemplate = template; 142 return this; 143 } 144 145 /*** 146 * Set the text template for the mail. This is the Webmacro 147 * template to execute for the text part. Path is relative to the 148 * WM templates directory 149 * 150 * @param template A String. 151 * @return A WebMacroHtmlEmail (self). 152 */ 153 public WebMacroHtmlEmail setTextTemplate(String template) 154 { 155 this.textTemplate = template; 156 return this; 157 } 158 159 /*** 160 * Set the context object that will be merged with the 161 * template. 162 * 163 * @param context A WebMacro context object. 164 * @return A WebMacroEmail (self). 165 */ 166 public WebMacroHtmlEmail setContext(WebContext context) 167 { 168 this.context = context; 169 return (this); 170 } 171 172 /*** 173 * Get the context object that will be merged with the 174 * template. 175 * 176 * @return A WebContext (self). 177 */ 178 public WebContext getContext() 179 { 180 return this.context; 181 } 182 183 /*** 184 * Actually send the mail. 185 * 186 * @exception MessagingException. 187 */ 188 public void send() 189 throws MessagingException 190 { 191 context.put("mail",this); 192 193 String htmlbody = ""; 194 String textbody = ""; 195 196 // Process the templates. 197 try 198 { 199 if( htmlTemplate != null ) 200 htmlbody = TurbineWebMacro.handleRequest(context, 201 htmlTemplate); 202 if( textTemplate != null ) 203 textbody = TurbineWebMacro.handleRequest(context, 204 textTemplate); 205 } 206 catch( Exception e) 207 { 208 throw new MessagingException("Cannot parse template", e); 209 } 210 211 setHtmlMsg(htmlbody); 212 setTextMsg(textbody); 213 214 super.send(); 215 } 216 217 /*** 218 * Embed a file in the mail. The file can be referenced through 219 * its Content-ID. This function also registers the CID in an 220 * internal map, so the embedded file can be referenced more than 221 * once by using the getCid() function. This may be useful in a 222 * template. 223 * 224 * <p>Example of template: 225 * 226 * <code><pre width="80"> 227 * <html> 228 * <!-- $mail.embed("http://server/border.gif","border.gif"); --> 229 * <img src=$mail.getCid("border.gif")> 230 * <p>This is your content 231 * <img src=$mail.getCid("border.gif")> 232 * </html> 233 * </pre></code> 234 * 235 * @param surl A String. 236 * @param name A String. 237 * @return A String with the cid of the embedded file. 238 * @exception MessagingException. 239 * @see HtmlEmail#embed(URL surl, String name) embed. 240 */ 241 public String embed(String surl, 242 String name) 243 throws MessagingException 244 { 245 String cid =""; 246 try 247 { 248 URL url = new URL(surl); 249 cid = super.embed(url, name); 250 embmap.put(name, cid); 251 } 252 catch (Exception e) 253 { 254 Log.error("cannot embed " + surl + ": ", e); 255 } 256 return cid; 257 } 258 259 /*** 260 * Get the cid of an embedded file. 261 * 262 * @param filename A String. 263 * @return A String with the cid of the embedded file. 264 * @see #embed(String surl, String name) embed. 265 */ 266 public String getCid(String filename) 267 { 268 String cid = (String) embmap.get(filename); 269 return "cid:" + cid; 270 } 271 }

This page was automatically generated by Maven