1 package org.apache.turbine.util.velocity;
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.velocity.TurbineVelocity;
61 import org.apache.turbine.services.velocity.VelocityService;
62 import org.apache.turbine.util.Log;
63 import org.apache.turbine.util.RunData;
64 import org.apache.turbine.util.mail.HtmlEmail;
65 import org.apache.velocity.context.Context;
66
67 /***
68 * This is a simple class for sending html email from within Velocity.
69 * Essentially, the bodies (text and html) of the email are a Velocity
70 * Context objects. The beauty of this is that you can send email
71 * from within your Velocity template or from your business logic in
72 * your Java code. The body of the email is just a Velocity template
73 * so you can use all the template functionality of Velocity within
74 * your emails!
75 *
76 * <p>This class allows you to send HTML email with embedded content
77 * and/or with attachments. You can access the VelocityHtmlEmail
78 * instance within your templates trough the <code>$mail</code>
79 * Velocity variable.
80 * <p><code>VelocityHtmlEmail myEmail= new VelocityHtmlEmail(data);<br>
81 * context.put("mail", theMessage);</code>
82 *
83 *
84 * <p>The templates should be located under your Template turbine
85 * directory.
86 *
87 * <p>This class extends the HtmlEmail class. Thus, it uses the
88 * JavaMail API and also depends on having the mail.server property
89 * set in the TurbineResources.properties file. If you want to use
90 * this class outside of Turbine for general processing that is also
91 * possible by making sure to set the path to the
92 * TurbineResources.properties. See the
93 * TurbineResourceService.setPropertiesFileName() method for more
94 * information.
95 *
96 * <p>This class is basically a conversion of the WebMacroHtmlEmail
97 * written by Regis Koenig
98 *
99 * @author <a href="mailto:A.Schild@aarboard.ch">Andre Schild</a>
100 * @version $Id: VelocityHtmlEmail.java,v 1.2 2002/07/11 16:53:19 mpoeschl Exp $
101 */
102 public class VelocityHtmlEmail extends HtmlEmail
103 {
104 /***
105 * The html template to process, relative to VM's template
106 * directory.
107 */
108 private String htmlTemplate = null;
109
110 /***
111 * The text template to process, relative to VM's template
112 * directory.
113 */
114 private String textTemplate = null;
115
116 /*** The cached rundata object. */
117 private RunData data = null;
118
119 /*** The map of embedded files. */
120 private Hashtable embmap = null;
121
122 /***
123 * Constructor, sets the RunData object.
124 *
125 * @param data A Turbine RunData object.
126 * @exception MessagingException.
127 */
128 public VelocityHtmlEmail(RunData data)
129 throws MessagingException
130 {
131 super.init();
132 this.data = data;
133 embmap = new Hashtable();
134 }
135
136 /***
137 * Set the HTML template for the mail. This is the Webmacro
138 * template to execute for the HTML part. Path is relative to the
139 * VM templates directory.
140 *
141 * @param template A String.
142 * @return A VelocityHtmlEmail (self).
143 */
144 public VelocityHtmlEmail setHtmlTemplate(String template)
145 {
146 this.htmlTemplate = template;
147 return this;
148 }
149
150 /***
151 * Set the text template for the mail. This is the Velocity
152 * template to execute for the text part. Path is relative to the
153 * VM templates directory
154 *
155 * @param template A String.
156 * @return A VelocityHtmlEmail (self).
157 */
158 public VelocityHtmlEmail setTextTemplate(String template)
159 {
160 this.textTemplate = template;
161 return this;
162 }
163
164 /***
165 * Actually send the mail.
166 *
167 * @exception MessagingException.
168 */
169 public void send()
170 throws MessagingException
171 {
172 Context context = getContext(data);
173 context.put("mail",this);
174
175 String htmlbody = "";
176 String textbody = "";
177
178 // Process the templates.
179 try
180 {
181 if (htmlTemplate != null)
182 {
183 htmlbody = TurbineVelocity.handleRequest(context, htmlTemplate);
184 }
185 if (textTemplate != null)
186 {
187 textbody = TurbineVelocity.handleRequest(context, textTemplate);
188 }
189 }
190 catch (Exception e)
191 {
192 throw new MessagingException("Cannot parse template", e);
193 }
194
195 setHtmlMsg(htmlbody);
196 setTextMsg(textbody);
197
198 super.send();
199 }
200
201 /***
202 * Embed a file in the mail. The file can be referenced through
203 * its Content-ID. This function also registers the CID in an
204 * internal map, so the embedded file can be referenced more than
205 * once by using the getCid() function. This may be useful in a
206 * template.
207 *
208 * <p>Example of template:
209 *
210 * <code><pre width="80">
211 * <html>
212 * <!-- $mail.embed("http://server/border.gif","border.gif"); -->
213 * <img src=$mail.getCid("border.gif")>
214 * <p>This is your content
215 * <img src=$mail.getCid("border.gif")>
216 * </html>
217 * </pre></code>
218 *
219 * @param surl A String.
220 * @param name A String.
221 * @return A String with the cid of the embedded file.
222 * @exception MessagingException.
223 * @see HtmlEmail#embed(URL surl, String name) embed.
224 */
225 public String embed(String surl, String name) throws MessagingException
226 {
227 String cid ="";
228 try
229 {
230 URL url = new URL(surl);
231 cid = super.embed(url, name);
232 embmap.put(name,cid);
233 }
234 catch (Exception e)
235 {
236 Log.error("cannot embed " + surl + ": ", e);
237 }
238 return cid;
239 }
240
241 /***
242 * Get the cid of an embedded file.
243 *
244 * @param filename A String.
245 * @return A String with the cid of the embedded file.
246 * @see #embed(String surl, String name) embed.
247 */
248 public String getCid(String filename)
249 {
250 String cid = (String) embmap.get(filename);
251 return "cid:" + cid;
252 }
253
254 /***
255 * Return the Context needed by Velocity.
256 *
257 * @param data A Turbine RunData object.
258 * @return A Context.
259 */
260 private static final Context getContext(RunData data)
261 {
262 // Attempt to get it from the RunData first. If it doesn't
263 // exist, create it and then stuff it into the RunData.
264 Context vc = (Context) data.getTemplateInfo()
265 .getTemplateContext(VelocityService.CONTEXT);
266 if (vc == null)
267 {
268 vc = TurbineVelocity.getContext(data);
269 data.getTemplateInfo()
270 .setTemplateContext(VelocityService.CONTEXT, vc);
271 }
272 return vc;
273 }
274 }
This page was automatically generated by Maven