parent
d9dfa5a06d
commit
688a3183cd
|
@ -1,5 +1,5 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||||
<wb-module deploy-name="nlphub-1.0.1-SNAPSHOT">
|
<wb-module deploy-name="nlphub-1.1.0-SNAPSHOT">
|
||||||
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
|
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
|
||||||
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
||||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
|
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
<root>
|
||||||
|
<facet id="jst.jaxrs">
|
||||||
|
<node name="libprov">
|
||||||
|
<attribute name="provider-id" value="jaxrs-no-op-library-provider"/>
|
||||||
|
</node>
|
||||||
|
</facet>
|
||||||
|
</root>
|
|
@ -4,4 +4,5 @@
|
||||||
<installed facet="wst.jsdt.web" version="1.0"/>
|
<installed facet="wst.jsdt.web" version="1.0"/>
|
||||||
<installed facet="java" version="1.8"/>
|
<installed facet="java" version="1.8"/>
|
||||||
<installed facet="jst.web" version="3.0"/>
|
<installed facet="jst.web" version="3.0"/>
|
||||||
|
<installed facet="jst.jaxrs" version="2.0"/>
|
||||||
</faceted-project>
|
</faceted-project>
|
||||||
|
|
11
CHANGELOG.md
11
CHANGELOG.md
|
@ -2,8 +2,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||||
|
|
||||||
# Changelog for "nlphub"
|
# Changelog for "nlphub"
|
||||||
|
|
||||||
|
## [v1.1.0] - 2020-09-08
|
||||||
|
|
||||||
## [v1-0-1] - 2019-06-18
|
### Features
|
||||||
|
|
||||||
|
- Updated to StorageHub [#21970]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## [v1.0.1] - 2019-06-18
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
|
@ -11,7 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## [v1-0-0] - 2019-02-25
|
## [v1.0.0] - 2019-02-25
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
|
|
13
pom.xml
13
pom.xml
|
@ -12,7 +12,7 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.gcube.data.analysis</groupId>
|
<groupId>org.gcube.data.analysis</groupId>
|
||||||
<artifactId>nlphub</artifactId>
|
<artifactId>nlphub</artifactId>
|
||||||
<version>1.0.1-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<scm>
|
<scm>
|
||||||
|
@ -50,7 +50,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.distribution</groupId>
|
<groupId>org.gcube.distribution</groupId>
|
||||||
<artifactId>maven-portal-bom</artifactId>
|
<artifactId>maven-portal-bom</artifactId>
|
||||||
<version>3.6.0</version>
|
<version>3.6.3</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
@ -129,7 +129,13 @@
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- StorageHub -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.common</groupId>
|
||||||
|
<artifactId>storagehub-client-library</artifactId>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- LOGGER -->
|
<!-- LOGGER -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
|
@ -142,6 +148,7 @@
|
||||||
<artifactId>slf4j-log4j12</artifactId>
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- JUnit TEST -->
|
<!-- JUnit TEST -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|
|
@ -50,7 +50,8 @@ public class DiscoverDataMinerService extends HttpServlet {
|
||||||
|
|
||||||
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
logger.debug("Discover DataMinerService");
|
logger.debug("Discover DataMinerService");
|
||||||
String token = SessionUtils.getToken(request);
|
SessionUtils sessionUtils=new SessionUtils();
|
||||||
|
String token = sessionUtils.getToken(request);
|
||||||
|
|
||||||
discoverDataMinerService(request, response, token);
|
discoverDataMinerService(request, response, token);
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,8 @@ 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 {
|
||||||
logger.debug("NLPHub");
|
logger.debug("NLPHub");
|
||||||
String token = SessionUtils.getToken(request);
|
SessionUtils sessionUtils=new SessionUtils();
|
||||||
|
String token = sessionUtils.getToken(request);
|
||||||
|
|
||||||
if (request.getParameter("getInfo") != null) {
|
if (request.getParameter("getInfo") != null) {
|
||||||
getAlgorithmInfo(request, response, token);
|
getAlgorithmInfo(request, response, token);
|
||||||
|
|
|
@ -41,7 +41,8 @@ public class NLPInit extends HttpServlet {
|
||||||
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
|
||||||
logger.debug("NLPInit");
|
logger.debug("NLPInit");
|
||||||
String token = SessionUtils.getToken(request);
|
SessionUtils sessionUtils=new SessionUtils();
|
||||||
|
String token = sessionUtils.getToken(request);
|
||||||
String res = request.getParameter(Constants.REQUEST);
|
String res = request.getParameter(Constants.REQUEST);
|
||||||
RequestType r=RequestType.getRequestTypeFromString(res);
|
RequestType r=RequestType.getRequestTypeFromString(res);
|
||||||
if(r==null){
|
if(r==null){
|
||||||
|
|
|
@ -59,7 +59,8 @@ 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");
|
logger.debug("NLPMapper");
|
||||||
String token = SessionUtils.getToken(request);
|
SessionUtils sessionUtils=new SessionUtils();
|
||||||
|
String token = sessionUtils.getToken(request);
|
||||||
WorkspaceManager ws = new WorkspaceManager();
|
WorkspaceManager ws = new WorkspaceManager();
|
||||||
|
|
||||||
response.setContentType("application/json");
|
response.setContentType("application/json");
|
||||||
|
@ -154,14 +155,14 @@ public class NLPMapper extends HttpServlet {
|
||||||
private String saveResult(String jsonResult, String token, WorkspaceManager ws) throws NlpHubException {
|
private String saveResult(String jsonResult, String token, WorkspaceManager ws) throws NlpHubException {
|
||||||
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);
|
||||||
|
|
||||||
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)) {
|
||||||
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);
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
@ -62,7 +62,8 @@ public class NLPUploader extends HttpServlet {
|
||||||
|
|
||||||
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
logger.debug("NLPUploader");
|
logger.debug("NLPUploader");
|
||||||
String token = SessionUtils.getToken(request);
|
SessionUtils sessionUtils=new SessionUtils();
|
||||||
|
String token = sessionUtils.getToken(request);
|
||||||
WorkspaceManager ws = new WorkspaceManager();
|
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)
|
||||||
|
@ -85,12 +86,12 @@ public class NLPUploader extends HttpServlet {
|
||||||
String fileName = generateFileName();
|
String fileName = generateFileName();
|
||||||
PrintWriter writer = response.getWriter();
|
PrintWriter writer = response.getWriter();
|
||||||
try {
|
try {
|
||||||
if (!ws.uploadFile(content, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
|
if (!ws.uploadFile(content, fileName, Constants.DEFAULT_DESCRIPTION)) {
|
||||||
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 = ws.getPublicLink(fileName, token);
|
String link = ws.getPublicLink(fileName);
|
||||||
if (langParameter != null) {
|
if (langParameter != null) {
|
||||||
String sentence = NlpUtils.getLanguageRecognizerDigest(new String(content));
|
String sentence = NlpUtils.getLanguageRecognizerDigest(new String(content));
|
||||||
logger.info(sentence);
|
logger.info(sentence);
|
||||||
|
@ -143,15 +144,15 @@ public class NLPUploader extends HttpServlet {
|
||||||
String stringContent = new String(bufferedContent);
|
String stringContent = new String(bufferedContent);
|
||||||
stringContent = NlpUtils.replaceDirtyCharacters(stringContent);
|
stringContent = NlpUtils.replaceDirtyCharacters(stringContent);
|
||||||
|
|
||||||
ws.deleteFile(fileName, token);
|
ws.deleteFile(fileName);
|
||||||
|
|
||||||
if (!ws.uploadFile(stringContent.getBytes(), fileName, Constants.DEFAULT_DESCRIPTION, token)) {
|
if (!ws.uploadFile(stringContent.getBytes(), fileName, Constants.DEFAULT_DESCRIPTION)) {
|
||||||
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 = ws.getPublicLink(fileName, token);
|
String link = ws.getPublicLink(fileName);
|
||||||
String sentence = NlpUtils.getLanguageRecognizerDigest(stringContent);
|
String sentence = NlpUtils.getLanguageRecognizerDigest(stringContent);
|
||||||
logger.info(sentence);
|
logger.info(sentence);
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -4,6 +4,9 @@ public class NlpHubException extends Exception {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public NlpHubException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
public NlpHubException(String message, Throwable throwable) {
|
public NlpHubException(String message, Throwable throwable) {
|
||||||
super(message, throwable);
|
super(message, throwable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,12 @@ package org.gcube.data.analysis.nlphub.session;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||||
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||||
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.authorization.library.utils.Caller;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.data.analysis.nlphub.shared.Constants;
|
import org.gcube.data.analysis.nlphub.shared.Constants;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -10,37 +16,50 @@ import org.slf4j.LoggerFactory;
|
||||||
public class SessionUtils {
|
public class SessionUtils {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SessionUtils.class);
|
private static final Logger logger = LoggerFactory.getLogger(SessionUtils.class);
|
||||||
|
|
||||||
public static String getToken(HttpServletRequest request) throws ServletException {
|
public String getToken(HttpServletRequest request) throws ServletException {
|
||||||
String token = request.getParameter(Constants.TOKEN_PARAMETER);
|
String token = request.getParameter(Constants.TOKEN_PARAMETER);
|
||||||
logger.debug("Token in request: " + token);
|
logger.debug("Token in request: " + token);
|
||||||
if (token == null || token.isEmpty()) {
|
if (token == null || token.isEmpty()) {
|
||||||
logger.debug("Token is null");
|
logger.debug("Token is null");
|
||||||
throw new ServletException("Token is null");
|
throw new ServletException("Token is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setAuth(token);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
private void setAuth(String token) throws ServletException {
|
||||||
private static void checkToken(String token) throws Exception {
|
try {
|
||||||
if (token == null || token.isEmpty()) {
|
SecurityTokenProvider.instance.set(token);
|
||||||
logger.error("Token is null");
|
AuthorizationEntry authorizationEntry = org.gcube.common.authorization.client.Constants
|
||||||
throw new Exception("Token is null");
|
.authorizationService().get(token);
|
||||||
} else {
|
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||||
AuthorizationEntry entry;
|
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
|
||||||
|
String qualifier = authorizationEntry.getQualifier();
|
||||||
entry = authorizationService().get(token);
|
Caller caller = new Caller(clientInfo, qualifier);
|
||||||
ClientInfo clientInfo = entry.getClientInfo();
|
AuthorizationProvider.instance.set(caller);
|
||||||
if (clientInfo == null) {
|
ScopeProvider.instance.set(authorizationEntry.getContext());
|
||||||
logger.error("User not found.");
|
} catch (Exception e) {
|
||||||
throw new Exception("Authorization failed!");
|
logger.error("Error in set context: " + e.getLocalizedMessage(), e);
|
||||||
} else {
|
throw new ServletException("Error in set context: " + e.getLocalizedMessage(), e);
|
||||||
logger.debug("User: " + clientInfo.getId());
|
|
||||||
String context = entry.getContext();
|
|
||||||
logger.debug("Context: " + context);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}*/
|
/*
|
||||||
|
* private static void checkToken(String token) throws Exception { if (token
|
||||||
|
* == null || token.isEmpty()) { logger.error("Token is null"); throw new
|
||||||
|
* Exception("Token is null"); } else { AuthorizationEntry entry;
|
||||||
|
*
|
||||||
|
* entry = authorizationService().get(token); ClientInfo clientInfo =
|
||||||
|
* entry.getClientInfo(); if (clientInfo == null) {
|
||||||
|
* logger.error("User not found."); throw new
|
||||||
|
* Exception("Authorization failed!"); } else { logger.debug("User: " +
|
||||||
|
* clientInfo.getId()); String context = entry.getContext();
|
||||||
|
* logger.debug("Context: " + context); }
|
||||||
|
*
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,147 +1,158 @@
|
||||||
package org.gcube.data.analysis.nlphub.workspace;
|
package org.gcube.data.analysis.nlphub.workspace;
|
||||||
|
|
||||||
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
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.URL;
|
||||||
import java.net.URLEncoder;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
||||||
|
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
||||||
|
import org.gcube.common.storagehub.client.dsl.ItemContainer;
|
||||||
|
import org.gcube.common.storagehub.client.dsl.ListResolver;
|
||||||
|
import org.gcube.common.storagehub.client.dsl.OpenResolver;
|
||||||
|
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
||||||
|
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
||||||
|
import org.gcube.common.storagehub.model.items.Item;
|
||||||
import org.gcube.data.analysis.nlphub.legacy.NlpHubException;
|
import org.gcube.data.analysis.nlphub.legacy.NlpHubException;
|
||||||
import org.gcube.data.analysis.nlphub.shared.Constants;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class WorkspaceManager {
|
public class WorkspaceManager {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(WorkspaceManager.class);
|
private static final Logger logger = LoggerFactory.getLogger(WorkspaceManager.class);
|
||||||
|
|
||||||
public String getPublicLink(String fileName, String token) throws NlpHubException {
|
public String getPublicLink(String fileName) 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();
|
if (fileName == null || fileName.isEmpty()) {
|
||||||
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
String error = "Error in get public link, the filename requested is invalid: Null";
|
||||||
connection.setDoInput(true);
|
logger.error(error);
|
||||||
connection.setDoOutput(true);
|
throw new NlpHubException(error);
|
||||||
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("Error in WorkspaceManager for getPublicLink: "+e.getLocalizedMessage(), e);
|
|
||||||
throw new NlpHubException("Error in WorkspaceManager for getPublicLink: "+e.getLocalizedMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteFile(String fileName, String token) throws NlpHubException {
|
StorageHubClient shc = new StorageHubClient();
|
||||||
|
FolderContainer rootContainer = shc.getWSRoot();
|
||||||
|
|
||||||
|
ListResolver listResolver;
|
||||||
try {
|
try {
|
||||||
String user = authorizationService().get(token).getClientInfo().getId();
|
listResolver = rootContainer.findByName(fileName);
|
||||||
String wsRoot = "/Home/" + user + "/Workspace/";
|
} catch (StorageHubException e) {
|
||||||
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
String error = "Error retrieving SH Id for file " + fileName + " : " + e.getLocalizedMessage();
|
||||||
String uri = webapp + "/rest/Delete?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8");
|
logger.error(error, e);
|
||||||
URL url = new URL(uri);
|
throw new NlpHubException(error, e);
|
||||||
|
|
||||||
// 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();
|
|
||||||
logger.debug("deleteFile: " + xmlOut);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error(e.getLocalizedMessage(), e);
|
|
||||||
throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
|
List<? extends Item> items;
|
||||||
OutputStream output = null;
|
|
||||||
try {
|
try {
|
||||||
String user = authorizationService().get(token).getClientInfo().getId();
|
items = listResolver.getItems();
|
||||||
String wsRoot = "/Home/" + user + "/Workspace/";
|
} catch (StorageHubException e) {
|
||||||
String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
String error = "Error retrieving SH Id for file " + fileName + " : " + e.getLocalizedMessage();
|
||||||
String uri = webapp + "/rest/Upload?name=" + URLEncoder.encode(name, "UTF-8") + "&description="
|
logger.error(error, e);
|
||||||
+ URLEncoder.encode(description, "UTF-8") + "&parentPath=" + URLEncoder.encode(wsRoot, "UTF-8");
|
throw new NlpHubException(error, e);
|
||||||
URL url = new URL(uri);
|
}
|
||||||
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
if (items == null || items.isEmpty()) {
|
||||||
connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
String error = "Error file not found in workspace: " + fileName;
|
||||||
connection.setDoInput(true);
|
logger.error(error);
|
||||||
connection.setDoOutput(true);
|
throw new NlpHubException(error);
|
||||||
connection.setUseCaches(false);
|
} else {
|
||||||
connection.setRequestProperty(Constants.CONTENT_TYPE, Constants.MIME_TEXT);
|
Item item = items.get(0);
|
||||||
connection.setRequestMethod("POST");
|
logger.debug("Item: " + item);
|
||||||
output = connection.getOutputStream();
|
if (item != null) {
|
||||||
output.write(in);
|
logger.debug("Item Id=" + item.getId());
|
||||||
|
URL url;
|
||||||
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(), e);
|
|
||||||
throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
||||||
} finally {
|
|
||||||
// output stream must be closed anyway...
|
|
||||||
if (output != null)
|
|
||||||
try {
|
try {
|
||||||
output.close();
|
url = shc.open(item.getId()).asFile().getPublicLink();
|
||||||
} catch (IOException e) {
|
} catch (StorageHubException e) {
|
||||||
logger.error(e.getLocalizedMessage(), e);
|
String error = "Error in get public link for file " + fileName + " : " + e.getLocalizedMessage();
|
||||||
|
logger.error(error, e);
|
||||||
|
throw new NlpHubException(error, e);
|
||||||
}
|
}
|
||||||
|
logger.debug("getPublicLink: " + url);
|
||||||
|
return url.toString();
|
||||||
|
} else {
|
||||||
|
String error = "Error file not found in workspace: " + fileName;
|
||||||
|
logger.error(error);
|
||||||
|
throw new NlpHubException(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteFile(String fileName) throws NlpHubException {
|
||||||
|
if (fileName == null || fileName.isEmpty()) {
|
||||||
|
String error = "Error in delete file, the filename requested is invalid: Null";
|
||||||
|
logger.error(error);
|
||||||
|
throw new NlpHubException(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
StorageHubClient shc = new StorageHubClient();
|
||||||
|
FolderContainer rootContainer = shc.getWSRoot();
|
||||||
|
|
||||||
|
ListResolver listResolver;
|
||||||
|
try {
|
||||||
|
listResolver = rootContainer.findByName(fileName);
|
||||||
|
} catch (StorageHubException e) {
|
||||||
|
String error = "Error retrieving SH Id for file " + fileName + " : " + e.getLocalizedMessage();
|
||||||
|
logger.error(error, e);
|
||||||
|
throw new NlpHubException(error, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<? extends Item> items;
|
||||||
|
try {
|
||||||
|
items = listResolver.getItems();
|
||||||
|
} catch (StorageHubException e) {
|
||||||
|
String error = "Error retrieving SH Id for file " + fileName + " : " + e.getLocalizedMessage();
|
||||||
|
logger.error(error, e);
|
||||||
|
throw new NlpHubException(error, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (items == null || items.isEmpty()) {
|
||||||
|
String error = "Error file not found in workspace: " + fileName;
|
||||||
|
logger.error(error);
|
||||||
|
throw new NlpHubException(error);
|
||||||
|
} else {
|
||||||
|
Item item = items.get(0);
|
||||||
|
logger.debug("Item: " + item);
|
||||||
|
if (item != null) {
|
||||||
|
logger.debug("Item Id=" + item.getId());
|
||||||
|
try {
|
||||||
|
OpenResolver openResolver = shc.open(item.getId());
|
||||||
|
ItemContainer<Item> itemContainer = openResolver.asItem();
|
||||||
|
itemContainer.delete();
|
||||||
|
} catch (StorageHubException e) {
|
||||||
|
String error = "Error in delete operation for file " + fileName + " : " + e.getLocalizedMessage();
|
||||||
|
logger.error(error, e);
|
||||||
|
throw new NlpHubException(error, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
String error = "Error file not found in workspace: " + fileName;
|
||||||
|
logger.error(error);
|
||||||
|
throw new NlpHubException(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean uploadFile(byte[] in, String name, String description) throws NlpHubException {
|
||||||
|
if (name == null || name.isEmpty()) {
|
||||||
|
String error = "Error uploading the file, the filename is invalid: Null";
|
||||||
|
logger.error(error);
|
||||||
|
throw new NlpHubException(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
StorageHubClient shc = new StorageHubClient();
|
||||||
|
FolderContainer rootContainer = shc.getWSRoot();
|
||||||
|
InputStream inputStream = new ByteArrayInputStream(in);
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileContainer fileContainer = rootContainer.uploadFile(inputStream,
|
||||||
|
name,description);
|
||||||
|
logger.debug("Item uploaded: " + fileContainer.get().getId());
|
||||||
|
} catch (StorageHubException e) {
|
||||||
|
String error = "Error uploading the file " + name+ " : " + e.getLocalizedMessage();
|
||||||
|
logger.error(error, e);
|
||||||
|
throw new NlpHubException(error, e);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue