ref 11724: SAI - Update to StorageHUB

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

Updated to StorageHub 

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/statistical-algorithms-importer@171899 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2018-10-03 17:46:01 +00:00
parent 37f4e4d69e
commit affcf404e3
12 changed files with 325 additions and 335 deletions

View File

@ -89,4 +89,6 @@ public interface StatAlgoImporterService extends RemoteService {
public void repackageSoftware() throws StatAlgoImporterServiceException;
public ItemDescription getItemDescription(String itemId) throws StatAlgoImporterServiceException;
}

View File

@ -68,7 +68,8 @@ public interface StatAlgoImporterServiceAsync {
void getDeployOperationLogs(String operationId, AsyncCallback<String> callback);
void getItemDescription(String itemId, AsyncCallback<ItemDescription> asyncCallback);
}

View File

@ -1,13 +1,12 @@
package org.gcube.portlets.user.statisticalalgorithmsimporter.client.tools.explorer;
import org.gcube.portal.clientcontext.client.GCubeClientContext;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.BinaryCodeSetEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.DeleteItemEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.MainCodeSetEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.ProjectStatusEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.resource.StatAlgoImporterResources;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.utils.UtilsGXT3;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.Constants;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.workspace.DownloadWidget;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.ProjectSupportBashEdit;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.ProjectSupportBlackBox;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
@ -20,10 +19,7 @@ import org.gcube.portlets.widgets.wsexplorer.shared.Item;
import org.gcube.portlets.widgets.wsexplorer.shared.ItemType;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonScale;
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
@ -364,62 +360,11 @@ public class ExplorerProjectPanel extends ContentPanel {
private void openFile() {
if (selectedItem != null) {
if (selectedItem.isFolder()) {
StringBuilder url = new StringBuilder();
url.append(GWT.getModuleBaseURL());
url.append(Constants.DOWNLOAD_FOLDER_SERVLET + "?" + Constants.DOWNLOAD_FOLDER_SERVLET_ITEM_ID_PARAMETER
+ "=" + selectedItem.getId() + "&" + Constants.DOWNLOAD_FOLDER_SERVLET_FOLDER_NAME_PARAMETER
+ "=" + selectedItem.getName() + "&" + Constants.CURR_GROUP_ID + "="
+ GCubeClientContext.getCurrentContextId());
Log.debug("Retrieved link: " + url);
Window.open(URL.encode(url.toString()), "_blank", "");
} else {
final ItemDescription itemDescription = new ItemDescription(selectedItem.getId(),
selectedItem.getName(), selectedItem.getOwner(), selectedItem.getPath(),
selectedItem.getType().name());
Log.debug("ItemDescription: " + itemDescription);
String currentContextId = GCubeClientContext.getCurrentContextId();
StringBuilder url = new StringBuilder();
url.append(GWT.getModuleBaseURL());
url.append(Constants.DOWNLOAD_FILE_SERVLET + "/" + itemDescription.getName() + "?itemId="
+ itemDescription.getId() + "&" + Constants.CURR_GROUP_ID + "=" + currentContextId);
Window.open(URL.encode(url.toString()), "_blank", "");
/*
* Used to fix the problem of the PortalContext that lost the
* scope
*/
/*
* StatAlgoImporterServiceAsync.INSTANCE.getPublicLink(
* itemDescription, new AsyncCallback<String>() {
*
* @Override public void onFailure(Throwable caught) { if
* (caught instanceof StatAlgoImporterSessionExpiredException) {
* eventBus.fireEvent(new
* SessionExpiredEvent(SessionExpiredType.EXPIREDONSERVER)); }
* else { Log.error("Error open file: " +
* caught.getLocalizedMessage()); UtilsGXT3.alert("Error",
* caught.getLocalizedMessage()); } caught.printStackTrace();
*
* }
*
* @Override public void onSuccess(String link) {
* Log.debug("Retrieved link: " + link); Window.open(link,
* itemDescription.getName(), ""); }
*
* });
*/
}
DownloadWidget downloadWidget = new DownloadWidget(eventBus);
downloadWidget.download(selectedItem.getId());
} else {
UtilsGXT3.info("Attention", "Select a file!");
}
}
}

View File

