From 5d587265fae96c82a35279d76fbcf6c8eb2037c4 Mon Sep 17 00:00:00 2001 From: j2blake Date: Wed, 15 Jun 2011 18:23:07 +0000 Subject: [PATCH] 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. --- .../admin/UserAccountsAddPageStrategy.java | 6 +- .../admin/UserAccountsEditPageStrategy.java | 33 +-- .../user/UserAccountsCreatePasswordPage.java | 8 +- ...AccountsFirstTimeExternalPageStrategy.java | 7 +- .../UserAccountsMyAccountPageStrategy.java | 7 +- .../user/UserAccountsResetPasswordPage.java | 9 +- .../webapp/email/FreemarkerEmailMessage.java | 195 ++++-------------- .../webapp/web/directives/EmailDirective.java | 123 +++++------ .../userAccounts-acctCreatedEmail-text.ftl | 20 -- ....ftl => userAccounts-acctCreatedEmail.ftl} | 29 ++- ...Accounts-confirmEmailChangedEmail-text.ftl | 10 - ...userAccounts-confirmEmailChangedEmail.ftl} | 19 +- ...erAccounts-firstTimeExternalEmail-text.ftl | 12 -- ...> userAccounts-firstTimeExternalEmail.ftl} | 21 +- ...userAccounts-passwordCreatedEmail-text.ftl | 12 -- ... => userAccounts-passwordCreatedEmail.ftl} | 21 +- .../userAccounts-passwordResetEmail-text.ftl | 12 -- ...tl => userAccounts-passwordResetEmail.ftl} | 21 +- .../userAccounts-resetPasswordEmail-html.ftl | 40 ---- .../userAccounts-resetPasswordEmail-text.ftl | 19 -- 20 files changed, 237 insertions(+), 387 deletions(-) delete mode 100644 webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail-text.ftl rename webapp/web/templates/freemarker/body/accounts/{userAccounts-acctCreatedEmail-html.ftl => userAccounts-acctCreatedEmail.ftl} (60%) delete mode 100644 webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail-text.ftl rename webapp/web/templates/freemarker/body/accounts/{userAccounts-confirmEmailChangedEmail-html.ftl => userAccounts-confirmEmailChangedEmail.ftl} (55%) delete mode 100644 webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail-text.ftl rename webapp/web/templates/freemarker/body/accounts/{userAccounts-firstTimeExternalEmail-html.ftl => userAccounts-firstTimeExternalEmail.ftl} (58%) delete mode 100644 webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail-text.ftl rename webapp/web/templates/freemarker/body/accounts/{userAccounts-passwordCreatedEmail-html.ftl => userAccounts-passwordCreatedEmail.ftl} (57%) delete mode 100644 webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail-text.ftl rename webapp/web/templates/freemarker/body/accounts/{userAccounts-passwordResetEmail-html.ftl => userAccounts-passwordResetEmail.ftl} (58%) delete mode 100644 webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-html.ftl delete mode 100644 webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-text.ftl diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAddPageStrategy.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAddPageStrategy.java index 9061030e7..fe1e1c489 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAddPageStrategy.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAddPageStrategy.java @@ -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 body = new HashMap(); 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; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java index 3ca3c739c..b043c2422 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsEditPageStrategy.java @@ -103,41 +103,22 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage { Map body = new HashMap(); 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() { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsCreatePasswordPage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsCreatePasswordPage.java index d134fd737..40ae4d6d0 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsCreatePasswordPage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsCreatePasswordPage.java @@ -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 body = new HashMap(); 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(); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsFirstTimeExternalPageStrategy.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsFirstTimeExternalPageStrategy.java index 71ce98161..975551fdc 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsFirstTimeExternalPageStrategy.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsFirstTimeExternalPageStrategy.java @@ -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 body = new HashMap(); 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(); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsMyAccountPageStrategy.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsMyAccountPageStrategy.java index f220d3236..d962165cd 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsMyAccountPageStrategy.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsMyAccountPageStrategy.java @@ -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 body = new HashMap(); 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; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsResetPasswordPage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsResetPasswordPage.java index 7902cbe5b..3bd511b83 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsResetPasswordPage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/user/UserAccountsResetPasswordPage.java @@ -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 body = new HashMap(); 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(); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java index fac7981cf..906c46dea 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java @@ -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 recipients = new ArrayList(); 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 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 body) { if (body == null) { @@ -177,87 +153,25 @@ public class FreemarkerEmailMessage { this.bodyMap = new HashMap(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); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java index e492d40a7..58ca99ada 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/EmailDirective.java @@ -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 help(String name) { - Map map = new LinkedHashMap(); + 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 params = new HashMap(); - 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 examples = new ArrayList(); - examples.add("<email subject=\"Password reset confirmation\" html=html text=text>"); + 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 help(String name) { + Map map = new LinkedHashMap(); + + map.put("effect", + "Create an email message from the parameters set in the invoking template."); + + Map params = new HashMap(); + 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 examples = new ArrayList(); + examples.add("<email subject=\"Password reset confirmation\" html=html text=text>"); examples.add("<email html=html text=text>"); - map.put("examples", examples); - - return map; - } + map.put("examples", examples); + + return map; + } } diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail-text.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail-text.ftl deleted file mode 100644 index 69b8d5f30..000000000 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail-text.ftl +++ /dev/null @@ -1,20 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Confirmation that an account has been created. --> - -${userAccount.firstName} ${userAccount.lastName} - -Congratulations! - -We have created your new VIVO account associated with -${userAccount.emailAddress}. - -If you did not request this new account you can safely ignore this email. -This request will expire if not acted upon for 30 days. - -Paste the link below into your browser's address bar to create your password -for your new account using our secure server. - -${passwordLink} - -Thanks! diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail-html.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail.ftl similarity index 60% rename from webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail-html.ftl rename to webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail.ftl index 5fa2c5f44..4aa355468 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail-html.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-acctCreatedEmail.ftl @@ -2,9 +2,12 @@ <#-- Confirmation that an account has been created. --> +<#assign subject = "Your VIVO account has been created." /> + +<#assign html> - ${subjectLine} + ${subject}

@@ -40,4 +43,26 @@ Thanks!

- \ No newline at end of file + + + +<#assign text> +${userAccount.firstName} ${userAccount.lastName} + +Congratulations! + +We have created your new VIVO account associated with +${userAccount.emailAddress}. + +If you did not request this new account you can safely ignore this email. +This request will expire if not acted upon for 30 days. + +Paste the link below into your browser's address bar to create your password +for your new account using our secure server. + +${passwordLink} + +Thanks! + + +<@email subject=subject html=html text=text /> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail-text.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail-text.ftl deleted file mode 100644 index 78f1457bf..000000000 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail-text.ftl +++ /dev/null @@ -1,10 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Confirmation that the user has changed his email account. --> - -Hi, ${userAccount.firstName} ${userAccount.lastName} - -You recently changed the email address associated with -${userAccount.firstName} ${userAccount.lastName} - -Thank you. diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail-html.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail.ftl similarity index 55% rename from webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail-html.ftl rename to webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail.ftl index e06f7e1ae..9acec6fcf 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail-html.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-confirmEmailChangedEmail.ftl @@ -2,9 +2,12 @@ <#-- Confirmation that the user has changed his email account. --> +<#assign subject = "Your VIVO email account has been changed." /> + +<#assign html> - ${subjectLine} + ${subject}

@@ -20,4 +23,16 @@ Thank you.

- \ No newline at end of file + + + +<#assign text> +Hi, ${userAccount.firstName} ${userAccount.lastName} + +You recently changed the email address associated with +${userAccount.firstName} ${userAccount.lastName} + +Thank you. + + +<@email subject=subject html=html text=text /> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail-text.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail-text.ftl deleted file mode 100644 index 8b788d56d..000000000 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail-text.ftl +++ /dev/null @@ -1,12 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Confirmation that an account has been created for an externally-authenticated user. --> - -${userAccount.firstName} ${userAccount.lastName} - -Congratulations! - -We have created your new VIVO account associated with -${userAccount.emailAddress}. - -Thanks! diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail-html.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail.ftl similarity index 58% rename from webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail-html.ftl rename to webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail.ftl index 5faa53dff..434c65851 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail-html.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternalEmail.ftl @@ -2,9 +2,12 @@ <#-- Confirmation that an account has been created for an externally-authenticated user. --> +<#assign subject = "Your VIVO account has been created." /> + +<#assign html> - ${subjectLine} + ${subject}

@@ -23,4 +26,18 @@ Thanks!

- \ No newline at end of file + + + +<#assign text> +${userAccount.firstName} ${userAccount.lastName} + +Congratulations! + +We have created your new VIVO account associated with +${userAccount.emailAddress}. + +Thanks! + + +<@email subject=subject html=html text=text /> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail-text.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail-text.ftl deleted file mode 100644 index 2a339d4a9..000000000 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail-text.ftl +++ /dev/null @@ -1,12 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Confirmation that a password has been created. --> - -${userAccount.firstName} ${userAccount.lastName} - -Password successfully created. - -Your new password associated with ${userAccount.emailAddress} -has been created. - -Thank you. diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail-html.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail.ftl similarity index 57% rename from webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail-html.ftl rename to webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail.ftl index 1773766e9..a99c51e08 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail-html.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordCreatedEmail.ftl @@ -2,9 +2,12 @@ <#-- Confirmation that an password has been created. --> +<#assign subject = "Password successfully created." /> + +<#assign html> - ${subjectLine} + ${subject}

@@ -23,4 +26,18 @@ Thank you.

- \ No newline at end of file + + + +<#assign text> +${userAccount.firstName} ${userAccount.lastName} + +Password successfully created. + +Your new password associated with ${userAccount.emailAddress} +has been created. + +Thank you. + + +<@email subject=subject html=html text=text /> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail-text.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail-text.ftl deleted file mode 100644 index cfe0cd00f..000000000 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail-text.ftl +++ /dev/null @@ -1,12 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Confirmation that a password has been reset. --> - -${userAccount.firstName} ${userAccount.lastName} - -Password successfully changed. - -Your new password associated with ${userAccount.emailAddress} -has been changed. - -Thank you. diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail-html.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail.ftl similarity index 58% rename from webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail-html.ftl rename to webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail.ftl index 49c4eb531..6650ccd8e 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail-html.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-passwordResetEmail.ftl @@ -2,9 +2,12 @@ <#-- Confirmation that a password has been reset. --> +<#assign subject = "Password changed." /> + +<#assign html> - ${subjectLine} + ${subject}

@@ -23,4 +26,18 @@ Thank you.

- \ No newline at end of file + + + +<#assign text> +${userAccount.firstName} ${userAccount.lastName} + +Password successfully changed. + +Your new password associated with ${userAccount.emailAddress} +has been changed. + +Thank you. + + +<@email subject=subject html=html text=text /> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-html.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-html.ftl deleted file mode 100644 index 0fa37fba3..000000000 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-html.ftl +++ /dev/null @@ -1,40 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Notification that your password has been reset. --> - - - - ${subjectLine} - - -

- ${userAccount.firstName} ${userAccount.lastName} -

- -

- We received a request to reset the password for your account (${userAccount.emailAddress}). - Please follow the instructions below to proceed with your password reset. -

- -

- If you did not request this new account you can safely ignore this email. - This request will expire if not acted upon for 30 days. -

- -

- Click the link below to reset your password using our secure server. -

- -

- ${passwordLink} -

- -

- If the link above doesn't work, you can copy and paste the link directly into your browser's address bar. -

- -

- Thank you! -

- - \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-text.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-text.ftl deleted file mode 100644 index 82624c327..000000000 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-resetPasswordEmail-text.ftl +++ /dev/null @@ -1,19 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Notification that your password has been reset. --> - -${userAccount.firstName} ${userAccount.lastName} - -We received a request to reset the password for your account -(${userAccount.emailAddress}). -Please follow the instructions below to proceed with your password reset. - -If you did not request this new account you can safely ignore this email. -This request will expire if not acted upon for 30 days. - -Paste the link below into your browser's address bar to reset your password -using our secure server. - -${passwordLink} - -Thank you!