NIHVIVO-2693 Freemarker email messaging code refactoring
This commit is contained in:
parent
2fcff042e6
commit
f5c44298e1
6 changed files with 46 additions and 33 deletions
|
@ -200,7 +200,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
|
|||
|
||||
userAccountsDao.updateUserAccount(userAccount);
|
||||
|
||||
strategy.notifyUser(vreq);
|
||||
strategy.notifyUser();
|
||||
}
|
||||
|
||||
public boolean wasPasswordEmailSent() {
|
||||
|
|
|
@ -9,8 +9,8 @@ import java.net.URL;
|
|||
import java.util.HashMap;
|
||||
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.Status;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsPage;
|
||||
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 notifyUser(VitroRequest vreq);
|
||||
protected abstract void notifyUser();
|
||||
|
||||
protected abstract boolean wasPasswordEmailSent();
|
||||
|
||||
|
@ -95,7 +95,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void notifyUser(VitroRequest vreq) {
|
||||
protected void notifyUser() {
|
||||
if (!resetPassword) {
|
||||
return;
|
||||
}
|
||||
|
@ -103,24 +103,25 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
|
|||
Map<String, Object> body = new HashMap<String, Object>();
|
||||
body.put("userAccount", page.getUpdatedAccount());
|
||||
body.put("passwordLink", buildResetPasswordLink());
|
||||
//body.put("subjectLine", "Reset password request");
|
||||
body.put("siteName", getSiteName());
|
||||
|
||||
FreemarkerEmailMessage email = FreemarkerEmailFactory
|
||||
.createNewMessage(vreq);
|
||||
email.addRecipient(TO, page.getUpdatedAccount().getEmailAddress());
|
||||
email.setTemplate(EMAIL_TEMPLATE);
|
||||
email.setBodyMap(body);
|
||||
|
||||
vreq.setAttribute("email", email);
|
||||
|
||||
email.processTemplate(vreq, EMAIL_TEMPLATE, body);
|
||||
|
||||
//email.setSubject("Reset password request");
|
||||
//email.setHtmlTemplate("userAccounts-resetPasswordEmail-html.ftl");
|
||||
//email.setTextTemplate("userAccounts-resetPasswordEmail-text.ftl");
|
||||
//email.setBodyMap(body);
|
||||
//email.send();
|
||||
email.processTemplate(vreq);
|
||||
|
||||
sentEmail = true;
|
||||
}
|
||||
|
||||
private String getSiteName() {
|
||||
ApplicationBean appBean = vreq.getAppBean();
|
||||
return appBean.getApplicationName();
|
||||
}
|
||||
|
||||
private String buildResetPasswordLink() {
|
||||
try {
|
||||
|
@ -199,7 +200,7 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void notifyUser(VitroRequest vreq) {
|
||||
protected void notifyUser() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
|
|
|
@ -337,9 +337,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
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();
|
||||
map.put("siteName", siteName);
|
||||
|
||||
|
|
|
@ -61,6 +61,7 @@ public class FreemarkerEmailMessage {
|
|||
|
||||
private InternetAddress fromAddress = null;
|
||||
private String subject = "";
|
||||
private String templateName;
|
||||
private String htmlTemplateName;
|
||||
private String textTemplateName;
|
||||
private Map<String, Object> bodyMap = Collections.emptyMap();
|
||||
|
@ -144,27 +145,30 @@ public class FreemarkerEmailMessage {
|
|||
public void setTextTemplate(String templateName) {
|
||||
this.textTemplateName = nonNull(templateName, "");
|
||||
}
|
||||
|
||||
public void setTemplate(String templateName) {
|
||||
this.templateName = nonNull(templateName, "");
|
||||
}
|
||||
|
||||
public void setBodyMap(Map<String, Object> body) {
|
||||
if (body == null) {
|
||||
this.bodyMap = Collections.emptyMap();
|
||||
} else {
|
||||
this.bodyMap = Collections
|
||||
.unmodifiableMap(new HashMap<String, Object>(body));
|
||||
this.bodyMap = 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());
|
||||
map.put("email", new edu.cornell.mannlib.vitro.webapp.web.directives.EmailDirective());
|
||||
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(map, writer);
|
||||
Environment env = template.createProcessingEnvironment(bodyMap, writer);
|
||||
env.setCustomAttribute("request", vreq);
|
||||
env.process();
|
||||
} catch (TemplateException e) {
|
||||
|
|
|
@ -65,10 +65,10 @@ public class EmailDirective extends BaseTemplateDirectiveModel {
|
|||
|
||||
HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request");
|
||||
|
||||
o = (FreemarkerEmailMessage) request.getAttribute("email");
|
||||
o = (FreemarkerEmailMessage) request.getAttribute("emailMessage");
|
||||
if ( o == null) {
|
||||
throw new TemplateModelException(
|
||||
"No email object found in the request.");
|
||||
"No email message object found in the request.");
|
||||
}
|
||||
if ( ! (o instanceof FreemarkerEmailMessage)) {
|
||||
throw new TemplateModelException(
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
<#-- Confirmation email for user account password reset -->
|
||||
|
||||
<#assign subject = "Reset password request" />
|
||||
<#assign subject = "${siteName} reset password request" />
|
||||
|
||||
<#assign html>
|
||||
<html>
|
||||
|
@ -11,33 +11,44 @@
|
|||
</head>
|
||||
<body>
|
||||
<p>
|
||||
${userAccount.firstName} ${userAccount.lastName}
|
||||
Dear ${userAccount.firstName} ${userAccount.lastName}:
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Password successfully changed.</strong>
|
||||
We have received a request to reset the password for your ${siteName} account (${userAccount.emailAddress}).
|
||||
</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>
|
||||
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>
|
||||
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>
|
||||
</html>
|
||||
</#assign>
|
||||
|
||||
<#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}).
|
||||
|
||||
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.
|
||||
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
|
||||
using our secure server.
|
||||
|
|
Loading…
Add table
Reference in a new issue