@ -0,0 +1,80 @@
package org.gcube.portlets.user.statisticalalgorithmsimporter.client.workspace;
import org.gcube.portal.clientcontext.client.GCubeClientContext;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.rpc.StatAlgoImporterServiceAsync;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.SessionExpiredType;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.utils.UtilsGXT3;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.Constants;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterSessionExpiredException;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
*
* @author Giancarlo Panichi
*
*
*/
public class DownloadWidget {
private EventBus eventBus;
public DownloadWidget(EventBus eventBus) {
this.eventBus=eventBus;
}
public void download(String itemId) {
StatAlgoImporterServiceAsync.INSTANCE.getItemDescription(itemId, new AsyncCallback<ItemDescription>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error open file: " + caught.getLocalizedMessage());
UtilsGXT3.alert("Error", caught.getLocalizedMessage());
}
caught.printStackTrace();
}
@Override
public void onSuccess(ItemDescription itemDownloadInfo) {
Log.debug("Retrieved item download info: " + itemDownloadInfo);
requestDownload(itemDownloadInfo);
}
});
}
private void requestDownload(ItemDescription itemDescription) {
switch (itemDescription.getType()) {
case "AbstractFileItem":
case "FolderItem":
executeDownload(itemDescription);
break;
default:
UtilsGXT3.info("Attention", "This item does not support download operation!");
break;
}
}
private void executeDownload(ItemDescription itemDescription) {
StringBuilder url = new StringBuilder();
url.append(GWT.getModuleBaseURL());
url.append(Constants.DOWNLOAD_SERVLET + "/" + itemDescription.getName() + "?itemId=" + itemDescription.getId()
+ "&" + Constants.CURR_GROUP_ID + "=" + GCubeClientContext.getCurrentContextId());
Window.open(URL.encode(url.toString()), "_blank", "");
}
}

View File

@ -1,111 +0,0 @@
package org.gcube.portlets.user.statisticalalgorithmsimporter.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.IOUtils;
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage.FilesStorage;
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.util.ServiceCredentials;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Giancarlo Panichi
*
*
* Download Folder Servlet
*
*/
public class DownloadFolderServlet extends HttpServlet {
private static final long serialVersionUID = -1838255772767180518L;
private static Logger logger = LoggerFactory.getLogger(DownloadFolderServlet.class);
public DownloadFolderServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
createResponse(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
createResponse(request, response);
}
private void createResponse(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
logger.debug("DownloadFolderServlet");
HttpSession session = request.getSession();
if (session == null) {
logger.error("Error getting the session, no session valid found: " + session);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"ERROR-Error getting the user session, no session found " + session);
return;
}
logger.debug("DownloadFolderServlet session id: " + session.getId());
ServiceCredentials serviceCredentials;
String scopeGroupId = request.getHeader(Constants.CURR_GROUP_ID);
if (scopeGroupId == null || scopeGroupId.isEmpty()) {
scopeGroupId = request.getParameter(Constants.CURR_GROUP_ID);
if (scopeGroupId == null || scopeGroupId.isEmpty()) {
logger.error(
"CURR_GROUP_ID is null, it is a mandatory parameter in custom servlet: " + scopeGroupId);
throw new ServletException(
"CURR_GROUP_ID is null, it is a mandatory parameter in custom servlet: " + scopeGroupId);
}
}
serviceCredentials = SessionUtil.getServiceCredentials(request, scopeGroupId);
String itemId = request.getParameter(Constants.DOWNLOAD_FOLDER_SERVLET_ITEM_ID_PARAMETER);
String folderName = request.getParameter(Constants.DOWNLOAD_FOLDER_SERVLET_FOLDER_NAME_PARAMETER);
logger.debug("Request: [itemId=" + itemId + ", folderName=" + folderName + "]");
FilesStorage filesStorage = new FilesStorage();
File tmpZip = filesStorage.zipFolder(serviceCredentials.getUserName(), itemId);
response.setHeader("Content-Disposition", "attachment; filename=\"" + folderName + ".zip\"");
response.setContentType("application/zip");
response.setHeader("Content-Length", String.valueOf(tmpZip.length()));
try (OutputStream out = response.getOutputStream();FileInputStream fileTmpZip = new FileInputStream(tmpZip)){
IOUtils.copy(fileTmpZip, response.getOutputStream());
}
Files.delete(tmpZip.toPath());
return;
} catch (Throwable e) {
logger.error("Error in DownloadFolderServlet: " + e.getLocalizedMessage(), e);
throw new ServletException("Download:" + e.getLocalizedMessage(), e);
}
}
}

