From caf2034185a9f9b651240a06eb5b6d9eccebad06 Mon Sep 17 00:00:00 2001 From: "massimiliano.assante" Date: Wed, 30 Jan 2013 17:41:39 +0000 Subject: [PATCH] added email notificaiton class git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@68585 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 5 ++ .../ApplicationNotificationsManager.java | 14 +++- .../social/EmailNotification.java | 81 +++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java diff --git a/pom.xml b/pom.xml index 7831822..5fd870d 100644 --- a/pom.xml +++ b/pom.xml @@ -73,6 +73,11 @@ 2.0 ftp + + javax.mail + mail + 1.4.3 + 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 8229a1d..593a9c2 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java @@ -1,14 +1,17 @@ package org.gcube.applicationsupportlayer.social; import java.util.Date; +import java.util.List; import java.util.UUID; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.common.core.utils.logging.GCUBEClientLog; import org.gcube.portal.databook.shared.ApplicationProfile; import org.gcube.portal.databook.shared.Notification; +import org.gcube.portal.databook.shared.NotificationChannelType; import org.gcube.portal.databook.shared.NotificationType; import org.gcube.portal.databook.shared.RunningJob; +import org.gcube.portal.databook.shared.ex.NotificationChannelTypeNotFoundException; import org.gcube.portlets.user.homelibrary.home.exceptions.InternalErrorException; import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceFolder; import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceItem; @@ -54,7 +57,16 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen */ private boolean saveNotification(Notification notification2Save) { _log.trace("Trying to send notification to: " + notification2Save.getUserid() + " Type: " + notification2Save.getType()); - boolean result = getStoreInstance().saveNotification(notification2Save); + List channels = null; + try { + channels = getStoreInstance().getUserNotificationChannels(notification2Save.getUserid()); + } catch (NotificationChannelTypeNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + boolean result = false; + if (channels.contains(NotificationChannelType.PORTAL)) + result = getStoreInstance().saveNotification(notification2Save); if (result) _log.trace("Notification Saved Successfully! "); else diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java b/src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java new file mode 100644 index 0000000..d975cf9 --- /dev/null +++ b/src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java @@ -0,0 +1,81 @@ +package org.gcube.applicationsupportlayer.social; + +import java.util.Date; +import java.util.Properties; + +import javax.mail.Address; +import javax.mail.Message; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.antlr.grammar.v3.ANTLRv3Parser.throwsSpec_return; +import org.gcube.common.core.utils.logging.GCUBEClientLog; + +/** + * A class for sending email + * + * @author Massimiliano Assante + * + */ +public class EmailNotification { + + private static GCUBEClientLog _log = new GCUBEClientLog(EmailNotification.class); + + private String sender; + private String recipients[]; + private String subject; + private String body; + + /** + * + * @param sender + * @param subject + * @param body + * @param recipients + */ + public EmailNotification(String sender, String subject, String body, String... recipients) { + if (recipients == null || recipients.length == 0) + throw new IllegalArgumentException("There must be at least one recipient"); + this.sender = sender; + this.recipients = recipients; + this.subject = subject; + this.body = body; + } + + public void send() { + Properties props = System.getProperties(); + String mailServiceHost = "localhost"; + props.put("mail.smtp.host", mailServiceHost); + String mailServicePort = "25"; + props.put("mail.smtp.port", mailServicePort); + Session session = Session.getDefaultInstance(props, null); + session.setDebug(true); + Message mimeMessage = new MimeMessage(session); + + try { + // EMAIL SENDER + Address from = new InternetAddress(sender); + mimeMessage.setFrom(from); + + // EMAIL RECIPIENTS + for (int i=0; i < recipients.length; i++) { + Address address = new InternetAddress(recipients[i]); + mimeMessage.addRecipient(Message.RecipientType.TO, address); + + } + + mimeMessage.setSubject(subject); + // mimeMessage.setText(body); + mimeMessage.setContent(body, "text/html"); + mimeMessage.setSentDate(new Date()); + Transport.send(mimeMessage); + } catch (Exception e) { + e.printStackTrace(); + _log.error("Failed to send the notification email:", e); + } +} +} + +