improved centralized messaging

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/invites-common-library@128322 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2016-04-26 15:14:04 +00:00
parent 217f5fd371
commit cfbd94b4d4
1 changed files with 41 additions and 69 deletions

View File

@ -4,16 +4,10 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException; import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.ASLSession;
@ -46,8 +40,6 @@ import org.slf4j.LoggerFactory;
*/ */
public class InvitesManager { public class InvitesManager {
private static final Logger _log = LoggerFactory.getLogger(InvitesManager.class); private static final Logger _log = LoggerFactory.getLogger(InvitesManager.class);
private final static String MAIL_SERVICE_HOST = "localhost";
private final static String MAIL_SERVICE_PORT = "25";
private static InvitesManager instance; private static InvitesManager instance;
private static DatabookStore store; private static DatabookStore store;
@ -69,7 +61,7 @@ public class InvitesManager {
} }
return store; return store;
} }
public InviteOperationResult sendInvite( public InviteOperationResult sendInvite(
HttpServletRequest request, HttpServletRequest request,
ASLSession aslSession, ASLSession aslSession,
@ -79,11 +71,11 @@ public class InvitesManager {
String lastName, String lastName,
String email, String email,
String vreDescription) { String vreDescription) {
String username = aslSession.getUsername(); String username = aslSession.getUsername();
String fromFullName = aslSession.getUserFullName(); String fromFullName = aslSession.getUserFullName();
String controlcode = UUID.randomUUID().toString(); String controlcode = UUID.randomUUID().toString();
Invite invite = new Invite(UUID.randomUUID().toString(), username, aslSession.getScopeName(), email, controlcode, InviteStatus.PENDING, new Date(), fromFullName); Invite invite = new Invite(UUID.randomUUID().toString(), username, aslSession.getScopeName(), email, controlcode, InviteStatus.PENDING, new Date(), fromFullName);
InviteOperationResult result = null; InviteOperationResult result = null;
boolean emailResult = false; boolean emailResult = false;
@ -91,17 +83,17 @@ public class InvitesManager {
String vreName = aslSession.getGroupName(); String vreName = aslSession.getGroupName();
result = store.saveInvite(invite); result = store.saveInvite(invite);
emailResult = sendInviteEmail(request, aslSession, portalSenderEmail, portalURL, name, lastName, email, vreDescription); emailResult = sendInviteEmail(request, aslSession, portalSenderEmail, portalURL, name, lastName, email, vreDescription);
notifyInviteSent(username, aslSession.getScopeName(), portalURL, PortalContext.getConfiguration().getGatewayName(), invite, vreName); notifyInviteSent(request, username, aslSession.getScopeName(), invite, vreName);
} catch (AddressException e) { } catch (AddressException e) {
_log.error("Email not valid " + e.getMessage()); _log.error("Email not valid " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
return InviteOperationResult.FAILED; return InviteOperationResult.FAILED;
} }
return (emailResult) ? result : InviteOperationResult.FAILED; return (emailResult) ? result : InviteOperationResult.FAILED;
} }
private Boolean sendInviteEmail( private Boolean sendInviteEmail(
HttpServletRequest request, HttpServletRequest request,
ASLSession aslSession, ASLSession aslSession,
@ -111,38 +103,24 @@ public class InvitesManager {
String lastName, String lastName,
String email, String email,
String vreDescription) { String vreDescription) {
String vreName = aslSession.getGroupName(); String vreName = aslSession.getGroupName();
String fromFullName = aslSession.getUserFullName(); String fromFullName = aslSession.getUserFullName();
Properties props = System.getProperties();
Session session = null;
props.put("mail.smtp.host", MAIL_SERVICE_HOST);
props.put("mail.smtp.port", MAIL_SERVICE_PORT);
session = Session.getDefaultInstance(props);
session.setDebug(true);
try { try {
MimeMessage message = new MimeMessage(session); String subject = "Join me on " + vreName;
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
// Set From: header field of the header.
message.setFrom(new InternetAddress(portalSenderEmail, fromFullName));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(aslSession.getUserEmailAddress()));
// Set Subject: header field
message.setSubject("Join me on " + vreName);
long groupId = aslSession.getGroupId(); long groupId = aslSession.getGroupId();
final String linkToAcceptInvite = portalURL + PortalContext.getConfiguration().getSiteLandingPagePath(request)+"/explore?siteId="+groupId; final String linkToAcceptInvite = portalURL + PortalContext.getConfiguration().getSiteLandingPagePath(request)+"/explore?siteId="+groupId;
final String linkToCreateAccount = portalURL + "/web/guest/home?p_p_id=58&_58_struts_action=%2Flogin%2Fcreate_account"; final String linkToCreateAccount = portalURL + "/web/guest/home?p_p_id=58&_58_struts_action=%2Flogin%2Fcreate_account";
// Now set the actual message
message.setText(getTextEmail(aslSession, name, lastName, email, fromFullName, vreName, vreDescription, linkToAcceptInvite, linkToCreateAccount));
// Send message EmailNotification mailToAdmin = new EmailNotification(
Transport.send(message); email ,
subject,
getHTMLEmail(aslSession, name, lastName, email, fromFullName, vreName, vreDescription, linkToAcceptInvite, linkToCreateAccount),
request);
mailToAdmin.sendEmail();
_log.debug("Sent message successfully to " + email ); _log.debug("Sent message successfully to " + email );
} catch (Exception mex) { } catch (Exception mex) {
mex.printStackTrace(); mex.printStackTrace();
@ -151,7 +129,7 @@ public class InvitesManager {
} }
return true; return true;
} }
/** /**
* *
* @param aslSession * @param aslSession
@ -164,7 +142,7 @@ public class InvitesManager {
* @param vreDescription the description of the environment where you are inviting the person * @param vreDescription the description of the environment where you are inviting the person
* @return the email text * @return the email text
*/ */
private String getTextEmail( private String getHTMLEmail(
ASLSession aslSession, ASLSession aslSession,
String name, String name,
String lastName, String lastName,
@ -174,27 +152,21 @@ public class InvitesManager {
String vreDescription, String vreDescription,
String linkToAcceptInvite, String linkToAcceptInvite,
String linkToCreateAccount) { String linkToCreateAccount) {
StringBuilder body = new StringBuilder();
body.append("Dear " + name)
.append(",\n")
.append(fromFullName).append(" has invited you to " + vreName + ", you can find a brief description below:")
.append("\n")
.append("\n").append(convertHTML2Text(vreDescription))
.append("\n\n")
.append("To accept the invite just follow this link: " + linkToAcceptInvite)
.append("\n\n")
.append("Please note: if you do not have an account yet, sign up first: " + linkToCreateAccount)
.append("\n\n\n\n")
.append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain")
.append("information which is privileged, confidential, proprietary, or exempt from disclosure under applicable law. "
+ "If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, "
+ "you are strictly prohibited from disclosing, distributing, copying, or in any way using this message.")
.append("If you have received this communication in error, please notify the <sender> and destroy and delete any copies you may have received.");
StringBuilder body = new StringBuilder();
body.append("Dear " + name)
.append(", <p>")
.append(fromFullName).append(" has invited you to " + vreName + ", you can find a brief description below:")
.append("</p>")
.append("<p>").append(vreDescription)
.append("</p>")
.append("<p>To accept the invite just follow this link: " + linkToAcceptInvite)
.append("</p>")
.append("Please note: if you do not have an account yet, sign up first: " + linkToCreateAccount);
return body.toString(); return body.toString();
} }
@ -222,7 +194,7 @@ public class InvitesManager {
} }
return text; return text;
} }
private static StringBuffer buildStringFromNode(Node node) { private static StringBuffer buildStringFromNode(Node node) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
@ -245,7 +217,7 @@ public class InvitesManager {
return buffer; return buffer;
} }
private static ArrayList<String> getAdministratorsEmails(String scope) { private static ArrayList<String> getAdministratorsEmails(String scope) {
LiferayUserManager userManager = new LiferayUserManager(); LiferayUserManager userManager = new LiferayUserManager();
LiferayGroupManager groupManager = new LiferayGroupManager(); LiferayGroupManager groupManager = new LiferayGroupManager();
@ -287,28 +259,28 @@ public class InvitesManager {
} }
return adminEmailsList; return adminEmailsList;
} }
/** /**
* *
* @param scope . * @param scope .
* @param optionalMessage . * @param optionalMessage .
*/ */
public static void notifyInviteSent(String username, String scope, String portalbasicurl, String gatewayName, Invite invite, String vreName) { public static void notifyInviteSent(HttpServletRequest request, String username, String scope, Invite invite, String vreName) {
ArrayList<String> adminEmails = getAdministratorsEmails(scope); ArrayList<String> adminEmails = getAdministratorsEmails(scope);
StringBuffer body = new StringBuffer(); StringBuffer body = new StringBuffer();
body.append("<p>Dear manager of "+ scope +",<br />this email message was automatically generated by " + portalbasicurl +" to inform you that "); body.append("<p>Dear manager of "+ scope +",<br />this email message was automatically generated by " + PortalContext.getConfiguration().getGatewayURL(request) +" to inform you that ");
body.append("</p>"); body.append("</p>");
body.append("<p>"); body.append("<p>");
body.append("<b>"+invite.getSenderFullName() + " (" + invite.getSenderUserId() +")</b> has invited " + invite.getInvitedEmail() + " to the following environment:"); body.append("<b>"+invite.getSenderFullName() + " (" + invite.getSenderUserId() +")</b> has invited " + invite.getInvitedEmail() + " to the following environment:");
body.append("<br /><br />"); body.append("<br /><br />");
body.append("<b>" + scope+"</b>"); body.append("<b>" + scope+"</b>");
body.append("</p>"); body.append("</p>");
String[] allMails = new String[adminEmails.size()]; String[] allMails = new String[adminEmails.size()];
adminEmails.toArray(allMails); adminEmails.toArray(allMails);
EmailNotification mailToAdmin = new EmailNotification(allMails , "An invite was sent on " + vreName + " by " + invite.getSenderFullName(), body.toString()); EmailNotification mailToAdmin = new EmailNotification(allMails , "An invite was sent on " + vreName + " by " + invite.getSenderFullName(), body.toString(), request);
mailToAdmin.sendEmail(); mailToAdmin.sendEmail();
} }
} }