2018-05-10 19:21:15 +02:00
|
|
|
package org.gcube.data.analysis.nlphub.nlp;
|
2018-03-15 15:40:50 +01:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
|
2018-05-10 19:21:15 +02:00
|
|
|
import org.gcube.data.analysis.nlphub.legacy.AsyncHttpRequest;
|
2018-05-11 19:03:43 +02:00
|
|
|
import org.gcube.data.analysis.nlphub.legacy.NerOutput;
|
2018-05-11 19:14:11 +02:00
|
|
|
import org.gcube.data.analysis.nlphub.shared.Constants;
|
2018-05-11 19:03:43 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-03-15 15:40:50 +01:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
|
|
|
|
public class NlpAsyncNerRunner extends AsyncHttpRequest {
|
2018-05-11 19:03:43 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(NerOutput.class);
|
|
|
|
public static final String WPS_EXECUTE_URL = "/wps/WebProcessingService?request=Execute&service=WPS&Version=1.0.0";
|
|
|
|
public static final String WPS_DESCRIBE_PROCESS_URL = "/wps/WebProcessingService?request=DescribeProcess&service=WPS&Version=1.0.0";
|
|
|
|
private String identifier, token, httpMethod, annotations, publicLink, dataMiner;
|
2018-03-20 14:45:49 +01:00
|
|
|
private RunnerCommander commander;
|
2018-03-15 15:40:50 +01:00
|
|
|
|
2018-05-11 19:03:43 +02:00
|
|
|
public NlpAsyncNerRunner(String dataMiner, String identifier, String token, String publicLink, String annotations,
|
|
|
|
String language, RunnerCommander commander) {
|
2018-03-15 15:40:50 +01:00
|
|
|
super();
|
|
|
|
this.identifier = identifier;
|
|
|
|
this.token = token;
|
|
|
|
this.httpMethod = "GET";
|
|
|
|
this.annotations = annotations;
|
|
|
|
this.publicLink = publicLink;
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-20 14:45:49 +01:00
|
|
|
this.commander = commander;
|
2018-05-11 19:03:43 +02:00
|
|
|
if (dataMiner == null)
|
|
|
|
this.dataMiner = Constants.DEFAULT_DATAMINER_URL;
|
|
|
|
else
|
2018-03-22 18:01:48 +01:00
|
|
|
this.dataMiner = "http://" + dataMiner;
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
ArrayList<NlpParameter> params = buildParameterString();
|
2018-05-11 19:03:43 +02:00
|
|
|
String serviceUrl = this.dataMiner + WPS_EXECUTE_URL + "&gcube-token=" + token + "&lang=en-US&Identifier="
|
|
|
|
+ identifier;
|
2018-03-15 15:40:50 +01:00
|
|
|
serviceUrl += "&" + setUrl(params);
|
|
|
|
super.setBaseUrl(serviceUrl);
|
|
|
|
super.setMethod(httpMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
public NlpAsyncNerRunner(String baseUrl, String method) {
|
|
|
|
super(baseUrl, method, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getIdentifier() {
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken() {
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHttpMethod() {
|
|
|
|
return httpMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String setUrl(ArrayList<NlpParameter> parameters) {
|
|
|
|
String url = "DataInputs=";
|
|
|
|
for (NlpParameter p : parameters) {
|
|
|
|
try {
|
|
|
|
url += p.getName() + "=" + URLEncoder.encode((String) p.getValue(), "UTF-8") + ";";
|
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.error(ex.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ArrayList<NlpParameter> buildParameterString() {
|
|
|
|
ArrayList<NlpParameter> parameters = new ArrayList<>();
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
BufferedReader r = null;
|
|
|
|
try {
|
2018-03-22 18:01:48 +01:00
|
|
|
String finalUrl = dataMiner + WPS_DESCRIBE_PROCESS_URL + "&gcube-token=" + token;
|
2018-03-15 15:40:50 +01:00
|
|
|
finalUrl += "&lang=en-US&Identifier=" + identifier;
|
|
|
|
URL url = new URL(finalUrl);
|
|
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r));
|
|
|
|
doc.getDocumentElement().normalize();
|
|
|
|
NodeList nListInput = doc.getElementsByTagName("Input");
|
|
|
|
for (int i = 0; i < nListInput.getLength(); i++) {
|
|
|
|
Node nodeInput = nListInput.item(i);
|
|
|
|
NlpParameter nlpParam = new NlpParameter();
|
|
|
|
NodeList inputChildren = nodeInput.getChildNodes();
|
|
|
|
|
|
|
|
// try to find the name and the type of the input parameter
|
|
|
|
for (int j = 0; j < inputChildren.getLength(); j++) {
|
|
|
|
Node node = inputChildren.item(j);
|
|
|
|
// for the moment we limit the type at 'file' and
|
|
|
|
// 'annotations'
|
|
|
|
if (node.getNodeName().equals("ows:Identifier")) {
|
|
|
|
nlpParam.setName(node.getTextContent());
|
|
|
|
} else if (node.getNodeName().equals("ows:Title")) {
|
|
|
|
nlpParam.setDescription(node.getTextContent());
|
|
|
|
} else if (node.getNodeName().equals("ows:Abstract")) {
|
|
|
|
String text = node.getTextContent().toLowerCase();
|
|
|
|
if ((text.indexOf("file") >= 0) || (text.indexOf("text") >= 0)) {
|
|
|
|
nlpParam.setObjectType(NlpParameter.INPUT_FILE);
|
|
|
|
nlpParam.setValue(publicLink);
|
|
|
|
} else if ((text.indexOf("annotation") >= 0) || (text.indexOf("list") >= 0)) {
|
|
|
|
nlpParam.setObjectType(NlpParameter.INPUT_ANNOTATIONS);
|
|
|
|
nlpParam.setValue(annotations.replaceAll(",", "|"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parameters.add(nlpParam);
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:03:43 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getLocalizedMessage(), e);
|
2018-03-15 15:40:50 +01:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (r != null)
|
|
|
|
r.close();
|
|
|
|
if (connection != null)
|
|
|
|
connection.disconnect();
|
|
|
|
} catch (Exception e) {
|
2018-05-11 19:03:43 +02:00
|
|
|
logger.error(e.getLocalizedMessage(), e);
|
2018-03-15 15:40:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getElapsedTime() {
|
|
|
|
return elapsedTime;
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
@Override
|
|
|
|
public void asyncHttpRequestCallback() {
|
|
|
|
elapsedTime = System.currentTimeMillis() - elapsedTime;
|
|
|
|
logger.info("ID: " + identifier.substring(identifier.lastIndexOf(".") + 1) + " elapsed time: " + elapsedTime);
|
|
|
|
String result = super.getResult();
|
|
|
|
String theLink = "";
|
|
|
|
try {
|
2018-03-20 14:45:49 +01:00
|
|
|
BufferedReader r = new BufferedReader(
|
2018-05-11 19:03:43 +02:00
|
|
|
new InputStreamReader(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8))));
|
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r));
|
|
|
|
doc.getDocumentElement().normalize();
|
|
|
|
NodeList nListResult = doc.getElementsByTagName("ogr:Result");
|
2018-05-11 19:03:43 +02:00
|
|
|
|
|
|
|
for (int i = 0, found = 0; (i < nListResult.getLength()) && (found == 0); i++) {
|
2018-03-15 15:40:50 +01:00
|
|
|
Node nodeResult = nListResult.item(i);
|
|
|
|
NodeList list = nodeResult.getChildNodes();
|
|
|
|
String res = "";
|
2018-05-11 19:03:43 +02:00
|
|
|
for (int j = 0; j < list.getLength(); j++) {
|
2018-03-15 15:40:50 +01:00
|
|
|
Node node = list.item(j);
|
2018-05-11 19:03:43 +02:00
|
|
|
if (node.getNodeName().equals("d4science:Data")) {
|
2018-03-15 15:40:50 +01:00
|
|
|
res = node.getTextContent();
|
2018-05-11 19:03:43 +02:00
|
|
|
} else if (node.getNodeName().equals("d4science:MimeType")) {
|
|
|
|
if (node.getTextContent().equals("application/d4science")) {
|
2018-03-15 15:40:50 +01:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
if (found > 0) {
|
2018-03-15 15:40:50 +01:00
|
|
|
theLink = res;
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 14:45:49 +01:00
|
|
|
commander.updateResultList(identifier.substring(identifier.lastIndexOf(".") + 1) + ":::" + theLink);
|
2018-05-11 19:03:43 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
commander.updateResultList(
|
|
|
|
identifier.substring(identifier.lastIndexOf(".") + 1) + ":::" + Constants.ERROR_ID);
|
|
|
|
logger.error(e.getLocalizedMessage(), e);
|
2018-03-15 15:40:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|