ref 11719: Add management of VREs to the NLPHub interface

https://support.d4science.org/issues/11719

Fixed some issue on webapp:

Errors of shared variables on servlets
Added slf4j over the whole application and logs in nlphub.log
Problem of checking nulls on arrays in Javascript
Added the ability to retrieve the context and token from the RequestUri

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/nlphub@167438 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2018-05-11 17:03:43 +00:00
parent 186e910618
commit 6c8130fb64
26 changed files with 408 additions and 314 deletions

13
pom.xml
View File

@ -105,20 +105,13 @@
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.6.4</version> <version>1.7.5</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version> <scope>test</scope>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency> </dependency>
<!-- JUnit --> <!-- JUnit -->
<dependency> <dependency>

View File

@ -14,12 +14,12 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.legacy.Constants;
import org.gcube.data.analysis.nlphub.legacy.JsonManager; import org.gcube.data.analysis.nlphub.legacy.JsonManager;
import org.gcube.data.analysis.nlphub.nlp.NlpAsyncNerRunner; import org.gcube.data.analysis.nlphub.nlp.NlpAsyncNerRunner;
import org.gcube.data.analysis.nlphub.nlp.NlpParameter;
import org.gcube.data.analysis.nlphub.nlp.RunnerCommander; import org.gcube.data.analysis.nlphub.nlp.RunnerCommander;
import org.gcube.data.analysis.nlphub.session.SessionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
@ -30,12 +30,9 @@ import org.xml.sax.InputSource;
*/ */
@WebServlet(asyncSupported = true, name = "NLPServlet", urlPatterns = { "/nlphub-servlet" }) @WebServlet(asyncSupported = true, name = "NLPServlet", urlPatterns = { "/nlphub-servlet" })
public class NLPHub extends HttpServlet { public class NLPHub extends HttpServlet {
private Logger logger = Logger.getLogger(NLPHub.class.getSimpleName());
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final String service = "http://dataminer-prototypes.d4science.org/wps/"; private static final Logger logger = LoggerFactory.getLogger(NLPHub.class);
private String token; //= "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
private boolean devMode = true;
/** /**
* @see HttpServlet#HttpServlet() * @see HttpServlet#HttpServlet()
*/ */
@ -63,16 +60,18 @@ public class NLPHub extends HttpServlet {
} }
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("getInfo") != null) { logger.debug("NLPHub");
getAlgorithmInfo(request, response); String token = SessionUtils.getToken(request);
}
else { if (request.getParameter("getInfo") != null) {
runAlgorithms(request, response); getAlgorithmInfo(request, response, token);
} else {
runAlgorithms(request, response, token);
} }
} }
private void getAlgorithmInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { private void getAlgorithmInfo(HttpServletRequest request, HttpServletResponse response, String token)
token = Constants.getToken(request, devMode); throws ServletException, IOException {
try { try {
String dataMiner = request.getParameter("dataminer"); String dataMiner = request.getParameter("dataminer");
String algId = request.getParameter("algId"); String algId = request.getParameter("algId");
@ -82,7 +81,8 @@ public class NLPHub extends HttpServlet {
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
response.setContentType("application/json;charset=UTF-8"); response.setContentType("application/json;charset=UTF-8");
try { try {
String finalUrl = "https://" + dataMiner + NlpAsyncNerRunner.WPS_DESCRIBE_PROCESS_URL + "&gcube-token=" + token; String finalUrl = "https://" + dataMiner + NlpAsyncNerRunner.WPS_DESCRIBE_PROCESS_URL + "&gcube-token="
+ token;
finalUrl += "&lang=en-US&Identifier=" + algId; finalUrl += "&lang=en-US&Identifier=" + algId;
URL url = new URL(finalUrl); URL url = new URL(finalUrl);
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
@ -93,22 +93,21 @@ public class NLPHub extends HttpServlet {
r = new BufferedReader(new InputStreamReader(connection.getInputStream())); r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r)); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r));
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
NodeList nListInput = doc.getElementsByTagName("ows:Abstract"); NodeList nListInput = doc.getElementsByTagName("ows:Abstract");
if(nListInput.getLength() <= 0) { if (nListInput.getLength() <= 0) {
algAbsrtact = "No description."; algAbsrtact = "No description.";
writer.println(new JsonManager().getErrorJsonResponse(algAbsrtact)); writer.println(new JsonManager().getErrorJsonResponse(algAbsrtact));
} } else {
else {
Node nodeAbstract = nListInput.item(0); Node nodeAbstract = nListInput.item(0);
algAbsrtact = nodeAbstract.getTextContent(); algAbsrtact = nodeAbstract.getTextContent();
writer.println(new JsonManager().getSuccessJsonResponse("" + algAbsrtact)); writer.println(new JsonManager().getSuccessJsonResponse("" + algAbsrtact));
} }
writer.close(); writer.close();
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
writer.println(new JsonManager().getErrorJsonResponse("" + x.getLocalizedMessage())); writer.println(new JsonManager().getErrorJsonResponse("" + e.getLocalizedMessage()));
writer.close(); writer.close();
} finally { } finally {
try { try {
@ -120,31 +119,30 @@ public class NLPHub extends HttpServlet {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage());
} }
} }
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
} }
} }
private void runAlgorithms(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { private void runAlgorithms(HttpServletRequest request, HttpServletResponse response, String token)
token = Constants.getToken(request, devMode); throws ServletException, IOException {
try { try {
String dataMiner = request.getParameter("dataminer"); String dataMiner = request.getParameter("dataminer");
String[] algs = request.getParameter("algs").split(","); String[] algs = request.getParameter("algs").split(",");
for(int i=0; i<algs.length; i++) { for (int i = 0; i < algs.length; i++) {
algs[i] = algs[i].trim(); algs[i] = algs[i].trim();
} }
RunnerCommander commander = new RunnerCommander(dataMiner, algs, request.getParameter("plink"),
RunnerCommander commander = new RunnerCommander(dataMiner, algs, request.getParameter("plink"), request.getParameter("annotations"), token, request.getParameter("annotations"), token, response);
response);
commander.setSleepTime(100); commander.setSleepTime(100);
commander.setMaxWaitingTime(90*1000); commander.setMaxWaitingTime(90 * 1000);
commander.startProcess(); commander.startProcess();
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
} }
} }
} }

View File