View File

@ -14,11 +14,12 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage.FilesStorage;
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage.ItemDownload;
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.util.ServiceCredentials;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.Constants;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,16 +30,16 @@ import org.slf4j.LoggerFactory;
*
*
*/
public class DownloadFileServlet extends HttpServlet {
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 5389118370656932343L;
private static Logger logger = LoggerFactory.getLogger(DownloadFileServlet.class);
private static Logger logger = LoggerFactory.getLogger(DownloadServlet.class);
public DownloadFileServlet() {
public DownloadServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
@ -57,11 +58,10 @@ public class DownloadFileServlet extends HttpServlet {
createResponse(request, response);
}
private void createResponse(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
private void createResponse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
logger.debug("DownloadFileServlet()");
logger.debug("DownloadServlet()");
HttpSession session = req.getSession();
@ -71,7 +71,7 @@ public class DownloadFileServlet extends HttpServlet {
"ERROR-Error getting the user session, no session found" + session);
return;
}
logger.debug("DownloadFileServlet session id: " + session.getId());
logger.debug("DownloadServlet() session id: " + session.getId());
String scopeGroupId = req.getHeader(Constants.CURR_GROUP_ID);
if (scopeGroupId == null || scopeGroupId.isEmpty()) {
scopeGroupId = req.getParameter(Constants.CURR_GROUP_ID);
@ -82,26 +82,55 @@ public class DownloadFileServlet extends HttpServlet {
"CURR_GROUP_ID is null, it is a mandatory parameter in custom servlet: " + scopeGroupId);
}
}
String itemId = req.getParameter("itemId");
logger.info("DownloadFileServlet(): [scopeGroupId=" + scopeGroupId + ",ItemId=" + itemId + "]");
logger.info("DownloadServlet(): [scopeGroupId=" + scopeGroupId + ",ItemId=" + itemId + "]");
ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(req, scopeGroupId);
FilesStorage filesStorage = new FilesStorage();
ItemDescription itemDescription = filesStorage.getFileInfoOnWorkspace(serviceCredentials.getUserName(),
itemId);
logger.debug("Item retrieved: " + itemDescription);
InputStream inputStream = filesStorage.getFileOnWorkspace(serviceCredentials.getUserName(), itemId);
resp.setHeader("Content-Disposition:", "attachment;filename=\"" + itemDescription.getName() + "\"");
resp.setHeader("Content-Type", "application/force-download");
resp.setHeader("Content-Length", itemDescription.getLenght());
ItemDownload itemDownload = filesStorage.getItemDownload(serviceCredentials.getUserName(), itemId);
if (itemDownload == null) {
logger.error("This type of item does not support download operation");
throw new ServletException("This type of item does not support download operation");
} else {
if (itemDownload.getInputStream() == null) {
logger.error("This type of item does not support download operation");
throw new ServletException("This type of item does not support download operation");
} else {
String fileName;
if (itemDownload.getItemDescription() == null) {
fileName = "filename";
} else {
if (itemDownload.getItemDescription().getName() == null
|| itemDownload.getItemDescription().getName().isEmpty()) {
if (itemDownload.getItemDescription().getType() != null && itemDownload.getItemDescription()
.getType().compareTo(FolderItem.class.getSimpleName()) == 0) {
fileName = "folder.zip";
} else {
fileName = itemDownload.getItemDescription().getName();
}
} else {
if (itemDownload.getItemDescription().getType() != null && itemDownload.getItemDescription()
.getType().compareTo(FolderItem.class.getSimpleName()) == 0) {
fileName = itemDownload.getItemDescription().getName() + ".zip";
} else {
fileName = itemDownload.getItemDescription().getName();
}
}
}
resp.setHeader("Content-Disposition:", "attachment;filename=\"" + fileName + "\"");
resp.setHeader("Content-Type", "application/force-download");
stream(itemDownload.getInputStream(), resp.getOutputStream());
}
}
stream(inputStream, resp.getOutputStream());
} catch (StatAlgoImporterServiceException e) {
logger.error("DownloadFileServlet():"+e.getLocalizedMessage(), e);
logger.error("DownloadServlet():" + e.getLocalizedMessage(), e);
throw new ServletException(e.getLocalizedMessage(), e);
} catch (Throwable e) {
logger.error("DownloadFileServlet(): " + e.getLocalizedMessage(), e);
throw new ServletException("Download:" + e.getLocalizedMessage(), e);
logger.error("DownloadServlet(): " + e.getLocalizedMessage(), e);
throw new ServletException("Download item error: " + e.getLocalizedMessage(), e);
}
}

