2014-05-19 19:07:18 +02:00
package org.gcube.applicationsupportlayer.social.mailing ;
2014-07-01 17:15:20 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
2014-05-19 19:07:18 +02:00
import java.util.ArrayList ;
import java.util.Date ;
2014-07-01 17:15:20 +02:00
import java.util.List ;
2014-05-19 19:07:18 +02:00
import java.util.Properties ;
import javax.mail.Address ;
import javax.mail.Message ;
import javax.mail.MessagingException ;
2014-07-01 17:15:20 +02:00
import javax.mail.PasswordAuthentication ;
2014-05-19 19:07:18 +02:00
import javax.mail.Session ;
import javax.mail.Transport ;
2017-01-16 18:10:09 +01:00
import org.gcube.applicationsupportlayer.social.ScopeBeanExt ;
2014-07-01 17:15:20 +02:00
import org.gcube.common.encryption.StringEncrypter ;
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
2014-05-19 19:07:18 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
*
* @author Massimiliano Assante ISTI - CNR
*
* /
public class EmailNotificationsConsumer extends Thread {
private static Logger _log = LoggerFactory . getLogger ( EmailNotificationsConsumer . class ) ;
2014-07-01 17:15:20 +02:00
private static final String SERVICE_ENDPOINT_CATEGORY = " SMTPServer " ;
private static final String SERVICE_ENDPOINT_NAME = " SMTP-ISTI " ;
2014-05-19 19:07:18 +02:00
2014-07-01 17:15:20 +02:00
private String smtpUsername ;
private String smtpPasswd ;
private String mailServiceHost = " localhost " ;
private String mailServicePort = " 25 " ;
2014-06-13 18:37:30 +02:00
2017-01-16 18:10:09 +01:00
public EmailNotificationsConsumer ( String context ) {
2014-05-19 19:07:18 +02:00
super ( ) ;
2014-07-01 17:15:20 +02:00
_log . info ( " EmailNotificationsConsumer thread started at " + new Date ( ) + " trying to fetch SMTP configuration from infrastructure ... " ) ;
//query
try {
2017-01-16 18:10:09 +01:00
String currScope = ScopeProvider . instance . get ( ) ;
ScopeBeanExt sbe = new ScopeBeanExt ( context ) ;
String infraContext = sbe . getInfrastructureScope ( ) ;
List < ServiceEndpoint > resources = getConfigurationFromIS ( infraContext ) ;
2014-07-01 17:15:20 +02:00
if ( resources . size ( ) > 1 ) {
_log . error ( " Too many Service Endpoints having name " + SERVICE_ENDPOINT_NAME + " in this scope having Category " + SERVICE_ENDPOINT_CATEGORY ) ;
}
else if ( resources . size ( ) = = 0 ) {
_log . warn ( " There is no Service Endpoint having name " + SERVICE_ENDPOINT_NAME + " and Category " + SERVICE_ENDPOINT_CATEGORY + " in this scope. Using localhost:25 " ) ;
}
else {
for ( ServiceEndpoint res : resources ) {
AccessPoint found = res . profile ( ) . accessPoints ( ) . iterator ( ) . next ( ) ;
mailServiceHost = found . address ( ) . split ( " : " ) [ 0 ] . trim ( ) ;
mailServicePort = found . address ( ) . split ( " : " ) [ 1 ] . trim ( ) ;
smtpUsername = found . username ( ) ;
2017-01-16 18:10:09 +01:00
ScopeProvider . instance . set ( infraContext ) ;
2014-07-01 17:15:20 +02:00
smtpPasswd = StringEncrypter . getEncrypter ( ) . decrypt ( found . password ( ) ) ;
ScopeProvider . instance . set ( currScope ) ;
_log . info ( " Found SMTP Configuration: " + mailServiceHost + " : " + mailServicePort + " usr= " + smtpUsername + " pwd=******* " ) ;
}
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2014-05-19 19:07:18 +02:00
}
2014-07-01 17:15:20 +02:00
/ * *
*
2017-01-16 18:10:09 +01:00
* @param context
2014-07-01 17:15:20 +02:00
* @return the
* @throws Exception
* /
2017-01-16 18:10:09 +01:00
private List < ServiceEndpoint > getConfigurationFromIS ( String infraContext ) throws Exception {
;
String scope = infraContext ;
2014-07-01 17:15:20 +02:00
String currScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope ) ;
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( " $resource/Profile/Category/text() eq ' " + SERVICE_ENDPOINT_CATEGORY + " ' " ) ;
query . addCondition ( " $resource/Profile/Platform/Name/text() eq ' " + SERVICE_ENDPOINT_NAME + " ' " ) ;
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > toReturn = client . submit ( query ) ;
ScopeProvider . instance . set ( currScope ) ;
return toReturn ;
}
2014-05-19 19:07:18 +02:00
@Override
public void run ( ) {
Properties props = System . getProperties ( ) ;
2014-07-01 17:15:20 +02:00
Session session = null ;
2014-05-19 19:07:18 +02:00
props . put ( " mail.smtp.host " , mailServiceHost ) ;
props . put ( " mail.smtp.port " , mailServicePort ) ;
2014-07-01 17:15:20 +02:00
//if there is a service endpoint defined in the infrastructure for the SMTP Server authenticate against it
if ( smtpUsername ! = null ) {
props . put ( " mail.smtp.auth " , " true " ) ;
props . put ( " mail.smtp.starttls.enable " , " false " ) ;
session = Session . getInstance ( props ,
new javax . mail . Authenticator ( ) {
protected PasswordAuthentication getPasswordAuthentication ( ) {
return new PasswordAuthentication ( smtpUsername , smtpPasswd ) ;
}
} ) ;
}
else { //use localhost (probaly postfix instance)
session = Session . getDefaultInstance ( props ) ;
}
2014-05-19 19:07:18 +02:00
session . setDebug ( true ) ;
2014-07-01 17:15:20 +02:00
2014-05-19 19:07:18 +02:00
for ( ; ; ) {
try {
Thread . sleep ( 1000 * EmailPlugin . SECONDS2WAIT ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
2014-05-22 16:22:49 +02:00
//_log.debug("Checking Emails Buffer ... ");
2014-05-19 19:07:18 +02:00
if ( EmailPlugin . BUFFER_EMAILS ! = null & & EmailPlugin . BUFFER_EMAILS . size ( ) > 0 ) {
2014-05-22 16:22:49 +02:00
_log . debug ( " Emails Buffer not empty, sending emails " ) ;
2014-05-19 19:07:18 +02:00
Transport t = null ;
try {
2016-04-13 18:10:22 +02:00
t = session . getTransport ( " smtp " ) ;
2014-05-19 19:07:18 +02:00
t . connect ( ) ;
//sync method to ensure the producer do not put new emails in the meantime
synchronized ( EmailPlugin . BUFFER_EMAILS ) {
for ( NotificationMail mail : EmailPlugin . BUFFER_EMAILS ) {
2016-05-16 19:18:23 +02:00
Message m = mail . getMessageNotification ( session ) ;
2014-06-13 18:37:30 +02:00
if ( m ! = null ) {
m . saveChanges ( ) ;
Address [ ] addresses = m . getAllRecipients ( ) ;
2014-07-01 17:15:20 +02:00
try {
t . sendMessage ( m , addresses ) ;
}
catch ( com . sun . mail . smtp . SMTPSendFailedException ex ) {
_log . error ( " Error while trying to send emails, emptying the buffer... " ) ;
EmailPlugin . BUFFER_EMAILS = new ArrayList < NotificationMail > ( ) ;
ex . printStackTrace ( ) ;
}
_log . debug ( " Message sent to " + mail . getNotification2Send ( ) . getUserid ( ) ) ;
2014-06-13 18:37:30 +02:00
}
else {
_log . warn ( " Message not sent to " + mail . getNotification2Send ( ) . getUserid ( ) ) ;
}
2014-05-19 19:07:18 +02:00
}
//close session and empty the buffer
_log . info ( " Emails sent emptying the buffer " ) ;
EmailPlugin . BUFFER_EMAILS = new ArrayList < NotificationMail > ( ) ;
t . close ( ) ;
}
}
catch ( Exception e ) {
2014-07-01 17:15:20 +02:00
_log . error ( " Exception while trying to send emails, emptying the buffer... " ) ;
EmailPlugin . BUFFER_EMAILS = new ArrayList < NotificationMail > ( ) ;
2014-05-19 19:07:18 +02:00
e . printStackTrace ( ) ;
try {
t . close ( ) ;
} catch ( MessagingException e1 ) {
e1 . printStackTrace ( ) ;
}
}
}
}
}
}