From 93d7d597699629241ef3e8ef857993c3a80958ef Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Fri, 14 Sep 2018 13:42:36 +0000 Subject: [PATCH] Task #12489 Migrating delete operation to StorageHub git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@171467 82a268e6-3cf1-43bd-a215-b396298e98cf --- distro/changelog.xml | 6 + pom.xml | 8 +- .../client/AppControllerExplorer.java | 2 +- .../client/rpc/GWTWorkspaceService.java | 14 ++- .../client/rpc/GWTWorkspaceServiceAsync.java | 5 +- .../server/GWTWorkspaceServiceImpl.java | 38 ++++-- .../user/workspace/server/util/WsUtil.java | 36 ++++++ .../user/workspace/UploadServletTest.java | 31 ++++- .../workspace/UploadServletTestToken.java | 116 ++++++++++++++++++ 9 files changed, 232 insertions(+), 24 deletions(-) create mode 100644 src/test/java/org/gcube/portlets/user/workspace/UploadServletTestToken.java diff --git a/distro/changelog.xml b/distro/changelog.xml index ef0a5bf..a6883fb 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,10 @@ + + [Task #12489] Migrate delete operation to StorageHub + + integrated CLARIN Switchboard with service endpoint query for Switchboard service discovery + [Task #12089] Migrate to workspace-uploader 2.0 diff --git a/pom.xml b/pom.xml index 16daa1c..65aab9c 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.portlets.user workspace-tree-widget - 6.21.1-SNAPSHOT + 6.22.0-SNAPSHOT gCube Workspace Tree Widget gCube Workspace Tree Widget is a widget to navigate and interact with gCube Workspace @@ -237,6 +237,12 @@ compile + + org.gcube.common + storagehub-client-wrapper + [0.0.1-SNAPSHOT, 2.0.0-SNAPSHOT) + + commons-fileupload commons-fileupload diff --git a/src/main/java/org/gcube/portlets/user/workspace/client/AppControllerExplorer.java b/src/main/java/org/gcube/portlets/user/workspace/client/AppControllerExplorer.java index f95632d..3d7fa9f 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/client/AppControllerExplorer.java +++ b/src/main/java/org/gcube/portlets/user/workspace/client/AppControllerExplorer.java @@ -1466,7 +1466,7 @@ public class AppControllerExplorer implements EventHandler, TreeAppControllerInt if(clickedButton.equals(Dialog.YES)){ explorerPanel.getAsycTreePanel().mask("Deleting", ConstantsExplorer.LOADINGSTYLE); - rpcWorkspaceService.removeItem(event.getFileTarget().getIdentifier(), new AsyncCallback(){ + rpcWorkspaceService.deleteItem(event.getFileTarget().getIdentifier(), new AsyncCallback(){ @Override public void onFailure(Throwable caught) { diff --git a/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceService.java b/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceService.java index 81d47d2..1e247b6 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceService.java +++ b/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceService.java @@ -238,14 +238,15 @@ public interface GWTWorkspaceService extends RemoteService{ */ public Boolean moveItem(String itemId, String destinationId) throws Exception; + /** - * Removes the item. + * Delete item. * * @param itemId the item id * @return the boolean * @throws Exception the exception */ - public Boolean removeItem(String itemId) throws Exception; + public Boolean deleteItem(String itemId) throws Exception; /** * Rename item. @@ -788,10 +789,13 @@ public interface GWTWorkspaceService extends RemoteService{ */ List getListOfVREsForLoggedUser() throws Exception; + /** - * @param itemId - * @return - * @throws Exception + * Checks if is item under sync. + * + * @param itemId the item id + * @return the boolean + * @throws Exception the exception */ Boolean isItemUnderSync(String itemId) throws Exception; diff --git a/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceServiceAsync.java b/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceServiceAsync.java index 6afcbf5..eb3733b 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/workspace/client/rpc/GWTWorkspaceServiceAsync.java @@ -97,13 +97,14 @@ public interface GWTWorkspaceServiceAsync { */ void moveItem(String itemId, String destinationId, AsyncCallback callback); + /** - * Removes the item. + * Delete item. * * @param itemId the item id * @param callback the callback */ - void removeItem(String itemId, AsyncCallback callback); + void deleteItem(String itemId, AsyncCallback callback); /** * Rename item. diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java b/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java index 40987e6..aa05b3d 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java @@ -50,6 +50,7 @@ import org.gcube.common.homelibrary.home.workspace.trash.WorkspaceTrashItem; import org.gcube.common.portal.PortalContext; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.storagehubwrapper.server.StorageHubWrapper; import org.gcube.portal.wssynclibrary.shared.ItemNotSynched; import org.gcube.portal.wssynclibrary.shared.WorkspaceFolderLocked; import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status; @@ -950,17 +951,10 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT } /* (non-Javadoc) - * @see org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceService#removeItem(java.lang.String) - */ - /** - * Removes the item. - * - * @param itemId the item id - * @return the boolean - * @throws Exception the exception + * @see org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceService#deleteItem(java.lang.String) */ @Override - public Boolean removeItem(String itemId) throws Exception { + public Boolean deleteItem(String itemId) throws Exception { try { @@ -969,6 +963,28 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT checkItemLocked(itemId); + GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); + StorageHubWrapper storageHubWrapper = WsUtil.getStorageHubWrapper(this.getThreadLocalRequest(), null, user); + org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem theItem = storageHubWrapper.getWorkspace().getItem(itemId); + boolean sourceItemIsShared = theItem.isShared(); + String itemName = theItem.getName(); + String sourceFolderSharedId = null; + + if(sourceItemIsShared){ + org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem rootSharedFolder = storageHubWrapper.getWorkspace().getRootSharedFolder(itemId); + sourceFolderSharedId = rootSharedFolder.getId(); + } + + //HERE REMOVING THE ITEM + storageHubWrapper.getWorkspace().deleteItem(itemId); + + if(sourceFolderSharedId!=null) + NotificationsUtil.checkSendNotifyRemoveItemToShare(this.getThreadLocalRequest(), sourceItemIsShared, itemName, itemId, sourceFolderSharedId); + + + return Boolean.TRUE; + + /* Workspace workspace = getWorkspace(); workspaceLogger.trace("removeItem item for id: "+itemId); //NOTIFICATION @@ -987,7 +1003,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT if(sourceFolderSharedId!=null) NotificationsUtil.checkSendNotifyRemoveItemToShare(this.getThreadLocalRequest(), sourceItemIsShared, itemName, itemId, sourceFolderSharedId); + return Boolean.TRUE; + */ }catch (WorkspaceFolderLocked e1){ throw new Exception(e1.getMessage()); @@ -4438,7 +4456,7 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT return null; ServiceEndpoint res = conf.get(0); //reset the context - ScopeProvider.instance.set(currContext); + ScopeProvider.instance.set(currContext); return res.profile().runtime().hostedOn(); } diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java b/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java index d745e64..1ca143d 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java @@ -19,6 +19,7 @@ import org.gcube.common.homelibrary.home.workspace.Workspace; import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException; import org.gcube.common.portal.PortalContext; import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.storagehubwrapper.server.StorageHubWrapper; import org.gcube.portlets.user.urlshortener.UrlShortener; import org.gcube.portlets.user.workspace.client.model.InfoContactModel; import org.gcube.portlets.user.workspace.server.GWTWorkspaceBuilder; @@ -412,5 +413,40 @@ public class WsUtil { return psFolderReader; } + /** + * Gets the storage hub wrapper. + * + * @param request the request + * @param scopeGroupId the scope group id. If scopeGroupId is null the scope is read by using the request else by using the scopeGroupId + * @param user the user + * @return the storage hub wrapper + * @throws InternalErrorException the internal error exception + * @throws WorkspaceFolderNotFoundException the workspace folder not found exception + * @throws Exception the exception + */ + public static StorageHubWrapper getStorageHubWrapper(final HttpServletRequest request, String scopeGroupId, GCubeUser user) throws InternalErrorException, WorkspaceFolderNotFoundException, Exception{ + + if(user==null || user.getUsername().isEmpty()) + throw new Exception("Session expired"); + + try { + String scope; + PortalContext pContext = PortalContext.getConfiguration(); + if(WsUtil.isWithinPortal() && scopeGroupId!=null){ + scope = pContext.getCurrentScope(scopeGroupId); + logger.debug(scope+" has retrieved by using the scopeGroupId="+scopeGroupId); + } + else + scope = pContext.getCurrentScope(request); + + logger.debug("Instancing "+StorageHubWrapper.class.getSimpleName()+" for user: "+user.getUsername() +", by using the scope: "+scope); + String token = pContext.getCurrentUserToken(scope, user.getUsername()); + return new StorageHubWrapper(scope, token); + } catch (Exception e) { + logger.error("Error during getting storageHub wrapper", e); + throw new InternalErrorException("Error on gettig the StorageHub wrapper for userId: "+user); + } + } + } diff --git a/src/test/java/org/gcube/portlets/user/workspace/UploadServletTest.java b/src/test/java/org/gcube/portlets/user/workspace/UploadServletTest.java index ce99c63..1c66a99 100644 --- a/src/test/java/org/gcube/portlets/user/workspace/UploadServletTest.java +++ b/src/test/java/org/gcube/portlets/user/workspace/UploadServletTest.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException; import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; @@ -22,26 +23,46 @@ import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNot import org.gcube.common.homelibrary.home.workspace.exceptions.WrongDestinationException; import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile; import org.gcube.common.homelibrary.util.WorkspaceUtil; -import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; + /** - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Jun 20, 2013 + * The Class UploadServletTest. * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Jul 23, 2018 */ public class UploadServletTest { + + public static String YOUR_TOKEN = "YOUR TOKEN"; + public static String YOUR_USERNAME = "name.surname"; + + /** + * The main method. + * + * @param args the arguments + * @throws InternalErrorException the internal error exception + * @throws WorkspaceFolderNotFoundException the workspace folder not found exception + * @throws HomeNotFoundException the home not found exception + * @throws UserNotFoundException the user not found exception + * @throws InsufficientPrivilegesException the insufficient privileges exception + * @throws ItemAlreadyExistException the item already exist exception + * @throws WrongDestinationException the wrong destination exception + * @throws IOException Signals that an I/O exception has occurred. + */ public static void main(String[] args) throws InternalErrorException, WorkspaceFolderNotFoundException, HomeNotFoundException, UserNotFoundException, InsufficientPrivilegesException, ItemAlreadyExistException, WrongDestinationException, IOException { ScopeBean scope = new ScopeBean("/gcube/devsec"); - ScopeProvider.instance.set(scope.toString()); + + + SecurityTokenProvider.instance.set("YOUR TOKEN"); Workspace workspace = HomeLibrary.getHomeManagerFactory() - .getHomeManager().getHome("francesco.mangiacrapa") + .getHomeManager().getHome(YOUR_USERNAME) .getWorkspace(); String filePath = "/home/francesco-mangiacrapa"; diff --git a/src/test/java/org/gcube/portlets/user/workspace/UploadServletTestToken.java b/src/test/java/org/gcube/portlets/user/workspace/UploadServletTestToken.java new file mode 100644 index 0000000..775e6f5 --- /dev/null +++ b/src/test/java/org/gcube/portlets/user/workspace/UploadServletTestToken.java @@ -0,0 +1,116 @@ +/** + * + */ +package org.gcube.portlets.user.workspace; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.gcube.common.homelibrary.home.HomeLibrary; +import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException; +import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; +import org.gcube.common.homelibrary.home.exceptions.UserNotFoundException; +import org.gcube.common.homelibrary.home.workspace.Workspace; +import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder; +import org.gcube.common.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException; +import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException; +import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException; +import org.gcube.common.homelibrary.home.workspace.exceptions.WrongDestinationException; +import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile; +import org.gcube.common.homelibrary.util.WorkspaceUtil; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Jun 20, 2013 + * + */ +public class UploadServletTestToken { + + public static void main(String[] args) throws InternalErrorException, + WorkspaceFolderNotFoundException, HomeNotFoundException, + UserNotFoundException, InsufficientPrivilegesException, + ItemAlreadyExistException, WrongDestinationException, IOException { + + ScopeBean scope = new ScopeBean("/gcube/devsec"); + ScopeProvider.instance.set(scope.toString()); + + Workspace workspace = HomeLibrary.getHomeManagerFactory() + .getHomeManager().getHome("francesco.mangiacrapa") + .getWorkspace(); + + String filePath = "/home/francesco-mangiacrapa"; + + String fileName = "Geo Explorer 2016-01-15 18-51-51.png"; + + String fullPath = filePath + "/" + fileName; + + // String newFilePath = + // "/home/francesco-mangiacrapa/Desktop/icongeoexplorer/testupload.txt"; + + // String name = "TestReplaceContentImage"; + + // ExternalPDFFile file = workspace.createExternalPDFFile(name, "", + // null, new FileInputStream(new File(filePath)), + // workspace.getRoot().getId()); + + WorkspaceFolder root = workspace.getRoot(); + // ExternalImage file = (ExternalImage) root.find(name); + + // UPLOAD FILE +// String contentType = MimeTypeUtil.getMimeType(fileName, new BufferedInputStream( +// new FileInputStream(new File(fullPath)))); + + String itemName = WorkspaceUtil.getUniqueName(fileName, root); + System.out.println("Storing data...."); + System.out.println("content type " + null); + System.out.println("itemName " + itemName); + + ExternalFile file = (ExternalFile) WorkspaceUtil.createExternalFile( + root, itemName, "", null, new FileInputStream(new File( + fullPath))); + + System.out.println("Storing data - OK" + "File [id: " + file.getId() + + ", name: " + file.getName() + "]"); + // + // System.out.println("File mimeType" + file.getMimeType()); + // System.out.println("File size" + file.getLength()); + // + + // + // file.setData(new FileInputStream(new File(newFilePath))); + // System.out.println("New File mimeType" + file.getMimeType()); + // System.out.println("New File size" + file.getLength()); + + // + + System.out.println("Recovering data"); + InputStream inputStream = file.getData(); + // write the inputStream to a FileOutputStream + OutputStream out = new FileOutputStream(new File( + "/home/francesco-mangiacrapa/Desktop/download/"+fileName)); + + int read = 0; + byte[] bytes = new byte[1024]; + + while ((read = inputStream.read(bytes)) != -1) { + out.write(bytes, 0, read); + } + + inputStream.close(); + out.flush(); + out.close(); + + System.out.println("Recovering data - OK"); + + // File file = new File("/home/gioia/Desktop/test"); + // InputStream is = new FileInputStream(file); + + } + +}