View File

@ -442,7 +442,7 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
Project project = SessionUtil.getProjectSession(httpRequest, serviceCredentials);
checkProjectInfoForDelete(itemDescription, httpRequest, serviceCredentials, project);
FilesStorage fileStorage = new FilesStorage();
fileStorage.deleteItemOnFolder(serviceCredentials.getUserName(), itemDescription.getId());
fileStorage.deleteItem(serviceCredentials.getUserName(), itemDescription.getId());
return project;
} catch (StatAlgoImporterServiceException e) {
@ -827,4 +827,23 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
}
}
@Override
public ItemDescription getItemDescription(String itemId) throws StatAlgoImporterServiceException {
try {
HttpServletRequest httpRequest = this.getThreadLocalRequest();
ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest);
logger.debug("getItemDescription(): [itemId=" + itemId + "]");
FilesStorage storageUtil = new FilesStorage();
ItemDescription itemDownloadInfo = storageUtil.getItemDescription(serviceCredentials.getUserName(), itemId);
logger.debug("ItemDescription info: " + itemDownloadInfo);
return itemDownloadInfo;
} catch (StatAlgoImporterServiceException e) {
logger.error(e.getLocalizedMessage(), e);
throw e;
} catch (Throwable e) {
logger.error("Error retrieving item description: " + e.getLocalizedMessage(), e);
throw new StatAlgoImporterServiceException("Error retrieving item description: " + e.getLocalizedMessage(), e);
}
}
}

View File

