blob: 0f3b8c1fefcfd45e386817961bc413444a31b1a4 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>VelocityHtmlEmail.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.util.velocity</a> &gt; <span class="el_source">VelocityHtmlEmail.java</span></div><h1>VelocityHtmlEmail.java</h1><pre class="source lang-java linenums">package org.apache.turbine.util.velocity;
import java.net.MalformedURLException;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.net.URL;
import java.util.Hashtable;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail2.core.EmailException;
import org.apache.commons.mail2.jakarta.HtmlEmail;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.Turbine;
import org.apache.turbine.TurbineConstants;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.velocity.VelocityService;
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!
*
* &lt;p&gt;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 &lt;code&gt;$mail&lt;/code&gt;
* Velocity variable.
* &lt;p&gt;&lt;code&gt;VelocityHtmlEmail myEmail= new VelocityHtmlEmail(data);&lt;br&gt;
* context.put(&quot;mail&quot;, myMail);&lt;/code&gt;
* &lt;b&gt;or&lt;/b&gt;
* &lt;code&gt;VelocityHtmlEmail myEmail= new VelocityHtmlEmail(context);&lt;br&gt;
* context.put(&quot;mail&quot;, myMail);&lt;/code&gt;
*
*
* &lt;p&gt;The templates should be located under your Template turbine
* directory.
*
* &lt;p&gt;This class wraps the HtmlEmail class from commons-email. 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.
*
* &lt;p&gt;This class is basically a conversion of the WebMacroHtmlEmail
* written by Regis Koenig
*
* &lt;p&gt;You can turn on debugging for the JavaMail API by calling
* setDebug(true). The debugging messages will be written to System.out.
*
* @author &lt;a href=&quot;mailto:epugh@upstate.com&quot;&gt;Eric Pugh&lt;/a&gt;
* @author &lt;a href=&quot;mailto:A.Schild@aarboard.ch&quot;&gt;Andre Schild&lt;/a&gt;
* @author &lt;a href=&quot;mailto:quintonm@bellsouth.net&quot;&gt;Quinton McCombs&lt;/a&gt;
* @author &lt;a href=&quot;mailto:hps@intermeta.de&quot;&gt;Henning P. Schmiedehausen&lt;/a&gt;
* @author &lt;a href=&quot;mailto:peter@courcoux.biz&quot;&gt;Peter Courcoux&lt;/a&gt;
* @version $Id$
*/
public class VelocityHtmlEmail extends HtmlEmail
{
/** Logging */
<span class="nc" id="L88"> private static final Logger log = LogManager.getLogger(VelocityHtmlEmail.class);</span>
/**
* The html template to process, relative to VM's template
* directory.
*/
<span class="nc" id="L94"> private String htmlTemplate = null;</span>
/**
* The text template to process, relative to VM's template
* directory.
*/
<span class="nc" id="L100"> private String textTemplate = null;</span>
/** The cached context object. */
<span class="nc" id="L103"> private Context context = null;</span>
/** The map of embedded files. */
<span class="nc" id="L106"> private Hashtable&lt;String, String&gt; embmap = null;</span>
/** Address of outgoing mail server */
private String mailServer;
/**
* Constructor
*/
public VelocityHtmlEmail()
{
<span class="nc" id="L116"> super();</span>
<span class="nc" id="L117"> }</span>
/**
* Constructor, sets the context object.
*
* @param context A Velocity context object.
*/
public VelocityHtmlEmail(Context context)
{
<span class="nc" id="L126"> this();</span>
<span class="nc" id="L127"> this.context = context;</span>
<span class="nc" id="L128"> embmap = new Hashtable&lt;&gt;();</span>
<span class="nc" id="L129"> }</span>
/**
* Set the HTML template for the mail. This is the Velocity
* 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)
{
<span class="nc" id="L141"> this.htmlTemplate = template;</span>
<span class="nc" id="L142"> return this;</span>
}
/**
* 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)
{
<span class="nc" id="L155"> this.textTemplate = template;</span>
<span class="nc" id="L156"> return this;</span>
}
/**
* Sets the address of the outgoing mail server. This method
* should be used when you need to override the value stored in
* TR.props.
*
* @param serverAddress host name of your outgoing mail server
*/
public void setMailServer(String serverAddress)
{
<span class="nc" id="L168"> this.mailServer = serverAddress;</span>
<span class="nc" id="L169"> }</span>
/**
* Gets the host name of the outgoing mail server. If the server
* name has not been set by calling setMailServer(), the value
* from TR.props for mail.server will be returned. If TR.props
* has no value for mail.server, localhost will be returned.
*
* @return host name of the mail server.
*/
public String getMailServer()
{
<span class="nc bnc" id="L181" title="All 2 branches missed."> return StringUtils.isNotEmpty(mailServer) ? mailServer</span>
<span class="nc" id="L182"> : Turbine.getConfiguration().getString(</span>
TurbineConstants.MAIL_SERVER_KEY,
TurbineConstants.MAIL_SERVER_DEFAULT);
}
/**
* Actually send the mail.
*
* @throws EmailException thrown if mail cannot be sent.
*/
@Override
public String send() throws EmailException
{
<span class="nc" id="L195"> context.put(&quot;mail&quot;, this);</span>
try
{
<span class="nc" id="L199"> VelocityService velocityService = (VelocityService)TurbineServices.getInstance().getService(VelocityService.SERVICE_NAME);</span>
<span class="nc bnc" id="L201" title="All 2 branches missed."> if (htmlTemplate != null)</span>
{
<span class="nc" id="L203"> setHtmlMsg(velocityService.handleRequest(context, htmlTemplate));</span>
}
<span class="nc bnc" id="L205" title="All 2 branches missed."> if (textTemplate != null)</span>
{
<span class="nc" id="L207"> setTextMsg(velocityService.handleRequest(context, textTemplate));</span>
}
}
<span class="nc" id="L210"> catch (Exception e)</span>
{
<span class="nc" id="L212"> throw new EmailException(&quot;Cannot parse velocity template&quot;, e);</span>
<span class="nc" id="L213"> }</span>
<span class="nc" id="L214"> setHostName(getMailServer());</span>
<span class="nc" id="L215"> return super.send();</span>
}
/**
* 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.
*
* &lt;p&gt;Example of template:
*
* {@code
* &lt;html&gt;
* &lt;!-- $mail.embed(&quot;http://server/border.gif&quot;,&quot;border.gif&quot;); --&gt;
* &lt;img src=$mail.getCid(&quot;border.gif&quot;)&gt;
* &lt;p&gt;This is your content
* &lt;img src=$mail.getCid(&quot;border.gif&quot;)&gt;
* &lt;/html&gt;
* }
*
* @param surl A String.
* @param name A String.
* @return A String with the cid of the embedded file.
*
* @see HtmlEmail#embed(URL surl, String name) embed.
*/
@Override
public String embed(String surl, String name)
{
<span class="nc" id="L245"> String cid = &quot;&quot;;</span>
try
{
<span class="nc" id="L248"> URL url = new URL(surl);</span>
<span class="nc" id="L249"> cid = super.embed(url, name);</span>
<span class="nc" id="L250"> embmap.put(name, cid);</span>
}
<span class="nc" id="L252"> catch (MalformedURLException | EmailException e)</span>
{
<span class="nc" id="L254"> log.error(&quot;cannot embed {}&quot;, surl, e);</span>
<span class="nc" id="L255"> }</span>
<span class="nc" id="L256"> return cid;</span>
}
/**
* 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)
{
<span class="nc" id="L268"> String cid = embmap.get(filename);</span>
<span class="nc" id="L269"> return &quot;cid:&quot; + cid;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>