From 7790c9d91d68e3ddc1680d73503e2aeb0684ac89 Mon Sep 17 00:00:00 2001 From: "massimiliano.assante" Date: Wed, 13 Apr 2016 16:10:22 +0000 Subject: [PATCH] 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 --- pom.xml | 11 +++- .../ApplicationNotificationsManager.java | 51 ++++++++++++------- .../social/SocialPortalBridge.java | 6 +-- .../mailing/EmailNotificationsConsumer.java | 3 +- .../social/mailing/EmailPlugin.java | 30 +++++------ 5 files changed, 61 insertions(+), 40 deletions(-) diff --git a/pom.xml b/pom.xml index 7ae8671..abb087e 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.applicationsupportlayer aslsocial - 0.16.0-SNAPSHOT + 1.0.0-SNAPSHOT jar Social Portal ASL Extension @@ -45,6 +45,7 @@ org.gcube.common.portal portal-manager + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) provided @@ -65,11 +66,13 @@ org.gcube.dvos usermanagement-core + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) provided org.gcube.portal custom-portal-handler + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) provided @@ -133,6 +136,12 @@ portlet-api provided + + javax.servlet + javax.servlet-api + 3.0.1 + provided + junit junit diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java b/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java index cb2ba42..282b52b 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java @@ -5,6 +5,8 @@ import java.util.Date; import java.util.List; import java.util.UUID; +import javax.servlet.http.HttpServletRequest; + import org.gcube.application.framework.core.session.ASLSession; import org.gcube.applicationsupportlayer.social.mailing.EmailPlugin; 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.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; -import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager; -import org.gcube.vomanagement.usermanagement.model.UserModel; +import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,20 +39,23 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen private String portalName; 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 * @param ASLSession the ASLSession instance */ @Deprecated public ApplicationNotificationsManager(ASLSession session) { super(session); - portalName = PortalContext.getConfiguration().getGatewayName(); - senderEmail = PortalContext.getConfiguration().getSenderEmail(); + PortalContext context = PortalContext.getConfiguration(); + portalName = context.getGatewayName(); + senderEmail = context.getSenderEmail(); + portalURL = context.getGatewayURL(); _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, * make sure you create your application profile on the infrastructure. * @@ -62,18 +67,19 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen @Deprecated public ApplicationNotificationsManager(ASLSession session, String portletClassName) { super(session, portletClassName); - portalName = PortalContext.getConfiguration().getGatewayName(); - senderEmail = PortalContext.getConfiguration().getSenderEmail(); + PortalContext context = PortalContext.getConfiguration(); + portalName = context.getGatewayName(); + senderEmail = context.getSenderEmail(); + portalURL = context.getGatewayURL(); } /** * Use this constructor if you do not need notifications to point back to your applications * @param scope the current scope * @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); - portalName = PortalContext.getConfiguration().getGatewayName(); - senderEmail = PortalContext.getConfiguration().getSenderEmail(); + setContext(request); _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 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); - portalName = PortalContext.getConfiguration().getGatewayName(); - senderEmail = PortalContext.getConfiguration().getSenderEmail(); + setContext(request); + } + /** + * 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 @@ -117,9 +132,9 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen else _log.error("Error While trying to save Notification"); } - if (channels.contains(NotificationChannelType.EMAIL)) - EmailPlugin.getInstance().sendNotification(notification2Save, currGroupName, portalName, senderEmail, hashtags); - + if (channels.contains(NotificationChannelType.EMAIL)) { + EmailPlugin.getInstance(portalURL).sendNotification(notification2Save, currGroupName, portalName, senderEmail, hashtags); + } if (channels.isEmpty()) { _log.info("Notification was not needed as "+ notification2Save.getUserid() +" decided not to be notified for " + notification2Save.getType()); result = true; @@ -264,7 +279,7 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen @Override public boolean notifyFolderAddedUser(String userIdToNotify, WorkspaceSharedFolder sharedFolder, String newAddedUserId) throws InternalErrorException, UserManagementSystemException, UserRetrievalFault, UserManagementPortalException { UserManager um = new LiferayUserManager(); - UserModel user = um.getUserByScreenName(newAddedUserId); + GCubeUser user = um.getUserByUsername(newAddedUserId); Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.WP_FOLDER_ADDEDUSER, diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/SocialPortalBridge.java b/src/main/java/org/gcube/applicationsupportlayer/social/SocialPortalBridge.java index 99a354b..30e185f 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/SocialPortalBridge.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/SocialPortalBridge.java @@ -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.impl.QueryBox; 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.LoggerFactory; import org.w3c.dom.Node; @@ -100,10 +100,10 @@ public class SocialPortalBridge { return store; } - protected String getScopeByOrganizationId(String vreid) { + protected String getScopeByVREId(long vreid) { GroupManager gm = new LiferayGroupManager(); try { - return gm.getScope(vreid); + return gm.getInfrastructureScope(vreid); } catch (Exception e) { _log.error("Could not find a scope for this VREid: " + vreid); return null; diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailNotificationsConsumer.java b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailNotificationsConsumer.java index 95d13fe..21a566a 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailNotificationsConsumer.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailNotificationsConsumer.java @@ -43,7 +43,6 @@ public class EmailNotificationsConsumer extends Thread { public EmailNotificationsConsumer() { super(); _log.info("EmailNotificationsConsumer thread started at " + new Date() + " trying to fetch SMTP configuration from infrastructure ..."); - //query try { List resources = getConfigurationFromIS(); @@ -127,7 +126,7 @@ public class EmailNotificationsConsumer extends Thread { _log.debug("Emails Buffer not empty, sending emails "); Transport t = null; try { - t = session.getTransport(); + t = session.getTransport("smtp"); t.connect(); //sync method to ensure the producer do not put new emails in the meantime diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java index 116d8d9..2d32e56 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java @@ -15,13 +15,14 @@ import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; 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.DatabookStore; import org.gcube.portal.databook.shared.Comment; import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.Notification; 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.nodes.Document; import org.jsoup.nodes.Element; @@ -30,10 +31,6 @@ import org.jsoup.nodes.TextNode; import org.slf4j.Logger; 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 @@ -48,13 +45,16 @@ public class EmailPlugin { private static EmailPlugin singleton; private static DatabookStore store = new DBCassandraAstyanaxImpl(); - public static EmailPlugin getInstance() { + private static String portalURL; + + public static EmailPlugin getInstance(String portalURL) { if (singleton == null) - singleton = new EmailPlugin(); + singleton = new EmailPlugin(portalURL); return singleton; } - private EmailPlugin() { + private EmailPlugin(String portalURL) { + EmailPlugin.portalURL = portalURL; 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 { - UserModel user = null; - String portalUrl = null; + GCubeUser user = null; try { - user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), notification2Save.getUserid()); - portalUrl = PortalUtil.getPortalURL(OrganizationsUtil.getCompany().getVirtualHost(), 443, true); + user = new LiferayUserManager().getUserByUsername(notification2Save.getUserid()); } catch (Exception e1) { e1.printStackTrace(); _log.warn("While trying to get email for user/group: " + notification2Save.getUserid()); return null; } - String email = user.getEmailAddress(); + String email = user.getEmail(); Message msg2Return = new MimeMessage(session); @@ -448,13 +446,13 @@ public class EmailPlugin { } // 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(); - 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(); - 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"); mp.addBodyPart(textPart);