parent
d9dfa5a06d
commit
688a3183cd
@ -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>
|
@ -1,147 +1,158 @@
|
||||
package org.gcube.data.analysis.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.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
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.shared.Constants;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class WorkspaceManager {
|
||||
private static final Logger logger = LoggerFactory.getLogger(WorkspaceManager.class);
|
||||
|
||||
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);
|
||||
}
|
||||
public String getPublicLink(String fileName) throws NlpHubException {
|
||||
|
||||
String xmlOut = response.toString();
|
||||
if (fileName == null || fileName.isEmpty()) {
|
||||
String error = "Error in get public link, the filename requested is invalid: Null";
|
||||
logger.error(error);
|
||||
throw new NlpHubException(error);
|
||||
}
|
||||
|
||||
String begin = "<string>";
|
||||
String end = "</string>";
|
||||
int b = xmlOut.indexOf(begin);
|
||||
int e = xmlOut.indexOf(end);
|
||||
StorageHubClient shc = new StorageHubClient();
|
||||
FolderContainer rootContainer = shc.getWSRoot();
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
link = xmlOut.substring(b + begin.length(), e);
|
||||
return link;
|
||||
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);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error in WorkspaceManager for getPublicLink: "+e.getLocalizedMessage(), e);
|
||||
throw new NlpHubException("Error in WorkspaceManager for getPublicLink: "+e.getLocalizedMessage(), 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());
|
||||
URL url;
|
||||
try {
|
||||
url = shc.open(item.getId()).asFile().getPublicLink();
|
||||
} catch (StorageHubException 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, 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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
String xmlOut = response.toString();
|
||||
logger.debug("deleteFile: " + xmlOut);
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
throw new NlpHubException(e.getLocalizedMessage(), e);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
|
||||
OutputStream output = null;
|
||||
List<? extends Item> items;
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
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 {
|
||||
output.close();
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getLocalizedMessage(), e);
|
||||
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