2016-09-27 16:27:28 +02:00
package org.gcube.dataanalysis.ecoengine.interfaces ;
2016-09-29 12:49:16 +02:00
import java.net.URLEncoder ;
2016-09-27 16:27:28 +02:00
2016-09-29 12:49:16 +02:00
import org.gcube.contentmanagement.graphtools.utils.HttpRequest ;
import org.gcube.dataanalysis.executor.util.InfraRetrieval ;
2017-06-12 11:14:52 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2016-09-29 12:49:16 +02:00
public abstract class StandardLocalInfraAlgorithm extends StandardLocalExternalAlgorithm {
2017-06-12 11:14:52 +02:00
private Logger LOGGER = LoggerFactory . getLogger ( StandardLocalInfraAlgorithm . class ) ;
2016-09-27 16:27:28 +02:00
2016-09-29 12:49:16 +02:00
2016-09-29 12:55:29 +02:00
public void sendNotification ( String subject , String body ) throws Exception {
2016-09-29 12:49:16 +02:00
2017-06-12 11:14:52 +02:00
LOGGER . debug ( " Emailing System->Starting request of email in scope " + config . getGcubeScope ( ) ) ;
2016-09-29 12:49:16 +02:00
String serviceAddress = InfraRetrieval . findEmailingSystemAddress ( config . getGcubeScope ( ) ) ;
2016-10-26 12:09:17 +02:00
if ( ! serviceAddress . endsWith ( " / " ) )
serviceAddress = serviceAddress + " / " ;
2017-09-08 16:17:24 +02:00
String requestForMessage = serviceAddress + " 2/messages/writeMessageToUsers " + " ?gcube-token= " + config . getGcubeToken ( ) ;
2016-09-29 12:54:00 +02:00
requestForMessage = requestForMessage . replace ( " http:// " , " https:// " ) . replace ( " :80 " , " " ) ; // remove the port (or set it to 443) otherwise you get an SSL error
2016-09-29 12:49:16 +02:00
2017-06-12 11:14:52 +02:00
LOGGER . debug ( " Emailing System->Request url is going to be " + requestForMessage ) ;
2016-09-29 12:49:16 +02:00
// put the sender, the recipients, subject and body of the mail here
subject = URLEncoder . encode ( subject , " UTF-8 " ) ;
body = URLEncoder . encode ( body , " UTF-8 " ) ;
String requestParameters = " sender=dataminer&recipients= " + config . getGcubeUserName ( ) + " &subject= " + subject + " &body= " + body ;
String response = HttpRequest . sendPostRequest ( requestForMessage , requestParameters ) ;
2017-06-12 11:14:52 +02:00
LOGGER . debug ( " Emailing System->Emailing response OK " ) ;
2016-09-29 12:49:16 +02:00
if ( response = = null ) {
Exception e = new Exception ( " Error in email sending response " ) ;
throw e ;
}
2016-09-27 16:27:28 +02:00
}
2016-09-29 12:49:16 +02:00
2016-09-27 16:27:28 +02:00
}