package eu.dnetlib.ariadneplus.workflows.nodes; import com.google.common.collect.Lists; import com.google.gson.Gson; import eu.dnetlib.enabling.resultset.ResultSetInfo; import eu.dnetlib.enabling.resultset.client.ResultSetClient; import eu.dnetlib.msro.workflows.graph.Arc; import eu.dnetlib.msro.workflows.nodes.AsyncJobNode; import eu.dnetlib.msro.workflows.procs.Env; import eu.dnetlib.msro.workflows.procs.Token; import eu.dnetlib.msro.workflows.util.ResultsetProgressProvider; import eu.dnetlib.msro.workflows.util.WorkflowsConstants; import eu.dnetlib.rmi.common.ResultSet; import eu.dnetlib.rmi.manager.MSROException; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; import java.util.List; public class IndexOnESJobNode extends AsyncJobNode { private static final Log log = LogFactory.getLog(IndexOnESJobNode.class); private String eprParam; @Autowired private ResultSetClient resultSetClient; private String publisherEndpoint; private String datasourceInterface; private String datasource; //for parallel requests to the publisher endpoint // private int nThreads = 5; @Override protected String execute(final Env env) throws Exception { int statusCode = -1; String indexOnESResult = "noResult"; log.info("Publisher endpoint: " + getPublisherEndpoint()); // PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // cm.setMaxTotal(nThreads); // CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build(); CloseableHttpClient client = HttpClients.createDefault(); log.info("IndexOnES endpoint: " + getIndexOnESEndpoint()); CloseableHttpResponse responsePOST = null; try { HttpPost post = new HttpPost(getIndexOnESEndpoint()); List params = Lists.newArrayList(); String[] splits = getDatasourceInterface().split("::"); String datasource = splits[2]; String collectionId = splits[3]; params.add(new BasicNameValuePair("datasource", datasource)); params.add(new BasicNameValuePair("collectionId", collectionId)); UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, "UTF-8"); post.setEntity(ent); log.info("Calling IndexOnES endpoint with params: "+getDatasource()+" "+getDatasourceInterface()); responsePOST = client.execute(post); statusCode = responsePOST.getStatusLine().getStatusCode(); try(InputStream responseBody = responsePOST.getEntity().getContent()) { indexOnESResult = IOUtils.toString(responseBody, "UTF-8"); } catch (Exception e) {} switch (statusCode) { case 200: log.info("index on ES completed"); break; default: log.error("error indexing on ES " + responsePOST.getStatusLine().getStatusCode() + ": " + responsePOST.getStatusLine().getReasonPhrase()); break; } } catch (ConnectException ce) { log.error(ce); throw new MSROException("Unable to connect to Publisher endpoint" + getIndexOnESEndpoint()); } catch (IOException e) { log.error(e); throw new MSROException("IO Error" + getIndexOnESEndpoint()); } finally{ if(responsePOST != null) responsePOST.close(); client.close(); // cm.shutdown(); } env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "statusCode", Integer.toString(statusCode)); env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + "indexResult", indexOnESResult); if (statusCode!=200) { throw new MSROException("Error from Publisher endpoint [ status code: " + statusCode + " ]"); } return Arc.DEFAULT_ARC; } public String getPublisherEndpoint() { return publisherEndpoint; } private String getIndexOnESEndpoint() { return publisherEndpoint.concat("/indexOnES"); } public void setPublisherEndpoint(final String publisherEndpoint) { this.publisherEndpoint = publisherEndpoint; } public ResultSetClient getResultSetClient() { return resultSetClient; } public void setResultSetClient(final ResultSetClient resultSetClient) { this.resultSetClient = resultSetClient; } public String getEprParam() { return eprParam; } public void setEprParam(String eprParam) { this.eprParam = eprParam; } public String getDatasourceInterface() { return datasourceInterface; } public void setDatasourceInterface(String datasourceInterface) { this.datasourceInterface = datasourceInterface; } @Override protected void beforeStart(Token token) { token.setProgressProvider(new ResultsetProgressProvider(token.getEnv().getAttribute(getEprParam(), ResultSet.class), this.resultSetClient)); } public String getDatasource() { return datasource; } public void setDatasource(String datasource) { this.datasource = datasource; } }