send notification modified

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-data-indexer-se-plugin@144394 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-02-28 11:26:25 +00:00
parent 6bd62feb81
commit 95be55661c
1 changed files with 38 additions and 18 deletions

View File

@ -3,12 +3,14 @@ package org.gcube.socialnetworking.socialdataindexer.utils;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
@ -66,10 +68,13 @@ public class SendNotification extends PluginStateNotification {
logger.info("Recipient of the notification is " + recipient + ". Base path found for the notification service is " + basePath);
if(basePath != null && recipient != null){
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
basePath += NOTIFY_METHOD + "?gcube-token=" + SecurityTokenProvider.instance.get();
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
CloseableHttpClient httpClient = clientBuilder.build();
HttpPost request = new HttpPost(basePath + NOTIFY_METHOD);
JSONObject obj = new JSONObject();
obj.put("job_id", pluginStateEvolution.getUuid());
obj.put("recipient", recipient);
@ -80,41 +85,56 @@ public class SendNotification extends PluginStateNotification {
+ ". Exception is " + exception != null ? exception.getMessage() : null);
logger.debug("Request json is going to be " + obj.toJSONString());
request.addHeader("gcube-token", SecurityTokenProvider.instance.get());
request.addHeader("Content-type", ContentType.APPLICATION_JSON.toString());
StringEntity params = new StringEntity(obj.toJSONString(), ContentType.APPLICATION_JSON);
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
HttpResponse response = performRequest(httpClient, basePath, obj.toJSONString());
logger.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
int status = response.getStatusLine().getStatusCode();
if(status != HttpURLConnection.HTTP_OK && (status == HttpURLConnection.HTTP_MOVED_TEMP
if(status == HttpURLConnection.HTTP_OK){
logger.info("Notifications sent");
}
else if(status != HttpURLConnection.HTTP_OK && (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)){
// redirect -> fetch new location
Header[] locations = response.getHeaders("Location");
logger.debug("Locations returned are " + locations);
Header lastLocation = locations[locations.length - 1];
String realLocation = lastLocation.getValue();
logger.debug("New location is " + realLocation);
request = new HttpPost(realLocation);
request.addHeader("gcube-token", SecurityTokenProvider.instance.get());
request.addHeader("Content-type", ContentType.APPLICATION_JSON.toString());
params = new StringEntity(obj.toJSONString(), ContentType.APPLICATION_JSON);
request.setEntity(params);
response = httpClient.execute(request);
response = performRequest(httpClient, realLocation, obj.toJSONString());
logger.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
}else
logger.debug(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
logger.warn(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
}
break;
default: logger.info("No notification is going to be sent, because the status of the plugin execution is " + pluginStateEvolution.getPluginState().name());
}
}
/**
* Perform the post/json request
* @param httpClient
* @param path
* @param params
* @return
* @throws ClientProtocolException
* @throws IOException
*/
private static HttpResponse performRequest(CloseableHttpClient httpClient, String path, String params) throws ClientProtocolException, IOException{
HttpPost request = new HttpPost(path);
request.addHeader("Content-type", ContentType.APPLICATION_JSON.toString());
StringEntity paramsEntity = new StringEntity(params, ContentType.APPLICATION_JSON);
request.setEntity(paramsEntity);
return httpClient.execute(request);
}
/**
* Discover the social networking service base path.
@ -140,7 +160,7 @@ public class SendNotification extends PluginStateNotification {
if(basePath==null)
throw new Exception("Endpoint:"+resource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context);
logger.info("found entyname "+basePath+" for ckanResource: "+resource);
logger.info("found entryname "+basePath+" for ckanResource: "+resource);
}catch(Exception e){
logger.error("Unable to retrieve such service endpoint information!", e);