changed http request method for indexing

This commit is contained in:
Enrico Ottonello 2020-10-15 17:09:35 +02:00
parent f5cd3cdce5
commit 0e96774895
2 changed files with 67 additions and 37 deletions

View File

@ -69,7 +69,7 @@ public class AriadnePlusPublisherController {
return getAriadnePlusPublisherHelper().feedFromURL(dataUrl, context, getTarget(DEFAULT_TARGET_ENDPOINT)); return getAriadnePlusPublisherHelper().feedFromURL(dataUrl, context, getTarget(DEFAULT_TARGET_ENDPOINT));
} }
@RequestMapping(value = "/indexOnES", method = RequestMethod.POST) @RequestMapping(value = "/indexOnES", method = RequestMethod.GET)
public String indexOnES(@RequestParam final String datasource, @RequestParam final String collectionId) throws AriadnePlusPublisherException { public String indexOnES(@RequestParam final String datasource, @RequestParam final String collectionId) throws AriadnePlusPublisherException {
return getAriadnePlusPublisherHelper().indexOnES(datasource, collectionId, getTarget(DEFAULT_TARGET_ENDPOINT)); return getAriadnePlusPublisherHelper().indexOnES(datasource, collectionId, getTarget(DEFAULT_TARGET_ENDPOINT));
} }

View File

@ -9,18 +9,25 @@ import eu.dnetlib.rmi.manager.MSROException;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.SocketConfig; import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.URI;
import java.util.List; import java.util.List;
@ -36,55 +43,78 @@ public class IndexOnESJobNode extends AsyncJobNode {
protected String execute(final Env env) throws Exception { protected String execute(final Env env) throws Exception {
int statusCode = -1; int statusCode = -1;
String indexOnESResult = "noResult"; String response = "";
log.info("Publisher endpoint: " + getPublisherEndpoint()); // String indexOnESResult = "noResult";
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).build(); // log.info("Publisher endpoint: " + getPublisherEndpoint());
CloseableHttpClient client = HttpClientBuilder.create() // SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).build();
.setDefaultSocketConfig(socketConfig).build(); // CloseableHttpClient client = HttpClientBuilder.create()
// .setDefaultSocketConfig(socketConfig).build();
log.info("IndexOnES endpoint: " + getIndexOnESEndpoint()); log.info("IndexOnES endpoint: " + getIndexOnESEndpoint());
CloseableHttpResponse responsePOST = null; // CloseableHttpResponse responsePOST = null;
HttpClient client = null;
try { try {
HttpPost post = new HttpPost(getIndexOnESEndpoint()); // HttpPost post = new HttpPost(getIndexOnESEndpoint());
List<NameValuePair> params = Lists.newArrayList(); // List<NameValuePair> params = Lists.newArrayList();
String[] splits = getDatasourceInterface().split("::"); String[] splits = getDatasourceInterface().split("::");
String datasource = splits[2]; String datasource = splits[2];
String collectionId = splits[3]; String collectionId = splits[3];
params.add(new BasicNameValuePair("datasource", datasource)); // params.add(new BasicNameValuePair("datasource", datasource));
params.add(new BasicNameValuePair("collectionId", collectionId)); // params.add(new BasicNameValuePair("collectionId", collectionId));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, "UTF-8"); // UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(ent); // post.setEntity(ent);
log.info("Calling IndexOnES endpoint with params: "+getDatasource()+" "+getDatasourceInterface()); // log.info("Calling IndexOnES endpoint with params: "+getDatasource()+" "+getDatasourceInterface());
responsePOST = client.execute(post); // responsePOST = client.execute(post);
statusCode = responsePOST.getStatusLine().getStatusCode();
try(InputStream responseBody = responsePOST.getEntity().getContent()) { URI getURI = new URIBuilder(getIndexOnESEndpoint())
indexOnESResult = IOUtils.toString(responseBody, "UTF-8"); .addParameter("datasource", datasource)
} catch (Exception e) { .addParameter("collectionId", collectionId)
log.error(e); .build();
client = HttpClients.createDefault();
HttpResponse res = client.execute(new HttpGet(getURI));
response = EntityUtils.toString(res.getEntity());
if (res.getStatusLine()!=null) {
statusCode = res.getStatusLine().getStatusCode();
} }
switch (statusCode) {
case 200: // statusCode = responsePOST.getStatusLine().getStatusCode();
log.info("index on ES completed"); // try(InputStream responseBody = responsePOST.getEntity().getContent()) {
break; // indexOnESResult = IOUtils.toString(responseBody, "UTF-8");
default: // } catch (Exception e) {
log.error("error indexing on ES " + responsePOST.getStatusLine().getStatusCode() + ": " + responsePOST.getStatusLine().getReasonPhrase()); // log.error(e);
break; // }
} // switch (statusCode) {
} catch (ConnectException ce) { // case 200:
log.error(ce); // log.info("index on ES completed");
throw new MSROException("Unable to connect to Publisher endpoint" + getIndexOnESEndpoint()); // break;
// default:
// log.error("error indexing on ES " + responsePOST.getStatusLine().getStatusCode() + ": " + responsePOST.getStatusLine().getReasonPhrase());
// break;
// }
} }
catch (IOException e) { // catch (ConnectException ce) {
log.error(e); // log.error(ce);
throw new MSROException("IO Error" + getIndexOnESEndpoint()); // throw new MSROException("Unable to connect to Publisher endpoint" + getIndexOnESEndpoint());
// }
// catch (IOException e) {
// log.error(e);
// throw new MSROException("IO Error" + getIndexOnESEndpoint());
// }
catch (Throwable t) {
log.error(t);
throw new MSROException("Indexing on Elastic Search: " + t.getMessage());
} }
finally{ finally{
if(responsePOST != null) responsePOST.close(); // if(responsePOST != null) responsePOST.close();
client.close(); // client.close();
// cm.shutdown(); // cm.shutdown();
} }
env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "statusCode", Integer.toString(statusCode)); env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "statusCode", Integer.toString(statusCode));
env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "indexResult", indexOnESResult); // env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "indexResult", indexOnESResult);
env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "response", response);
if (statusCode!=200) { if (statusCode!=200) {
throw new MSROException("Error from Publisher endpoint [ status code: " + statusCode + " ]"); throw new MSROException("Error from Publisher endpoint [ status code: " + statusCode + " ]");