NIHVIVO-2299 Improve the way Email templates are handled - specify HTML and plain text in the same template. Refine the EmailDirective to permit optional parameters.

This commit is contained in:
j2blake 2011-06-15 18:23:07 +00:00
parent d42acf48d8
commit 5d587265fa
20 changed files with 237 additions and 387 deletions

View file

@ -57,6 +57,7 @@ public abstract class UserAccountsAddPageStrategy extends UserAccountsPage {
private static class EmailStrategy extends UserAccountsAddPageStrategy {
public static final String CREATE_PASSWORD_URL = "/accounts/createPassword";
private static final String EMAIL_TEMPLATE = "userAccounts-acctCreatedEmail.ftl";
private boolean sentEmail;
@ -91,15 +92,14 @@ public abstract class UserAccountsAddPageStrategy extends UserAccountsPage {
Map<String, Object> body = new HashMap<String, Object>();
body.put("userAccount", page.getAddedAccount());
body.put("passwordLink", buildCreatePasswordLink());
body.put("subjectLine", "Your VIVO account has been created.");
FreemarkerEmailMessage email = FreemarkerEmailFactory
.createNewMessage(vreq);
email.addRecipient(TO, page.getAddedAccount().getEmailAddress());
email.setSubject("Your VIVO account has been created.");
email.setHtmlTemplate("userAccounts-acctCreatedEmail-html.ftl");
email.setTextTemplate("userAccounts-acctCreatedEmail-text.ftl");
email.setTemplate(EMAIL_TEMPLATE);
email.setBodyMap(body);
email.processTemplate();
email.send();
sentEmail = true;

View file

@ -103,41 +103,22 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
Map<String, Object> body = new HashMap<String, Object>();
body.put("userAccount", page.getUpdatedAccount());
body.put("passwordLink", buildResetPasswordLink());
String siteName = getSiteName();
body.put("siteName", siteName);
body.put("siteName", getSiteName());
FreemarkerEmailMessage email = FreemarkerEmailFactory
.createNewMessage(vreq);
email.addRecipient(TO, page.getUpdatedAccount().getEmailAddress());
email.setTemplate(EMAIL_TEMPLATE);
email.setBodyMap(body);
email.setDefaultSubject(getDefaultSubject(siteName));
email.setDefaultHtml(getDefaultHtml());
email.setDefaultText(getDefaultText());
vreq.setAttribute("email", email);
email.processTemplate(vreq);
email.setBodyMap(body);
email.processTemplate();
email.send();
sentEmail = true;
}
private String getSiteName() {
ApplicationBean appBean = vreq.getAppBean();
return appBean.getApplicationName();
}
private String getDefaultSubject(String siteName) {
return siteName + " reset password request";
}
private String getDefaultHtml() {
return "";
}
private String getDefaultText() {
return "Default text for user accounts edit page";
ApplicationBean appBean = vreq.getAppBean();
return appBean.getApplicationName();
}
private String buildResetPasswordLink() {

View file

@ -26,6 +26,7 @@ public class UserAccountsCreatePasswordPage extends
.getLog(UserAccountsCreatePasswordPage.class);
private static final String TEMPLATE_NAME = "userAccounts-createPassword.ftl";
private static final String EMAIL_TEMPLATE = "userAccounts-passwordCreatedEmail.ftl";
public UserAccountsCreatePasswordPage(VitroRequest vreq) {
super(vreq);
@ -39,7 +40,7 @@ public class UserAccountsCreatePasswordPage extends
userAccountsDao.updateUserAccount(userAccount);
log.debug("Set password on '" + userAccount.getEmailAddress()
+ "' to '" + newPassword + "'");
notifyUser();
}
@ -56,15 +57,14 @@ public class UserAccountsCreatePasswordPage extends
private void notifyUser() {
Map<String, Object> body = new HashMap<String, Object>();
body.put("userAccount", userAccount);
body.put("subjectLine", "Password successfully created.");
FreemarkerEmailMessage email = FreemarkerEmailFactory
.createNewMessage(vreq);
email.addRecipient(TO, userAccount.getEmailAddress());
email.setSubject("Password successfully created.");
email.setHtmlTemplate("userAccounts-passwordCreatedEmail-html.ftl");
email.setTextTemplate("userAccounts-passwordCreatedEmail-text.ftl");
email.setTemplate(EMAIL_TEMPLATE);
email.setBodyMap(body);
email.processTemplate();
email.send();
}
}

View file

@ -52,6 +52,8 @@ public abstract class UserAccountsFirstTimeExternalPageStrategy extends
public static class EmailStrategy extends
UserAccountsFirstTimeExternalPageStrategy {
private static final String EMAIL_TEMPLATE = "userAccounts-firstTimeExternalEmail.ftl";
public EmailStrategy(VitroRequest vreq,
UserAccountsFirstTimeExternalPage page) {
super(vreq, page);
@ -66,15 +68,14 @@ public abstract class UserAccountsFirstTimeExternalPageStrategy extends
public void notifyUser(UserAccount ua) {
Map<String, Object> body = new HashMap<String, Object>();
body.put("userAccount", ua);
body.put("subjectLine", "Your VIVO account has been created.");
FreemarkerEmailMessage email = FreemarkerEmailFactory
.createNewMessage(vreq);
email.addRecipient(TO, ua.getEmailAddress());
email.setSubject("Your VIVO account has been created.");
email.setHtmlTemplate("userAccounts-firstTimeExternalEmail-html.ftl");
email.setTextTemplate("userAccounts-firstTimeExternalEmail-text.ftl");
email.setTemplate(EMAIL_TEMPLATE);
email.setBodyMap(body);
email.processTemplate();
email.send();
}

View file

@ -107,6 +107,8 @@ public abstract class UserAccountsMyAccountPageStrategy extends
private static final String ERROR_WRONG_PASSWORD_LENGTH = "errorPasswordIsWrongLength";
private static final String ERROR_PASSWORDS_DONT_MATCH = "errorPasswordsDontMatch";
private static final String EMAIL_TEMPLATE = "userAccounts-confirmEmailChangedEmail.ftl";
private final String originalEmail;
@ -167,15 +169,14 @@ public abstract class UserAccountsMyAccountPageStrategy extends
Map<String, Object> body = new HashMap<String, Object>();
body.put("userAccount", page.getUserAccount());
body.put("subjectLine", "Your VIVO email account has been changed.");
FreemarkerEmailMessage email = FreemarkerEmailFactory
.createNewMessage(vreq);
email.addRecipient(TO, page.getUserAccount().getEmailAddress());
email.setSubject("Your VIVO email account has been changed.");
email.setHtmlTemplate("userAccounts-confirmEmailChangedEmail-html.ftl");
email.setTextTemplate("userAccounts-confirmEmailChangedEmail-text.ftl");
email.setTemplate(EMAIL_TEMPLATE);
email.setBodyMap(body);
email.processTemplate();
email.send();
emailSent = true;

View file

@ -26,6 +26,8 @@ public class UserAccountsResetPasswordPage extends UserAccountsPasswordBasePage
private static final String TEMPLATE_NAME = "userAccounts-resetPassword.ftl";
private static final String EMAIL_TEMPLATE = "userAccounts-passwordResetEmail.ftl";
protected UserAccountsResetPasswordPage(VitroRequest vreq) {
super(vreq);
}
@ -38,7 +40,7 @@ public class UserAccountsResetPasswordPage extends UserAccountsPasswordBasePage
userAccountsDao.updateUserAccount(userAccount);
log.debug("Set password on '" + userAccount.getEmailAddress()
+ "' to '" + newPassword + "'");
notifyUser();
}
@ -55,15 +57,14 @@ public class UserAccountsResetPasswordPage extends UserAccountsPasswordBasePage
private void notifyUser() {
Map<String, Object> body = new HashMap<String, Object>();
body.put("userAccount", userAccount);
body.put("subjectLine", "Password changed.");
FreemarkerEmailMessage email = FreemarkerEmailFactory
.createNewMessage(vreq);
email.addRecipient(TO, userAccount.getEmailAddress());
email.setSubject("Password changed.");
email.setHtmlTemplate("userAccounts-passwordResetEmail-html.ftl");
email.setTextTemplate("userAccounts-passwordResetEmail-text.ftl");
email.setTemplate(EMAIL_TEMPLATE);
email.setBodyMap(body);
email.processTemplate();
email.send();
}

View file

@ -23,17 +23,13 @@ import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper.TemplateProcessingException;
import edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.Template;
@ -43,8 +39,14 @@ import freemarker.template.TemplateException;
* A framework that makes it simpler to send email messages with a body built
* from a Freemarker template.
*
* In fact, the body can be plain text from a template, HTML from a template, or
* both.
* The template must contain the @email directive, which may provide the subject
* line, the HTML content, and the plain text content. If these values are not
* provided by the directive, they default to empty strings, or to values that
* were set by the controller.
*
* The directive also calls the send() method here.
*
* @see EmailDirective
*/
public class FreemarkerEmailMessage {
private static final Log log = LogFactory
@ -55,22 +57,16 @@ public class FreemarkerEmailMessage {
private final HttpServletRequest req;
private final Session session;
private final Configuration config;
private final ServletContext ctx;
private final List<Recipient> recipients = new ArrayList<Recipient>();
private final InternetAddress replyToAddress;
private InternetAddress fromAddress = null;
private String subject = "";
private String defaultSubject = "";
private String defaultHtml = "";
private String defaultText = "";
private String templateName;
private String templateName = "";
private String htmlContent = "";
private String textContent = "";
private Map<String, Object> bodyMap = Collections.emptyMap();
// TO BE REMOVED
private String htmlTemplateName;
private String textTemplateName;
/**
* Package access - should only be created by the factory.
@ -81,8 +77,6 @@ public class FreemarkerEmailMessage {
this.session = session;
this.replyToAddress = replyToAddress;
this.ctx = req.getSession().getServletContext();
Object o = req.getAttribute(ATTRIBUTE_NAME);
if (!(o instanceof Configuration)) {
String oClass = (o == null) ? "null" : o.getClass().getName();
@ -129,46 +123,28 @@ public class FreemarkerEmailMessage {
try {
recipients.add(new Recipient(type, emailAddress, personalName));
} catch (AddressException e) {
log.warn("invalid recipient address: " + type + ", '"
+ emailAddress + "', personal name '" + personalName + "'");
return;
} catch (UnsupportedEncodingException e) {
log.warn("invalid recipient address: " + type + ", '"
+ emailAddress + "', personal name '" + personalName + "'");
return;
}
}
// TO BE REMOVED
public void setHtmlTemplate(String templateName) {
this.htmlTemplateName = nonNull(templateName, "");
}
// TO BE REMOVED
public void setTextTemplate(String templateName) {
this.textTemplateName = nonNull(templateName, "");
}
public void setSubject(String subject) {
this.subject = nonNull(subject, "");
}
public void setHtmlContent(String htmlContent) {
this.htmlContent = nonNull(htmlContent, "");
}
public void setTextContent(String textContent) {
this.textContent = nonNull(textContent, "");
}
public void setTemplate(String templateName) {
this.templateName = nonNull(templateName, "");
this.templateName = nonNull(templateName, "");
}
public void setDefaultSubject(String defaultSubject) {
this.defaultSubject = nonNull(defaultSubject, "");
}
public void setDefaultHtml(String defaultHtml) {
this.defaultHtml = nonNull(defaultHtml, "");
}
public void setDefaultText(String defaultText) {
this.defaultText = nonNull(defaultText, "");
}
public void setBodyMap(Map<String, Object> body) {
if (body == null) {
@ -177,87 +153,25 @@ public class FreemarkerEmailMessage {
this.bodyMap = new HashMap<String, Object>(body);
}
}
public void processTemplate(VitroRequest vreq) {
vreq.setAttribute("emailMessage", this);
bodyMap.putAll(FreemarkerHttpServlet.getDirectivesForAllEnvironments());
bodyMap.put("email", new edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective());
try {
Template template = config.getTemplate(templateName);
StringWriter writer = new StringWriter();
Environment env = template.createProcessingEnvironment(bodyMap, writer);
env.setCustomAttribute("request", vreq);
env.process();
} catch (TemplateException e) {
log.error(e, e);
} catch (IOException e) {
log.error(e, e);
}
}
public void send(String subject, String html, String text) {
try {
MimeMessage msg = new MimeMessage(session);
msg.setReplyTo(new Address[] { replyToAddress });
public void processTemplate() {
bodyMap.putAll(FreemarkerHttpServlet.getDirectivesForAllEnvironments());
bodyMap.put("email", new EmailDirective(this));
if (fromAddress == null) {
msg.addFrom(new Address[] { replyToAddress });
} else {
msg.addFrom(new Address[] { fromAddress });
}
for (Recipient recipient : recipients) {
msg.addRecipient(recipient.type, recipient.address);
}
if (subject == null) {
log.debug("No email subject specified in template. Using default subject.");
subject = defaultSubject;
}
msg.setSubject(subject);
if (html == null) {
log.debug("No html email specified in template. Using default html.");
html = defaultHtml;
}
if (text == null) {
log.debug("No plain text email specified in template. Using default html.");
text = defaultText;
}
if (StringUtils.isEmpty(text)) {
if (StringUtils.isEmpty(html)) {
log.error("Message has neither text body nor HTML body");
} else {
msg.setContent(html, "text/html");
}
} else if (StringUtils.isEmpty(html)) {
msg.setContent(text, "text/plain");
} else {
MimeMultipart content = new MimeMultipart("alternative");
addBodyPart(content, text, "text/plain");
addBodyPart(content, html, "text/html");
msg.setContent(content);
}
msg.setSentDate(new Date());
Transport.send(msg);
} catch (MessagingException e) {
log.error("Failed to send message.", e);
}
try {
Template template = config.getTemplate(templateName);
Environment env = template.createProcessingEnvironment(bodyMap,
new StringWriter());
env.setCustomAttribute("request", req);
env.process();
} catch (TemplateException e) {
log.error(e, e);
} catch (IOException e) {
log.error(e, e);
}
}
// TO BE REMOVED
public void send() {
String textBody = figureMessageBody(textTemplateName);
String htmlBody = figureMessageBody(htmlTemplateName);
try {
MimeMessage msg = new MimeMessage(session);
msg.setReplyTo(new Address[] { replyToAddress });
@ -274,19 +188,19 @@ public class FreemarkerEmailMessage {
msg.setSubject(subject);
if (textBody.isEmpty()) {
if (htmlBody.isEmpty()) {
if (textContent.isEmpty()) {
if (htmlContent.isEmpty()) {
log.error("Message has neither text body nor HTML body");
} else {
msg.setContent(htmlBody, "text/html");
msg.setContent(htmlContent, "text/html");
}
} else {
if (htmlBody.isEmpty()) {
msg.setContent(textBody, "text/plain");
if (htmlContent.isEmpty()) {
msg.setContent(textContent, "text/plain");
} else {
MimeMultipart content = new MimeMultipart("alternative");
addBodyPart(content, textBody, "text/plain");
addBodyPart(content, htmlBody, "text/html");
addBodyPart(content, textContent, "text/plain");
addBodyPart(content, htmlContent, "text/html");
msg.setContent(content);
}
}
@ -300,27 +214,6 @@ public class FreemarkerEmailMessage {
}
}
/**
* Process the template. If there is no template name or if there is a
* problem with the process, return an empty string.
*/
// TO BE REMOVED
private String figureMessageBody(String templateName) {
if (templateName.isEmpty()) {
return "";
}
try {
TemplateProcessingHelper helper = new TemplateProcessingHelper(
config, req, ctx);
return helper.processTemplate(templateName, bodyMap).toString();
} catch (TemplateProcessingException e) {
log.warn("Exception while processing email template '"
+ templateName + "'", e);
return "";
}
}
private void addBodyPart(MimeMultipart content, String textBody, String type)
throws MessagingException {
MimeBodyPart bodyPart = new MimeBodyPart();
@ -343,7 +236,7 @@ public class FreemarkerEmailMessage {
}
public Recipient(RecipientType type, String address, String personalName)
throws AddressException, UnsupportedEncodingException {
throws UnsupportedEncodingException {
this.type = type;
this.address = new InternetAddress(address, personalName);
}

View file

@ -9,8 +9,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -22,70 +20,79 @@ import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
/**
* Process the inputs for a FreemarkerEmailMessage.
*
* @see FreemarkerEmailMessage
*/
public class EmailDirective extends BaseTemplateDirectiveModel {
private static final Log log = LogFactory.getLog(EmailDirective.class);
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
private static final Log log = LogFactory.getLog(EmailDirective.class);
HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request");
FreemarkerEmailMessage email = null;
private final FreemarkerEmailMessage message;
Object paramValue = (FreemarkerEmailMessage) request.getAttribute("emailMessage");
if ( paramValue == null) {
throw new TemplateModelException(
"No email message object found in the request.");
}
if ( ! (paramValue instanceof FreemarkerEmailMessage)) {
throw new TemplateModelException(
"Invalid value for request email attribute");
}
email = (FreemarkerEmailMessage) paramValue;
public EmailDirective(FreemarkerEmailMessage message) {
this.message = message;
}
// Read in parameter values. If a value is undefined by the template, the
// default values defined by the email object will be used.
String subject = null;
paramValue = params.get("subject");
if (paramValue != null && paramValue instanceof SimpleScalar) {
subject = paramValue.toString();
}
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
String html = null;
paramValue = params.get("html");
if (paramValue != null && paramValue instanceof SimpleScalar) {
html = paramValue.toString();
}
String text = null;
paramValue = params.get("text");
if (paramValue != null && paramValue instanceof SimpleScalar) {
text = paramValue.toString();
}
String subject = getOptionalSimpleScalarParameter(params, "subject");
if (subject != null) {
message.setSubject(subject);
}
email.send(subject, html, text);
}
@Override
public Map<String, Object> help(String name) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
String htmlContent = getOptionalSimpleScalarParameter(params, "html");
if (htmlContent != null) {
message.setHtmlContent(htmlContent);
}
map.put("effect", "Create an email message from the parameters set in the invoking template.");
map.put("comment", "Parameter values undefined by the template will be provided by controller default values.");
Map<String, String> params = new HashMap<String, String>();
params.put("subject", "email subject (optional)");
params.put("html", "HTML version of email message (optional)");
params.put("text", "Plain text version of email message (optional)");
map.put("parameters", params);
String textContent = getOptionalSimpleScalarParameter(params, "text");
if (textContent != null) {
message.setTextContent(textContent);
}
List<String> examples = new ArrayList<String>();
examples.add("&lt;email subject=\"Password reset confirmation\" html=html text=text&gt;");
if ((htmlContent == null) && (textContent == null)) {
throw new TemplateModelException("The email directive must have "
+ "either a 'html' parameter or a 'text' parameter.");
}
}
private String getOptionalSimpleScalarParameter(Map<?, ?> params,
String name) throws TemplateModelException {
Object o = params.get(name);
if (o == null) {
return null;
}
if (!(o instanceof SimpleScalar)) {
throw new TemplateModelException("The '" + name + "' parameter "
+ "for the email directive must be a string value.");
}
return o.toString();
}
@Override
public Map<String, Object> help(String name) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("effect",
"Create an email message from the parameters set in the invoking template.");
Map<String, String> params = new HashMap<String, String>();
params.put("subject", "email subject (optional)");
params.put("html", "HTML version of email message (optional)");
params.put("text", "Plain text version of email message (optional)");
map.put("parameters", params);
List<String> examples = new ArrayList<String>();
examples.add("&lt;email subject=\"Password reset confirmation\" html=html text=text&gt;");
examples.add("&lt;email html=html text=text&gt;");
map.put("examples", examples);
return map;
}
map.put("examples", examples);
return map;
}
}