emails are working

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@128068 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2016-04-13 16:10:22 +00:00
parent cc6a9872a3
commit 7790c9d91d
5 changed files with 61 additions and 40 deletions

11
pom.xml
View File

@ -10,7 +10,7 @@
<groupId>org.gcube.applicationsupportlayer</groupId> <groupId>org.gcube.applicationsupportlayer</groupId>
<artifactId>aslsocial</artifactId> <artifactId>aslsocial</artifactId>
<version>0.16.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Social Portal ASL Extension</name> <name>Social Portal ASL Extension</name>
<description> <description>
@ -45,6 +45,7 @@
<dependency> <dependency>
<groupId>org.gcube.common.portal</groupId> <groupId>org.gcube.common.portal</groupId>
<artifactId>portal-manager</artifactId> <artifactId>portal-manager</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -65,11 +66,13 @@
<dependency> <dependency>
<groupId>org.gcube.dvos</groupId> <groupId>org.gcube.dvos</groupId>
<artifactId>usermanagement-core</artifactId> <artifactId>usermanagement-core</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.portal</groupId> <groupId>org.gcube.portal</groupId>
<artifactId>custom-portal-handler</artifactId> <artifactId>custom-portal-handler</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- FWS DEPS --> <!-- FWS DEPS -->
@ -133,6 +136,12 @@
<artifactId>portlet-api</artifactId> <artifactId>portlet-api</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@ -5,6 +5,8 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.applicationsupportlayer.social.mailing.EmailPlugin; import org.gcube.applicationsupportlayer.social.mailing.EmailPlugin;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser; import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
@ -21,8 +23,8 @@ import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException; import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.UserModel; import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -37,20 +39,23 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
private String portalName; private String portalName;
private String senderEmail; private String senderEmail;
private String portalURL;
/** /**
* deprecated method:please use ApplicationNotificationsManager(String scope, {@link SocialNetworkingUser} currUser, String portletClassName) * @deprecated:please use ApplicationNotificationsManager(String scope, {@link SocialNetworkingUser} currUser, String portletClassName)
* Use this constructor if you do not need notifications to point back to your applications * Use this constructor if you do not need notifications to point back to your applications
* @param ASLSession the ASLSession instance * @param ASLSession the ASLSession instance
*/ */
@Deprecated @Deprecated
public ApplicationNotificationsManager(ASLSession session) { public ApplicationNotificationsManager(ASLSession session) {
super(session); super(session);
portalName = PortalContext.getConfiguration().getGatewayName(); PortalContext context = PortalContext.getConfiguration();
senderEmail = PortalContext.getConfiguration().getSenderEmail(); portalName = context.getGatewayName();
senderEmail = context.getSenderEmail();
portalURL = context.getGatewayURL();
_log.warn("Asked for Simple Notification (without redirect to creator)"); _log.warn("Asked for Simple Notification (without redirect to creator)");
} }
/** /**
* deprecated method: please use ApplicationNotificationsManager(String scope, {@link SocialNetworkingUser} currUser, String portletClassName) * @deprecated: please use ApplicationNotificationsManager(String scope, {@link SocialNetworkingUser} currUser, String portletClassName)
* Use this constructor if you do need notifications to point back to your applications, * Use this constructor if you do need notifications to point back to your applications,
* make sure you create your application profile on the infrastructure. * make sure you create your application profile on the infrastructure.
* *
@ -62,18 +67,19 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
@Deprecated @Deprecated
public ApplicationNotificationsManager(ASLSession session, String portletClassName) { public ApplicationNotificationsManager(ASLSession session, String portletClassName) {
super(session, portletClassName); super(session, portletClassName);
portalName = PortalContext.getConfiguration().getGatewayName(); PortalContext context = PortalContext.getConfiguration();
senderEmail = PortalContext.getConfiguration().getSenderEmail(); portalName = context.getGatewayName();
senderEmail = context.getSenderEmail();
portalURL = context.getGatewayURL();
} }
/** /**
* Use this constructor if you do not need notifications to point back to your applications * Use this constructor if you do not need notifications to point back to your applications
* @param scope the current scope * @param scope the current scope
* @param currUser an instance of {@link SocialNetworkingUser} filled with the required user data * @param currUser an instance of {@link SocialNetworkingUser} filled with the required user data
*/ */
public ApplicationNotificationsManager(String scope, SocialNetworkingUser currUser) { public ApplicationNotificationsManager(HttpServletRequest request, String scope, SocialNetworkingUser currUser) {
super(scope, currUser); super(scope, currUser);
portalName = PortalContext.getConfiguration().getGatewayName(); setContext(request);
senderEmail = PortalContext.getConfiguration().getSenderEmail();
_log.warn("Asked for Simple Notification (without redirect to creator)"); _log.warn("Asked for Simple Notification (without redirect to creator)");
} }
/** /**
@ -87,10 +93,19 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
* @param currUser an instance of {@link SocialNetworkingUser} filled with the required user data * @param currUser an instance of {@link SocialNetworkingUser} filled with the required user data
* @param portletClassName your portlet class name will be used ad unique identifier for your applicationProfile * @param portletClassName your portlet class name will be used ad unique identifier for your applicationProfile
*/ */
public ApplicationNotificationsManager(String scope, SocialNetworkingUser currUser, String portletClassName) { public ApplicationNotificationsManager(HttpServletRequest request, String scope, SocialNetworkingUser currUser, String portletClassName) {
super(scope, currUser, portletClassName); super(scope, currUser, portletClassName);
portalName = PortalContext.getConfiguration().getGatewayName(); setContext(request);
senderEmail = PortalContext.getConfiguration().getSenderEmail(); }
/**
* set the current portal context (name, emal, url)
* @param request
*/
private void setContext(HttpServletRequest request) {
PortalContext context = PortalContext.getConfiguration();
portalName = context.getGatewayName(request);
senderEmail = context.getSenderEmail(request);
portalURL = context.getGatewayURL(request);
} }
/** /**
* actually save the notification to the store * actually save the notification to the store
@ -117,9 +132,9 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
else else
_log.error("Error While trying to save Notification"); _log.error("Error While trying to save Notification");
} }
if (channels.contains(NotificationChannelType.EMAIL)) if (channels.contains(NotificationChannelType.EMAIL)) {
EmailPlugin.getInstance().sendNotification(notification2Save, currGroupName, portalName, senderEmail, hashtags); EmailPlugin.getInstance(portalURL).sendNotification(notification2Save, currGroupName, portalName, senderEmail, hashtags);
}
if (channels.isEmpty()) { if (channels.isEmpty()) {
_log.info("Notification was not needed as "+ notification2Save.getUserid() +" decided not to be notified for " + notification2Save.getType()); _log.info("Notification was not needed as "+ notification2Save.getUserid() +" decided not to be notified for " + notification2Save.getType());
result = true; result = true;
@ -264,7 +279,7 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
@Override @Override
public boolean notifyFolderAddedUser(String userIdToNotify, WorkspaceSharedFolder sharedFolder, String newAddedUserId) throws InternalErrorException, UserManagementSystemException, UserRetrievalFault, UserManagementPortalException { public boolean notifyFolderAddedUser(String userIdToNotify, WorkspaceSharedFolder sharedFolder, String newAddedUserId) throws InternalErrorException, UserManagementSystemException, UserRetrievalFault, UserManagementPortalException {
UserManager um = new LiferayUserManager(); UserManager um = new LiferayUserManager();
UserModel user = um.getUserByScreenName(newAddedUserId); GCubeUser user = um.getUserByUsername(newAddedUserId);
Notification not = new Notification( Notification not = new Notification(
UUID.randomUUID().toString(), UUID.randomUUID().toString(),
NotificationType.WP_FOLDER_ADDEDUSER, NotificationType.WP_FOLDER_ADDEDUSER,

View File

@ -22,7 +22,7 @@ import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.api.Query;
import org.gcube.resources.discovery.client.queries.impl.QueryBox; import org.gcube.resources.discovery.client.queries.impl.QueryBox;
import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Node; import org.w3c.dom.Node;
@ -100,10 +100,10 @@ public class SocialPortalBridge {
return store; return store;
} }
protected String getScopeByOrganizationId(String vreid) { protected String getScopeByVREId(long vreid) {
GroupManager gm = new LiferayGroupManager(); GroupManager gm = new LiferayGroupManager();
try { try {
return gm.getScope(vreid); return gm.getInfrastructureScope(vreid);
} catch (Exception e) { } catch (Exception e) {
_log.error("Could not find a scope for this VREid: " + vreid); _log.error("Could not find a scope for this VREid: " + vreid);
return null; return null;

View File

@ -43,7 +43,6 @@ public class EmailNotificationsConsumer extends Thread {
public EmailNotificationsConsumer() { public EmailNotificationsConsumer() {
super(); super();
_log.info("EmailNotificationsConsumer thread started at " + new Date() + " trying to fetch SMTP configuration from infrastructure ..."); _log.info("EmailNotificationsConsumer thread started at " + new Date() + " trying to fetch SMTP configuration from infrastructure ...");
//query //query
try { try {
List<ServiceEndpoint> resources = getConfigurationFromIS(); List<ServiceEndpoint> resources = getConfigurationFromIS();
@ -127,7 +126,7 @@ public class EmailNotificationsConsumer extends Thread {
_log.debug("Emails Buffer not empty, sending emails "); _log.debug("Emails Buffer not empty, sending emails ");
Transport t = null; Transport t = null;
try { try {
t = session.getTransport(); t = session.getTransport("smtp");
t.connect(); t.connect();
//sync method to ensure the producer do not put new emails in the meantime //sync method to ensure the producer do not put new emails in the meantime

View File

@ -15,13 +15,14 @@ import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import org.gcube.portal.custom.communitymanager.OrganizationsUtil;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl; import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
import org.gcube.portal.databook.server.DatabookStore; import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.Comment; import org.gcube.portal.databook.shared.Comment;
import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.Feed;
import org.gcube.portal.databook.shared.Notification; import org.gcube.portal.databook.shared.Notification;
import org.gcube.portal.databook.shared.NotificationType; import org.gcube.portal.databook.shared.NotificationType;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -30,10 +31,6 @@ import org.jsoup.nodes.TextNode;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.liferay.portal.model.UserModel;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
/** /**
* *
* @author Massimiliano Assante * @author Massimiliano Assante
@ -48,13 +45,16 @@ public class EmailPlugin {
private static EmailPlugin singleton; private static EmailPlugin singleton;
private static DatabookStore store = new DBCassandraAstyanaxImpl(); private static DatabookStore store = new DBCassandraAstyanaxImpl();
public static EmailPlugin getInstance() { private static String portalURL;
public static EmailPlugin getInstance(String portalURL) {
if (singleton == null) if (singleton == null)
singleton = new EmailPlugin(); singleton = new EmailPlugin(portalURL);
return singleton; return singleton;
} }
private EmailPlugin() { private EmailPlugin(String portalURL) {
EmailPlugin.portalURL = portalURL;
new EmailNotificationsConsumer().start(); new EmailNotificationsConsumer().start();
} }
@ -394,17 +394,15 @@ public class EmailPlugin {
protected static Message getMessageNotification(Session session, Notification notification2Save, String vreName, String portalName, String senderEmail, String ... hashtags) throws Exception { protected static Message getMessageNotification(Session session, Notification notification2Save, String vreName, String portalName, String senderEmail, String ... hashtags) throws Exception {
UserModel user = null; GCubeUser user = null;
String portalUrl = null;
try { try {
user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), notification2Save.getUserid()); user = new LiferayUserManager().getUserByUsername(notification2Save.getUserid());
portalUrl = PortalUtil.getPortalURL(OrganizationsUtil.getCompany().getVirtualHost(), 443, true);
} catch (Exception e1) { } catch (Exception e1) {
e1.printStackTrace(); e1.printStackTrace();
_log.warn("While trying to get email for user/group: " + notification2Save.getUserid()); _log.warn("While trying to get email for user/group: " + notification2Save.getUserid());
return null; return null;
} }
String email = user.getEmailAddress(); String email = user.getEmail();
Message msg2Return = new MimeMessage(session); Message msg2Return = new MimeMessage(session);
@ -448,13 +446,13 @@ public class EmailPlugin {
} }
// set subject // set subject
msg2Return.setSubject(getSubjectByNotificationType(notification2Save, portalUrl, vreName, user.getFirstName(), hashtags)); msg2Return.setSubject(getSubjectByNotificationType(notification2Save, portalURL, vreName, user.getFirstName(), hashtags));
final MimeBodyPart textPart = new MimeBodyPart(); final MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(getTextEmail(notification2Save, user.getFirstName(), portalUrl, email, feed, comments, commentKey, hashtags), "text/plain; charset=UTF-8"); textPart.setContent(getTextEmail(notification2Save, user.getFirstName(), portalURL, email, feed, comments, commentKey, hashtags), "text/plain; charset=UTF-8");
final MimeBodyPart htmlPart = new MimeBodyPart(); final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(getHTMLEmail(notification2Save, user.getFirstName(), portalUrl, email, vreName, feed, comments, commentKey, hashtags), "text/html; charset=UTF-8"); htmlPart.setContent(getHTMLEmail(notification2Save, user.getFirstName(), portalURL, email, vreName, feed, comments, commentKey, hashtags), "text/html; charset=UTF-8");
final Multipart mp = new MimeMultipart("alternative"); final Multipart mp = new MimeMultipart("alternative");
mp.addBodyPart(textPart); mp.addBodyPart(textPart);