From cd439b8aaa7f6aac3d2096a6c6514e77d545e486 Mon Sep 17 00:00:00 2001
From: ryounes likely viewing page " + originalReferer );
- }
-
- msgBuf.append(lineSeparator + " BLANK MESSAGE "+comments+" "+cal.getTime()+" REJECTED - SPAM "+spamReason+"" + deliveryfrom + "
" + lineSeparator );
- msgBuf.append("From: "+webusername +" (" + webuseremail + ")" +
- " at IP address " + ipAddr + "
"+lineSeparator);
-
- if (!(originalReferer == null || originalReferer.equals("none"))){
- //The spam filter that is being used by the listsrv is rejecting likely viewing page " + stripProtocol(originalReferer) );
- msgBuf.append("Comments:
" + lineSeparator );
- if (comments==null || comments.equals("")) {
- msgBuf.append("
");
- outFile.println();
- outFile.println("Add new account
+User accounts > Add new account
<#if errorEmailIsEmpty??>
<#assign errorMessage = "You must supply an email address." />
@@ -93,7 +93,7 @@
* required fields
From 22507b11c083dba2778342c09e53e0d76d8efd88 Mon Sep 17 00:00:00 2001 From: j2blakeRoles *
<#list roles as role> @@ -70,13 +70,13 @@ <#if !emailIsEnabled??> - +Minimum of ${minimumLength} characters in length.
Leaving this blank means that the password will not be changed.
- + #if>Associate a profile with this account
@@ -98,7 +98,7 @@ #if> - or Cancel + or Cancel* required fields
Note: if email changes, a confirmation email will be sent to the new email address entered above.
- +Note: if email changes, a confirmation email will be sent to the new email address entered above.
+ - + - + <#if !externalAuth??> - + -Minimum of ${minimumLength} characters in length.
-Leaving this blank means that the password will not be changed.
+Minimum of ${minimumLength} characters in length. Leaving this blank means that the password will not be changed.
- + #if> - +* required fields
Please enter your new password for ${userAccount.emailAddress}
+ <#if errorPasswordIsEmpty??> <#assign errorMessage = "No password supplied." /> #if> @@ -24,26 +26,22 @@ #if>* required fields
++ ${userAccount.firstName} ${userAccount.lastName} +
+ ++ Password successfully changed. +
+ ++ Your new password associated with ${userAccount.emailAddress} has been changed. +
+ ++ Thank you. +
+ + +#assign> + +<#assign text> +${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! +#assign> + +<@email subject=subject html=html text=text /> \ No newline at end of file From f5c44298e1232802da1fc0f05d6a19d93bbf409f Mon Sep 17 00:00:00 2001 From: ryounes- ${userAccount.firstName} ${userAccount.lastName} + Dear ${userAccount.firstName} ${userAccount.lastName}:
- Password successfully changed. + We have received a request to reset the password for your ${siteName} account (${userAccount.emailAddress}).
- Your new password associated with ${userAccount.emailAddress} has been changed. + Please follow the instructions below to proceed with your password reset.
- 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.
+ ++ Click on the link below or paste it into your browser's address bar to reset your password + using our secure server. +
+ +${passwordLink}
+ +Thank you!
#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. From cedd095a077fe473301a22d9ba32205c0548b019 Mon Sep 17 00:00:00 2001 From: manolobeviaMinimum of ${minimumLength} characters in length. Leaving this blank means that the password will not be changed.
- + #if> From 4282da5ef2d6e0d69fdf1c0df49a60f017751794 Mon Sep 17 00:00:00 2001 From: j2blake- * Record that the user has logged in but with only external authentication - * info, so no internal user account. - * - this involves everything except updating the user record. - *- * - * TODO JB This goes away. - */ - public abstract void recordLoginWithoutUserAccount(String individualUri); - /** *
* Record that the current user has logged out: - notify other users of the diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java index 2135f007f..f84bf041b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java @@ -3,7 +3,6 @@ package edu.cornell.mannlib.vitro.webapp.controller.authenticate; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -123,24 +122,12 @@ public class BasicAuthenticator extends Authenticator { } recordLoginOnUserRecord(userAccount); - recordLoginWithOrWithoutUserAccount(userAccount.getUri(), authSource); - } - // TODO JB This goes away. - @Override - public void recordLoginWithoutUserAccount(String individualUri) { - recordLoginWithOrWithoutUserAccount(individualUri, - AuthenticationSource.EXTERNAL); - } - - /** This much is in common on login, whether or not you have a user account. */ - private void recordLoginWithOrWithoutUserAccount(String userUri, - AuthenticationSource authSource) { HttpSession session = request.getSession(); - createLoginStatusBean(userUri, authSource, session); + createLoginStatusBean(userAccount.getUri(), authSource, session); setSessionTimeoutLimit(session); - recordInUserSessionMap(userUri, session); - notifyOtherUsers(userUri, session); + recordInUserSessionMap(userAccount.getUri(), session); + notifyOtherUsers(userAccount.getUri(), session); } /** diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginExternalAuthReturn.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginExternalAuthReturn.java index d970db6cd..ce008c64e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginExternalAuthReturn.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginExternalAuthReturn.java @@ -5,7 +5,6 @@ package edu.cornell.mannlib.vitro.webapp.controller.authenticate; import static edu.cornell.mannlib.vitro.webapp.controller.authenticate.LoginExternalAuthSetup.ATTRIBUTE_REFERRER; import java.io.IOException; -import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -16,6 +15,8 @@ import org.apache.commons.logging.LogFactory; import edu.cornell.mannlib.vedit.beans.LoginStatusBean.AuthenticationSource; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; +import edu.cornell.mannlib.vitro.webapp.controller.accounts.user.UserAccountsFirstTimeExternalPage; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean; /** @@ -57,37 +58,26 @@ public class LoginExternalAuthReturn extends BaseLoginServlet { MESSAGE_LOGIN_FAILED); return; } - + String afterLoginUrl = LoginProcessBean.getBean(req).getAfterLoginUrl(); removeLoginProcessArtifacts(req); UserAccount userAccount = getAuthenticator(req) .getAccountForExternalAuth(externalAuthId); - if (userAccount != null) { + if (userAccount == null) { + log.debug("Creating new account for " + externalAuthId + + ", return to '" + afterLoginUrl + "'"); + UserAccountsFirstTimeExternalPage.setExternalLoginInfo(req, + externalAuthId, afterLoginUrl); + resp.sendRedirect(UrlBuilder.getUrl("/accounts/firstTimeExternal")); + return; + } else { log.debug("Logging in as " + userAccount.getUri()); getAuthenticator(req).recordLoginAgainstUserAccount(userAccount, AuthenticationSource.EXTERNAL); new LoginRedirector(req, afterLoginUrl).redirectLoggedInUser(resp); return; } - - ListassociatedUris = getAuthenticator(req) - .getAssociatedIndividualUris(userAccount); - // TODO JB - this case should lead to creating a new account. - if (!associatedUris.isEmpty()) { - log.debug("Recognize '" + externalAuthId + "' as self-editor for " - + associatedUris); - String uri = associatedUris.get(0); - - getAuthenticator(req).recordLoginWithoutUserAccount(uri); - new LoginRedirector(req, afterLoginUrl).redirectLoggedInUser(resp); - return; - } - - log.debug("User is not recognized: " + externalAuthId); - removeLoginProcessArtifacts(req); - new LoginRedirector(req, afterLoginUrl).redirectUnrecognizedExternalUser(resp, - externalAuthId); } private void removeLoginProcessArtifacts(HttpServletRequest req) { diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java index 6171387f9..3dd4fabd0 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java @@ -182,10 +182,4 @@ public class AuthenticatorStub extends Authenticator { "AuthenticatorStub.accountRequiresEditing() not implemented."); } - @Override - public void recordLoginWithoutUserAccount(String individualUri) { - throw new RuntimeException( - "AuthenticatorStub.recordLoginWithoutUserAccount() not implemented."); - } - } diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternal.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternal.ftl index 1b6b78791..8367ca164 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternal.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-firstTimeExternal.ftl @@ -39,6 +39,7 @@ + From 558bcfb13f42987d64dd2451aa41986408670c29 Mon Sep 17 00:00:00 2001 From: j2blake Date: Tue, 14 Jun 2011 17:16:37 +0000 Subject: [PATCH 17/47] Expose the user's first name for use in the Identity template. --- .../mannlib/vitro/webapp/web/templatemodels/User.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java index e533847e5..9f1eef762 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java @@ -41,6 +41,14 @@ public class User extends BaseTemplateModel { return firstName + " " + lastName; } + public String getFirstName() { + if (currentUser == null) { + return ""; + } else { + return currentUser.getFirstName(); + } + } + public boolean getHasSiteAdminAccess() { return PolicyHelper.isAuthorizedForActions(vreq, SiteAdminController.REQUIRED_ACTIONS); } From 367d0bf9b4192d3df6fd3a4216efda432266d955 Mon Sep 17 00:00:00 2001 From: manolobevia Date: Tue, 14 Jun 2011 18:49:03 +0000 Subject: [PATCH 18/47] NIHVIVO-2280: Changed note to "Instructions for resetting the password will be emailed to the address entered above. The password will not be reset until the user follows the link provided in this email." --- .../freemarker/body/accounts/userAccounts-edit.ftl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webapp/web/templates/freemarker/body/accounts/userAccounts-edit.ftl b/webapp/web/templates/freemarker/body/accounts/userAccounts-edit.ftl index 2d732c408..0d9e30718 100644 --- a/webapp/web/templates/freemarker/body/accounts/userAccounts-edit.ftl +++ b/webapp/web/templates/freemarker/body/accounts/userAccounts-edit.ftl @@ -92,9 +92,9 @@ <#if emailIsEnabled??> - Note: A confirmation email with instructions for resetting a password - will be sent to the address entered above. - The password will not be reset until the user follows the link provided in this email. + Note: Instructions for resetting the password will + be emailed to the address entered above. The password will not + be reset until the user follows the link provided in this email.
#if> From 4f3e29890f2f6470f97f479015acf8998135de6d Mon Sep 17 00:00:00 2001 From: ryounesDate: Tue, 14 Jun 2011 19:36:22 +0000 Subject: [PATCH 19/47] NIHVIVO-2709 Fix code error to prevent error during variable dump --- .../webapp/web/templatemodels/VClassTemplateModel.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/VClassTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/VClassTemplateModel.java index 059d70d6a..676dbfe02 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/VClassTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/VClassTemplateModel.java @@ -6,6 +6,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route; @@ -36,7 +37,8 @@ public class VClassTemplateModel extends BaseTemplateModel { return vclass.getEntityCount(); } - public VClassGroupTemplateModel getGroup(){ - return new VClassGroupTemplateModel(vclass.getGroup()); + public VClassGroupTemplateModel getGroup() { + VClassGroup group = vclass.getGroup(); + return (group == null) ? null : new VClassGroupTemplateModel(vclass.getGroup()); } } From edfd1798efe56fe15cf14dba079587272eca7b9f Mon Sep 17 00:00:00 2001 From: j2blake Date: Tue, 14 Jun 2011 20:00:33 +0000 Subject: [PATCH 20/47] Expose the user's last name for use in the Identity template. --- .../mannlib/vitro/webapp/web/templatemodels/User.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java index 9f1eef762..5caeb8f0a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java @@ -49,6 +49,14 @@ public class User extends BaseTemplateModel { } } + public String getLastName() { + if (currentUser == null) { + return ""; + } else { + return currentUser.getLastName(); + } + } + public boolean getHasSiteAdminAccess() { return PolicyHelper.isAuthorizedForActions(vreq, SiteAdminController.REQUIRED_ACTIONS); } From ef3affb349ebfb1210adcd5339d7c1675d926749 Mon Sep 17 00:00:00 2001 From: j2blake Date: Tue, 14 Jun 2011 20:12:57 +0000 Subject: [PATCH 21/47] Write a warning to the log if a root user already exists with an email address that is different from the one in the deploy.properties --- .../webapp/auth/policy/RootUserPolicy.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/policy/RootUserPolicy.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/policy/RootUserPolicy.java index 85e069d12..aeb67bd5c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/policy/RootUserPolicy.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/policy/RootUserPolicy.java @@ -75,6 +75,8 @@ public class RootUserPolicy implements PolicyIface { UserAccountsDao uaDao = getUserAccountsDao(ctx); OntModel userAccountsModel = getUserAccountsModel(ctx); + checkForWrongRootUser(ctx, uaDao); + if (!rootUserExists(uaDao)) { createRootUser(ctx, uaDao, userAccountsModel); } @@ -103,13 +105,34 @@ public class RootUserPolicy implements PolicyIface { .getUserAccountsModel(); } + private void checkForWrongRootUser(ServletContext ctx, + UserAccountsDao uaDao) { + UserAccount root = getRootUser(uaDao); + if (root == null) { + return; + } + String actualRootEmail = root.getEmailAddress(); + + String configRootEmail = ConfigurationProperties.getBean(ctx) + .getProperty(PROPERTY_ROOT_USER_EMAIL); + if (actualRootEmail.equals(configRootEmail)) { + return; + } + + log.warn("Root user '" + actualRootEmail + "' already exists."); + } + private boolean rootUserExists(UserAccountsDao uaDao) { + return (getRootUser(uaDao) != null); + } + + private UserAccount getRootUser(UserAccountsDao uaDao) { for (UserAccount ua : uaDao.getAllUserAccounts()) { if (uaDao.isRootUser(ua)) { - return true; + return ua; } } - return false; + return null; } /** From 6f58b677cbd9ac340ae5c59a9531ab7f3817f757 Mon Sep 17 00:00:00 2001 From: brianjlowe Date: Tue, 14 Jun 2011 21:14:49 +0000 Subject: [PATCH 22/47] NIHVIVO-2708 removed unnecessary iteration in getMainImageUri() --- .../vitro/webapp/dao/jena/IndividualSDB.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualSDB.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualSDB.java index 95216e798..32c6ee464 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualSDB.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualSDB.java @@ -456,13 +456,13 @@ public class IndividualSDB extends IndividualImpl implements Individual { if (this.mainImageUri != NOT_INITIALIZED) { return mainImageUri; } else { - for (ObjectPropertyStatement stmt : getObjectPropertyStatements()) { - if (stmt.getPropertyURI() - .equals(VitroVocabulary.IND_MAIN_IMAGE)) { - mainImageUri = stmt.getObjectURI(); - return mainImageUri; - } - } + List mainImgStmts = + getObjectPropertyStatements(VitroVocabulary.IND_MAIN_IMAGE); + if (mainImgStmts != null && mainImgStmts.size() > 0) { + // arbitrarily return the first value in the list + mainImageUri = mainImgStmts.get(0).getObjectURI(); + return mainImageUri; + } return null; } } From e970abcae946aeb81da42b5e73980dcfa054735a Mon Sep 17 00:00:00 2001 From: tworrall Date: Wed, 15 Jun 2011 14:39:22 +0000 Subject: [PATCH 23/47] NIHVIVO-2710: improved type facet on the search results page --- webapp/web/css/vitro.css | 38 +++++++++++++++++++ .../body/search/search-pagedResults.ftl | 12 ++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/webapp/web/css/vitro.css b/webapp/web/css/vitro.css index 184c7f688..8452671b3 100644 --- a/webapp/web/css/vitro.css +++ b/webapp/web/css/vitro.css @@ -74,3 +74,41 @@ height:620px; overflow:visible; } +/* <------ SEARCH RESULTS PAGE*/ +.searchTOC { + margin-bottom: 1.5em; + float:right; + margin-right:35px; + margin-left:45px; + width:182px; + text-align:center; +/* border: 1px solid #dde4e3;*/ + padding-top:4px; + color: #fff; + background: #5e6363; +} +.searchTOC span { + color: #fff; +} +.searchTOC ul { + width: 160px; + border: 1px solid #dde4e3; + background: #f1f2ee; + padding: 0 10px 0px 10px; + margin-top: 4px; + text-align: left; +} +.searchTOC ul li { + display: block; + border-bottom: 1px solid #dde4e3; + font-size: 14px; + height: 35px; + line-height: 35px; +} +.searchTOC ul li:last-child { + border-bottom: none +} +.searchTOC ul a { + display: block; + padding-left: 15px; +} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl b/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl index bf4600af4..8030d496b 100644 --- a/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl +++ b/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl @@ -13,19 +13,23 @@ <#-- Refinement links --> <#if classGroupLinks?has_content> - Show only results of this type: + Display only +#if> <#if classLinks?has_content><#list classGroupLinks as link> - ${link.text} +
- ${link.text}
#list> +- Show only results of this subtype: + Limit ${classGroupName} to +#if> From 3c1cbff0e08a660992f197e73621404616dbd731 Mon Sep 17 00:00:00 2001 From: j2blake<#list classLinks as link> - ${link.text} +
- ${link.text}
#list> +Date: Wed, 15 Jun 2011 14:50:44 +0000 Subject: [PATCH 24/47] Change the way that Login name is configured. --- .../vitro/webapp/web/templatemodels/User.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java index 5caeb8f0a..5b9a217d7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/User.java @@ -32,13 +32,11 @@ public class User extends BaseTemplateModel { return ""; } - String firstName = currentUser.getFirstName(); - String lastName = currentUser.getLastName(); - if (firstName.isEmpty() && lastName.isEmpty()) { - return currentUser.getEmailAddress(); - } - - return firstName + " " + lastName; + if (currentUser.getFirstName().isEmpty()) { + return currentUser.getEmailAddress(); + } + + return currentUser.getFirstName(); } public String getFirstName() { From c94ad4511b6d0a1b02bbbb8feadcbd6460c242b7 Mon Sep 17 00:00:00 2001 From: ryounes Date: Wed, 15 Jun 2011 16:24:48 +0000 Subject: [PATCH 25/47] NIHVIVO-2693 Add option to set default subject, html, and text attributes of email object in case template doesn't supply them. --- .../admin/UserAccountsEditPageStrategy.java | 27 +++++-- .../webapp/email/FreemarkerEmailMessage.java | 68 +++++++++++----- .../webapp/web/directives/EmailDirective.java | 77 ++++++++----------- 3 files changed, 106 insertions(+), 66 deletions(-) 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 1c1a3f383..3ca3c739c 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,13 +103,18 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage { Map body = new HashMap (); body.put("userAccount", page.getUpdatedAccount()); body.put("passwordLink", buildResetPasswordLink()); - body.put("siteName", getSiteName()); + + String siteName = getSiteName(); + body.put("siteName", siteName); FreemarkerEmailMessage email = FreemarkerEmailFactory .createNewMessage(vreq); email.addRecipient(TO, page.getUpdatedAccount().getEmailAddress()); email.setTemplate(EMAIL_TEMPLATE); - email.setBodyMap(body); + email.setBodyMap(body); + email.setDefaultSubject(getDefaultSubject(siteName)); + email.setDefaultHtml(getDefaultHtml()); + email.setDefaultText(getDefaultText()); vreq.setAttribute("email", email); @@ -117,10 +122,22 @@ public abstract class UserAccountsEditPageStrategy extends UserAccountsPage { sentEmail = true; } - + private String getSiteName() { - ApplicationBean appBean = vreq.getAppBean(); - return appBean.getApplicationName(); + 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"; } private String buildResetPasswordLink() { 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 53e0b0c8e..5e2ec8597 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java @@ -26,6 +26,7 @@ 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; @@ -61,10 +62,15 @@ public class FreemarkerEmailMessage { private InternetAddress fromAddress = null; private String subject = ""; + private String defaultSubject = ""; + private String defaultHtml = ""; + private String defaultText = ""; private String templateName; - private String htmlTemplateName; - private String textTemplateName; private Map bodyMap = Collections.emptyMap(); + + // TO BE REMOVED + private String htmlTemplateName; + private String textTemplateName; /** * Package access - should only be created by the factory. @@ -133,22 +139,36 @@ public class FreemarkerEmailMessage { return; } } - - public void setSubject(String subject) { - this.subject = nonNull(subject, ""); - } - + + // 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 setTemplate(String 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) { @@ -192,24 +212,36 @@ public class FreemarkerEmailMessage { 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.isEmpty()) { - if (html.isEmpty()) { + 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 { - if (html.isEmpty()) { - msg.setContent(text, "text/plain"); - } else { - MimeMultipart content = new MimeMultipart("alternative"); - addBodyPart(content, text, "text/plain"); - addBodyPart(content, html, "text/html"); - msg.setContent(content); - } + MimeMultipart content = new MimeMultipart("alternative"); + addBodyPart(content, text, "text/plain"); + addBodyPart(content, html, "text/html"); + msg.setContent(content); } msg.setSentDate(new Date()); 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 515acf72e..e492d40a7 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 @@ -29,54 +29,43 @@ public class EmailDirective extends BaseTemplateDirectiveModel { @Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { - - Object o = params.get("subject"); - if (o == null) { - throw new TemplateModelException( - "The email directive requires a value for parameter 'subject'."); - } - if (! ( o instanceof SimpleScalar)) { - throw new TemplateModelException( - "The email directive requires a string value for parameter 'subject'."); - } - String subject = o.toString(); - - o = params.get("html"); - if (o == null) { - throw new TemplateModelException( - "The email directive requires a value for parameter 'html'."); - } - if (! ( o instanceof SimpleScalar)) { - throw new TemplateModelException( - "The email directive requires a string value for parameter 'html'."); - } - String html = o.toString(); - - o = params.get("text"); - if (o == null) { - throw new TemplateModelException( - "The email directive requires a value for parameter 'text'."); - } - if (! ( o instanceof SimpleScalar)) { - throw new TemplateModelException( - "The email directive requires a string value for parameter 'text'."); - } - String text = o.toString(); HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request"); - - o = (FreemarkerEmailMessage) request.getAttribute("emailMessage"); - if ( o == null) { + FreemarkerEmailMessage email = null; + + Object paramValue = (FreemarkerEmailMessage) request.getAttribute("emailMessage"); + if ( paramValue == null) { throw new TemplateModelException( "No email message object found in the request."); } - if ( ! (o instanceof FreemarkerEmailMessage)) { + if ( ! (paramValue instanceof FreemarkerEmailMessage)) { throw new TemplateModelException( "Invalid value for request email attribute"); } - FreemarkerEmailMessage email = (FreemarkerEmailMessage) o; - email.send(subject, html, text); + email = (FreemarkerEmailMessage) paramValue; + + // 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(); + } + + 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(); + } + + email.send(subject, html, text); } @Override @@ -84,15 +73,17 @@ public class EmailDirective extends BaseTemplateDirectiveModel { Map map = new LinkedHashMap (); 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"); - params.put("html", "HTML version of email message"); - params.put("text", "Plain text version of email message"); + 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; From d42acf48d8c3537de134c5c0c42f95ac2df92c34 Mon Sep 17 00:00:00 2001 From: ryounes Date: Wed, 15 Jun 2011 16:31:26 +0000 Subject: [PATCH 26/47] NIHVIVO-2693 Mark code to be removed with a comment --- .../mannlib/vitro/webapp/email/FreemarkerEmailMessage.java | 2 ++ 1 file changed, 2 insertions(+) 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 5e2ec8597..fac7981cf 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/email/FreemarkerEmailMessage.java @@ -253,6 +253,7 @@ public class FreemarkerEmailMessage { } } + // TO BE REMOVED public void send() { String textBody = figureMessageBody(textTemplateName); String htmlBody = figureMessageBody(htmlTemplateName); @@ -303,6 +304,7 @@ 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 ""; From 5d587265fae96c82a35279d76fbcf6c8eb2037c4 Mon Sep 17 00:00:00 2001 From: j2blake Date: Wed, 15 Jun 2011 18:23:07 +0000 Subject: [PATCH 27/47] 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> + +<#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! +#assign> + +<@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 +