diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java index 870ac5d..c754771 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java @@ -89,4 +89,6 @@ public interface StatAlgoImporterService extends RemoteService { public void repackageSoftware() throws StatAlgoImporterServiceException; + public ItemDescription getItemDescription(String itemId) throws StatAlgoImporterServiceException; + } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java index 67c5bbf..8867dfc 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java @@ -68,7 +68,8 @@ public interface StatAlgoImporterServiceAsync { void getDeployOperationLogs(String operationId, AsyncCallback callback); - + void getItemDescription(String itemId, AsyncCallback asyncCallback); + } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/explorer/ExplorerProjectPanel.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/explorer/ExplorerProjectPanel.java index a4ecc67..ddf0419 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/explorer/ExplorerProjectPanel.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/explorer/ExplorerProjectPanel.java @@ -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() { - * - * @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!"); } - } } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/workspace/DownloadWidget.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/workspace/DownloadWidget.java new file mode 100644 index 0000000..206538a --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/workspace/DownloadWidget.java @@ -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() { + + @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", ""); + + } + +} diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadFolderServlet.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadFolderServlet.java deleted file mode 100644 index 6087eba..0000000 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadFolderServlet.java +++ /dev/null @@ -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); - - } - } - -} diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadFileServlet.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadServlet.java similarity index 58% rename from src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadFileServlet.java rename to src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadServlet.java index f3dc16f..7fd0055 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadFileServlet.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/DownloadServlet.java @@ -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); } } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java index cfa814b..b0d3a03 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java @@ -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); + } + } + } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java index c1711dd..67d90e2 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java @@ -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(); diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/storage/FilesStorage.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/storage/FilesStorage.java index 33ed827..553f848 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/storage/FilesStorage.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/storage/FilesStorage.java @@ -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 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 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 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; + } + } + } + } + } } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/storage/ItemDownload.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/storage/ItemDownload.java new file mode 100644 index 0000000..8280925 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/storage/ItemDownload.java @@ -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 + "]"; + } + +} diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/Constants.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/Constants.java index c80eece..e0e16a3 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/Constants.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/Constants.java @@ -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"; diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 60fdef7..64408d5 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -26,14 +26,9 @@ - DownloadFileServlet - org.gcube.portlets.user.statisticalalgorithmsimporter.server.DownloadFileServlet + DownloadServlet + org.gcube.portlets.user.statisticalalgorithmsimporter.server.DownloadServlet - - DownloadFolderServlet - org.gcube.portlets.user.statisticalalgorithmsimporter.server.DownloadFolderServlet - - @@ -83,14 +78,9 @@ - DownloadFileServlet - /statalgoimporter/DownloadFileServlet/* - - - DownloadFolderServlet - /statalgoimporter/DownloadFolderServlet - - + DownloadServlet + /statalgoimporter/DownloadServlet/* +