2018-05-10 19:21:15 +02:00
|
|
|
package org.gcube.data.analysis.nlphub.nlp;
|
2018-03-08 14:28:45 +01:00
|
|
|
|
2018-03-09 14:40:21 +01:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2018-03-12 17:46:20 +01:00
|
|
|
import java.net.URLEncoder;
|
2018-03-08 14:28:45 +01:00
|
|
|
|
2018-03-09 14:40:21 +01:00
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2018-03-12 17:46:20 +01:00
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
2018-03-09 14:40:21 +01:00
|
|
|
|
2019-03-11 20:15:04 +01:00
|
|
|
import org.gcube.data.analysis.nlphub.is.DMDiscover;
|
2018-05-10 19:21:15 +02:00
|
|
|
import org.gcube.data.analysis.nlphub.legacy.JsonManager;
|
|
|
|
import org.gcube.data.analysis.nlphub.legacy.NlpHubException;
|
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-12 17:46:20 +01:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
2018-05-11 19:03:43 +02:00
|
|
|
import org.w3c.dom.NodeList;
|
2018-03-12 17:46:20 +01:00
|
|
|
import org.xml.sax.InputSource;
|
2018-03-08 14:28:45 +01:00
|
|
|
|
2018-03-27 17:01:18 +02:00
|
|
|
public class NLpLanguageRecognizer {
|
2018-05-11 19:03:43 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(NLpLanguageRecognizer.class);
|
|
|
|
|
|
|
|
// private HttpServletResponse response;
|
|
|
|
// private String sentence, publicLink;
|
2018-03-08 14:28:45 +01:00
|
|
|
public final static String RECOGNIZER_ID = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.LANGUAGE_RECOGNIZER";
|
2018-05-11 19:03:43 +02:00
|
|
|
// private String dataMiner = null;
|
2018-03-12 17:46:20 +01:00
|
|
|
|
2018-03-08 14:28:45 +01:00
|
|
|
public NLpLanguageRecognizer(String service, String token, String sentence) {
|
2018-05-11 19:03:43 +02:00
|
|
|
logger.debug(
|
|
|
|
"NLpLanguageRecognizer: [service=" + service + ", token=" + token + ", sentence=" + sentence + "]");
|
|
|
|
// this.sentence = sentence;
|
|
|
|
// response = null;
|
2018-03-08 14:28:45 +01:00
|
|
|
}
|
2018-03-12 17:46:20 +01:00
|
|
|
|
2018-03-22 18:01:48 +01:00
|
|
|
public NLpLanguageRecognizer(String dataMiner, String service, String token, String sentence, String publicLink,
|
2018-03-12 17:46:20 +01:00
|
|
|
HttpServletResponse response) {
|
2018-05-11 19:03:43 +02:00
|
|
|
logger.debug("NLpLanguageRecognizer: [dataMiner=" + dataMiner + ",service=" + service + ", token=" + token
|
|
|
|
+ ", sentence=" + sentence + ",publicLink" + publicLink + "]");
|
|
|
|
// this.sentence = sentence;
|
|
|
|
// this.response = response;
|
|
|
|
// this.publicLink = publicLink;
|
|
|
|
// this.dataMiner = dataMiner;
|
2018-03-09 14:40:21 +01:00
|
|
|
}
|
2018-03-12 17:46:20 +01:00
|
|
|
|
2018-05-11 19:03:43 +02:00
|
|
|
public static void run(String dataMiner, String sentence, String token, String publicLink,
|
|
|
|
HttpServletResponse response) throws NlpHubException {
|
2018-03-08 14:28:45 +01:00
|
|
|
try {
|
2019-03-11 19:23:29 +01:00
|
|
|
try {
|
2019-03-11 20:15:04 +01:00
|
|
|
DMDiscover discoverDataMinerService=new DMDiscover();
|
2019-03-11 19:23:29 +01:00
|
|
|
dataMiner=discoverDataMinerService.retrieveServiceUrl(token);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error retrieving DataMiner service:" + e.getMessage(),e);
|
|
|
|
throw new Exception("Error retrieving DataMiner service:" + e.getMessage());
|
|
|
|
}
|
|
|
|
|
2018-07-02 11:36:22 +02:00
|
|
|
//else
|
|
|
|
// dataMiner = "http://" + dataMiner;
|
|
|
|
String urlService = dataMiner + "?request=Execute&service=WPS&Version=1.0.0";
|
2018-03-12 17:46:20 +01:00
|
|
|
urlService += "&gcube-token=" + token;
|
|
|
|
urlService += "&lang=en-US";
|
|
|
|
urlService += "&Identifier=" + RECOGNIZER_ID;
|
|
|
|
urlService += "&DataInputs=sentence=" + URLEncoder.encode(sentence, "UTF-8");
|
|
|
|
URL url = new URL(urlService);
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
2018-05-11 19:03:43 +02:00
|
|
|
// connection.setRequestProperty(Constants.TOKEN_PARAMETER,
|
|
|
|
// super.getToken());
|
2018-03-12 17:46:20 +01:00
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
|
|
|
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r));
|
|
|
|
doc.getDocumentElement().normalize();
|
|
|
|
NodeList nListData = doc.getElementsByTagName("d4science:Data");
|
|
|
|
NodeList nListDesc = doc.getElementsByTagName("d4science:Description");
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-12 17:46:20 +01:00
|
|
|
int len = nListData.getLength();
|
2018-05-11 19:03:43 +02:00
|
|
|
for (int i = 0; i < len; i++) {
|
2018-03-12 17:46:20 +01:00
|
|
|
Node data = nListData.item(i);
|
|
|
|
Node description = nListDesc.item(i);
|
|
|
|
String link = data.getTextContent();
|
|
|
|
String type = description.getTextContent();
|
2018-05-11 19:03:43 +02:00
|
|
|
if (type.equals("outfile")) {
|
|
|
|
// System.out.println(link);
|
2018-03-12 17:46:20 +01:00
|
|
|
String content = readFileContent(link, token);
|
|
|
|
if (response != null) {
|
2018-05-11 19:03:43 +02:00
|
|
|
response.getWriter().println(new JsonManager().getSuccessJsonResponse(content, publicLink));
|
|
|
|
} else {
|
|
|
|
logger.debug(new JsonManager().getSuccessJsonResponse(content, publicLink));
|
2018-03-12 17:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-08 14:28:45 +01:00
|
|
|
} catch (Exception e) {
|
2018-05-11 19:03:43 +02:00
|
|
|
logger.error(e.getLocalizedMessage(), e);
|
2018-03-08 14:28:45 +01:00
|
|
|
throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-08 14:28:45 +01:00
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-12 17:46:20 +01:00
|
|
|
private static String readFileContent(String link, String token) throws Exception {
|
|
|
|
URL url = new URL(link);
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
|
|
|
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
StringBuffer response = new StringBuffer();
|
|
|
|
String inputLine;
|
|
|
|
while ((inputLine = r.readLine()) != null) {
|
|
|
|
response.append(inputLine);
|
|
|
|
}
|
|
|
|
connection.disconnect();
|
|
|
|
String out = response.toString();
|
|
|
|
return out;
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
/*
|
|
|
|
* private String readFileContent(String link) throws Exception { URL url =
|
|
|
|
* new URL(link); HttpURLConnection connection = (HttpURLConnection)
|
|
|
|
* url.openConnection();
|
|
|
|
* connection.setRequestProperty(Constants.TOKEN_PARAMETER,
|
|
|
|
* super.getToken()); connection.setDoInput(true);
|
|
|
|
* connection.setDoOutput(true); connection.setUseCaches(false);
|
|
|
|
* connection.setRequestMethod("GET");
|
|
|
|
*
|
|
|
|
* BufferedReader r = new BufferedReader(new
|
|
|
|
* InputStreamReader(connection.getInputStream())); StringBuffer response =
|
|
|
|
* new StringBuffer(); String inputLine; while ((inputLine = r.readLine())
|
|
|
|
* != null) { response.append(inputLine); } connection.disconnect(); String
|
|
|
|
* out = response.toString(); return out; }
|
|
|
|
*/
|
2018-03-09 14:40:21 +01:00
|
|
|
|
2018-03-08 14:28:45 +01:00
|
|
|
}
|