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
This commit is contained in:
Massimiliano Assante 2013-01-30 17:41:39 +00:00
parent 6661655a30
commit caf2034185
3 changed files with 99 additions and 1 deletions

View File

@ -73,6 +73,11 @@
<version>2.0</version>
<classifier>ftp</classifier>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -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<NotificationChannelType> 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

View File

@ -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);
}
}
}