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
This commit is contained in:
parent
82528e23fc
commit
dc31718c4c
|
@ -18,7 +18,7 @@
|
|||
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
|
|
4
pom.xml
4
pom.xml
|
@ -23,8 +23,8 @@
|
|||
</scm>
|
||||
<properties>
|
||||
<distroDirectory>distro</distroDirectory>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<liferay.version>6.2.5</liferay.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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<InternetAddress> emailRecipientsInCC;
|
||||
private List<InternetAddress> emailRecipientsInBCC;
|
||||
/**
|
||||
* The list of recipient addresses with cc
|
||||
*/
|
||||
private final List<InternetAddress> emailRecipientsInCC;
|
||||
/**
|
||||
* The list of recipient addresses with bcc
|
||||
*/
|
||||
private final List<InternetAddress> 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:
|
||||
* <ul>
|
||||
* <li>The subject of the mail</li>
|
||||
* <li>The body of the mail in HTML</li>
|
||||
* <li>The recipient addresses</li>
|
||||
* <li>the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @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:
|
||||
* <ul>
|
||||
* <li>The subject of the mail</li>
|
||||
* <li>The body of the mail in HTML</li>
|
||||
* <li>One or several "to" recipient addresses</li>
|
||||
* <li>the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @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:
|
||||
* <ul>
|
||||
* <li>The subject of the mail</li>
|
||||
* <li>The body of the mail in HTML</li>
|
||||
* <li>One or several "to" recipient addresses</li>
|
||||
* <li>the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @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<String> 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<String> 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<InternetAddress>();
|
||||
emailRecipientsInBCC = new ArrayList<InternetAddress>();
|
||||
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<InternetAddress>();
|
||||
this.emailRecipientsInBCC = new ArrayList<InternetAddress>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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("<p>")
|
||||
|
@ -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<InternetAddress> emailRecipientsInCC;
|
||||
/**
|
||||
* The list of recipient addresses with cbc
|
||||
*/
|
||||
private List<InternetAddress> emailRecipientsInBCC;
|
||||
|
||||
private HttpServletRequest httpServletRequest;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Initialize the email with the following information:
|
||||
* <ul>
|
||||
* <li>The subject of the mail</li>
|
||||
* <li>The body of the mail in HTML</li>
|
||||
* <li>One or several "to" recipient addresses</li>
|
||||
* </ul>
|
||||
*
|
||||
* @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<InternetAddress>();
|
||||
emailRecipientsInBCC = new ArrayList<InternetAddress>();
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @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?");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue