SendNotification now manages redirect (due to nodes with nginx that switches between http and https)

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-data-indexer-se-plugin@142427 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-02-09 13:42:31 +00:00
parent 1ee5a84674
commit 6bd62feb81
2 changed files with 38 additions and 18 deletions

View File

@ -25,7 +25,7 @@
<serviceClass>social-networking</serviceClass>
<elasticSearchVersion>2.2.0</elasticSearchVersion>
<guavaVersion>18.0</guavaVersion>
<apache.http.version>4.1.2</apache.http.version>
<apache.http.version>4.3</apache.http.version>
<astyanaxVersion>2.0.2</astyanaxVersion>
</properties>
@ -124,7 +124,6 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.http.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -3,13 +3,17 @@ 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.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.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
@ -62,9 +66,10 @@ 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.contains("http:") ? basePath.replace("http", "https") : basePath;
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
CloseableHttpClient httpClient = clientBuilder.build();
HttpPost request = new HttpPost(basePath + NOTIFY_METHOD);
DefaultHttpClient httpClient = new DefaultHttpClient();
JSONObject obj = new JSONObject();
obj.put("job_id", pluginStateEvolution.getUuid());
obj.put("recipient", recipient);
@ -75,20 +80,36 @@ public class SendNotification extends PluginStateNotification {
+ ". Exception is " + exception != null ? exception.getMessage() : null);
logger.debug("Request json is going to be " + obj.toJSONString());
try{
request.addHeader("gcube-token", SecurityTokenProvider.instance.get());
StringEntity params = new StringEntity(obj.toJSONString());
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
logger.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
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);
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
|| 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);
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());
}catch (Exception ex) {
logger.error("Error while sending notification ", ex);
}finally{
if(httpClient != null)
httpClient.getConnectionManager().shutdown();
}
}
break;
default: logger.info("No notification is going to be sent, because the status of the plugin execution is " + pluginStateEvolution.getPluginState().name());