fixed SendNotification class

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-data-indexer-se-plugin@144774 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-03-09 08:57:00 +00:00
parent 95be55661c
commit 26020247a9
1 changed files with 49 additions and 44 deletions

View File

@ -21,6 +21,7 @@ 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.api.SimpleQuery;
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
import org.gcube.vremanagement.executor.plugin.PluginStateNotification;
import org.json.simple.JSONObject;
@ -35,7 +36,7 @@ public class SendNotification extends PluginStateNotification {
private Map<String, String> pluginInputs;
private static Logger logger = LoggerFactory.getLogger(SendNotification.class);
private static final String NOTIFY_METHOD = "/2/notifications/notify-job-status";
private static final String NOTIFY_METHOD = "2/notifications/notify-job-status";
private static final String resource = "jersey-servlet";
private static final String serviceName = "SocialNetworking";
private static final String serviceClass = "Portal";
@ -44,7 +45,7 @@ public class SendNotification extends PluginStateNotification {
private final static String RECIPIENT_KEY = "recipient";
// service name
private final static String SERVICE_NAME = "Smart Executor";
private final static String SERVICE_NAME = "Smart-Executor";
public SendNotification(Map<String, String> inputs) {
super(inputs);
@ -58,6 +59,7 @@ public class SendNotification extends PluginStateNotification {
switch(pluginStateEvolution.getPluginState()){
case DONE:
case STOPPED:
case FAILED:
case DISCARDED:
@ -68,55 +70,59 @@ 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();
JSONObject obj = new JSONObject();
obj.put("job_id", pluginStateEvolution.getUuid());
obj.put("recipient", recipient);
obj.put("job_name", pluginStateEvolution.getPluginDeclaration().getName());
obj.put("service_name", SERVICE_NAME);
obj.put("status", "FAILED");
obj.put("status_message", "original status reported by " + SERVICE_NAME + " was " + pluginStateEvolution.getPluginState()
+ ". Exception is " + exception != null ? exception.getMessage() : null);
basePath = basePath.trim();
logger.debug("Request json is going to be " + obj.toJSONString());
HttpResponse response = performRequest(httpClient, basePath, obj.toJSONString());
logger.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()){
JSONObject obj = new JSONObject();
obj.put("job_id", pluginStateEvolution.getUuid().toString());
obj.put("recipient", recipient);
obj.put("job_name", pluginStateEvolution.getPluginDeclaration().getName());
obj.put("service_name", SERVICE_NAME);
if(pluginStateEvolution.getPluginState().equals(PluginState.DONE))
obj.put("status", "SUCCEEDED");
else{
obj.put("status", "FAILED");
obj.put("status_message", "original status reported by " + SERVICE_NAME + " was " + pluginStateEvolution.getPluginState()
+ ". Exception is " + exception != null ? exception.getMessage() : null);
}
logger.debug("Request json is going to be " + obj.toJSONString());
int status = response.getStatusLine().getStatusCode();
if(status == HttpURLConnection.HTTP_OK){
logger.info("Notifications sent");
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){
logger.info("Notification sent");
}
else if(status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER){
// redirect -> fetch new location
Header[] locations = response.getHeaders("Location");
Header lastLocation = locations[locations.length - 1];
String realLocation = lastLocation.getValue();
logger.info("New location is " + realLocation);
response = performRequest(httpClient, realLocation, obj.toJSONString());
logger.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
}else
logger.warn(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
}catch (Exception e) {
logger.warn("Something failed when trying to notify the user", e);
}
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");
Header lastLocation = locations[locations.length - 1];
String realLocation = lastLocation.getValue();
logger.debug("New location is " + realLocation);
response = performRequest(httpClient, realLocation, obj.toJSONString());
logger.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
}else
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
@ -127,13 +133,12 @@ public class SendNotification extends PluginStateNotification {
* @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);
}
/**