@ -14,13 +14,15 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.legacy.Constants; import org.gcube.data.analysis.nlphub.legacy.Constants;
import org.gcube.data.analysis.nlphub.legacy.JsonManager; import org.gcube.data.analysis.nlphub.legacy.JsonManager;
import org.gcube.data.analysis.nlphub.legacy.NlpHubException; import org.gcube.data.analysis.nlphub.legacy.NlpHubException;
import org.gcube.data.analysis.nlphub.mapper.DefaultMapper; import org.gcube.data.analysis.nlphub.mapper.DefaultMapper;
import org.gcube.data.analysis.nlphub.mapper.JsonMapper; import org.gcube.data.analysis.nlphub.mapper.JsonMapper;
import org.gcube.data.analysis.nlphub.session.SessionUtils;
import org.gcube.data.analysis.nlphub.workspace.WorkspaceManager; import org.gcube.data.analysis.nlphub.workspace.WorkspaceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Servlet implementation class NlpMapper * Servlet implementation class NlpMapper
@ -28,10 +30,8 @@ import org.gcube.data.analysis.nlphub.workspace.WorkspaceManager;
@WebServlet("/nlphub-mapper-servlet") @WebServlet("/nlphub-mapper-servlet")
public class NLPMapper extends HttpServlet { public class NLPMapper extends HttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Logger logger = Logger.getLogger(NLPMapper.class.getSimpleName()); private static final Logger logger = LoggerFactory.getLogger(NLPUploader.class);
private String token; //= "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
private boolean devMode = true;
/** /**
* @see HttpServlet#HttpServlet() * @see HttpServlet#HttpServlet()
*/ */
@ -58,31 +58,37 @@ public class NLPMapper extends HttpServlet {
} }
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.debug("NLPMapper");
String token = SessionUtils.getToken(request);
WorkspaceManager ws = new WorkspaceManager();
response.setContentType("application/json"); response.setContentType("application/json");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
token = Constants.getToken(request, devMode); String documentLink = request.getParameter("plink"); // link to text
String documentLink = request.getParameter("plink"); // link to text file (workspace) // file
// (workspace)
String toBeMap = request.getParameter("tobemap"); String toBeMap = request.getParameter("tobemap");
String[] tokens; String[] tokens;
if(toBeMap.indexOf("|") > 0) if (toBeMap.indexOf("|") > 0)
tokens = toBeMap.split("\\|"); tokens = toBeMap.split("\\|");
else { else {
tokens = new String[1]; tokens = new String[1];
tokens[0] = toBeMap; tokens[0] = toBeMap;
} }
String annotations = request.getParameter("annotations"); String annotations = request.getParameter("annotations");
String language = request.getParameter("lang"); String language = request.getParameter("lang");
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
try { try {
String text = getDocument(documentLink); String text = getDocument(documentLink);
text = text.replaceAll("\n", "\\\\n");; text = text.replaceAll("\n", "\\\\n");
text = text.replaceAll("\r", "\\\\r"); text = text.replaceAll("\r", "\\\\r");
text = text.replaceAll("\t", "\\\\t"); text = text.replaceAll("\t", "\\\\t");
text = text.replaceAll("\"", "\\\\\""); text = text.replaceAll("\"", "\\\\\"");
String out = "{"; String out = "{";
out += "\"text\":\"" + text + "\","; out += "\"text\":\"" + text + "\",";
out += "\"annotations\":\"" + annotations + "\","; out += "\"annotations\":\"" + annotations + "\",";
@ -90,9 +96,9 @@ public class NLPMapper extends HttpServlet {
out += "\"result\": ["; out += "\"result\": [";
for (int i = 0; i < tokens.length; i++) { for (int i = 0; i < tokens.length; i++) {
String token = tokens[i]; String tk = tokens[i];
String[] t = token.split(":::"); String[] t = tk.split(":::");
if(t[1].equals(Constants.ERROR_ID)) { if (t[1].equals(Constants.ERROR_ID)) {
logger.error("Algorithm " + t[0] + " in error. Bypass..."); logger.error("Algorithm " + t[0] + " in error. Bypass...");
continue; continue;
} }
@ -106,24 +112,26 @@ public class NLPMapper extends HttpServlet {
} }
} }
out += "]}"; out += "]}";
String resultLink = saveResult(out); String resultLink = saveResult(out, token, ws);
String outResult = "{"; String outResult = "{";
outResult += "\"link\":\"" + resultLink + "\","; outResult += "\"link\":\"" + resultLink + "\",";
outResult += "\"output\":" + out; outResult += "\"output\":" + out;
outResult += "}"; outResult += "}";
writer.append(outResult); writer.append(outResult);
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
writer.println(new JsonManager().getErrorJsonResponse(x.getLocalizedMessage())); writer.println(new JsonManager().getErrorJsonResponse(e.getLocalizedMessage()));
} }
// response.getWriter().write(json); // response.getWriter().write(json);
} }
private Class getMapper(String algId) throws Exception { private Class<DefaultMapper> getMapper(String algId) throws Exception {
return Class.forName("org.gcube.nlphub.mapper.DefaultMapper"); return DefaultMapper.class;
// return
// Class.forName("org.gcube.data.analynlphub.mapper.DefaultMapper");
} }
private String getDocument(String plink) throws Exception { private String getDocument(String plink) throws Exception {
@ -142,20 +150,19 @@ public class NLPMapper extends HttpServlet {
} }
return content; return content;
} }
private String saveResult(String jsonResult) throws NlpHubException { private String saveResult(String jsonResult, String token, WorkspaceManager ws) throws NlpHubException {
WorkspaceManager ws = new WorkspaceManager();
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
String fileName = "result-nlp-" + now + ".json"; String fileName = "result-nlp-" + now + ".json";
ws.deleteFile(fileName, token); ws.deleteFile(fileName, token);
byte[] byteContent = jsonResult.getBytes(StandardCharsets.UTF_8); byte[] byteContent = jsonResult.getBytes(StandardCharsets.UTF_8);
if (!ws.uploadFile(byteContent, fileName, Constants.DEFAULT_DESCRIPTION, token)) { if (!ws.uploadFile(byteContent, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
throw new NlpHubException("Error writing file: " + fileName + " on workspace (token: " + token + ")", null); throw new NlpHubException("Error writing file: " + fileName + " on workspace (token: " + token + ")", null);
} }
String link = ws.getPublicLink(fileName, token); String link = ws.getPublicLink(fileName, token);
logger.info("Output json [" + fileName + "] created in " + (System.currentTimeMillis() - now) + " millisec."); logger.info("Output json [" + fileName + "] created in " + (System.currentTimeMillis() - now) + " millisec.");
return link; return link;
} }

View File

@ -14,13 +14,15 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part; import javax.servlet.http.Part;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.legacy.Constants; import org.gcube.data.analysis.nlphub.legacy.Constants;
import org.gcube.data.analysis.nlphub.legacy.JsonManager; import org.gcube.data.analysis.nlphub.legacy.JsonManager;
import org.gcube.data.analysis.nlphub.legacy.NlpHubException; import org.gcube.data.analysis.nlphub.legacy.NlpHubException;
import org.gcube.data.analysis.nlphub.nlp.NLpLanguageRecognizer; import org.gcube.data.analysis.nlphub.nlp.NLpLanguageRecognizer;
import org.gcube.data.analysis.nlphub.nlp.NlpUtils; import org.gcube.data.analysis.nlphub.nlp.NlpUtils;
import org.gcube.data.analysis.nlphub.session.SessionUtils;
import org.gcube.data.analysis.nlphub.workspace.WorkspaceManager; import org.gcube.data.analysis.nlphub.workspace.WorkspaceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Servlet implementation class NLPUploader * Servlet implementation class NLPUploader
@ -30,10 +32,7 @@ import org.gcube.data.analysis.nlphub.workspace.WorkspaceManager;
@MultipartConfig @MultipartConfig
public class NLPUploader extends HttpServlet { public class NLPUploader extends HttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Logger logger = Logger.getLogger(NLPUploader.class.getSimpleName()); private static final Logger logger = LoggerFactory.getLogger(NLPUploader.class);
private boolean devMode = true;
private String token; // = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
private WorkspaceManager ws;
/** /**
* @see HttpServlet#HttpServlet() * @see HttpServlet#HttpServlet()
@ -58,21 +57,22 @@ public class NLPUploader extends HttpServlet {
*/ */
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
token = Constants.getToken(request, devMode);
doWork(request, response); doWork(request, response);
} }
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ws = new WorkspaceManager(); logger.debug("NLPUploader");
String token = SessionUtils.getToken(request);
WorkspaceManager ws = new WorkspaceManager();
response.setContentType("application/json;charset=UTF-8"); response.setContentType("application/json;charset=UTF-8");
if (request.getParameter("freetext") == null) if (request.getParameter("freetext") == null)
handleFileUpload(request, response); handleFileUpload(request, response, token, ws);
else else
handleFreeText(request, response); handleFreeText(request, response, token, ws);
} }
private void handleFreeText(HttpServletRequest request, HttpServletResponse response) private void handleFreeText(HttpServletRequest request, HttpServletResponse response, String token,
throws ServletException, IOException { WorkspaceManager ws) throws ServletException, IOException {
String freeText = request.getParameter("freetext"); String freeText = request.getParameter("freetext");
freeText = NlpUtils.replaceDirtyCharacters(freeText); freeText = NlpUtils.replaceDirtyCharacters(freeText);
@ -86,25 +86,24 @@ public class NLPUploader extends HttpServlet {
return; return;
} }
String link = ws.getPublicLink(fileName, token); String link = ws.getPublicLink(fileName, token);
if(request.getParameter("getlang") != null) { if (request.getParameter("getlang") != null) {
String sentence = NlpUtils.getLanguageRecognizerDigest(new String(content)); String sentence = NlpUtils.getLanguageRecognizerDigest(new String(content));
logger.info(sentence); logger.info(sentence);
try { try {
NLpLanguageRecognizer.run(request.getParameter("dataminer"), sentence, token, link, response); NLpLanguageRecognizer.run(request.getParameter("dataminer"), sentence, token, link, response);
} catch (NlpHubException ex) { } catch (NlpHubException ex) {
writer.println(new JsonManager().getSuccessJsonResponse(Constants.UNAVAILABLE, link)); writer.println(new JsonManager().getSuccessJsonResponse(Constants.UNAVAILABLE, link));
} }
} } else
else writer.println(new JsonManager().getSuccessJsonResponse("" + link)); writer.println(new JsonManager().getSuccessJsonResponse("" + link));
} catch (Exception x) { } catch (Exception e) {
x.printStackTrace(); logger.error(e.getLocalizedMessage(),e);
logger.error(x.getClass().getName() + ": " + x.getLocalizedMessage()); writer.println(new JsonManager().getErrorJsonResponse(e.getLocalizedMessage()));
writer.println(new JsonManager().getErrorJsonResponse(x.getLocalizedMessage()));
} }
} }
private void handleFileUpload(HttpServletRequest request, HttpServletResponse response) private void handleFileUpload(HttpServletRequest request, HttpServletResponse response, String token,
throws ServletException, IOException { WorkspaceManager ws) throws ServletException, IOException {
int contentLength = request.getContentLength(); int contentLength = request.getContentLength();
Part filePart = request.getPart("mytxtfile"); Part filePart = request.getPart("mytxtfile");
@ -154,10 +153,9 @@ public class NLPUploader extends HttpServlet {
} }
// writer.println(new JsonManager().getSuccessJsonResponse("" + // writer.println(new JsonManager().getSuccessJsonResponse("" +
// link)); // link));
} catch (Exception x) { } catch (Exception e) {
x.printStackTrace(); logger.error(e.getLocalizedMessage(),e);
logger.error(x.getClass().getName() + ": " + x.getLocalizedMessage()); writer.println(new JsonManager().getErrorJsonResponse(e.getLocalizedMessage()));
writer.println(new JsonManager().getErrorJsonResponse(x.getLocalizedMessage()));
} }
} }

View File

@ -6,13 +6,17 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.nlp.NlpParameter; import org.gcube.data.analysis.nlphub.nlp.NlpParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AsyncHttpRequest extends Thread { public class AsyncHttpRequest extends Thread {
private String baseUrl, finalUrl, result, method; private String baseUrl, finalUrl, result, method;
private ArrayList<NlpParameter> parameters; private ArrayList<NlpParameter> parameters;
private Logger logger = Logger.getLogger(AsyncHttpRequest.class.getSimpleName()); private static final Logger logger = LoggerFactory.getLogger(AsyncHttpRequest.class);
protected long elapsedTime; protected long elapsedTime;
public AsyncHttpRequest() { public AsyncHttpRequest() {
@ -20,13 +24,13 @@ public class AsyncHttpRequest extends Thread {
this.parameters = null; this.parameters = null;
this.method = "GET"; this.method = "GET";
finalUrl = null; finalUrl = null;
elapsedTime = 0; elapsedTime = 0;
} }
public AsyncHttpRequest(String baseUrl, String method, ArrayList<NlpParameter> parameters) { public AsyncHttpRequest(String baseUrl, String method, ArrayList<NlpParameter> parameters) {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.parameters = parameters; this.parameters = parameters;
if(method == null) if (method == null)
this.method = "GET"; this.method = "GET";
else else
this.method = (method.equalsIgnoreCase("GET") || method.equalsIgnoreCase("POST")) ? method : "GET"; this.method = (method.equalsIgnoreCase("GET") || method.equalsIgnoreCase("POST")) ? method : "GET";
@ -36,7 +40,7 @@ public class AsyncHttpRequest extends Thread {
public void run() { public void run() {
elapsedTime = System.currentTimeMillis(); elapsedTime = System.currentTimeMillis();
if(finalUrl == null) if (finalUrl == null)
finalUrl = baseUrl; finalUrl = baseUrl;
HttpURLConnection connection = null; HttpURLConnection connection = null;
BufferedReader r = null; BufferedReader r = null;
@ -56,11 +60,11 @@ public class AsyncHttpRequest extends Thread {
if (line != null) if (line != null)
result += line.trim(); result += line.trim();
} }
asyncHttpRequestCallback(); asyncHttpRequestCallback();
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
asyncHttpRequestCallback(); asyncHttpRequestCallback();
} finally { } finally {
try { try {
@ -69,19 +73,19 @@ public class AsyncHttpRequest extends Thread {
if (connection != null) if (connection != null)
connection.disconnect(); connection.disconnect();
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
} }
} }
} }
public String getResult() { public String getResult() {
return result; return result;
} }
public void asyncHttpRequestCallback() { public void asyncHttpRequestCallback() {
elapsedTime = System.currentTimeMillis() - elapsedTime; elapsedTime = System.currentTimeMillis() - elapsedTime;
} }
public void setBaseUrl(String baseUrl) { public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
} }
@ -103,8 +107,8 @@ public class AsyncHttpRequest extends Thread {
try { try {
finalUrl += p.getName() + "=" + URLEncoder.encode((String) p.getValue(), "UTF-8"); finalUrl += p.getName() + "=" + URLEncoder.encode((String) p.getValue(), "UTF-8");
finalUrl += "&"; finalUrl += "&";
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
} }
} }
finalUrl = finalUrl.substring(0, finalUrl.length() - 1); finalUrl = finalUrl.substring(0, finalUrl.length() - 1);

View File

@ -1,29 +1,29 @@
package org.gcube.data.analysis.nlphub.legacy; package org.gcube.data.analysis.nlphub.legacy;
import javax.servlet.http.HttpServletRequest;
public class Constants { public class Constants {
//public static String DATAMINER_URL = "http://dataminer0-proto.d4science.org"; public static final boolean DEBUG = false;
//public static String DATAMINER_URL = "http://dataminer2-proto.d4science.org"; public static final boolean TEST_ENABLE = false;
public static String DATAMINER_URL = "http://dataminer-prototypes.d4science.org";
public static String DEFAULT_DESCRIPTION = "NlpHub upload";
public static String TOKEN_PARAMETER = "gcube-token";
public static String TEST_TOKEN = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
public static String PUBLIC_TOKEN = "f57441ac-b361-4c2d-992a-40db034f1b8c-843339462";
public static String MIME_TEXT = "text/plain";
public static String CONTENT_TYPE = "Content-Type";
public static String UNAVAILABLE = "unavailable";
public static String ERROR_ID = "ERROR";
public static String INPUT_FILE_PARAMETER = "input";
public static String getToken(HttpServletRequest request, boolean devMode) { public static final String DEFAULT_USER = "giancarlo.panichi";
String token = request.getParameter(TOKEN_PARAMETER); public static final String DEFAULT_SCOPE = "/gcube/devNext/NextNext";
if(devMode) { public static final String DEFAULT_TOKEN = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
if(token == null) token = TEST_TOKEN;
}
return token;
}
public static String hexDump(byte[] bytes) { public static final String DEFAULT_DATAMINER_URL = "http://dataminer-prototypes.d4science.org";
public static final String TOKEN_PARAMETER = "gcube-token";
public static final String DEFAULT_DESCRIPTION = "NlpHub upload";
//public static String PUBLIC_TOKEN = "f57441ac-b361-4c2d-992a-40db034f1b8c-843339462";
public static final String MIME_TEXT = "text/plain";
public static final String CONTENT_TYPE = "Content-Type";
public static final String UNAVAILABLE = "unavailable";
public static final String ERROR_ID = "ERROR";
public static final String INPUT_FILE_PARAMETER = "input";
/*public static String hexDump(byte[] bytes) {
char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 3]; char[] hexChars = new char[bytes.length * 3];
for (int j = 0; j < bytes.length; j++) { for (int j = 0; j < bytes.length; j++) {
@ -32,5 +32,5 @@ public class Constants {
hexChars[3*j+2] = ' '; hexChars[3*j+2] = ' ';
} }
return new String(hexChars); return new String(hexChars);
} }*/
} }

View File

@ -1,6 +1,10 @@
package org.gcube.data.analysis.nlphub.legacy; package org.gcube.data.analysis.nlphub.legacy;
class DataminerClientException extends Exception { class DataminerClientException extends Exception {
private static final long serialVersionUID = 1L;
public DataminerClientException(String message, Throwable throwable) { public DataminerClientException(String message, Throwable throwable) {
super(message, throwable); super(message, throwable);
} }

View File

@ -2,14 +2,18 @@ package org.gcube.data.analysis.nlphub.legacy;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
public class JsonManager { public class JsonManager {
private static final Logger logger = LoggerFactory.getLogger(JsonManager.class);
public static String TEXT = "text"; public static String TEXT = "text";
public static String ANNOTATIONS = "annotations"; public static String ANNOTATIONS = "annotations";
public static String LANGUAGE = "language"; public static String LANGUAGE = "language";
@ -23,9 +27,10 @@ public class JsonManager {
public static String MESSAGE = "message"; public static String MESSAGE = "message";
private JsonObject jsonObjectRoot = null; private JsonObject jsonObjectRoot = null;
private JsonParser jsonParser = null; //private JsonParser jsonParser = null;
public JsonManager() { public JsonManager() {
logger.debug("JsonManager");
jsonObjectRoot = new JsonObject(); jsonObjectRoot = new JsonObject();
} }

View File

@ -1,14 +1,22 @@
package org.gcube.data.analysis.nlphub.legacy; package org.gcube.data.analysis.nlphub.legacy;
import java.util.ArrayList; import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class NerAlgorithm { public class NerAlgorithm {
private static final Logger logger = LoggerFactory.getLogger(NerAlgorithm.class);
private String name; private String name;
private ArrayList<NerAnnotationData> annotationsData; private ArrayList<NerAnnotationData> annotationsData;
public NerAlgorithm(String name) { public NerAlgorithm(String name) {
logger.debug("NerAlgorithm: "+name);
this.name = name; this.name = name;
annotationsData = new ArrayList<>(); annotationsData = new ArrayList<>();
} }

View File

@ -1,10 +1,16 @@
package org.gcube.data.analysis.nlphub.legacy; package org.gcube.data.analysis.nlphub.legacy;
import java.util.ArrayList; import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class NerAnnotationData { public class NerAnnotationData {
private static final Logger logger = LoggerFactory.getLogger(NerAnnotationData.class);
private String name; private String name;
private ArrayList<NerEntity> nerEntities; private ArrayList<NerEntity> nerEntities;
@ -13,6 +19,7 @@ public class NerAnnotationData {
* @param name * @param name
*/ */
public NerAnnotationData(String name) { public NerAnnotationData(String name) {
logger.debug("NerAnnotationData: "+name);
this.name = name; this.name = name;
nerEntities = new ArrayList<>(); nerEntities = new ArrayList<>();
} }

View File

@ -2,11 +2,17 @@ package org.gcube.data.analysis.nlphub.legacy;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class NerEntity { public class NerEntity {
private static final Logger logger = LoggerFactory.getLogger(NerEntity.class);
private int startIndex, endIndex; private int startIndex, endIndex;
private HashMap<String, String> properties = null; private HashMap<String, String> properties = null;
@ -16,6 +22,7 @@ public class NerEntity {
* @param endIndex the end index of the matching annotation * @param endIndex the end index of the matching annotation
*/ */
public NerEntity(int startIndex, int endIndex) { public NerEntity(int startIndex, int endIndex) {
logger.debug("NerEntity: [startIndex="+startIndex+", endIndex="+endIndex+"]");
this.startIndex = startIndex; this.startIndex = startIndex;
this.endIndex = endIndex; this.endIndex = endIndex;
properties = new HashMap<>(); properties = new HashMap<>();

View File

@ -1,14 +1,21 @@
package org.gcube.data.analysis.nlphub.legacy; package org.gcube.data.analysis.nlphub.legacy;
import java.util.ArrayList; import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class NerOutput { public class NerOutput {
private static final Logger logger = LoggerFactory.getLogger(NerOutput.class);
private String text, annotations, language; private String text, annotations, language;
private ArrayList<NerAlgorithm> result; private ArrayList<NerAlgorithm> result;
public NerOutput(String text, String annotations, String language) { public NerOutput(String text, String annotations, String language) {
logger.debug("NerOutput: [text="+text+", annotations="+annotations+", language="+language+"]");
this.text = text; this.text = text;
this.annotations = annotations; this.annotations = annotations;
this.language = language; this.language = language;

View File

@ -1,6 +1,9 @@
package org.gcube.data.analysis.nlphub.legacy; package org.gcube.data.analysis.nlphub.legacy;
public class NlpHubException extends Exception { public class NlpHubException extends Exception {
private static final long serialVersionUID = 1L;
public NlpHubException(String message, Throwable throwable) { public NlpHubException(String message, Throwable throwable) {
super(message, throwable); super(message, throwable);
} }

View File

@ -10,13 +10,15 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.legacy.NerAlgorithm; import org.gcube.data.analysis.nlphub.legacy.NerAlgorithm;
import org.gcube.data.analysis.nlphub.legacy.NerAnnotationData; import org.gcube.data.analysis.nlphub.legacy.NerAnnotationData;
import org.gcube.data.analysis.nlphub.legacy.NerEntity; import org.gcube.data.analysis.nlphub.legacy.NerEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultMapper implements JsonMapper { public class DefaultMapper implements JsonMapper {
private Logger logger = Logger.getLogger(DefaultMapper.class.getSimpleName()); private static final Logger logger = LoggerFactory.getLogger(DefaultMapper.class);
public String getJson(String alg, String link) { public String getJson(String alg, String link) {
NerAlgorithm algInfo = new NerAlgorithm(alg); NerAlgorithm algInfo = new NerAlgorithm(alg);
@ -59,33 +61,10 @@ public class DefaultMapper implements JsonMapper {
} }
} }
return algInfo.toJson().toString(); return algInfo.toJson().toString();
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(),e);
return null; return null;
} }
} }
/*
public static void main(String[] args) {
String remoteJson = "http://data.d4science.org/a2JtV0lOUUxsenZCM2RmcFRqVGtWWm42NU9LQnFGTk9HbWJQNStIS0N6Yz0-VLT";
String alg = "ENGLISH_NAMED_ENTITY_RECOGNIZER";
//remoteJson = "http://data.d4science.org/T21IcHlMSzFJRCttZ1lHVzMxd2dyWjVOUEpyY2dwUVNHbWJQNStIS0N6Yz0-VLT";
//remoteJson = "http://data.d4science.org/bDZLVkdlaVBjZCtLSWsrWUNQdHk3MUs4cEtDUGF5NktHbWJQNStIS0N6Yz0";
DefaultMapper dm = new DefaultMapper();
try {
String s = dm.getJson(alg, remoteJson);
System.out.println("" + s);
String file = "/home/erico/debug.json";
java.io.File f = new java.io.File(file);
f.createNewFile();
java.io.FileWriter w = new java.io.FileWriter(f);
w.write(s);
w.flush();
w.close();
} catch (Exception x) {
System.out.println(x.getLocalizedMessage());
x.printStackTrace();
}
}*/
} }

View File

@ -5,45 +5,50 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.legacy.Constants; import org.gcube.data.analysis.nlphub.legacy.Constants;
import org.gcube.data.analysis.nlphub.legacy.JsonManager; import org.gcube.data.analysis.nlphub.legacy.JsonManager;
import org.gcube.data.analysis.nlphub.legacy.NlpHubException; import org.gcube.data.analysis.nlphub.legacy.NlpHubException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
public class NLpLanguageRecognizer { public class NLpLanguageRecognizer {
private HttpServletResponse response; private static final Logger logger = LoggerFactory.getLogger(NLpLanguageRecognizer.class);
private Logger logger = Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName());
private String sentence, publicLink; // private HttpServletResponse response;
// private String sentence, publicLink;
public final static String RECOGNIZER_ID = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.LANGUAGE_RECOGNIZER"; public final static String RECOGNIZER_ID = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.LANGUAGE_RECOGNIZER";
private String dataMiner = null; // private String dataMiner = null;
public NLpLanguageRecognizer(String service, String token, String sentence) { public NLpLanguageRecognizer(String service, String token, String sentence) {
this.sentence = sentence; logger.debug(
response = null; "NLpLanguageRecognizer: [service=" + service + ", token=" + token + ", sentence=" + sentence + "]");
// this.sentence = sentence;
// response = null;
} }
public NLpLanguageRecognizer(String dataMiner, String service, String token, String sentence, String publicLink, public NLpLanguageRecognizer(String dataMiner, String service, String token, String sentence, String publicLink,
HttpServletResponse response) { HttpServletResponse response) {
this.sentence = sentence; logger.debug("NLpLanguageRecognizer: [dataMiner=" + dataMiner + ",service=" + service + ", token=" + token
this.response = response; + ", sentence=" + sentence + ",publicLink" + publicLink + "]");
this.publicLink = publicLink; // this.sentence = sentence;
this.dataMiner = dataMiner; // this.response = response;
// this.publicLink = publicLink;
// this.dataMiner = dataMiner;
} }
public static void run(String dataMiner, String sentence, String token, String publicLink, HttpServletResponse response) throws NlpHubException { public static void run(String dataMiner, String sentence, String token, String publicLink,
HttpServletResponse response) throws NlpHubException {
try { try {
if(dataMiner == null) if (dataMiner == null)
dataMiner = Constants.DATAMINER_URL; dataMiner = Constants.DEFAULT_DATAMINER_URL;
else else
dataMiner = "http://" + dataMiner; dataMiner = "http://" + dataMiner;
String urlService = dataMiner + "/wps/WebProcessingService?request=Execute&service=WPS&Version=1.0.0"; String urlService = dataMiner + "/wps/WebProcessingService?request=Execute&service=WPS&Version=1.0.0";
@ -53,7 +58,8 @@ public class NLpLanguageRecognizer {
urlService += "&DataInputs=sentence=" + URLEncoder.encode(sentence, "UTF-8"); urlService += "&DataInputs=sentence=" + URLEncoder.encode(sentence, "UTF-8");
URL url = new URL(urlService); URL url = new URL(urlService);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//connection.setRequestProperty(Constants.TOKEN_PARAMETER, super.getToken()); // connection.setRequestProperty(Constants.TOKEN_PARAMETER,
// super.getToken());
connection.setDoInput(true); connection.setDoInput(true);
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setUseCaches(false); connection.setUseCaches(false);
@ -64,33 +70,31 @@ public class NLpLanguageRecognizer {
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
NodeList nListData = doc.getElementsByTagName("d4science:Data"); NodeList nListData = doc.getElementsByTagName("d4science:Data");
NodeList nListDesc = doc.getElementsByTagName("d4science:Description"); NodeList nListDesc = doc.getElementsByTagName("d4science:Description");
int len = nListData.getLength(); int len = nListData.getLength();
for(int i=0; i<len; i++) { for (int i = 0; i < len; i++) {
Node data = nListData.item(i); Node data = nListData.item(i);
Node description = nListDesc.item(i); Node description = nListDesc.item(i);
String link = data.getTextContent(); String link = data.getTextContent();
String type = description.getTextContent(); String type = description.getTextContent();
if(type.equals("outfile")) { if (type.equals("outfile")) {
//System.out.println(link); // System.out.println(link);
String content = readFileContent(link, token); String content = readFileContent(link, token);
if (response != null) { if (response != null) {
response.getWriter() response.getWriter().println(new JsonManager().getSuccessJsonResponse(content, publicLink));
.println(new JsonManager().getSuccessJsonResponse(content, publicLink)); } else {
} logger.debug(new JsonManager().getSuccessJsonResponse(content, publicLink));
else {
Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName()).debug(new JsonManager().getSuccessJsonResponse(content, publicLink));
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
Logger.getLogger(NLpLanguageRecognizer.class.getSimpleName()).error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage(), e);
throw new NlpHubException(e.getLocalizedMessage(), e); throw new NlpHubException(e.getLocalizedMessage(), e);
} }
} }
private static String readFileContent(String link, String token) throws Exception { private static String readFileContent(String link, String token) throws Exception {
URL url = new URL(link); URL url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
@ -110,25 +114,20 @@ public class NLpLanguageRecognizer {
String out = response.toString(); String out = response.toString();
return out; return out;
} }
/* /*
private String readFileContent(String link) throws Exception { * private String readFileContent(String link) throws Exception { URL url =
URL url = new URL(link); * new URL(link); HttpURLConnection connection = (HttpURLConnection)
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); * url.openConnection();
connection.setRequestProperty(Constants.TOKEN_PARAMETER, super.getToken()); * connection.setRequestProperty(Constants.TOKEN_PARAMETER,
connection.setDoInput(true); * super.getToken()); connection.setDoInput(true);
connection.setDoOutput(true); * connection.setDoOutput(true); connection.setUseCaches(false);
connection.setUseCaches(false); * connection.setRequestMethod("GET");
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; }
*/
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;
}*/
} }

View File

@ -11,37 +11,41 @@ import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.legacy.AsyncHttpRequest; import org.gcube.data.analysis.nlphub.legacy.AsyncHttpRequest;
import org.gcube.data.analysis.nlphub.legacy.Constants; import org.gcube.data.analysis.nlphub.legacy.Constants;
import org.gcube.data.analysis.nlphub.legacy.NerOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
public class NlpAsyncNerRunner extends AsyncHttpRequest { public class NlpAsyncNerRunner extends AsyncHttpRequest {
public static String WPS_EXECUTE_URL = "/wps/WebProcessingService?request=Execute&service=WPS&Version=1.0.0"; private static final Logger logger = LoggerFactory.getLogger(NerOutput.class);
public static String WPS_DESCRIBE_PROCESS_URL = "/wps/WebProcessingService?request=DescribeProcess&service=WPS&Version=1.0.0"; public static final String WPS_EXECUTE_URL = "/wps/WebProcessingService?request=Execute&service=WPS&Version=1.0.0";
private String identifier, token, httpMethod, annotations, publicLink, language, dataMiner; public static final String WPS_DESCRIBE_PROCESS_URL = "/wps/WebProcessingService?request=DescribeProcess&service=WPS&Version=1.0.0";
private Logger logger = Logger.getLogger(NlpAsyncNerRunner.class.getSimpleName()); private String identifier, token, httpMethod, annotations, publicLink, dataMiner;
private RunnerCommander commander; private RunnerCommander commander;
public NlpAsyncNerRunner(String dataMiner, String identifier, String token, String publicLink, String annotations, String language, RunnerCommander commander) { public NlpAsyncNerRunner(String dataMiner, String identifier, String token, String publicLink, String annotations,
String language, RunnerCommander commander) {
super(); super();
this.identifier = identifier; this.identifier = identifier;
this.token = token; this.token = token;
this.httpMethod = "GET"; this.httpMethod = "GET";
this.annotations = annotations; this.annotations = annotations;
this.publicLink = publicLink; this.publicLink = publicLink;
this.language = language; // not used for the moment...
this.commander = commander; this.commander = commander;
if(dataMiner == null) if (dataMiner == null)
this.dataMiner = Constants.DATAMINER_URL; this.dataMiner = Constants.DEFAULT_DATAMINER_URL;
else else
this.dataMiner = "http://" + dataMiner; this.dataMiner = "http://" + dataMiner;
ArrayList<NlpParameter> params = buildParameterString(); ArrayList<NlpParameter> params = buildParameterString();
String serviceUrl = this.dataMiner + WPS_EXECUTE_URL + "&gcube-token=" + token + "&lang=en-US&Identifier=" + identifier; String serviceUrl = this.dataMiner + WPS_EXECUTE_URL + "&gcube-token=" + token + "&lang=en-US&Identifier="
+ identifier;
serviceUrl += "&" + setUrl(params); serviceUrl += "&" + setUrl(params);
super.setBaseUrl(serviceUrl); super.setBaseUrl(serviceUrl);
super.setMethod(httpMethod); super.setMethod(httpMethod);
@ -120,8 +124,8 @@ public class NlpAsyncNerRunner extends AsyncHttpRequest {
parameters.add(nlpParam); parameters.add(nlpParam);
} }
} catch (Exception x) { } catch (Exception e) {
logger.error(x.getLocalizedMessage()); logger.error(e.getLocalizedMessage(), e);
} finally { } finally {
try { try {
if (r != null) if (r != null)
@ -129,7 +133,7 @@ public class NlpAsyncNerRunner extends AsyncHttpRequest {
if (connection != null) if (connection != null)
connection.disconnect(); connection.disconnect();
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage(), e);
} }
} }
return parameters; return parameters;
@ -138,7 +142,7 @@ public class NlpAsyncNerRunner extends AsyncHttpRequest {
public long getElapsedTime() { public long getElapsedTime() {
return elapsedTime; return elapsedTime;
} }
@Override @Override
public void asyncHttpRequestCallback() { public void asyncHttpRequestCallback() {
elapsedTime = System.currentTimeMillis() - elapsedTime; elapsedTime = System.currentTimeMillis() - elapsedTime;
@ -147,35 +151,35 @@ public class NlpAsyncNerRunner extends AsyncHttpRequest {
String theLink = ""; String theLink = "";
try { try {
BufferedReader r = new BufferedReader( BufferedReader r = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)))); new InputStreamReader(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8))));
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r)); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(r));
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
NodeList nListResult = doc.getElementsByTagName("ogr:Result"); NodeList nListResult = doc.getElementsByTagName("ogr:Result");
for (int i=0, found=0; (i<nListResult.getLength()) && (found==0); i++) { for (int i = 0, found = 0; (i < nListResult.getLength()) && (found == 0); i++) {
Node nodeResult = nListResult.item(i); Node nodeResult = nListResult.item(i);
NodeList list = nodeResult.getChildNodes(); NodeList list = nodeResult.getChildNodes();
String res = ""; String res = "";
for(int j=0; j<list.getLength(); j++) { for (int j = 0; j < list.getLength(); j++) {
Node node = list.item(j); Node node = list.item(j);
if(node.getNodeName().equals("d4science:Data")) { if (node.getNodeName().equals("d4science:Data")) {
res = node.getTextContent(); res = node.getTextContent();
} } else if (node.getNodeName().equals("d4science:MimeType")) {
else if(node.getNodeName().equals("d4science:MimeType")) { if (node.getTextContent().equals("application/d4science")) {
if(node.getTextContent().equals("application/d4science")) {
found = 1; found = 1;
} }
} }
} }
if(found > 0) { if (found > 0) {
theLink = res; theLink = res;
} }
} }
commander.updateResultList(identifier.substring(identifier.lastIndexOf(".") + 1) + ":::" + theLink); commander.updateResultList(identifier.substring(identifier.lastIndexOf(".") + 1) + ":::" + theLink);
} catch (Exception x) { } catch (Exception e) {
commander.updateResultList(identifier.substring(identifier.lastIndexOf(".") + 1) + ":::" + Constants.ERROR_ID); commander.updateResultList(
logger.error(x.getLocalizedMessage()); identifier.substring(identifier.lastIndexOf(".") + 1) + ":::" + Constants.ERROR_ID);
logger.error(e.getLocalizedMessage(), e);
} }
} }
} }

View File

@ -1,7 +1,5 @@
package org.gcube.data.analysis.nlphub.nlp; package org.gcube.data.analysis.nlphub.nlp;
import java.util.ArrayList;
public class NlpUtils { public class NlpUtils {
public static String getLanguageRecognizerDigest(String content) { public static String getLanguageRecognizerDigest(String content) {

View File

@ -5,11 +5,14 @@ import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.legacy.Constants; import org.gcube.data.analysis.nlphub.legacy.Constants;
import org.gcube.data.analysis.nlphub.legacy.JsonManager; import org.gcube.data.analysis.nlphub.legacy.JsonManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RunnerCommander extends Thread { public class RunnerCommander extends Thread {
private static final Logger logger = LoggerFactory.getLogger(RunnerCommander.class);
private HttpServletResponse response = null; private HttpServletResponse response = null;
private String annotationList = ""; private String annotationList = "";
private String[] identifiers = null; private String[] identifiers = null;
@ -19,7 +22,6 @@ public class RunnerCommander extends Thread {
private ArrayList<String> outResultList = null; private ArrayList<String> outResultList = null;
private long sleepTime = 500l; private long sleepTime = 500l;
private long maxWaitingTime = 5l * 60l * 1000l; private long maxWaitingTime = 5l * 60l * 1000l;
private Logger logger = Logger.getLogger(RunnerCommander.class.getSimpleName());
private boolean complete = false; private boolean complete = false;
public RunnerCommander(String dataMiner, String[] identifiers, String link, String annotationList, String token) { public RunnerCommander(String dataMiner, String[] identifiers, String link, String annotationList, String token) {
@ -76,8 +78,8 @@ public class RunnerCommander extends Thread {
counter += sleepTime; counter += sleepTime;
if(counter > 2*maxWaitingTime) if(counter > 2*maxWaitingTime)
complete = true; complete = true;
} catch (InterruptedException x) { } catch (InterruptedException e) {
logger.info("Interrupted."); logger.info("Interrupted. "+identifiers);
} }
} }
} }
@ -107,7 +109,7 @@ public class RunnerCommander extends Thread {
} }
logger.error("Timeout error."); logger.error("Timeout error.");
timeoutHandler(); timeoutHandler();
} catch (InterruptedException x) { } catch (InterruptedException e) {
logger.info("Elapsed time: " + counter + " msec."); logger.info("Elapsed time: " + counter + " msec.");
logger.info("Thread interrupted."); logger.info("Thread interrupted.");
timeoutHandler(); timeoutHandler();
@ -148,8 +150,7 @@ public class RunnerCommander extends Thread {
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.error(ex.getLocalizedMessage()); logger.error(ex.getLocalizedMessage(),ex);
ex.printStackTrace();
} finally { } finally {
complete = true; complete = true;
if (writer != null) if (writer != null)

View File

@ -0,0 +1,22 @@
package org.gcube.data.analysis.nlphub.session;
import javax.servlet.http.HttpServletRequest;
import org.gcube.data.analysis.nlphub.legacy.Constants;
public class SessionUtils {
public static String getToken(HttpServletRequest request) {
String token = request.getParameter(Constants.TOKEN_PARAMETER);
if(token == null || token.isEmpty()){
if(Constants.DEBUG){
token = Constants.DEFAULT_TOKEN;
} else {
}
}
return token;
}
}

View File

@ -10,15 +10,13 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import javax.servlet.http.Part;
import org.apache.log4j.Logger;
import org.gcube.data.analysis.nlphub.NLPUploader;
import org.gcube.data.analysis.nlphub.legacy.Constants; import org.gcube.data.analysis.nlphub.legacy.Constants;
import org.gcube.data.analysis.nlphub.legacy.NlpHubException; import org.gcube.data.analysis.nlphub.legacy.NlpHubException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WorkspaceManager { public class WorkspaceManager {
private Logger logger = Logger.getLogger(WorkspaceManager.class.getSimpleName()); private static final Logger logger = LoggerFactory.getLogger(WorkspaceManager.class);
public String getPublicLink(String fileName, String token) throws NlpHubException { public String getPublicLink(String fileName, String token) throws NlpHubException {
try { try {
@ -62,7 +60,7 @@ public class WorkspaceManager {
return link; return link;
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage(), e);
throw new NlpHubException(e.getLocalizedMessage(), e); throw new NlpHubException(e.getLocalizedMessage(), e);
} }
} }
@ -93,8 +91,9 @@ public class WorkspaceManager {
} }
String xmlOut = response.toString(); String xmlOut = response.toString();
logger.debug("deleteFile: " + xmlOut);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage(), e);
throw new NlpHubException(e.getLocalizedMessage(), e); throw new NlpHubException(e.getLocalizedMessage(), e);
} }
} }
@ -133,7 +132,7 @@ public class WorkspaceManager {
return true; return true;
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage(), e);
throw new NlpHubException(e.getLocalizedMessage(), e); throw new NlpHubException(e.getLocalizedMessage(), e);
} finally { } finally {
// output stream must be closed anyway... // output stream must be closed anyway...
@ -141,7 +140,7 @@ public class WorkspaceManager {
try { try {
output.close(); output.close();
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage(), e);
} }
} }
} }

View File

@ -1,12 +0,0 @@
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# Print only messages of level TRACE or above in the package org.gcube
log4j.logger.org.gcube=TRACE
log4j.logger.org.gcube.application.framework.core.session=INFO
log4j.logger.org.gcube.common.scope.impl.DefaultScopeProvider=ERROR
log4j.logger.com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor=ERROR

View File

@ -1,8 +0,0 @@
log4j.rootLogger=INFO,stdout
log4j.logger.com.endeca=INFO
# Logger for crawl metrics
log4j.logger.com.endeca.itl.web.metrics=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube" level="INFO" />
<logger name="org.gcube.vremanagement.smartexecutor" level="TRACE" />
<logger name="org.gcube.dataharvest" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,83 @@
<configuration scan="true" debug="false">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/gcube/tomcat/logs/ghn.log</file>
<append>true</append>
<encoder>
<pattern>%date [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/home/gcube/tomcat/logs/ghn.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<maxFileSize>10MB</maxFileSize>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="ACCOUNT_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/gcube/tomcat/logs/accounting.log</file>
<append>true</append>
<encoder>
<pattern>%date [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/home/gcube/tomcat/logs/accounting.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<maxFileSize>10MB</maxFileSize>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="ACCESS_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/gcube/tomcat/logs/access.log</file>
<append>true</append>
<encoder>
<pattern>%date [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/home/gcube/tomcat/logs/access.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<maxFileSize>10MB</maxFileSize>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="NLP_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/gcube/tomcat/logs/nlphub.log</file>
<append>true</append>
<encoder>
<pattern>%date [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>/home/gcube/tomcat/logs/nlphub.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<maxFileSize>10MB</maxFileSize>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
</appender>
<logger name="org.gcube.data.publishing" level="ERROR">
<appender-ref ref="ACCOUNT_FILE" />
</logger>
<logger name="org.gcube.documentstore" level="ERROR">
<appender-ref ref="ACCOUNT_FILE" />
</logger>
<logger name="org.gcube.smartgears.handlers.application.request" level="INFO">
<appender-ref ref="ACCESS_FILE" />
</logger>
<logger name="org.gcube" level="WARN" />
<logger name="org.gcube.smartgears" level="WARN" />
<logger name="org.gcube.common.events" level="WARN" />
<logger name="org.gcube.data.analysis.nlphub" level="DEBUG">
<appender-ref ref="NLP_FILE" />
</logger>
<root level="WARN">
<appender-ref ref="FILE" />
</root>
</configuration>

View File

@ -11,7 +11,7 @@
</welcome-file-list> </welcome-file-list>
<servlet> <servlet>
<servlet-name>NLPServlet</servlet-name> <servlet-name>NLPServlet</servlet-name>
<servlet-class>org.gcube.nlphub.NLPHub</servlet-class> <servlet-class>org.gcube.data.analysis.nlphub.NLPHub</servlet-class>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>NLPServlet</servlet-name> <servlet-name>NLPServlet</servlet-name>
@ -19,7 +19,7 @@
</servlet-mapping> </servlet-mapping>
<servlet> <servlet>
<servlet-name>NLPUploader</servlet-name> <servlet-name>NLPUploader</servlet-name>
<servlet-class>org.gcube.nlphub.NLPUploader</servlet-class> <servlet-class>org.gcube.data.analysis.nlphub.NLPUploader</servlet-class>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>NLPUploader</servlet-name> <servlet-name>NLPUploader</servlet-name>
@ -27,7 +27,7 @@
</servlet-mapping> </servlet-mapping>
<servlet> <servlet>
<servlet-name>NLPMapper</servlet-name> <servlet-name>NLPMapper</servlet-name>
<servlet-class>org.gcube.nlphub.NLPMapper</servlet-class> <servlet-class>org.gcube.data.analysis.nlphub.NLPMapper</servlet-class>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>NLPMapper</servlet-name> <servlet-name>NLPMapper</servlet-name>

View File

@ -23,7 +23,15 @@
<script type="text/javascript"> <script type="text/javascript">
var activePage = "Named Entity Recognition"; var activePage = "Named Entity Recognition";
var inputFile = '<%= ((request.getParameter(Constants.INPUT_FILE_PARAMETER) == null) ? "" : "" + request.getParameter(Constants.INPUT_FILE_PARAMETER)) %>'; var inputFile = '<%= ((request.getParameter(Constants.INPUT_FILE_PARAMETER) == null) ? "" : "" + request.getParameter(Constants.INPUT_FILE_PARAMETER)) %>';
var gCubeToken = '<%= ((request.getParameter(Constants.TOKEN_PARAMETER) == null) ? "" : "" + request.getParameter(Constants.TOKEN_PARAMETER)) %>'; <%
String requestToken=request.getParameter(Constants.TOKEN_PARAMETER);
if( requestToken == null||requestToken.isEmpty()) {
%>
var gCubeToken = "fea75a5a-d84c-495f-b0ca-09cdd95bacce-843339462";
<% } else { %>
var gCubeToken = <%=requestToken%>
<% } %>
var scheme = '<%= request.getScheme() %>'; var scheme = '<%= request.getScheme() %>';
var contextPath = '<%= request.getContextPath() %>'; var contextPath = '<%= request.getContextPath() %>';
</script> </script>