This commit is contained in:
Erik Perrone 2018-02-22 16:48:15 +00:00
parent 3b1f20d761
commit 75a08add56
4 changed files with 325 additions and 145 deletions

View File

@ -23,6 +23,7 @@ import org.gcube.data.analysis.dataminermanagercl.shared.data.output.Resource;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.FileParameter;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ObjectParameter;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.Parameter;
import org.gcube.nlphub.legacy.Constants;
import org.gcube.nlphub.legacy.DataminerClient;
import org.gcube.nlphub.legacy.JsonManager;
//import org.gcube.dataminerclient.DataminerClient;
@ -37,6 +38,7 @@ public class NLPHub extends HttpServlet {
private static final long serialVersionUID = 1L;
private String service = "http://dataminer-prototypes.d4science.org/wps/";
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
private boolean devMode = true;
/**
* @see HttpServlet#HttpServlet()
@ -65,6 +67,7 @@ public class NLPHub extends HttpServlet {
}
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
token = Constants.getToken(request, devMode);
try {
System.out.println("annotations: " + request.getParameter("annotations"));
System.out.println("lang: " + request.getParameter("lang"));

View File

@ -7,6 +7,7 @@ import java.io.PrintWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
@ -18,8 +19,11 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.gcube.nlphub.nlp.NlpNerRunner;
import org.gcube.nlphub.workspace.WorkspaceManager;
import org.gcube.nlphub.mapper.JsonMapper;
import org.gcube.nlphub.legacy.Constants;
import org.gcube.nlphub.legacy.JsonManager;
import org.gcube.nlphub.legacy.NlpHubException;
import org.gcube.nlphub.mapper.DefaultMapper;
/**
@ -29,7 +33,9 @@ import org.gcube.nlphub.mapper.DefaultMapper;
public class NLPMapper extends HttpServlet {
private static final long serialVersionUID = 1L;
private Logger logger = Logger.getLogger(NLPMapper.class.getSimpleName());
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
private boolean devMode = true;
/**
* @see HttpServlet#HttpServlet()
*/
@ -56,11 +62,9 @@ public class NLPMapper extends HttpServlet {
}
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.getWriter().append("Served at:
// ").append(request.getContextPath());
String documentLink = request.getParameter("plink"); // link al testo
// (sul
// workspace)
token = Constants.getToken(request, devMode);
String documentLink = request.getParameter("plink"); // link to text file (workspace)
String toBeMap = request.getParameter("tobemap");
System.out.println("tobemap: " + toBeMap);
String[] tokens;
@ -99,7 +103,13 @@ public class NLPMapper extends HttpServlet {
}
out += "]}";
writer.append(out);
String resultLink = saveResult(out);
String outResult = "{";
outResult += "\"link\":\"" + resultLink + "\",";
outResult += "\"output\":" + out;
outResult += "}";
writer.append(outResult);
} catch (Exception x) {
logger.error(x.getLocalizedMessage());
@ -129,4 +139,19 @@ public class NLPMapper extends HttpServlet {
}
return content;
}
private String saveResult(String jsonResult) throws NlpHubException {
WorkspaceManager ws = new WorkspaceManager();
long now = System.currentTimeMillis();
String fileName = "result-nlp-" + now + ".json";
ws.deleteFile(fileName, token);
byte[] byteContent = jsonResult.getBytes(StandardCharsets.UTF_8);
if (!ws.uploadFile(byteContent, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
throw new NlpHubException("Error writing file: " + fileName + " on workspace (token: " + token + ")", null);
}
String link = ws.getPublicLink(fileName, token);
return link;
}
}

View File

@ -24,6 +24,7 @@ import org.apache.log4j.Logger;
import org.gcube.nlphub.legacy.Constants;
import org.gcube.nlphub.legacy.JsonManager;
import org.gcube.nlphub.legacy.NlpHubException;
import org.gcube.nlphub.workspace.WorkspaceManager;
/**
* Servlet implementation class NLPUploader
@ -36,7 +37,8 @@ public class NLPUploader extends HttpServlet {
private Logger logger = Logger.getLogger(NLPUploader.class.getSimpleName());
private boolean devMode = true;
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
private WorkspaceManager ws;
/**
* @see HttpServlet#HttpServlet()
*/
@ -60,10 +62,12 @@ public class NLPUploader extends HttpServlet {
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
token = Constants.getToken(request, devMode);
doWork(request, response);
}
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ws = new WorkspaceManager();
response.setContentType("application/json;charset=UTF-8");
if (request.getParameter("freetext") == null)
handleFileUpload(request, response);
@ -79,12 +83,12 @@ public class NLPUploader extends HttpServlet {
String fileName = generateFileName();
PrintWriter writer = response.getWriter();
try {
if (!uploadFile(content, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
if (!ws.uploadFile(content, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
writer.println(new JsonManager().getErrorJsonResponse(
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
return;
}
String link = getPublicLink(fileName, token);
String link = ws.getPublicLink(fileName, token);
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
} catch (Exception x) {
x.printStackTrace();
@ -124,15 +128,15 @@ public class NLPUploader extends HttpServlet {
} else
bufferedContent = buffer;
deleteFile(fileName, token);
ws.deleteFile(fileName, token);
if (!uploadFile(bufferedContent, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
if (!ws.uploadFile(bufferedContent, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
writer.println(new JsonManager().getErrorJsonResponse(
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
return;
}
String link = getPublicLink(fileName, token);
String link = ws.getPublicLink(fileName, token);
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
} catch (Exception x) {
x.printStackTrace();
@ -153,138 +157,138 @@ public class NLPUploader extends HttpServlet {
return null;
}
private String getPublicLink(String fileName, String token) throws NlpHubException {
try {
String link = "";
String user = authorizationService().get(token).getClientInfo().getId();
String wsRoot = "/Home/" + user + "/Workspace/";
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
String uri = webapp + "/rest/GetPublicLink?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8")
+ "&shortUrl=false";
URL url = new URL(uri);
// System.out.println(uri);
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);
}
String xmlOut = response.toString();
// System.out.println("xmlOut: " + xmlOut);
String begin = "<string>";
String end = "</string>";
int b = xmlOut.indexOf(begin);
int e = xmlOut.indexOf(end);
if (xmlOut.contains("Exception") || (e < 0) || (b < 0)) {
String message = "Invalid link: " + URLEncoder.encode(xmlOut, "UTF-8");
logger.error(message);
throw new NlpHubException(message, null);
}
link = xmlOut.substring(b + begin.length(), e);
return link;
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
}
}
private void deleteFile(String fileName, String token) throws NlpHubException {
try {
String user = authorizationService().get(token).getClientInfo().getId();
String wsRoot = "/Home/" + user + "/Workspace/";
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
String uri = webapp + "/rest/Delete?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8");
URL url = new URL(uri);
// System.out.println(uri);
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);
}
String xmlOut = response.toString();
// System.out.println(xmlOut);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
}
}
private boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
OutputStream output = null;
try {
String user = authorizationService().get(token).getClientInfo().getId();
String wsRoot = "/Home/" + user + "/Workspace/";
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
String uri = webapp + "/rest/Upload?name=" + URLEncoder.encode(name, "UTF-8") + "&description="
+ URLEncoder.encode(description, "UTF-8") + "&parentPath=" + URLEncoder.encode(wsRoot, "UTF-8");
URL url = new URL(uri);
// System.out.println(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty(Constants.CONTENT_TYPE, Constants.MIME_TEXT);
connection.setRequestMethod("POST");
output = connection.getOutputStream();
output.write(in);
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = r.readLine()) != null) {
response.append(inputLine);
}
String xmlOut = response.toString();
// System.out.println(xmlOut);
if (xmlOut.contains("Exception"))
return false;
return true;
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
} finally {
// output stream must be closed anyway...
if (output != null)
try {
output.close();
} catch (IOException e) {
logger.error(e.getLocalizedMessage());
}
}
}
// private String getPublicLink(String fileName, String token) throws NlpHubException {
// try {
// String link = "";
// String user = authorizationService().get(token).getClientInfo().getId();
// String wsRoot = "/Home/" + user + "/Workspace/";
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
// String uri = webapp + "/rest/GetPublicLink?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8")
// + "&shortUrl=false";
// URL url = new URL(uri);
//
// // System.out.println(uri);
//
// 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);
// }
//
// String xmlOut = response.toString();
// // System.out.println("xmlOut: " + xmlOut);
//
// String begin = "<string>";
// String end = "</string>";
// int b = xmlOut.indexOf(begin);
// int e = xmlOut.indexOf(end);
//
// if (xmlOut.contains("Exception") || (e < 0) || (b < 0)) {
// String message = "Invalid link: " + URLEncoder.encode(xmlOut, "UTF-8");
// logger.error(message);
// throw new NlpHubException(message, null);
// }
//
// link = xmlOut.substring(b + begin.length(), e);
// return link;
//
// } catch (Exception e) {
// logger.error(e.getLocalizedMessage());
// throw new NlpHubException(e.getLocalizedMessage(), e);
// }
// }
//
// private void deleteFile(String fileName, String token) throws NlpHubException {
// try {
// String user = authorizationService().get(token).getClientInfo().getId();
// String wsRoot = "/Home/" + user + "/Workspace/";
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
// String uri = webapp + "/rest/Delete?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8");
// URL url = new URL(uri);
//
// // System.out.println(uri);
//
// 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);
// }
//
// String xmlOut = response.toString();
// // System.out.println(xmlOut);
// } catch (Exception e) {
// logger.error(e.getLocalizedMessage());
// throw new NlpHubException(e.getLocalizedMessage(), e);
// }
// }
//
// private boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
// OutputStream output = null;
// try {
// String user = authorizationService().get(token).getClientInfo().getId();
// String wsRoot = "/Home/" + user + "/Workspace/";
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
// String uri = webapp + "/rest/Upload?name=" + URLEncoder.encode(name, "UTF-8") + "&description="
// + URLEncoder.encode(description, "UTF-8") + "&parentPath=" + URLEncoder.encode(wsRoot, "UTF-8");
// URL url = new URL(uri);
//
// // System.out.println(uri);
//
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
// connection.setDoInput(true);
// connection.setDoOutput(true);
// connection.setUseCaches(false);
// connection.setRequestProperty(Constants.CONTENT_TYPE, Constants.MIME_TEXT);
// connection.setRequestMethod("POST");
// output = connection.getOutputStream();
// output.write(in);
//
// BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//
// StringBuffer response = new StringBuffer();
// String inputLine;
// while ((inputLine = r.readLine()) != null) {
// response.append(inputLine);
// }
//
// String xmlOut = response.toString();
// // System.out.println(xmlOut);
// if (xmlOut.contains("Exception"))
// return false;
// return true;
//
// } catch (Exception e) {
// logger.error(e.getLocalizedMessage());
// throw new NlpHubException(e.getLocalizedMessage(), e);
// } finally {
// // output stream must be closed anyway...
// if (output != null)
// try {
// output.close();
// } catch (IOException e) {
// logger.error(e.getLocalizedMessage());
// }
// }
// }
private String generateFileName() {
long now = System.currentTimeMillis();

View File

@ -0,0 +1,148 @@
package org.gcube.nlphub.workspace;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.servlet.http.Part;
import org.apache.log4j.Logger;
import org.gcube.nlphub.NLPUploader;
import org.gcube.nlphub.legacy.Constants;
import org.gcube.nlphub.legacy.NlpHubException;
public class WorkspaceManager {
private Logger logger = Logger.getLogger(WorkspaceManager.class.getSimpleName());
public String getPublicLink(String fileName, String token) throws NlpHubException {
try {
String link = "";
String user = authorizationService().get(token).getClientInfo().getId();
String wsRoot = "/Home/" + user + "/Workspace/";
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
String uri = webapp + "/rest/GetPublicLink?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8")
+ "&shortUrl=false";
URL url = new URL(uri);
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);
}
String xmlOut = response.toString();
String begin = "<string>";
String end = "</string>";
int b = xmlOut.indexOf(begin);
int e = xmlOut.indexOf(end);
if (xmlOut.contains("Exception") || (e < 0) || (b < 0)) {
String message = "Invalid link: " + URLEncoder.encode(xmlOut, "UTF-8");
logger.error(message);
throw new NlpHubException(message, null);
}
link = xmlOut.substring(b + begin.length(), e);
return link;
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
}
}
public void deleteFile(String fileName, String token) throws NlpHubException {
try {
String user = authorizationService().get(token).getClientInfo().getId();
String wsRoot = "/Home/" + user + "/Workspace/";
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
String uri = webapp + "/rest/Delete?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8");
URL url = new URL(uri);
// System.out.println(uri);
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);
}
String xmlOut = response.toString();
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
}
}
public boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
OutputStream output = null;
try {
String user = authorizationService().get(token).getClientInfo().getId();
String wsRoot = "/Home/" + user + "/Workspace/";
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
String uri = webapp + "/rest/Upload?name=" + URLEncoder.encode(name, "UTF-8") + "&description="
+ URLEncoder.encode(description, "UTF-8") + "&parentPath=" + URLEncoder.encode(wsRoot, "UTF-8");
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty(Constants.CONTENT_TYPE, Constants.MIME_TEXT);
connection.setRequestMethod("POST");
output = connection.getOutputStream();
output.write(in);
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = r.readLine()) != null) {
response.append(inputLine);
}
String xmlOut = response.toString();
if (xmlOut.contains("Exception"))
return false;
return true;
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
throw new NlpHubException(e.getLocalizedMessage(), e);
} finally {
// output stream must be closed anyway...
if (output != null)
try {
output.close();
} catch (IOException e) {
logger.error(e.getLocalizedMessage());
}
}
}
}