git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/nlphub@164541 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
3b1f20d761
commit
75a08add56
|
@ -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.FileParameter;
|
||||||
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ObjectParameter;
|
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ObjectParameter;
|
||||||
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.Parameter;
|
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.DataminerClient;
|
||||||
import org.gcube.nlphub.legacy.JsonManager;
|
import org.gcube.nlphub.legacy.JsonManager;
|
||||||
//import org.gcube.dataminerclient.DataminerClient;
|
//import org.gcube.dataminerclient.DataminerClient;
|
||||||
|
@ -37,6 +38,7 @@ public class NLPHub extends HttpServlet {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private String service = "http://dataminer-prototypes.d4science.org/wps/";
|
private String service = "http://dataminer-prototypes.d4science.org/wps/";
|
||||||
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
|
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
|
||||||
|
private boolean devMode = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see HttpServlet#HttpServlet()
|
* @see HttpServlet#HttpServlet()
|
||||||
|
@ -65,6 +67,7 @@ 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 {
|
||||||
|
token = Constants.getToken(request, devMode);
|
||||||
try {
|
try {
|
||||||
System.out.println("annotations: " + request.getParameter("annotations"));
|
System.out.println("annotations: " + request.getParameter("annotations"));
|
||||||
System.out.println("lang: " + request.getParameter("lang"));
|
System.out.println("lang: " + request.getParameter("lang"));
|
||||||
|
|
|
@ -7,6 +7,7 @@ import java.io.PrintWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@ -18,8 +19,11 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.gcube.nlphub.nlp.NlpNerRunner;
|
import org.gcube.nlphub.nlp.NlpNerRunner;
|
||||||
|
import org.gcube.nlphub.workspace.WorkspaceManager;
|
||||||
import org.gcube.nlphub.mapper.JsonMapper;
|
import org.gcube.nlphub.mapper.JsonMapper;
|
||||||
|
import org.gcube.nlphub.legacy.Constants;
|
||||||
import org.gcube.nlphub.legacy.JsonManager;
|
import org.gcube.nlphub.legacy.JsonManager;
|
||||||
|
import org.gcube.nlphub.legacy.NlpHubException;
|
||||||
import org.gcube.nlphub.mapper.DefaultMapper;
|
import org.gcube.nlphub.mapper.DefaultMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,7 +33,9 @@ import org.gcube.nlphub.mapper.DefaultMapper;
|
||||||
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 Logger logger = Logger.getLogger(NLPMapper.class.getSimpleName());
|
||||||
|
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
|
||||||
|
private boolean devMode = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see HttpServlet#HttpServlet()
|
* @see HttpServlet#HttpServlet()
|
||||||
*/
|
*/
|
||||||
|
@ -56,11 +62,9 @@ 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 {
|
||||||
// response.getWriter().append("Served at:
|
token = Constants.getToken(request, devMode);
|
||||||
// ").append(request.getContextPath());
|
|
||||||
String documentLink = request.getParameter("plink"); // link al testo
|
String documentLink = request.getParameter("plink"); // link to text file (workspace)
|
||||||
// (sul
|
|
||||||
// workspace)
|
|
||||||
String toBeMap = request.getParameter("tobemap");
|
String toBeMap = request.getParameter("tobemap");
|
||||||
System.out.println("tobemap: " + toBeMap);
|
System.out.println("tobemap: " + toBeMap);
|
||||||
String[] tokens;
|
String[] tokens;
|
||||||
|
@ -99,7 +103,13 @@ public class NLPMapper extends HttpServlet {
|
||||||
}
|
}
|
||||||
out += "]}";
|
out += "]}";
|
||||||
|
|
||||||
writer.append(out);
|
|
||||||
|
String resultLink = saveResult(out);
|
||||||
|
String outResult = "{";
|
||||||
|
outResult += "\"link\":\"" + resultLink + "\",";
|
||||||
|
outResult += "\"output\":" + out;
|
||||||
|
outResult += "}";
|
||||||
|
writer.append(outResult);
|
||||||
|
|
||||||
} catch (Exception x) {
|
} catch (Exception x) {
|
||||||
logger.error(x.getLocalizedMessage());
|
logger.error(x.getLocalizedMessage());
|
||||||
|
@ -129,4 +139,19 @@ public class NLPMapper extends HttpServlet {
|
||||||
}
|
}
|
||||||
return content;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import org.apache.log4j.Logger;
|
||||||
import org.gcube.nlphub.legacy.Constants;
|
import org.gcube.nlphub.legacy.Constants;
|
||||||
import org.gcube.nlphub.legacy.JsonManager;
|
import org.gcube.nlphub.legacy.JsonManager;
|
||||||
import org.gcube.nlphub.legacy.NlpHubException;
|
import org.gcube.nlphub.legacy.NlpHubException;
|
||||||
|
import org.gcube.nlphub.workspace.WorkspaceManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Servlet implementation class NLPUploader
|
* Servlet implementation class NLPUploader
|
||||||
|
@ -36,7 +37,8 @@ public class NLPUploader extends HttpServlet {
|
||||||
private Logger logger = Logger.getLogger(NLPUploader.class.getSimpleName());
|
private Logger logger = Logger.getLogger(NLPUploader.class.getSimpleName());
|
||||||
private boolean devMode = true;
|
private boolean devMode = true;
|
||||||
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
|
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
|
||||||
|
private WorkspaceManager ws;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see HttpServlet#HttpServlet()
|
* @see HttpServlet#HttpServlet()
|
||||||
*/
|
*/
|
||||||
|
@ -60,10 +62,12 @@ 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();
|
||||||
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);
|
||||||
|
@ -79,12 +83,12 @@ public class NLPUploader extends HttpServlet {
|
||||||
String fileName = generateFileName();
|
String fileName = generateFileName();
|
||||||
PrintWriter writer = response.getWriter();
|
PrintWriter writer = response.getWriter();
|
||||||
try {
|
try {
|
||||||
if (!uploadFile(content, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
|
if (!ws.uploadFile(content, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
|
||||||
writer.println(new JsonManager().getErrorJsonResponse(
|
writer.println(new JsonManager().getErrorJsonResponse(
|
||||||
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
|
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String link = getPublicLink(fileName, token);
|
String link = ws.getPublicLink(fileName, token);
|
||||||
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
|
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
|
||||||
} catch (Exception x) {
|
} catch (Exception x) {
|
||||||
x.printStackTrace();
|
x.printStackTrace();
|
||||||
|
@ -124,15 +128,15 @@ public class NLPUploader extends HttpServlet {
|
||||||
} else
|
} else
|
||||||
bufferedContent = buffer;
|
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(
|
writer.println(new JsonManager().getErrorJsonResponse(
|
||||||
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
|
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String link = getPublicLink(fileName, token);
|
String link = ws.getPublicLink(fileName, token);
|
||||||
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
|
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
|
||||||
} catch (Exception x) {
|
} catch (Exception x) {
|
||||||
x.printStackTrace();
|
x.printStackTrace();
|
||||||
|
@ -153,138 +157,138 @@ public class NLPUploader extends HttpServlet {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getPublicLink(String fileName, String token) throws NlpHubException {
|
// private String getPublicLink(String fileName, String token) throws NlpHubException {
|
||||||
try {
|
// try {
|
||||||
String link = "";
|
// String link = "";
|
||||||
String user = authorizationService().get(token).getClientInfo().getId();
|
// String user = authorizationService().get(token).getClientInfo().getId();
|
||||||
String wsRoot = "/Home/" + user + "/Workspace/";
|
// String wsRoot = "/Home/" + user + "/Workspace/";
|
||||||
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
||||||
String uri = webapp + "/rest/GetPublicLink?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8")
|
// String uri = webapp + "/rest/GetPublicLink?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8")
|
||||||
+ "&shortUrl=false";
|
// + "&shortUrl=false";
|
||||||
URL url = new URL(uri);
|
// URL url = new URL(uri);
|
||||||
|
//
|
||||||
// System.out.println(uri);
|
// // System.out.println(uri);
|
||||||
|
//
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
// connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
||||||
connection.setDoInput(true);
|
// 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()));
|
// BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
//
|
||||||
StringBuffer response = new StringBuffer();
|
// StringBuffer response = new StringBuffer();
|
||||||
String inputLine;
|
// String inputLine;
|
||||||
while ((inputLine = r.readLine()) != null) {
|
// while ((inputLine = r.readLine()) != null) {
|
||||||
response.append(inputLine);
|
// response.append(inputLine);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
String xmlOut = response.toString();
|
// String xmlOut = response.toString();
|
||||||
// System.out.println("xmlOut: " + xmlOut);
|
// // System.out.println("xmlOut: " + xmlOut);
|
||||||
|
//
|
||||||
String begin = "<string>";
|
// String begin = "<string>";
|
||||||
String end = "</string>";
|
// String end = "</string>";
|
||||||
int b = xmlOut.indexOf(begin);
|
// int b = xmlOut.indexOf(begin);
|
||||||
int e = xmlOut.indexOf(end);
|
// int e = xmlOut.indexOf(end);
|
||||||
|
//
|
||||||
if (xmlOut.contains("Exception") || (e < 0) || (b < 0)) {
|
// if (xmlOut.contains("Exception") || (e < 0) || (b < 0)) {
|
||||||
String message = "Invalid link: " + URLEncoder.encode(xmlOut, "UTF-8");
|
// String message = "Invalid link: " + URLEncoder.encode(xmlOut, "UTF-8");
|
||||||
logger.error(message);
|
// logger.error(message);
|
||||||
throw new NlpHubException(message, null);
|
// throw new NlpHubException(message, null);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
link = xmlOut.substring(b + begin.length(), e);
|
// link = xmlOut.substring(b + begin.length(), e);
|
||||||
return link;
|
// return link;
|
||||||
|
//
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
logger.error(e.getLocalizedMessage());
|
// logger.error(e.getLocalizedMessage());
|
||||||
throw new NlpHubException(e.getLocalizedMessage(), e);
|
// throw new NlpHubException(e.getLocalizedMessage(), e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private void deleteFile(String fileName, String token) throws NlpHubException {
|
// private void deleteFile(String fileName, String token) throws NlpHubException {
|
||||||
try {
|
// try {
|
||||||
String user = authorizationService().get(token).getClientInfo().getId();
|
// String user = authorizationService().get(token).getClientInfo().getId();
|
||||||
String wsRoot = "/Home/" + user + "/Workspace/";
|
// String wsRoot = "/Home/" + user + "/Workspace/";
|
||||||
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
||||||
String uri = webapp + "/rest/Delete?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8");
|
// String uri = webapp + "/rest/Delete?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8");
|
||||||
URL url = new URL(uri);
|
// URL url = new URL(uri);
|
||||||
|
//
|
||||||
// System.out.println(uri);
|
// // System.out.println(uri);
|
||||||
|
//
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
// connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
||||||
connection.setDoInput(true);
|
// 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()));
|
// BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
//
|
||||||
StringBuffer response = new StringBuffer();
|
// StringBuffer response = new StringBuffer();
|
||||||
String inputLine;
|
// String inputLine;
|
||||||
while ((inputLine = r.readLine()) != null) {
|
// while ((inputLine = r.readLine()) != null) {
|
||||||
response.append(inputLine);
|
// response.append(inputLine);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
String xmlOut = response.toString();
|
// String xmlOut = response.toString();
|
||||||
// System.out.println(xmlOut);
|
// // System.out.println(xmlOut);
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
logger.error(e.getLocalizedMessage());
|
// logger.error(e.getLocalizedMessage());
|
||||||
throw new NlpHubException(e.getLocalizedMessage(), e);
|
// throw new NlpHubException(e.getLocalizedMessage(), e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
|
// private boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
|
||||||
OutputStream output = null;
|
// OutputStream output = null;
|
||||||
try {
|
// try {
|
||||||
String user = authorizationService().get(token).getClientInfo().getId();
|
// String user = authorizationService().get(token).getClientInfo().getId();
|
||||||
String wsRoot = "/Home/" + user + "/Workspace/";
|
// String wsRoot = "/Home/" + user + "/Workspace/";
|
||||||
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
||||||
String uri = webapp + "/rest/Upload?name=" + URLEncoder.encode(name, "UTF-8") + "&description="
|
// String uri = webapp + "/rest/Upload?name=" + URLEncoder.encode(name, "UTF-8") + "&description="
|
||||||
+ URLEncoder.encode(description, "UTF-8") + "&parentPath=" + URLEncoder.encode(wsRoot, "UTF-8");
|
// + URLEncoder.encode(description, "UTF-8") + "&parentPath=" + URLEncoder.encode(wsRoot, "UTF-8");
|
||||||
URL url = new URL(uri);
|
// URL url = new URL(uri);
|
||||||
|
//
|
||||||
// System.out.println(uri);
|
// // System.out.println(uri);
|
||||||
|
//
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
// connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
||||||
connection.setDoInput(true);
|
// connection.setDoInput(true);
|
||||||
connection.setDoOutput(true);
|
// connection.setDoOutput(true);
|
||||||
connection.setUseCaches(false);
|
// connection.setUseCaches(false);
|
||||||
connection.setRequestProperty(Constants.CONTENT_TYPE, Constants.MIME_TEXT);
|
// connection.setRequestProperty(Constants.CONTENT_TYPE, Constants.MIME_TEXT);
|
||||||
connection.setRequestMethod("POST");
|
// connection.setRequestMethod("POST");
|
||||||
output = connection.getOutputStream();
|
// output = connection.getOutputStream();
|
||||||
output.write(in);
|
// output.write(in);
|
||||||
|
//
|
||||||
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
// BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
//
|
||||||
StringBuffer response = new StringBuffer();
|
// StringBuffer response = new StringBuffer();
|
||||||
String inputLine;
|
// String inputLine;
|
||||||
while ((inputLine = r.readLine()) != null) {
|
// while ((inputLine = r.readLine()) != null) {
|
||||||
response.append(inputLine);
|
// response.append(inputLine);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
String xmlOut = response.toString();
|
// String xmlOut = response.toString();
|
||||||
// System.out.println(xmlOut);
|
// // System.out.println(xmlOut);
|
||||||
if (xmlOut.contains("Exception"))
|
// if (xmlOut.contains("Exception"))
|
||||||
return false;
|
// return false;
|
||||||
return true;
|
// return true;
|
||||||
|
//
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
logger.error(e.getLocalizedMessage());
|
// logger.error(e.getLocalizedMessage());
|
||||||
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...
|
||||||
if (output != null)
|
// if (output != null)
|
||||||
try {
|
// try {
|
||||||
output.close();
|
// output.close();
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
logger.error(e.getLocalizedMessage());
|
// logger.error(e.getLocalizedMessage());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
private String generateFileName() {
|
private String generateFileName() {
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue