git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngineSmartExecutor@154402 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
bd6011946e
commit
c5e12d5439
|
@ -1,9 +1,17 @@
|
||||||
package org.gcube.dataanalysis.ecoengine.interfaces;
|
package org.gcube.dataanalysis.ecoengine.interfaces;
|
||||||
|
|
||||||
import java.net.URLEncoder;
|
import static org.gcube.resources.discovery.icclient.ICFactory.client;
|
||||||
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||||
|
|
||||||
import org.gcube.contentmanagement.graphtools.utils.HttpRequest;
|
import java.util.List;
|
||||||
import org.gcube.dataanalysis.executor.util.InfraRetrieval;
|
|
||||||
|
import org.apache.commons.httpclient.HttpClient;
|
||||||
|
import org.apache.commons.httpclient.methods.PostMethod;
|
||||||
|
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
||||||
|
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||||
|
import org.gcube.resources.discovery.client.queries.impl.XQuery;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -11,35 +19,61 @@ public abstract class StandardLocalInfraAlgorithm extends StandardLocalExternalA
|
||||||
|
|
||||||
private Logger LOGGER = LoggerFactory.getLogger(StandardLocalInfraAlgorithm.class);
|
private Logger LOGGER = LoggerFactory.getLogger(StandardLocalInfraAlgorithm.class);
|
||||||
|
|
||||||
|
private static final String SEND_MESSAGE_METHOD ="/messages/write-message";
|
||||||
|
|
||||||
public void sendNotification(String subject, String body) throws Exception {
|
public void sendNotification(String subject, String body) throws Exception {
|
||||||
|
|
||||||
LOGGER.debug("Emailing System->Starting request of email in scope "+config.getGcubeScope());
|
LOGGER.debug("Emailing System->Starting request of email in scope "+config.getGcubeScope());
|
||||||
|
|
||||||
String serviceAddress = InfraRetrieval.findEmailingSystemAddress(config.getGcubeScope());
|
if (!sendMessage(config.getGcubeToken(), config.getGcubeUserName(), subject, body)){
|
||||||
|
LOGGER.error("error sending message to {}",config.getGcubeUserName());
|
||||||
if (!serviceAddress.endsWith("/"))
|
throw new Exception("error sending message to "+config.getGcubeUserName());
|
||||||
serviceAddress = serviceAddress+"/";
|
|
||||||
|
|
||||||
String requestForMessage = serviceAddress + "2/messages/write-message" + "?gcube-token=" + config.getGcubeToken();
|
|
||||||
requestForMessage = requestForMessage.replace("http://", "https://").replace(":80", ""); // remove the port (or set it to 443) otherwise you get an SSL error
|
|
||||||
|
|
||||||
LOGGER.debug("Emailing System->Request url is going to be " + requestForMessage);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
LOGGER.debug("Emailing System->Emailing response OK ");
|
|
||||||
|
|
||||||
if (response==null){
|
|
||||||
Exception e = new Exception("Error in email sending response");
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean sendMessage(String token, String email, String subject, String body) throws Exception {
|
||||||
|
|
||||||
|
String socialServiceEnpoint = retrieveSocialServiceEnpoint();
|
||||||
|
|
||||||
|
PostMethod putMessage = new PostMethod(socialServiceEnpoint+SEND_MESSAGE_METHOD+"?gcube-token="+token);
|
||||||
|
String jsonRequest = String.format("{\"subject\":\"%s\", \"body\":\"%s\", \"recipients\":[\"%s\"]}",subject,body, email);
|
||||||
|
|
||||||
|
putMessage.setRequestEntity(new StringRequestEntity(jsonRequest, "application/json" , "UTF-8"));
|
||||||
|
|
||||||
|
LOGGER.debug("json request is {}", jsonRequest);
|
||||||
|
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
|
||||||
|
int returnedStatus = -1;
|
||||||
|
try {
|
||||||
|
returnedStatus = httpClient.executeMethod(putMessage);
|
||||||
|
LOGGER.info("response from social networking service is {}",returnedStatus);
|
||||||
|
return returnedStatus>=200 && returnedStatus<=205;
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("error trying to send invitation",e);
|
||||||
|
throw new Exception("error trying to send invitation",e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String retrieveSocialServiceEnpoint() throws Exception {
|
||||||
|
XQuery query = queryFor(GCoreEndpoint.class);
|
||||||
|
query.addCondition("$resource/Profile/ServiceClass/text() eq 'Portal'");
|
||||||
|
query.addCondition("$resource/Profile/ServiceName/text() eq 'SocialNetworking'");
|
||||||
|
query.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint");
|
||||||
|
query.addCondition("$entry/@EntryName/string() eq 'jersey-servlet'");
|
||||||
|
query.setResult("$entry/text()");
|
||||||
|
DiscoveryClient<String> client = client();
|
||||||
|
|
||||||
|
List<String> socialServiceEnpoints = client.submit(query);
|
||||||
|
|
||||||
|
if (socialServiceEnpoints.size()==0) throw new Exception("Social servioce enpooint not found in the current scope "+ScopeProvider.instance.get());
|
||||||
|
|
||||||
|
String socialServiceEnpoint = socialServiceEnpoints.get(0);
|
||||||
|
|
||||||
|
return socialServiceEnpoint+"/2";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue