NIHVIVO-2693 Freemarker email messaging code refactoring

This commit is contained in:
ryounes 2011-06-14 13:26:52 +00:00
parent 2fcff042e6
commit f5c44298e1
6 changed files with 46 additions and 33 deletions

View file

@ -200,7 +200,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
userAccountsDao.updateUserAccount(userAccount); userAccountsDao.updateUserAccount(userAccount);
strategy.notifyUser(vreq); strategy.notifyUser();
} }
public boolean wasPasswordEmailSent() { public boolean wasPasswordEmailSent() {

View file

@ -9,8 +9,8 @@ import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount.Status;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsPage; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsPage;
import edu.cornell.mannlib.vitro.webapp.controller.authenticate.Authenticator; import edu.cornell.mannlib.vitro.webapp.controller.authenticate.Authenticator;
@ -47,7 +47,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
protected abstract void setAdditionalProperties(UserAccount u); protected abstract void setAdditionalProperties(UserAccount u);
protected abstract void notifyUser(VitroRequest vreq); protected abstract void notifyUser();
protected abstract boolean wasPasswordEmailSent(); protected abstract boolean wasPasswordEmailSent();
@ -95,7 +95,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
} }
@Override @Override
protected void notifyUser(VitroRequest vreq) { protected void notifyUser() {
if (!resetPassword) { if (!resetPassword) {
return; return;
} }
@ -103,25 +103,26 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
Map<String, Object> body = new HashMap<String, Object>(); Map<String, Object> body = new HashMap<String, Object>();
body.put("userAccount", page.getUpdatedAccount()); body.put("userAccount", page.getUpdatedAccount());
body.put("passwordLink", buildResetPasswordLink()); body.put("passwordLink", buildResetPasswordLink());
//body.put("subjectLine", "Reset password request"); body.put("siteName", getSiteName());
FreemarkerEmailMessage email = FreemarkerEmailFactory FreemarkerEmailMessage email = FreemarkerEmailFactory
.createNewMessage(vreq); .createNewMessage(vreq);
email.addRecipient(TO, page.getUpdatedAccount().getEmailAddress()); email.addRecipient(TO, page.getUpdatedAccount().getEmailAddress());
email.setTemplate(EMAIL_TEMPLATE);
email.setBodyMap(body);
vreq.setAttribute("email", email); vreq.setAttribute("email", email);
email.processTemplate(vreq, EMAIL_TEMPLATE, body); email.processTemplate(vreq);
//email.setSubject("Reset password request");
//email.setHtmlTemplate("userAccounts-resetPasswordEmail-html.ftl");
//email.setTextTemplate("userAccounts-resetPasswordEmail-text.ftl");
//email.setBodyMap(body);
//email.send();
sentEmail = true; sentEmail = true;
} }
private String getSiteName() {
ApplicationBean appBean = vreq.getAppBean();
return appBean.getApplicationName();
}
private String buildResetPasswordLink() { private String buildResetPasswordLink() {
try { try {
String email = page.getUpdatedAccount().getEmailAddress(); String email = page.getUpdatedAccount().getEmailAddress();
@ -199,7 +200,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
} }
@Override @Override
protected void notifyUser(VitroRequest vreq) { protected void notifyUser() {
// Do nothing. // Do nothing.
} }

View file

@ -337,9 +337,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
ApplicationBean appBean = vreq.getAppBean(); ApplicationBean appBean = vreq.getAppBean();
// Ideally, templates wouldn't need portal id. Currently used as a hidden input value
// in the site search box, so needed for now.
String siteName = appBean.getApplicationName(); String siteName = appBean.getApplicationName();
map.put("siteName", siteName); map.put("siteName", siteName);

View file

@ -61,6 +61,7 @@ public class FreemarkerEmailMessage {
private InternetAddress fromAddress = null; private InternetAddress fromAddress = null;
private String subject = ""; private String subject = "";
private String templateName;
private String htmlTemplateName; private String htmlTemplateName;
private String textTemplateName; private String textTemplateName;
private Map<String, Object> bodyMap = Collections.emptyMap(); private Map<String, Object> bodyMap = Collections.emptyMap();
@ -145,26 +146,29 @@ public class FreemarkerEmailMessage {
this.textTemplateName = nonNull(templateName, ""); this.textTemplateName = nonNull(templateName, "");
} }
public void setTemplate(String templateName) {
this.templateName = nonNull(templateName, "");
}
public void setBodyMap(Map<String, Object> body) { public void setBodyMap(Map<String, Object> body) {
if (body == null) { if (body == null) {
this.bodyMap = Collections.emptyMap(); this.bodyMap = Collections.emptyMap();
} else { } else {
this.bodyMap = Collections this.bodyMap = new HashMap<String, Object>(body);
.unmodifiableMap(new HashMap<String, Object>(body));
} }
} }
public void processTemplate(VitroRequest vreq, String templateName, Map<String, Object> map) { public void processTemplate(VitroRequest vreq) {
vreq.setAttribute("email", this); vreq.setAttribute("emailMessage", this);
map.putAll(FreemarkerHttpServlet.getDirectivesForAllEnvironments()); bodyMap.putAll(FreemarkerHttpServlet.getDirectivesForAllEnvironments());
map.put("email", new edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective()); bodyMap.put("email", new edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective());
try { try {
Template template = config.getTemplate(templateName); Template template = config.getTemplate(templateName);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
Environment env = template.createProcessingEnvironment(map, writer); Environment env = template.createProcessingEnvironment(bodyMap, writer);
env.setCustomAttribute("request", vreq); env.setCustomAttribute("request", vreq);
env.process(); env.process();
} catch (TemplateException e) { } catch (TemplateException e) {

View file

@ -65,10 +65,10 @@ public class EmailDirective extends BaseTemplateDirectiveModel {
HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request"); HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request");
o = (FreemarkerEmailMessage) request.getAttribute("email"); o = (FreemarkerEmailMessage) request.getAttribute("emailMessage");
if ( o == null) { if ( o == null) {
throw new TemplateModelException( throw new TemplateModelException(
"No email object found in the request."); "No email message object found in the request.");
} }
if ( ! (o instanceof FreemarkerEmailMessage)) { if ( ! (o instanceof FreemarkerEmailMessage)) {
throw new TemplateModelException( throw new TemplateModelException(

View file

@ -2,7 +2,7 @@
<#-- Confirmation email for user account password reset --> <#-- Confirmation email for user account password reset -->
<#assign subject = "Reset password request" /> <#assign subject = "${siteName} reset password request" />
<#assign html> <#assign html>
<html> <html>
@ -11,33 +11,44 @@
</head> </head>
<body> <body>
<p> <p>
${userAccount.firstName} ${userAccount.lastName} Dear ${userAccount.firstName} ${userAccount.lastName}:
</p> </p>
<p> <p>
<strong>Password successfully changed.</strong> We have received a request to reset the password for your ${siteName} account (${userAccount.emailAddress}).
</p> </p>
<p> <p>
Your new password associated with ${userAccount.emailAddress} has been changed. Please follow the instructions below to proceed with your password reset.
</p> </p>
<p> <p>
Thank you. If you did not request this new account you can safely ignore this email.
This request will expire if not acted upon within 30 days.
</p> </p>
<p>
Click on the link below or paste it into your browser's address bar to reset your password
using our secure server.
</p>
<p>${passwordLink}</p>
<p>Thank you!</p>
</body> </body>
</html> </html>
</#assign> </#assign>
<#assign text> <#assign text>
${userAccount.firstName} ${userAccount.lastName} Dear ${userAccount.firstName} ${userAccount.lastName}:
We received a request to reset the password for your account We have received a request to reset the password for your ${siteName} account
(${userAccount.emailAddress}). (${userAccount.emailAddress}).
Please follow the instructions below to proceed with your password reset. 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. 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. This request will expire if not acted upon within 30 days.
Paste the link below into your browser's address bar to reset your password Paste the link below into your browser's address bar to reset your password
using our secure server. using our secure server.