From dc31718c4c6b1be4703fa0fa516ca189295a738f Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 11 May 2017 12:56:07 +0000 Subject: [PATCH] Added support for email notification templates git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@148468 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 2 +- pom.xml | 4 +- .../common/portal/GCubePortalConstants.java | 3 +- .../portal/mailing/EmailNotification.java | 297 ++++++++++++++---- .../mailing/templates/AbstractTemplate.java | 44 +++ .../portal/mailing/templates/Template.java | 14 + 6 files changed, 299 insertions(+), 65 deletions(-) create mode 100644 src/main/java/org/gcube/common/portal/mailing/templates/AbstractTemplate.java create mode 100644 src/main/java/org/gcube/common/portal/mailing/templates/Template.java diff --git a/.classpath b/.classpath index 5cca4e3..1729369 100644 --- a/.classpath +++ b/.classpath @@ -18,7 +18,7 @@ - + diff --git a/pom.xml b/pom.xml index 49623cb..4d22626 100644 --- a/pom.xml +++ b/pom.xml @@ -23,8 +23,8 @@ distro - 1.7 - 1.7 + 1.8 + 1.8 6.2.5 UTF-8 UTF-8 diff --git a/src/main/java/org/gcube/common/portal/GCubePortalConstants.java b/src/main/java/org/gcube/common/portal/GCubePortalConstants.java index fa9fd97..58ad730 100644 --- a/src/main/java/org/gcube/common/portal/GCubePortalConstants.java +++ b/src/main/java/org/gcube/common/portal/GCubePortalConstants.java @@ -11,7 +11,8 @@ public class GCubePortalConstants { public static final String USER_MESSAGES_FRIENDLY_URL = "/messages"; public static final String VRES_EXPLORE_FRIENDLY_URL = "/explore"; public static final String CATALOGUE_FRIENDLY_URL = "/data-catalogue"; - public static final String AUTHORIZATION_FRIENDLY_URL = "/authorization"; + public static final String AUTHORIZATION_FRIENDLY_URL = "/authorization"; + public static final String MANAGE_USERS_REQUESTS_FRIENDLY_URL = "/manage-user-and-requests"; public static final String INFRASTRUCTURE_NAME = "infrastructure"; public static final String SCOPES = "scopes"; diff --git a/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java b/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java index 53411f0..1330523 100644 --- a/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java +++ b/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java @@ -1,6 +1,7 @@ package org.gcube.common.portal.mailing; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Properties; @@ -18,27 +19,35 @@ import javax.mail.internet.MimeMultipart; import javax.servlet.http.HttpServletRequest; import org.gcube.common.portal.PortalContext; +import org.gcube.common.portal.mailing.templates.Template; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * - * @author Massimiliano Assante + * @author Massimiliano Assante, CNR-ISTI * */ public class EmailNotification { private static Logger _log = LoggerFactory.getLogger(EmailNotification.class); + private static final String MAIL_SERVICE_HOST = "localhost"; + private static final String MAIL_SERVICE_PORT = "25"; /** - * The recipients of the email + * The list of recipient addresses in "to" */ private String emailrecipients[]; - - private List emailRecipientsInCC; - private List emailRecipientsInBCC; + /** + * The list of recipient addresses with cc + */ + private final List emailRecipientsInCC; + /** + * The list of recipient addresses with bcc + */ + private final List emailRecipientsInBCC; /** * Email's subject */ - private String emailSubject; + private final String emailSubject; /** * Email's body message in text/html */ @@ -48,13 +57,20 @@ public class EmailNotification { */ private StringBuffer emailBodyTextPlain; /** - * + * Email's message from template */ - private HttpServletRequest request; + private Template selectedTemplate; - private final String MAIL_SERVICE_HOST = "localhost"; - private String MAIL_SERVICE_PORT = "25"; + private HttpServletRequest request; /** + * + * Initialize the email with the following information: + *
    + *
  • The subject of the mail
  • + *
  • The body of the mail in HTML
  • + *
  • The recipient addresses
  • + *
  • the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)
  • + *
* * @param recipient an email address * @param subject the subject of your email @@ -62,74 +78,70 @@ public class EmailNotification { * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case) */ public EmailNotification(String recipient, String subject, String bodyTextHTML, HttpServletRequest httpServletRequest) { - String[] emailRecipients = new String[1]; - emailRecipients[0] = recipient; - init(httpServletRequest, emailRecipients, subject, bodyTextHTML, null); + this(new EmailBuilder(subject, httpServletRequest, new String[]{recipient}).contentTextHTML(bodyTextHTML)); } /** + * + * Initialize the email with the following information: + *
    + *
  • The subject of the mail
  • + *
  • The body of the mail in HTML
  • + *
  • One or several "to" recipient addresses
  • + *
  • the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)
  • + *
* - * @param recipient an email address - * @param subject the subject of your email - * @param bodyTextHTML the body of your email in text/html - * @param bodyTextPlain the body of your email in text/plain - * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case) - */ - public EmailNotification(String recipient, String subject, String bodyTextHTML, String bodyTextPlain, HttpServletRequest httpServletRequest) { - String[] emailRecipients = new String[1]; - emailRecipients[0] = recipient; - init(httpServletRequest, emailRecipients, subject, bodyTextHTML, bodyTextPlain); - } - /** - * @param recipients an array of email addresses + * @param recipients an array of email addresses * @param subject the subject of your email * @param bodyTextHTML the body of your email in HTML * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case) */ public EmailNotification(String recipients[], String subject, String bodyTextHTML, HttpServletRequest httpServletRequest) { - init(httpServletRequest, recipients, subject, bodyTextHTML, null); + this(new EmailBuilder(subject, httpServletRequest, recipients).contentTextHTML(bodyTextHTML)); } /** - * @param recipients an array of email addresses - * @param subject the subject of your email - * @param bodyTextHTML the body of your email in text/html - * @param bodyTextPlain the body of your email in text/plain - * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case) - */ - public EmailNotification(String recipients[], String subject, String bodyTextHTML, String bodyTextPlain, HttpServletRequest httpServletRequest) { - init(httpServletRequest, recipients, subject, bodyTextHTML, bodyTextPlain); - } - - /** + * + * Initialize the email with the following information: + *
    + *
  • The subject of the mail
  • + *
  • The body of the mail in HTML
  • + *
  • One or several "to" recipient addresses
  • + *
  • the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)
  • + *
+ * * @param recipients a list of email addresses * @param subject the subject of your email * @param bodyTextHTML the body of your email in text/html * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case) */ public EmailNotification(List recipients, String subject, String bodyTextHTML, HttpServletRequest httpServletRequest) { - init(httpServletRequest, recipients.toArray(new String[recipients.size()]), subject, bodyTextHTML, null); + this(new EmailBuilder(subject, httpServletRequest, recipients.toArray(new String[recipients.size()])).contentTextHTML(bodyTextHTML)); } + /** - * @param recipients a list of email addresses - * @param subject the subject of your email - * @param bodyTextHTML the body of your email in text/html - * @param bodyTextPlain the body of your email in text/plain - * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case) + * private constructor for the see {@EmailBuilder} + * @param builder */ - public EmailNotification(List recipients, String subject, String bodyTextHTML, String bodyTextPlain, HttpServletRequest httpServletRequest) { - init(httpServletRequest, recipients.toArray(new String[recipients.size()]), subject, bodyTextHTML, bodyTextPlain); - } - - private void init(HttpServletRequest httpServletRequest, String recipients[], String subject, String bodyHTML, String bodyPlainText) { - request = httpServletRequest; - emailrecipients = recipients; - emailSubject = subject; - emailRecipientsInCC = new ArrayList(); - emailRecipientsInBCC = new ArrayList(); + private EmailNotification(EmailBuilder builder) { + this.emailSubject = builder.subject; + this.request = builder.httpServletRequest; + this.emailrecipients = builder.emailrecipients; + this.selectedTemplate = builder.template; + if (this.selectedTemplate != null) { + this.emailBodyTextHTML = new StringBuffer(selectedTemplate.getTextHTML()).append(getWarningLegalText(false)); + this.emailBodyTextPlain = new StringBuffer(selectedTemplate.getTextPLAIN()).append(getWarningLegalText(true)); + } else { + this.emailBodyTextHTML = (builder.bodyTextHTML != null) ? new StringBuffer(builder.bodyTextHTML).append(getWarningLegalText(false)) : null; + this.emailBodyTextPlain = (builder.bodyTextPlain != null) ? new StringBuffer(builder.bodyTextPlain).append(getWarningLegalText(true)) : null; + } - emailBodyTextHTML = new StringBuffer(bodyHTML).append(getWarningLegalText(false)); - emailBodyTextPlain = (bodyPlainText != null) ? new StringBuffer(bodyPlainText).append(getWarningLegalText(true)) : null; + this.emailRecipientsInCC = new ArrayList(); + this.emailRecipientsInBCC = new ArrayList(); } + /** + * @deprecated use fluent API {@link} EmailBuilder e.g. EmailNotification en = new EmailBuilder(subject, bodyTextHTML, httpServletRequest, recipients).cc(email).build(); + * @param email + */ public void addRecipientInCC(String email) { try { emailRecipientsInCC.add(new InternetAddress(email)); @@ -137,7 +149,10 @@ public class EmailNotification { e.printStackTrace(); } } - + /** + * You can also use fluent API {@link} EmailBuilder e.g. EmailNotification en = new EmailBuilder(subject, bodyTextHTML, httpServletRequest, recipients).bcc(email).build(); + * @param email the recipient addresses + */ public void addRecipientInBCC(String email) { try { emailRecipientsInBCC.add(new InternetAddress(email)); @@ -146,6 +161,18 @@ public class EmailNotification { } } + /** + * You can also use {@link} EmailBuilder e.g. EmailNotification en = new EmailBuilder(subject, bodyTextHTML, httpServletRequest, recipients).bcc(email).build(); + * @param email the recipient addresses + */ + public void addContentTextPlain(String bodyTextPlain) { + this.emailBodyTextPlain = new StringBuffer(bodyTextPlain).append(getWarningLegalText(true)); + } + /** + * Sends the email message. The message can be anything with any content and that must be delivered to something or someone. + * + */ + @SuppressWarnings("deprecation") public void sendEmail() { Properties props = System.getProperties(); props.put("mail.smtp.host", MAIL_SERVICE_HOST); @@ -186,10 +213,10 @@ public class EmailNotification { if (emailBodyTextPlain != null) { final MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent(emailBodyTextPlain.toString(), "text/plain; charset=UTF-8"); - + final MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(emailBodyTextHTML.toString(), "text/html; charset=UTF-8"); - + final Multipart mp = new MimeMultipart("alternative"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); @@ -212,8 +239,12 @@ public class EmailNotification { _log.error("Failed to send the email message.", e); } } - - public static StringBuffer getWarningLegalText(boolean plainText) { + /** + * + * @param plainText + * @return + */ + private static StringBuffer getWarningLegalText(boolean plainText) { StringBuffer toReturn = new StringBuffer(); if (!plainText) { toReturn.append("

") @@ -230,6 +261,150 @@ public class EmailNotification { } return toReturn; } + + + @Override + public String toString() { + return "EmailNotification [emailrecipients=" + Arrays.toString(emailrecipients) + ", emailRecipientsInCC=" + + emailRecipientsInCC + ", emailRecipientsInBCC=" + emailRecipientsInBCC + ", emailSubject=" + + emailSubject + ", emailBodyTextHTML=" + emailBodyTextHTML + ", emailBodyTextPlain=" + + emailBodyTextPlain + "]"; + } + /** + * + * EmailBuilder class for builder pattern + * + */ + public static class EmailBuilder { + + private final String[] emailrecipients; + /** + * The subject + */ + private final String subject; + /** + * The template, if any + */ + private Template template; + /** + * The email body content in HTML + */ + private String bodyTextHTML; + /** + * The email body content in Simple Text + */ + private String bodyTextPlain; + /** + * The list of recipient addresses with cc + */ + private List emailRecipientsInCC; + /** + * The list of recipient addresses with cbc + */ + private List emailRecipientsInBCC; + + private HttpServletRequest httpServletRequest; + + + /** + * + * Initialize the email with the following information: + *

    + *
  • The subject of the mail
  • + *
  • The body of the mail in HTML
  • + *
  • One or several "to" recipient addresses
  • + *
+ * + * @param emailrecipients an list of email addresses to be added in the TO + * @param subject the subject of your email + * @param bodyTextHTML the body of your email in HTML + * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case) + */ + public EmailBuilder(String subject, HttpServletRequest httpServletRequest, String ... emailrecipients) { + this.subject = subject; + this.httpServletRequest = httpServletRequest; + this.emailrecipients = emailrecipients; + emailRecipientsInCC = new ArrayList(); + emailRecipientsInBCC = new ArrayList(); + } + /** + * + * @param bodyTextPlain + * @return + */ + public EmailBuilder withTemplate(Template selectedTemplate) { + this.template = selectedTemplate; + return this; + } + /** + * + * @param bodyTextHTML + * @return + */ + public EmailBuilder contentTextHTML(String bodyTextHTML) { + this.bodyTextHTML = bodyTextHTML; + return this; + } + /** + * + * @param bodyTextPlain + * @return + */ + public EmailBuilder contentTextPlain(String bodyTextPlain) { + this.bodyTextPlain = bodyTextPlain; + return this; + } + /** + * Add a "cc" recipient address. + * + * @param cc one or several recipient addresses + * @return this instance for fluent use + */ + public EmailBuilder cc(String... cc) { + for (String email : cc) { + try { + this.emailRecipientsInCC.add(new InternetAddress(email)); + } catch (AddressException e) { + e.printStackTrace(); + } + } + return this; + } + /** + * Add a "bcc" recipient address. + * + * @param bcc one or several recipient addresses + * @return this instance for fluent use + */ + public EmailBuilder bcc(String... bcc) { + for (String email : bcc) { + try { + this.emailRecipientsInBCC.add(new InternetAddress(email)); + } catch (AddressException e) { + e.printStackTrace(); + } + } + return this; + } + /** + * build the see {@link EmailNotification} object + */ + public EmailNotification build() { + EmailNotification email = new EmailNotification(this); + validateUserObject(email); + return email; + } + + private void validateUserObject(EmailNotification email) { + if (emailrecipients.length == 0) { + throw new IllegalArgumentException("The array of email recipients cannot be empty"); + } + //if a template exists but also the body params are not empty + if (template != null && (bodyTextHTML != null || bodyTextPlain != null)) { + throw new IllegalArgumentException("Template is not null, but also the bodyText (plain or html) which one should I use?"); + } + } + } } diff --git a/src/main/java/org/gcube/common/portal/mailing/templates/AbstractTemplate.java b/src/main/java/org/gcube/common/portal/mailing/templates/AbstractTemplate.java new file mode 100644 index 0000000..c32d78d --- /dev/null +++ b/src/main/java/org/gcube/common/portal/mailing/templates/AbstractTemplate.java @@ -0,0 +1,44 @@ +package org.gcube.common.portal.mailing.templates; + +import org.gcube.vomanagement.usermanagement.GroupManager; +import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; +import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; +import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; + +public abstract class AbstractTemplate { + + private String gatewayName; + private String gatewayURL; + private GroupManager gm; + + public AbstractTemplate(String gatewayName, String gatewayURL) { + super(); + this.gatewayName = gatewayName; + this.gatewayURL = gatewayURL; + this.gm = new LiferayGroupManager(); + } + + public String getGatewayLogoURL() { + long gatewayGroupId; + long gatewayLogoId = 0; + try { + gatewayGroupId = gm.getGroupId(gatewayName); + gatewayLogoId = gm.getGroup(gatewayGroupId).getLogoId(); + } catch (UserManagementSystemException | GroupRetrievalFault e) { + e.printStackTrace(); + } + return gatewayURL + gm.getGroupLogoURL(gatewayLogoId); + } + + public String getGatewayName() { + return gatewayName; + } + + public String getGatewayURL() { + return gatewayURL; + } + + public GroupManager getGroupManagerImpl() { + return gm; + } +} diff --git a/src/main/java/org/gcube/common/portal/mailing/templates/Template.java b/src/main/java/org/gcube/common/portal/mailing/templates/Template.java new file mode 100644 index 0000000..f7860b9 --- /dev/null +++ b/src/main/java/org/gcube/common/portal/mailing/templates/Template.java @@ -0,0 +1,14 @@ +package org.gcube.common.portal.mailing.templates; + + +/** + * Base interface for any email Template. A template must + * + * @author M. Assante, CNR-ISTI + * + */ +public interface Template { + public String compile(String templateContent); + public String getTextHTML(); + public String getTextPLAIN(); +}