();
}
+ /**
+ * @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();
+}