@ -142,7 +142,7 @@ public class ProjectBuilder {
ItemDescription mainCodeItemDescription = project.getMainCode().getItemDescription();
if (mainCodeItemDescription.getId() != null && !mainCodeItemDescription.getId().isEmpty()) {
FilesStorage filesStorage = new FilesStorage();
filesStorage.deleteItemOnFolder(serviceCredentials.getUserName(), mainCodeItemDescription.getId());
filesStorage.deleteItem(serviceCredentials.getUserName(), mainCodeItemDescription.getId());
}
}
MainGenerator mainGenerator = new MainGenerator();
@ -233,7 +233,7 @@ public class ProjectBuilder {
if (project.getProjectTarget().getProjectDeploy().getCodeJar() != null
&& project.getProjectTarget().getProjectDeploy().getCodeJar().getId() != null
&& !project.getProjectTarget().getProjectDeploy().getCodeJar().getId().isEmpty()) {
filesStorage.deleteItemOnFolder(serviceCredentials.getUserName(),
filesStorage.deleteItem(serviceCredentials.getUserName(),
project.getProjectTarget().getProjectDeploy().getCodeJar().getId());
}
@ -589,10 +589,10 @@ public class ProjectBuilder {
Path packageHomeDir = packageHome.toPath();
Path ecologicalEngineJar = new File(tempDirectory.toFile(), ECOLOGICAL_ENGINE_JAR).toPath();
storage.downloadInputFile(ECOLOGICAL_ENGINE_JAR_URL, ecologicalEngineJar);
storage.downloadExternalInputFile(ECOLOGICAL_ENGINE_JAR_URL, ecologicalEngineJar);
Path ecologicalEngineSmartExecutorJar = new File(tempDirectory.toFile(), ECOLOGICAL_ENGINE_SMART_EXECUTOR_JAR)
.toPath();
storage.downloadInputFile(ECOLOGICAL_ENGINE_SMART_EXECUTOR_JAR_URL, ecologicalEngineSmartExecutorJar);
storage.downloadExternalInputFile(ECOLOGICAL_ENGINE_SMART_EXECUTOR_JAR_URL, ecologicalEngineSmartExecutorJar);
Path algorithmTempFile = new File(packageHomeDir.toFile(),
project.getInputData().getProjectInfo().getAlgorithmNameToClassName() + ALGORITHM_EXTENTION).toPath();

View File

@ -21,8 +21,10 @@ import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.common.storagehub.model.Metadata;
import org.gcube.common.storagehub.model.items.AbstractFileItem;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.GCubeItem;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.items.SharedFolder;
import org.gcube.common.storagehub.model.items.TrashItem;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
import org.slf4j.Logger;
@ -95,7 +97,7 @@ public class FilesStorage {
FileContainer fileCreatedContainer = fileContainer.copy(folderContainer, fileContainer.get().getName());
AbstractFileItem item = fileCreatedContainer.get();
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), item.getClass().getSimpleName());
item.getPath(), getItemType(item));
return itemDescription;
} catch (Throwable e) {
logger.error("Copy item on folder on workspace: " + e.getLocalizedMessage(), e);
@ -134,8 +136,8 @@ public class FilesStorage {
FileContainer fileCreatedContainer = fileContainer.copy(folderContainer, newName);
AbstractFileItem item = fileCreatedContainer.get();
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), item.getClass().getSimpleName());
item.getPath(), getItemType(item));
logger.debug("File created: " + itemDescription);
return itemDescription;
} catch (Throwable e) {
@ -154,7 +156,7 @@ public class FilesStorage {
* @throws StatAlgoImporterServiceException
* exception
*/
public void deleteItemOnFolder(String user, String itemId) throws StatAlgoImporterServiceException {
public void deleteItem(String user, String itemId) throws StatAlgoImporterServiceException {
try {
logger.info("Delete Item: [User=" + user + ", ItemId=" + itemId + "]");
StorageHubClient shc = new StorageHubClient();
@ -246,7 +248,7 @@ public class FilesStorage {
FolderItem folderCreated = folderContainerNew.get();
ItemDescription itemDescription = new ItemDescription(folderCreated.getId(), folderCreated.getName(),
folderCreated.getOwner(), folderCreated.getPath(), folderCreated.getClass().getSimpleName());
folderCreated.getOwner(), folderCreated.getPath(), getItemType(folderCreated));
return itemDescription;
} catch (Throwable e) {
@ -287,7 +289,7 @@ public class FilesStorage {
if (item != null) {
logger.debug("Item Id=" + item.getId());
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), item.getClass().getSimpleName());
item.getPath(), getItemType(item));
return itemDescription;
} else {
@ -413,8 +415,8 @@ public class FilesStorage {
AbstractFileItem fileCreated = fileContainerNew.get();
ItemDescription itemDescription = new ItemDescription(fileCreated.getId(), fileCreated.getName(),
fileCreated.getOwner(), fileCreated.getPath(), fileCreated.getClass().getSimpleName());
fileCreated.getOwner(), fileCreated.getPath(), getItemType(fileCreated));
return itemDescription;
} catch (Throwable e) {
logger.error("Create item on workspace: " + e.getLocalizedMessage(), e);
@ -435,8 +437,7 @@ public class FilesStorage {
* Exception
*
*/
public InputStream getProjectItemOnWorkspace(String user, String folderId)
throws StatAlgoImporterServiceException {
public InputStream getProjectItemOnWorkspace(String user, String folderId) throws StatAlgoImporterServiceException {
try {
logger.info("Retrieve project item on workspace: [user=" + user + ", folderId=" + folderId + "]");
StorageHubClient shc = new StorageHubClient();
@ -517,67 +518,7 @@ public class FilesStorage {
* @throws StatAlgoImporterServiceException
* Exception
*/
public ItemDescription getItemInfoOnWorkspace(String user, String itemId)
throws StatAlgoImporterServiceException {
try {
logger.info("Retrieve item info on workspace: [user=" + user + ", itemId=" + itemId + "]");
StorageHubClient shc = new StorageHubClient();
OpenResolver openResolver = shc.open(itemId);
ItemContainer<Item> fileContainer = openResolver.asItem();
Item item = fileContainer.get();
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), item.getClass().getSimpleName());
return itemDescription;
} catch (Throwable e) {
logger.error("Retrieve item info on workspace: " + e.getLocalizedMessage(), e);
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
/**
*
* @param user
* User
* @param itemId
* Item id
* @return Input stream
* @throws StatAlgoImporterServiceException
* Excetpion
*/
public InputStream getItemOnWorkspace(String user, String itemId) throws StatAlgoImporterServiceException {
try {
logger.info("Get item on workspace: [user=" + user + ", itemId=" + itemId + "]");
StorageHubClient shc = new StorageHubClient();
StreamDescriptor streamDescr = shc.open(itemId).asItem().download();
InputStream is = streamDescr.getStream();
return is;
} catch (Throwable e) {
logger.error("Get item on workspace: " + e.getLocalizedMessage(), e);
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
/**
*
* @param user
* User
* @param itemId
* Item id
* @return Item description
* @throws StatAlgoImporterServiceException
* Exception
*/
public ItemDescription getFileInfoOnWorkspace(String user, String itemId)
throws StatAlgoImporterServiceException {
public ItemDescription getFileInfoOnWorkspace(String user, String itemId) throws StatAlgoImporterServiceException {
try {
logger.info("Retrieve file info on workspace: [user=" + user + ", itemId=" + itemId + "]");
StorageHubClient shc = new StorageHubClient();
@ -586,7 +527,7 @@ public class FilesStorage {
FileContainer fileContainer = openResolver.asFile();
AbstractFileItem item = fileContainer.get();
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), item.getClass().getSimpleName());
item.getPath(), getItemType(item));
itemDescription.setMimeType(item.getContent().getMimeType());
itemDescription.setLenght(String.valueOf(item.getContent().getSize()));
return itemDescription;
@ -597,8 +538,6 @@ public class FilesStorage {
}
}
/**
*
* @param user
@ -619,7 +558,7 @@ public class FilesStorage {
FolderItem item = openResolver.asFolder().get();
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), item.getClass().getSimpleName());
item.getPath(), getItemType(item));
return itemDescription;
@ -730,46 +669,6 @@ public class FilesStorage {
}
}
/**
*
* @param user
* User
* @param folderId
* Folder Id
* @return Zip folder
* @throws StatAlgoImporterServiceException
* Exception
*/
public File zipFolder(String user, String folderId) throws StatAlgoImporterServiceException {
try {
logger.info("Zip folder: [user=" + user + ", folderId=" + folderId + "]");
StorageHubClient shc = new StorageHubClient();
StreamDescriptor streamDescr = shc.open(folderId).asFolder().download();
File fileZip = Files.createTempFile(streamDescr.getFileName(), "").toFile();
logger.debug("File zip: " + fileZip.getAbsolutePath());
try (FileOutputStream fos = new FileOutputStream(fileZip)) {
InputStream is = streamDescr.getStream();
byte[] buf = new byte[2048];
int read = -1;
while ((read = is.read(buf)) != -1) {
fos.write(buf, 0, read);
}
}
return fileZip;
} catch (Throwable e) {
logger.error("Zip folder on workspace: " + e.getLocalizedMessage(), e);
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
/**
*
* @param user
@ -822,7 +721,7 @@ public class FilesStorage {
* @throws StatAlgoImporterServiceException
* Exception
*/
public void downloadInputFile(String fileUrl, Path destination) throws StatAlgoImporterServiceException {
public void downloadExternalInputFile(String fileUrl, Path destination) throws StatAlgoImporterServiceException {
try {
logger.info("Download input file: [fileUrl=" + fileUrl + ", destination=" + destination + "]");
@ -899,4 +798,100 @@ public class FilesStorage {
}
}
/**
*
* @param user
* User
* @param itemId
* Item id
* @return ItemDownload
* @throws StatAlgoImporterServiceException
* Exception
*/
public ItemDownload getItemDownload(String user, String itemId) throws StatAlgoImporterServiceException {
try {
logger.debug("getItemDownload: [user=" + user + ", itemId=" + itemId + "]");
StorageHubClient shc = new StorageHubClient();
OpenResolver openResolver = shc.open(itemId);
ItemContainer<Item> itemContainer = openResolver.asItem();
Item item = itemContainer.get();
StreamDescriptor streamDescriptor = null;
if (item instanceof AbstractFileItem) {
FileContainer fileContainer = openResolver.asFile();
streamDescriptor = fileContainer.download();
} else {
if (item instanceof FolderItem) {
FolderContainer folderContainer = openResolver.asFolder();
streamDescriptor = folderContainer.download();
} else {
}
}
if (streamDescriptor == null) {
logger.error("This type of item does not support download: " + itemId);
return null;
} else {
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), getItemType(item));
ItemDownload itemDownload = new ItemDownload(itemDescription, streamDescriptor.getStream());
return itemDownload;
}
} catch (Throwable e) {
logger.error("Error retrieving InputStream for item: " + e.getLocalizedMessage(), e);
throw new StatAlgoImporterServiceException(e.getLocalizedMessage(), e);
}
}
/**
*
* @param user
* User
* @param itemId
* Item id
* @return Item Description
* @throws StatAlgoImporterServiceException
* Exception
*/
public ItemDescription getItemDescription(String user, String itemId) throws StatAlgoImporterServiceException {
try {
logger.info("Retrieve file info on workspace: [user=" + user + ", itemId=" + itemId + "]");
StorageHubClient shc = new StorageHubClient();
OpenResolver openResolver = shc.open(itemId);
ItemContainer<Item> itemContainer = openResolver.asItem();
Item item = itemContainer.get();
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
item.getPath(), getItemType(item));
return itemDescription;
} catch (Throwable e) {
logger.error("Retrieve file info on workspace: " + e.getLocalizedMessage(), e);
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
/**
*
* @param item
* Item
* @return Item type
*/
private String getItemType(Item item) {
if (item instanceof AbstractFileItem) {
return AbstractFileItem.class.getSimpleName();
} else {
if (item instanceof FolderItem) {
return FolderItem.class.getSimpleName();
} else {
if (item instanceof GCubeItem) {
return GCubeItem.class.getSimpleName();
} else {
if (item instanceof TrashItem) {
return TrashItem.class.getSimpleName();
} else {
return null;
}
}
}
}
}
}

View File

@ -0,0 +1,44 @@
package org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage;
import java.io.InputStream;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
/**
*
* @author Giancarlo Panichi
*
*
*/
public class ItemDownload {
private ItemDescription itemDescription;
private InputStream inputStream;
public ItemDownload(ItemDescription itemDescription, InputStream inputStream) {
super();
this.itemDescription = itemDescription;
this.inputStream = inputStream;
}
public ItemDescription getItemDescription() {
return itemDescription;
}
public void setItemDescription(ItemDescription itemDescription) {
this.itemDescription = itemDescription;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public String toString() {
return "ItemDownload [itemDescription=" + itemDescription + ", inputStream=" + inputStream + "]";
}
}

View File

@ -27,14 +27,10 @@ public class Constants {
// Session
public static final String CURR_GROUP_ID = "CURR_GROUP_ID";
// Download
public static final String DOWNLOAD_FILE_SERVLET = "DownloadFileServlet";
public static final String DOWNLOAD_FOLDER_SERVLET = "DownloadFolderServlet";
public static final String DOWNLOAD_FOLDER_SERVLET_ITEM_ID_PARAMETER = "itemId";
public static final String DOWNLOAD_FOLDER_SERVLET_FOLDER_NAME_PARAMETER = "folderName";
// Download
public static final String DOWNLOAD_SERVLET = "DownloadServlet";
// Upload
public static final String LOCAL_UPLOAD_SERVLET = "LocalUploadServlet";

View File

@ -26,14 +26,9 @@
<!-- Download -->
<servlet>
<servlet-name>DownloadFileServlet</servlet-name>
<servlet-class>org.gcube.portlets.user.statisticalalgorithmsimporter.server.DownloadFileServlet</servlet-class>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>org.gcube.portlets.user.statisticalalgorithmsimporter.server.DownloadServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DownloadFolderServlet</servlet-name>
<servlet-class>org.gcube.portlets.user.statisticalalgorithmsimporter.server.DownloadFolderServlet</servlet-class>
</servlet>
<!-- Workspace Explorer -->
@ -83,14 +78,9 @@
<!-- Download -->
<servlet-mapping>
<servlet-name>DownloadFileServlet</servlet-name>
<url-pattern>/statalgoimporter/DownloadFileServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DownloadFolderServlet</servlet-name>
<url-pattern>/statalgoimporter/DownloadFolderServlet</url-pattern>
</servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/statalgoimporter/DownloadServlet/*</url-pattern>
</servlet-mapping>
<!-- Workspace Explorer -->
<servlet-mapping>