From c5e12d543902e5a08245c98ce7ec400c9d5b3987 Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Thu, 21 Sep 2017 16:00:34 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngineSmartExecutor@154402 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../StandardLocalInfraAlgorithm.java | 82 +++++++++++++------ 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/gcube/dataanalysis/ecoengine/interfaces/StandardLocalInfraAlgorithm.java b/src/main/java/org/gcube/dataanalysis/ecoengine/interfaces/StandardLocalInfraAlgorithm.java index f0f8c40..39fa585 100644 --- a/src/main/java/org/gcube/dataanalysis/ecoengine/interfaces/StandardLocalInfraAlgorithm.java +++ b/src/main/java/org/gcube/dataanalysis/ecoengine/interfaces/StandardLocalInfraAlgorithm.java @@ -1,9 +1,17 @@ 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 org.gcube.dataanalysis.executor.util.InfraRetrieval; +import java.util.List; + +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.LoggerFactory; @@ -11,35 +19,61 @@ public abstract class StandardLocalInfraAlgorithm extends StandardLocalExternalA 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 { LOGGER.debug("Emailing System->Starting request of email in scope "+config.getGcubeScope()); - String serviceAddress = InfraRetrieval.findEmailingSystemAddress(config.getGcubeScope()); - - if (!serviceAddress.endsWith("/")) - 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; + if (!sendMessage(config.getGcubeToken(), config.getGcubeUserName(), subject, body)){ + LOGGER.error("error sending message to {}",config.getGcubeUserName()); + throw new Exception("error sending message to "+config.getGcubeUserName()); } } + 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 client = client(); + + List 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"; + } }