From 6ef3fe98bc059cb180640569487b61fda78da0e7 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 28 Apr 2022 11:29:25 +0200 Subject: [PATCH] #23225 Updated the getSharedFolderMembers method --- .classpath | 2 + .settings/org.eclipse.wst.common.component | 12 + CHANGELOG.md | 4 +- pom.xml | 10 +- .../server/StorageHubClientService.java | 457 +++++++++--------- .../server/converter/HLMapper.java | 317 ++++++------ src/test/.gitignore | 1 + src/test/java/WorkspaceInstance.java | 150 +++--- 8 files changed, 472 insertions(+), 481 deletions(-) create mode 100644 src/test/.gitignore diff --git a/.classpath b/.classpath index 2a5ed4f..02c8f44 100644 --- a/.classpath +++ b/.classpath @@ -13,6 +13,7 @@ + @@ -32,6 +33,7 @@ + diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 2409cf9..79b1c51 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,7 +1,19 @@ + + + + + + + + + + + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f9fd2d..22e92dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,11 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [v1-1-1-SNAPSHOT] - 2022-03-08 +## [v1.1.2-SNAPSHOT] - 2022-04-28 #### Enhancements -[#22782] Moved to SHUB(1.x, 3.x) +- [#23225] Updated the method to read the members of VREs ## [v1-1-0] - 2021-05-12 diff --git a/pom.xml b/pom.xml index 82d3226..70bd2aa 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - 4.0.0 @@ -10,7 +11,7 @@ org.gcube.common storagehub-client-wrapper - 1.1.1-SNAPSHOT + 1.1.2-SNAPSHOT storagehub-client-wrapper This is a wrapper of storagehub-client library. It allows to interact with storagehub in a simplified way by exposing several utilities. Moreover, It exposes by another inteface java beans as defined in (the old) HL interfaces @@ -19,9 +20,12 @@ https://code-repo.d4science.org/gCubeSystem/${project.artifactId} + 1.8 + 1.8 UTF-8 distro + @@ -88,6 +92,7 @@ org.slf4j slf4j-log4j12 1.6.4 + test @@ -96,6 +101,7 @@ 4.12 test + diff --git a/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java b/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java index e9a0bf2..a6f0a61 100644 --- a/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java +++ b/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java @@ -2,6 +2,7 @@ package org.gcube.common.storagehubwrapper.server; import java.io.InputStream; import java.net.URL; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; @@ -36,44 +37,39 @@ import org.gcube.common.storagehub.model.items.nodes.Accounting; import org.gcube.common.storagehub.model.items.nodes.ImageContent; import org.gcube.common.storagehub.model.service.Version; import org.gcube.common.storagehubwrapper.server.converter.HLMapper; -import org.gcube.common.storagehubwrapper.server.converter.ObjectMapper; import org.gcube.common.storagehubwrapper.shared.ACLType; import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceSharedFolder; import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.ItemNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - // TODO: Auto-generated Javadoc /** * The Class StorageHubClientService. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) - * Nov 22, 2018 + * Nov 22, 2018 */ public class StorageHubClientService { - /** The logger. */ private static Logger logger = LoggerFactory.getLogger(StorageHubClientService.class); - + /** The Constant ACCOUNTING_HL_NODE_NAME. */ public static final String ACCOUNTING_HL_NODE_NAME = "hl:accounting"; - + /** The scope. */ private String scope; - + /** The authorization token. */ private String authorizationToken; - - - private StorageHubClient shClient; + private StorageHubClient shClient; /** * Instantiates a new storage hub service util. * - * @param scope the scope + * @param scope the scope * @param authorizationToken the authorization token */ public StorageHubClientService(String scope, String authorizationToken) { @@ -83,19 +79,20 @@ public class StorageHubClientService { this.authorizationToken = authorizationToken; setContextProviders(scope, authorizationToken); shClient = new StorageHubClient(); - logger.info("Instancied the "+StorageHubClientService.class.getSimpleName()+" as: "+this.toString()); + logger.info("Instancied the " + StorageHubClientService.class.getSimpleName() + " as: " + this.toString()); } /** * Sets the context providers. * - * @param scope the scope + * @param scope the scope * @param authorizationToken the authorization token */ - private void setContextProviders(String scope, String authorizationToken){ + private void setContextProviders(String scope, String authorizationToken) { ScopeProvider.instance.set(scope); SecurityTokenProvider.instance.set(authorizationToken); - logger.debug("Saved the contexts [scope: "+scope+", token: "+authorizationToken.substring(0,15)+"-MASKED]"); + logger.debug( + "Saved the contexts [scope: " + scope + ", token: " + authorizationToken.substring(0, 15) + "-MASKED]"); } /** @@ -122,112 +119,114 @@ public class StorageHubClientService { return trash; } - /** * Gets the children. * - * @param id the id - * @param withAccounting the with accounting + * @param id the id + * @param withAccounting the with accounting * @param withMapProperties the with map properties * @return the children * @throws Exception the exception */ - public List getChildren(String id, boolean withAccounting, boolean withMapProperties) throws Exception{ + public List getChildren(String id, boolean withAccounting, boolean withMapProperties) + throws Exception { setContextProviders(scope, authorizationToken); - logger.trace("Requesting getChildren for id: "+id+" [withAccounting: "+withAccounting+", withMapProperties: "+withMapProperties+"]"); + logger.trace("Requesting getChildren for id: " + id + " [withAccounting: " + withAccounting + + ", withMapProperties: " + withMapProperties + "]"); ListResolverTyped resolverTyped = shClient.open(id).asFolder().list(); ListResolver theResolver = resolverTyped.withContent(); - if(withAccounting) + if (withAccounting) theResolver = theResolver.withAccounting(); - if(withMapProperties) + if (withMapProperties) theResolver = theResolver.withMetadata(); return theResolver.getItems(); - + } - + /** * Gets the children. * - * @param id the id - * @param withAccounting the with accounting + * @param id the id + * @param withAccounting the with accounting * @param withMapProperties the with map properties * @return the children * @throws Exception the exception */ - public List getChildren(String id, boolean withAccounting, boolean withMapProperties, boolean includeHidden) throws Exception{ + public List getChildren(String id, boolean withAccounting, boolean withMapProperties, + boolean includeHidden) throws Exception { setContextProviders(scope, authorizationToken); - logger.trace("Requesting getChildren for id: "+id+" [withAccounting: "+withAccounting+", withMapProperties: "+withMapProperties+"]"); + logger.trace("Requesting getChildren for id: " + id + " [withAccounting: " + withAccounting + + ", withMapProperties: " + withMapProperties + "]"); if (!includeHidden) { return getChildren(id, withAccounting, withMapProperties); } - ListResolver theResolver = shClient.open(id).asFolder().list().includeHidden().withContent(); + ListResolver theResolver = shClient.open(id).asFolder().list().includeHidden().withContent(); - if(withAccounting) + if (withAccounting) theResolver = theResolver.withAccounting(); - if(withMapProperties) + if (withMapProperties) theResolver = theResolver.withMetadata(); return theResolver.getItems(); - - } + } /** * Gets the filtered children. * - * @param id the id - * @param aType the a type - * @param withAccounting the with accounting + * @param id the id + * @param aType the a type + * @param withAccounting the with accounting * @param withMapProperties the with map properties * @return the filtered children * @throws Exception the exception */ - public List getFilteredChildren(String id, Class aType, boolean withAccounting, boolean withMapProperties) throws Exception{ + public List getFilteredChildren(String id, Class aType, boolean withAccounting, + boolean withMapProperties) throws Exception { setContextProviders(scope, authorizationToken); ListResolver resolverTyped = shClient.open(id).asFolder().list().ofType(aType); ListResolver theResolver = resolverTyped.withContent(); - if(withAccounting) + if (withAccounting) theResolver = theResolver.withAccounting(); - if(withMapProperties) + if (withMapProperties) theResolver = theResolver.withMetadata(); - + return theResolver.getItems(); } - /** * Gets the item. * - * @param itemId the item id + * @param itemId the item id * @param withAccounting the with accounting - * @param withMetadata the with metadata + * @param withMetadata the with metadata * @return the item * @throws Exception the exception */ - public Item getItem(String itemId, boolean withAccounting, boolean withMetadata) throws Exception{ + public Item getItem(String itemId, boolean withAccounting, boolean withMetadata) throws Exception { setContextProviders(scope, authorizationToken); ItemContainer itemCont; - //I'M GOING TO REMAIN THESE OPTIONS FOR POSSIBLE FUTURE SUPPORTING ON SHUB - if(withMetadata) { - itemCont = shClient.open(itemId).asItem(); //TODO - }else if(withAccounting){ - itemCont = shClient.open(itemId).asItem(); //TODO - } - else { + // I'M GOING TO REMAIN THESE OPTIONS FOR POSSIBLE FUTURE SUPPORTING ON SHUB + if (withMetadata) { + itemCont = shClient.open(itemId).asItem(); // TODO + } else if (withAccounting) { + itemCont = shClient.open(itemId).asItem(); // TODO + } else { itemCont = shClient.open(itemId).asItem(); } - + return itemCont.get(); } + /** * * @param folderId @@ -235,11 +234,11 @@ public class StorageHubClientService { * @return * @throws Exception */ - public Item getItemByPath(String folderId, String relativePath) throws Exception { + public Item getItemByPath(String folderId, String relativePath) throws Exception { setContextProviders(scope, authorizationToken); FolderContainer itemCont = shClient.open(folderId).asFolder(); FolderContainer attachmentFolder = null; - try { + try { OpenResolver oRes = itemCont.openByRelativePath(relativePath); attachmentFolder = oRes.asFolder(); return (Item) attachmentFolder.get(); @@ -256,29 +255,25 @@ public class StorageHubClientService { * @return the item * @throws Exception the exception */ - public Item getItem(String itemId) throws Exception{ - return getItem(itemId,false,true); + public Item getItem(String itemId) throws Exception { + return getItem(itemId, false, true); } - - /** * Sets the metadata and returns the Item with metadata updated. * - * @param itemId the item id + * @param itemId the item id * @param metadata the metadata * @return the item * @throws Exception the exception */ - public Item setMetadata(String itemId, Metadata metadata) throws Exception{ + public Item setMetadata(String itemId, Metadata metadata) throws Exception { setContextProviders(scope, authorizationToken); ItemContainer itemCont = shClient.open(itemId).asItem(); itemCont.setMetadata(metadata); return itemCont.get(); } - - /** * Gets the metadata. * @@ -286,17 +281,16 @@ public class StorageHubClientService { * @return the metadata * @throws Exception the exception */ - public Map getMetadata(String itemId) throws Exception{ + public Map getMetadata(String itemId) throws Exception { setContextProviders(scope, authorizationToken); ItemContainer itemCont = shClient.open(itemId).asItem(); Metadata metadata = shClient.open(itemId).asItem().get().getMetadata(); - if(metadata!=null) + if (metadata != null) return metadata.getMap(); return null; } - /** * Gets the folder container. * @@ -304,12 +298,11 @@ public class StorageHubClientService { * @return the folder container * @throws Exception the exception */ - public FolderContainer getFolderContainer(String itemId) throws Exception{ + public FolderContainer getFolderContainer(String itemId) throws Exception { setContextProviders(scope, authorizationToken); return shClient.open(itemId).asFolder(); } - /** * Gets the parents. * @@ -320,15 +313,14 @@ public class StorageHubClientService { public List getParents(String itemId) throws Exception { setContextProviders(scope, authorizationToken); ListResolver toReturn = shClient.open(itemId).asItem().getAnchestors(); - if(toReturn==null || toReturn.getItems()==null){ - logger.warn("Parent List of item id "+itemId+" is null"); + if (toReturn == null || toReturn.getItems() == null) { + logger.warn("Parent List of item id " + itemId + " is null"); return null; } return toReturn.getItems(); } - //TODO MAP OF SHARED ROOT ID - + // TODO MAP OF SHARED ROOT ID /** * Gets the id shared folder. @@ -342,7 +334,6 @@ public class StorageHubClientService { return getRootSharedFolder(itemId).getId(); } - /** * Gets the root shared folder. * @@ -356,7 +347,6 @@ public class StorageHubClientService { } - /** * Checks if is item shared. * @@ -369,8 +359,7 @@ public class StorageHubClientService { return shClient.open(itemId).asItem().get().isShared(); } - - + /** * Can write. * @@ -383,7 +372,6 @@ public class StorageHubClientService { return shClient.open(folderContainerId).asFolder().canWrite(); } - /** * Gets the root shared folder. * @@ -395,20 +383,21 @@ public class StorageHubClientService { FolderContainer rootSharedFolder = null; Item item = itemContainer.get(); - if(item.isShared()){ + if (item.isShared()) { rootSharedFolder = itemContainer.getRootSharedFolder(); - }else - throw new Exception("The item with id: "+item.getId() +" is not shared"); + } else + throw new Exception("The item with id: " + item.getId() + " is not shared"); - Validate.notNull(rootSharedFolder, "The root shared folder with children id "+item.getId()+" does not exist"); + Validate.notNull(rootSharedFolder, + "The root shared folder with children id " + item.getId() + " does not exist"); return rootSharedFolder.get(); } /** * Creates the folder. * - * @param parentId the parent id - * @param folderName the folder name + * @param parentId the parent id + * @param folderName the folder name * @param folderDescription the folder description * @return the item * @throws Exception the exception @@ -419,7 +408,6 @@ public class StorageHubClientService { return getItem(folderContainer.get().getId(), false, true); } - /** * Gets the VRE folders id. * @@ -433,27 +421,24 @@ public class StorageHubClientService { WorkspaceManagerClient wsclient = AbstractPlugin.workspace().build(); try { List list = wsclient.getVreFolders(ACCOUNTING_HL_NODE_NAME); - toReturn =list.iterator().next().getParentId(); + toReturn = list.iterator().next().getParentId(); } catch (Exception e) { logger.info("This user has no VRE Folders", e); return null; } - }catch (Exception e) { - logger.error("Get VRE Folders Id ",e); - //e.printStackTrace(); + } catch (Exception e) { + logger.error("Get VRE Folders Id ", e); + // e.printStackTrace(); } return toReturn; } - - - /** * Gets the user acl for folder id. * * @param infrastructureName the infrastructure name - * @param userName the user name - * @param folderId the folder id + * @param userName the user name + * @param folderId the folder id * @return the user acl for folder id * @throws Exception the exception */ @@ -468,7 +453,8 @@ public class StorageHubClientService { List acls = client.getACL(folderId); SharedFolder sharedFolder = (SharedFolder) theFolder; - boolean found = false; //this is needed because in case o VRE Foder the permission is assigned to the group and not to the user. + boolean found = false; // this is needed because in case o VRE Foder the permission is assigned to the + // group and not to the user. for (ACL acl : acls) { if (acl.getPricipal().compareTo(userName) == 0) { found = true; @@ -477,15 +463,15 @@ public class StorageHubClientService { } if (!found && sharedFolder.isVreFolder()) { for (ACL acl : acls) { - if (acl.getPricipal().startsWith(infrastructureName)); - return acl.getAccessTypes().get(0).toString(); + if (acl.getPricipal().startsWith(infrastructureName)) + ; + return acl.getAccessTypes().get(0).toString(); } } } return "UNDEFINED"; } - /** * Gets the item children count. * @@ -503,81 +489,78 @@ public class StorageHubClientService { /** * Upload file. * - * @param folderId the folder id - * @param is the is - * @param fileName the file name + * @param folderId the folder id + * @param is the is + * @param fileName the file name * @param fileDescription the file description * @return the item * @throws Exception the exception */ - public Item uploadFile(String folderId, InputStream is, String fileName, String fileDescription) throws Exception{ + public Item uploadFile(String folderId, InputStream is, String fileName, String fileDescription) throws Exception { setContextProviders(scope, authorizationToken); FileContainer fileCont = shClient.open(folderId).asFolder().uploadFile(is, fileName, fileDescription); return fileCont.get(); } - - /** * Download file. * - * @param itemId the item id - * @param versionName the version name. If is null or empty returns the latest version of file + * @param itemId the item id + * @param versionName the version name. If is null or empty returns the + * latest version of file * @param nodeIdsToExclude the node ids to exclude * @return the stream descriptor * @throws Exception the exception */ - public StreamDescriptor downloadFile(String itemId, String versionName, String... nodeIdsToExclude) throws Exception{ + public StreamDescriptor downloadFile(String itemId, String versionName, String... nodeIdsToExclude) + throws Exception { setContextProviders(scope, authorizationToken); StreamDescriptor streamDesc; - if(versionName!=null && !versionName.isEmpty()){ + if (versionName != null && !versionName.isEmpty()) { streamDesc = shClient.open(itemId).asFile().downloadSpecificVersion(versionName); - }else{ + } else { streamDesc = shClient.open(itemId).asFile().download(nodeIdsToExclude); } - return new StreamDescriptor(streamDesc.getStream(), streamDesc.getFileName(), streamDesc.getContentType(), streamDesc.getContentLenght()); + return new StreamDescriptor(streamDesc.getStream(), streamDesc.getFileName(), streamDesc.getContentType(), + streamDesc.getContentLenght()); } - /** * Download folder. * - * @param folderId the folder id + * @param folderId the folder id * @param nodeIdsToExclude the node ids to exclude * @return the stream descriptor * @throws Exception the exception */ - public StreamDescriptor downloadFolder(String folderId, String nodeIdsToExclude) throws Exception{ + public StreamDescriptor downloadFolder(String folderId, String nodeIdsToExclude) throws Exception { setContextProviders(scope, authorizationToken); StreamDescriptor streamDesc = shClient.open(folderId).asFolder().download(nodeIdsToExclude); - return new StreamDescriptor(streamDesc.getStream(), streamDesc.getFileName(), streamDesc.getContentType(), streamDesc.getContentLenght()); + return new StreamDescriptor(streamDesc.getStream(), streamDesc.getFileName(), streamDesc.getContentType(), + streamDesc.getContentLenght()); } - - /** * Upload archive. * - * @param folderId the folder id - * @param is the is + * @param folderId the folder id + * @param is the is * @param extractionFolderName the extraction folder name * @return the item * @throws Exception the exception */ - public Item uploadArchive(String folderId, InputStream is, String extractionFolderName) throws Exception{ + public Item uploadArchive(String folderId, InputStream is, String extractionFolderName) throws Exception { setContextProviders(scope, authorizationToken); FolderContainer folderCont = shClient.open(folderId).asFolder().uploadArchive(is, extractionFolderName); return folderCont.get(); } - - /** * Gets the shared folder members. * @@ -589,18 +572,40 @@ public class StorageHubClientService { setContextProviders(scope, authorizationToken); Item item = getItem(folderId, false, true); - if(item instanceof SharedFolder){ - return ObjectMapper.toListLogins((SharedFolder)item); - }else - throw new Exception("The item with "+folderId+ " is not a Shared Folder"); + List users = null; + if (item instanceof SharedFolder) { + SharedFolder sharedFolder = (SharedFolder) item; + if (sharedFolder.isVreFolder()) { + logger.info("Reading users from VRE folder by shClient.getVreFolderManager"); + users = shClient.getVreFolderManager(sharedFolder.getTitle()).getUsers(); + if (users == null) { + logger.warn("No users found, returning empty list of logins"); + return new ArrayList(1); + } + } else { + logger.info("Reading users from ACLs"); + List listACL = shClient.open(folderId).asFolder().getAcls(); + if (listACL == null) { + throw new Exception("The item with " + folderId + " has not a valid ACLs"); + } + users = new ArrayList(listACL.size()); + for (ACL acl : listACL) { + users.add(acl.getPricipal()); + } + } + } else { + throw new Exception("The item with " + folderId + " is not a Shared Folder"); + } + + logger.info("Returning " + users.size() + " user/s"); + return users; } - /** * Find by name. * - * @param name the name + * @param name the name * @param folderId the folder id * @return the list * @throws Exception the exception @@ -610,23 +615,21 @@ public class StorageHubClientService { Item item; try { item = getItem(folderId, false, true); - }catch (Exception e) { - logger.error("Error during get item with id: "+folderId,e); + } catch (Exception e) { + logger.error("Error during get item with id: " + folderId, e); throw new ItemNotFoundException(e.getMessage()); } - if(item instanceof FolderItem || item instanceof SharedFolder || item instanceof VreFolder){ + if (item instanceof FolderItem || item instanceof SharedFolder || item instanceof VreFolder) { return shClient.open(folderId).asFolder().findByName(name).withContent().getItems(); - }else + } else throw new Exception("The input folder id is not a folder"); } - - - + /** * Search for text. * - * @param text the text + * @param text the text * @param folderId the folder id * @return the list * @throws Exception the exception @@ -636,27 +639,25 @@ public class StorageHubClientService { Item item; try { item = getItem(folderId, false, true); - }catch (Exception e) { - logger.error("Error during get item with id: "+folderId,e); + } catch (Exception e) { + logger.error("Error during get item with id: " + folderId, e); throw new ItemNotFoundException(e.getMessage()); } - if(item instanceof FolderItem || item instanceof SharedFolder || item instanceof VreFolder){ - //this does not return the trashed items - return shClient.open(folderId).asFolder().search(text,true).withContent().getItems(); - }else + if (item instanceof FolderItem || item instanceof SharedFolder || item instanceof VreFolder) { + // this does not return the trashed items + return shClient.open(folderId).asFolder().search(text, true).withContent().getItems(); + } else throw new Exception("The input folder id is not a folder"); } - - /** * Delete item by id. * * @param itemId the item id * @throws Exception the exception */ - public void deleteItemById(String itemId) throws Exception{ + public void deleteItemById(String itemId) throws Exception { Validate.notNull(itemId, "Bad request to deleteItemById, the itemId is null"); setContextProviders(scope, authorizationToken); @@ -664,76 +665,70 @@ public class StorageHubClientService { } - - /** * Open trash. * * @return the item * @throws Exception the exception */ - public Item openTrash() throws Exception{ + public Item openTrash() throws Exception { setContextProviders(scope, authorizationToken); return shClient.openTrash().get(); } - - /** * Empty trash. * * @throws Exception the exception */ - public void emptyTrash() throws Exception{ + public void emptyTrash() throws Exception { setContextProviders(scope, authorizationToken); shClient.emptyTrash(); } - - + /** * Restore thrash item. * - * @param itemId the item id + * @param itemId the item id * @param destinationFolderId the destination folder id * @return the item * @throws Exception the exception */ - public Item restoreThrashItem(String itemId, String destinationFolderId) throws Exception{ + public Item restoreThrashItem(String itemId, String destinationFolderId) throws Exception { Validate.notNull(itemId, "Bad request to restoreThrashItem, the itemId is null"); setContextProviders(scope, authorizationToken); - + GenericItemContainer container = null; - if(destinationFolderId==null) + if (destinationFolderId == null) container = shClient.restoreThrashItem(itemId); else container = shClient.restoreThrashItem(itemId, destinationFolderId); - - if(container!=null){ + + if (container != null) { Item item = container.get(); - if(item!=null){ + if (item != null) { return item; - }else + } else throw new Exception("Restoring failed, FolderItem is null"); - }else + } else throw new Exception("Restoring failed, contanier is null"); } - /** * Move item. * - * @param itemId the item id + * @param itemId the item id * @param destFolderContainer the dest folder container * @return the abstract file item * @throws Exception the exception */ - public Item moveItem(String itemId, FolderContainer destFolderContainer) throws Exception{ + public Item moveItem(String itemId, FolderContainer destFolderContainer) throws Exception { Validate.notNull(itemId, "Bad request to moveItem, the itemId is null"); Validate.notNull(destFolderContainer, "Bad request to moveItem, the itemId is null"); @@ -743,37 +738,37 @@ public class StorageHubClientService { } - /** * Copy item. * - * @param fileItemId the copy item id + * @param fileItemId the copy item id * @param destFolderContainer the dest folder container - * @param newFileName the new file name + * @param newFileName the new file name * @return the abstract file item * @throws Exception the exception */ - public AbstractFileItem copyFileItem(String fileItemId, FolderContainer destFolderContainer, String newFileName) throws Exception{ + public AbstractFileItem copyFileItem(String fileItemId, FolderContainer destFolderContainer, String newFileName) + throws Exception { Validate.notNull(fileItemId, "Bad request to copyFileItem, the fileItemId is null"); Validate.notNull(destFolderContainer, "Bad request to copyFileItem, the destFolderContainer is null"); setContextProviders(scope, authorizationToken); FileContainer copyingItem = shClient.open(fileItemId).asFile(); - String newName = newFileName!=null && !newFileName.isEmpty()?newFileName:"Copy of "+copyingItem.get().getName(); + String newName = newFileName != null && !newFileName.isEmpty() ? newFileName + : "Copy of " + copyingItem.get().getName(); FileContainer newItem = copyingItem.copy(destFolderContainer, newName); return newItem.get(); } - /** * Rename item. * - * @param itemId the item id + * @param itemId the item id * @param newName the new name * @return the item * @throws Exception the exception */ - public Item renameItem(String itemId, String newName) throws Exception{ + public Item renameItem(String itemId, String newName) throws Exception { Validate.notNull(itemId, "Bad request to renameItem, the itemId is null"); setContextProviders(scope, authorizationToken); @@ -782,7 +777,6 @@ public class StorageHubClientService { } - /** * Gets the file public link. * @@ -790,7 +784,7 @@ public class StorageHubClientService { * @return the file public link * @throws Exception the exception */ - public URL getPublicLinkForFile(String fileItemId) throws Exception{ + public URL getPublicLinkForFile(String fileItemId) throws Exception { Validate.notNull(fileItemId, "Bad request to getPublicLinkForFile, the fileItemId is null"); setContextProviders(scope, authorizationToken); @@ -798,16 +792,15 @@ public class StorageHubClientService { } - /** * Gets the public link for file version. * * @param fileItemId the file item id - * @param version the version + * @param version the version * @return the public link for file version * @throws Exception the exception */ - public URL getPublicLinkForFileVersion(String fileItemId, String version) throws Exception{ + public URL getPublicLinkForFileVersion(String fileItemId, String version) throws Exception { Validate.notNull(fileItemId, "Bad request to getPublicLinkForFile, the fileItemId is null"); Validate.notNull(version, "Bad request to getPublicLinkForFileVersion, the version is null"); @@ -817,7 +810,6 @@ public class StorageHubClientService { } - /** * Gets the list versions. * @@ -825,7 +817,7 @@ public class StorageHubClientService { * @return the list versions * @throws Exception the exception */ - public List getListVersions(String fileItemId) throws Exception{ + public List getListVersions(String fileItemId) throws Exception { Validate.notNull(fileItemId, "Bad request to getListVersions, the fileItemId is null"); @@ -840,44 +832,42 @@ public class StorageHubClientService { * @return the image content * @throws Exception the exception */ - public ImageContent getImageContent(String itemId) throws Exception{ + public ImageContent getImageContent(String itemId) throws Exception { Validate.notNull(itemId, "Bad request to getImageContent, the itemId is null"); setContextProviders(scope, authorizationToken); ItemContainer itemCont = shClient.open(itemId).asItem(); Item item = itemCont.get(); - if(item instanceof org.gcube.common.storagehub.model.items.ImageFile){ - org.gcube.common.storagehub.model.items.ImageFile imgFI = (org.gcube.common.storagehub.model.items.ImageFile) item; //?? - return imgFI.getContent(); - }else - throw new Exception("Thumbnail Data is not available for type: "+item.getClass().getSimpleName()); + if (item instanceof org.gcube.common.storagehub.model.items.ImageFile) { + org.gcube.common.storagehub.model.items.ImageFile imgFI = (org.gcube.common.storagehub.model.items.ImageFile) item; // ?? + return imgFI.getContent(); + } else + throw new Exception("Thumbnail Data is not available for type: " + item.getClass().getSimpleName()); } - /** * Gets the total items. * * @return the total items * @throws Exception the exception */ - public long getTotalItems() throws Exception{ + public long getTotalItems() throws Exception { setContextProviders(scope, authorizationToken); return shClient.getTotalItemCount(); } - - + /** * Gets the disk usage. * * @return the disk usage * @throws Exception the exception */ - public long getDiskUsage() throws Exception{ + public long getDiskUsage() throws Exception { setContextProviders(scope, authorizationToken); return shClient.getTotalVolume(); } - + /** * Gets the accounting. * @@ -885,7 +875,7 @@ public class StorageHubClientService { * @return the accounting * @throws Exception the exception */ - public Accounting getAccounting(String itemId) throws Exception{ + public Accounting getAccounting(String itemId) throws Exception { Validate.notNull(itemId, "Bad request to getAccounting, the itemId is null"); setContextProviders(scope, authorizationToken); @@ -893,8 +883,7 @@ public class StorageHubClientService { Item item = itemCont.get(); return item.getAccounting(); } - - + /** * Gets the gcube item properties. * @@ -907,38 +896,36 @@ public class StorageHubClientService { setContextProviders(scope, authorizationToken); ItemContainer itemCont = shClient.open(gcubeItemId).asItem(); Item item = itemCont.get(); - - if(item instanceof GCubeItem) { - GCubeItem theGcubeItem = (GCubeItem) item; - return theGcubeItem.getMetadata(); - }else - throw new Exception("The item with id "+gcubeItemId+" is not an istance of "+GCubeItem.class.getSimpleName()); - } - + if (item instanceof GCubeItem) { + GCubeItem theGcubeItem = (GCubeItem) item; + return theGcubeItem.getMetadata(); + } else + throw new Exception( + "The item with id " + gcubeItemId + " is not an istance of " + GCubeItem.class.getSimpleName()); + } /** * Sets the metadata. * - * @param itemId the item id + * @param itemId the item id * @param mapProperties the map properties * @throws Exception the exception */ public void setMetadata(String itemId, Map mapProperties) throws Exception { Validate.notNull(itemId, "Bad request to setMetadata the itemId is null"); setContextProviders(scope, authorizationToken); - //ItemContainer itemCont = shcClient.open(itemId).asItem(); - //itemCont.get().setMetadata(new Metadata(mapProperties)); - //item.setMetadata(new Metadata(mapProperties)); + // ItemContainer itemCont = shcClient.open(itemId).asItem(); + // itemCont.get().setMetadata(new Metadata(mapProperties)); + // item.setMetadata(new Metadata(mapProperties)); shClient.open(itemId).asItem().setMetadata(new Metadata(mapProperties)); } - /** * Sets the Folder As Hidden or visible . * * @param itemId the folder id - * @param hide true to set hide, false to set visible + * @param hide true to set hide, false to set visible * @throws Exception the exception */ public void setFolderAsHidden(String folderId, boolean hide) throws Exception { @@ -946,17 +933,16 @@ public class StorageHubClientService { setContextProviders(scope, authorizationToken); FolderContainer folderContainer = shClient.open(folderId).asFolder(); if (hide) - folderContainer.setHidden(); //will not appear in the workspace GUI + folderContainer.setHidden(); // will not appear in the workspace GUI else - folderContainer.setVisible(); //will appear in the workspace GUI + folderContainer.setVisible(); // will appear in the workspace GUI } - - + /** * * @param folderId the folder id to share - * @param users the users to share - * @param type the permission + * @param users the users to share + * @param type the permission */ public WorkspaceSharedFolder shareFolder(String folderId, Set users, ACLType type) throws Exception { Validate.notNull(folderId, "Bad request to setFolderAsHidden the folderId is null"); @@ -985,11 +971,11 @@ public class StorageHubClientService { FolderContainer fContainer = shClient.open(folderId).asFolder().share(users, acType); return HLMapper.toWorkspaceItem(fContainer.get()); } - + /** * * @param folderId the folder id to share - * @param users the users to share + * @param users the users to share */ public WorkspaceSharedFolder unshareFolder(String folderId, Set users) throws Exception { Validate.notNull(folderId, "Bad request to setFolderAsHidden the folderId is null"); @@ -1001,9 +987,9 @@ public class StorageHubClientService { /** * Adds the URL. * - * @param folderId the folder id - * @param URL the url - * @param name the name + * @param folderId the folder id + * @param URL the url + * @param name the name * @param description the description * @return the external link * @throws StorageHubException the storage hub exception @@ -1012,15 +998,14 @@ public class StorageHubClientService { Validate.notNull(folderId, "Bad request to createURL the folderId is null"); setContextProviders(scope, authorizationToken); FolderContainer folder = shClient.open(folderId).asFolder(); - + return folder.addUrl(URL, name, description).get(); } - - + /** * Sets the folder as public. * - * @param folderId the folder id + * @param folderId the folder id * @param setPublic the set public * @return boolean value of isPublicItem() * @throws Exception the exception @@ -1029,23 +1014,22 @@ public class StorageHubClientService { Validate.notNull(folderId, "Bad request to setFolderAsPublic the folderId is null"); setContextProviders(scope, authorizationToken); FolderContainer folderCont = shClient.open(folderId).asFolder(); - - if(setPublic) { + + if (setPublic) { folderCont.publish(); - logger.debug("Published the folder id: "+folderId); - }else { + logger.debug("Published the folder id: " + folderId); + } else { folderCont.unpublish(); - logger.debug("Unpublished the folder id: "+folderId); + logger.debug("Unpublished the folder id: " + folderId); } - - return shClient.open(folderId).asFolder().get().isPublicItem(); + + return shClient.open(folderId).asFolder().get().isPublicItem(); } - /** * Update description for item. * - * @param itemId the item id + * @param itemId the item id * @param newDescription the new description * @return the description updated * @throws Exception the exception @@ -1053,19 +1037,20 @@ public class StorageHubClientService { public String updateDescriptionForItem(String itemId, String newDescription) throws Exception { Validate.notNull(itemId, "Bad request the itemId is null"); setContextProviders(scope, authorizationToken); - + shClient.open(itemId).asItem().setDescription(newDescription); return shClient.open(itemId).asItem().get().getDescription(); - - } + } /** * To string. * * @return the string */ - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see java.lang.Object#toString() */ @Override @@ -1075,13 +1060,11 @@ public class StorageHubClientService { builder.append("StorageHubClientService [scope="); builder.append(scope); builder.append(", authorizationToken="); - builder.append(authorizationToken.substring(0, authorizationToken.length()-5)+"XXXXX"); + builder.append(authorizationToken.substring(0, authorizationToken.length() - 5) + "XXXXX"); builder.append(", itemManagerClient="); builder.append(shClient); builder.append("]"); return builder.toString(); } - - } diff --git a/src/main/java/org/gcube/common/storagehubwrapper/server/converter/HLMapper.java b/src/main/java/org/gcube/common/storagehubwrapper/server/converter/HLMapper.java index 35bc464..02cec89 100644 --- a/src/main/java/org/gcube/common/storagehubwrapper/server/converter/HLMapper.java +++ b/src/main/java/org/gcube/common/storagehubwrapper/server/converter/HLMapper.java @@ -39,30 +39,16 @@ import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItemType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** * The Class HLMapper. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) - * Jun 20, 2018 + * Jun 20, 2018 */ public class HLMapper { private static Logger logger = LoggerFactory.getLogger(HLMapper.class); -// public static Function toAccountingEntry = new Function() { -// -// public AccountingEntry apply(org.gcube.common.storagehub.model.items.nodes.accounting.AccountEntry accountingEntry) { -// -// return new AccountingEntry( -// accountingEntry.getUser(), -// accountingEntry.getDate(), -// accountingEntry.getType(), -// accountingEntry.getVersion(), -// accountingEntry.getPrimaryType()); -// } -// } - /** * To URL file. * @@ -74,232 +60,227 @@ public class HLMapper { return (URLFile) toWorkspaceItem(extLink, false, false, false); } - /** * To workspace item. * - * @param the generic type + * @param the generic type * @param item the item * @return the t * @throws Exception the exception */ - public static T toWorkspaceItem(Item item) throws Exception{ + public static T toWorkspaceItem(Item item) throws Exception { return toWorkspaceItem(item, false, false, false); } - - /** * To accounting entries. * * @param item the item * @return the list */ - public static List toAccountingEntries(Item item){ + public static List toAccountingEntries(Item item) { - List accountingEntries = null; + List accountingEntries = null; Accounting accounting = item.getAccounting(); - if(accounting!=null){ - accountingEntries = accounting.getEntries(); -// for (AccountEntry ae : accounting.getEntries()) { -// accountingEntries.add(toAccountingEntry.apply(ae)); -// } - } + if (accounting != null) { + accountingEntries = accounting.getEntries(); + } - return accountingEntries; + return accountingEntries; } - - /** * To property map. * * @param metadata the metadata * @return the property map */ - public static PropertyMap toPropertyMap(Metadata metadata){ + public static PropertyMap toPropertyMap(Metadata metadata) { - PropertyMap pm = null; - if(metadata!=null) + PropertyMap pm = null; + if (metadata != null) pm = new PropertyMap(metadata.getMap()); - //IN CASE OF EMPTY MAP RETURNS NULL - if(pm!=null && pm.getValues()!=null && pm.getValues().isEmpty()) + // IN CASE OF EMPTY MAP RETURNS NULL + if (pm != null && pm.getValues() != null && pm.getValues().isEmpty()) pm = null; return pm; } - /** * To workspace file version. * * @param fileVersion the file version * @return the workspace file version */ - public static WorkspaceFileVersion toWorkspaceFileVersion(Version fileVersion){ + public static WorkspaceFileVersion toWorkspaceFileVersion(Version fileVersion) { WorkspaceFileVersion wsFileVersion = new WorkspaceFileVersion(); wsFileVersion.setId(fileVersion.getId()); wsFileVersion.setName(fileVersion.getName()); wsFileVersion.setCreated(fileVersion.getCreated()); wsFileVersion.setCurrentVersion(fileVersion.isCurrent()); - //TODO MUST BE TERMINATED + // TODO MUST BE TERMINATED return wsFileVersion; } /** * To workspace item. * - * @param the generic type - * @param item the item - * @param withAccounting the with accounting - * @param withFileDetails the with file details + * @param the generic type + * @param item the item + * @param withAccounting the with accounting + * @param withFileDetails the with file details * @param withMapProperties the with map properties * @return the t * @throws Exception the exception */ - public static T toWorkspaceItem(Item item, boolean withAccounting, boolean withFileDetails, boolean withMapProperties) throws Exception{ + public static T toWorkspaceItem(Item item, boolean withAccounting, + boolean withFileDetails, boolean withMapProperties) throws Exception { - try{ - List accountingEntries = null; + try { + List accountingEntries = null; - if(withAccounting) + if (withAccounting) accountingEntries = toAccountingEntries(item); - org.gcube.common.storagehubwrapper.shared.tohl.impl.WorkspaceItem theItem = null; - WorkspaceItemType type = null; - Boolean isFolder = false; + org.gcube.common.storagehubwrapper.shared.tohl.impl.WorkspaceItem theItem = null; + WorkspaceItemType type = null; + Boolean isFolder = false; - String itemName = item.getName(); - boolean isRoot = false; + String itemName = item.getName(); + boolean isRoot = false; - logger.trace("The item: "+item); - logger.trace("Converting Item: "+item.getName() + " with id: "+item.getId()); + logger.trace("The item: " + item); + logger.trace("Converting Item: " + item.getName() + " with id: " + item.getId()); - //THE ITEM IS A KIND OF FOLDER - if (item instanceof FolderItem){ - isFolder = true; - FolderItem folderItem = (FolderItem) item; //?? + // THE ITEM IS A KIND OF FOLDER + if (item instanceof FolderItem) { + isFolder = true; + FolderItem folderItem = (FolderItem) item; // ?? theItem = new WorkspaceFolder(); type = WorkspaceItemType.FOLDER; - if (item instanceof SharedFolder || item instanceof VreFolder) { - SharedFolder sharedfolder = (SharedFolder) item; //?? - theItem = new WorkspaceSharedFolder(); + if (item instanceof SharedFolder || item instanceof VreFolder) { + SharedFolder sharedfolder = (SharedFolder) item; // ?? + theItem = new WorkspaceSharedFolder(); type = WorkspaceItemType.SHARED_FOLDER; - itemName = sharedfolder.getTitle(); //IN CASE OF SHARED FOLDER THE NAME IS AN UUID, I'M USING THE TITLE + itemName = sharedfolder.getTitle(); // IN CASE OF SHARED FOLDER THE NAME IS AN UUID, I'M USING THE + // TITLE - if (sharedfolder.isVreFolder()){ - //logger.debug("Converting shared folder: "+theItem.getClass()); - //VreFolder vreFolder = (VreFolder) item; - //theItem = new WorkspaceVREFolder(); //NEVER INSTANCE THE WorkspaceVREFolder because VreFolder is never used by HL/StorageHub - itemName = sharedfolder.getDisplayName(); //IN CASE OF VRE FOLDER I'M USING THE DISPLAYNAME + if (sharedfolder.isVreFolder()) { + // logger.debug("Converting shared folder: "+theItem.getClass()); + // VreFolder vreFolder = (VreFolder) item; + // theItem = new WorkspaceVREFolder(); //NEVER INSTANCE THE WorkspaceVREFolder + // because VreFolder is never used by HL/StorageHub + itemName = sharedfolder.getDisplayName(); // IN CASE OF VRE FOLDER I'M USING THE DISPLAYNAME ((WorkspaceSharedFolder) theItem).setVreFolder(true); type = WorkspaceItemType.VRE_FOLDER; } - } + } - boolean isPublicItem = folderItem.isPublicItem(); - //((WorkspaceFolder) theItem).setPublicFolder(folderItem.isPublicItem()); - //logger.debug("Wrapped as Folder"); + boolean isPublicItem = folderItem.isPublicItem(); + // ((WorkspaceFolder) theItem).setPublicFolder(folderItem.isPublicItem()); + // logger.debug("Wrapped as Folder"); - //TODO THIS MUST BE REMOVED. Checking the old property isPublic added as "true" by HL - if(!isPublicItem){ - try{ - //Map map = item.getPropertyMap().getValues(); + // TODO THIS MUST BE REMOVED. Checking the old property isPublic added as + // "true" by HL + if (!isPublicItem) { + try { + // Map map = item.getPropertyMap().getValues(); // logger.debug("Property Map size: "+map.size()); // for (String key : map.keySet()) { // logger.debug("Key: "+key+ "value: "+map.get(key)); // } - String isPublic = (String) item.getMetadata().getMap().get("isPublic"); - logger.debug("The item name: "+item.getName()+ " has isPublic property like: "+isPublic); - isPublicItem = isPublic!=null?isPublic.toLowerCase().contains("true"):false; - }catch(Exception e){ - //silent - } - } + String isPublic = (String) item.getMetadata().getMap().get("isPublic"); + logger.debug("The item name: " + item.getName() + " has isPublic property like: " + isPublic); + isPublicItem = isPublic != null ? isPublic.toLowerCase().contains("true") : false; + } catch (Exception e) { + // silent + } + } - ((WorkspaceFolder) theItem).setPublicFolder(isPublicItem); - - } + ((WorkspaceFolder) theItem).setPublicFolder(isPublicItem); - //THE ITEM IS A KIND OF FILE - if(item instanceof AbstractFileItem){ - theItem = new FileItem(); - type = WorkspaceItemType.FILE_ITEM; - FileItemType fileItemType = null; - if(item instanceof ExternalURL){ - theItem = new URLFileItem(); - fileItemType = FileItemType.URL_DOCUMENT; - }else if(item instanceof GenericFileItem){ - GenericFileItem gFI = (GenericFileItem) item; //?? - fileItemType = FileItemType.DOCUMENT; - }else if(item instanceof PDFFileItem){ - theItem = new PDFFile(); - fileItemType = FileItemType.PDF_DOCUMENT; - if(withFileDetails){ - PDFFileItem pdfFI = (PDFFileItem) item; - PDFContent pdfContent = pdfFI.getContent(); - PDFFile thePDFFileItem = (PDFFile) theItem; - thePDFFileItem.setNumberOfPages(pdfContent.getNumberOfPages()); - thePDFFileItem.setProducer(pdfContent.getProducer()); - thePDFFileItem.setVersion(pdfContent.getVersion()); - thePDFFileItem.setTitle(pdfContent.getTitle()); - thePDFFileItem.setAuthor(pdfContent.getAuthor()); - } - }else if(item instanceof org.gcube.common.storagehub.model.items.ImageFile){ - theItem = new ImageFile(); - fileItemType = FileItemType.IMAGE_DOCUMENT; - if(withFileDetails){ - org.gcube.common.storagehub.model.items.ImageFile imgFI = (org.gcube.common.storagehub.model.items.ImageFile) item; //?? - ImageFile theImageFileItem = (ImageFile) theItem; - ImageContent content = imgFI.getContent(); - theImageFileItem.setHeight(content.getHeight()); - theImageFileItem.setWidth(content.getWidth()); - theImageFileItem.setThumbnailWidth(content.getThumbnailHeight()); - theImageFileItem.setThumbnailHeight(content.getThumbnailHeight()); - } - } + } - AbstractFileItem itemAb = (AbstractFileItem) item; - Content aC = itemAb.getContent(); - FileItem theFileItem = (FileItem) theItem; - theFileItem.setFileItemType(fileItemType); + // THE ITEM IS A KIND OF FILE + if (item instanceof AbstractFileItem) { + theItem = new FileItem(); + type = WorkspaceItemType.FILE_ITEM; + FileItemType fileItemType = null; + if (item instanceof ExternalURL) { + theItem = new URLFileItem(); + fileItemType = FileItemType.URL_DOCUMENT; + } else if (item instanceof GenericFileItem) { + GenericFileItem gFI = (GenericFileItem) item; // ?? + fileItemType = FileItemType.DOCUMENT; + } else if (item instanceof PDFFileItem) { + theItem = new PDFFile(); + fileItemType = FileItemType.PDF_DOCUMENT; + if (withFileDetails) { + PDFFileItem pdfFI = (PDFFileItem) item; + PDFContent pdfContent = pdfFI.getContent(); + PDFFile thePDFFileItem = (PDFFile) theItem; + thePDFFileItem.setNumberOfPages(pdfContent.getNumberOfPages()); + thePDFFileItem.setProducer(pdfContent.getProducer()); + thePDFFileItem.setVersion(pdfContent.getVersion()); + thePDFFileItem.setTitle(pdfContent.getTitle()); + thePDFFileItem.setAuthor(pdfContent.getAuthor()); + } + } else if (item instanceof org.gcube.common.storagehub.model.items.ImageFile) { + theItem = new ImageFile(); + fileItemType = FileItemType.IMAGE_DOCUMENT; + if (withFileDetails) { + org.gcube.common.storagehub.model.items.ImageFile imgFI = (org.gcube.common.storagehub.model.items.ImageFile) item; // ?? + ImageFile theImageFileItem = (ImageFile) theItem; + ImageContent content = imgFI.getContent(); + theImageFileItem.setHeight(content.getHeight()); + theImageFileItem.setWidth(content.getWidth()); + theImageFileItem.setThumbnailWidth(content.getThumbnailHeight()); + theImageFileItem.setThumbnailHeight(content.getThumbnailHeight()); + } + } - if(aC!=null){ - logger.debug("The content is not null for item: "+itemAb.getId()); - theFileItem.setMimeType(aC.getMimeType()); - theFileItem.setSize(aC.getSize()); - }else - logger.warn("The content is null for item: "+itemAb.getId()); + AbstractFileItem itemAb = (AbstractFileItem) item; + Content aC = itemAb.getContent(); + FileItem theFileItem = (FileItem) theItem; + theFileItem.setFileItemType(fileItemType); - //TODO ADD VERSION NAME + if (aC != null) { + logger.debug("The content is not null for item: " + itemAb.getId()); + theFileItem.setMimeType(aC.getMimeType()); + theFileItem.setSize(aC.getSize()); + } else + logger.warn("The content is null for item: " + itemAb.getId()); - } - - //ADDING (GCUBE) PROPERTIES - PropertyMap pm = null; - if(withMapProperties || isFolder){ - //System.out.println("Setting map: "+item.getPropertyMap().getValues()); - pm = toPropertyMap(item.getMetadata()); - } - - //CONVERTING EXTERNALLINK - if(item instanceof ExternalLink) { - logger.trace("FOUND an ExternalLink..."); - type = WorkspaceItemType.URL_ITEM; - ExternalLink extLink = (ExternalLink) item; - theItem = new URLFile(); - URLFile theURLFile = (URLFile) theItem; - theURLFile.setValue(extLink.getValue()); - } + // TODO ADD VERSION NAME - //CONVERTING TRASH ITEM - if(item.isTrashed() && item instanceof TrashItem){ + } + + // ADDING (GCUBE) PROPERTIES + PropertyMap pm = null; + if (withMapProperties || isFolder) { + // System.out.println("Setting map: "+item.getPropertyMap().getValues()); + pm = toPropertyMap(item.getMetadata()); + } + + // CONVERTING EXTERNALLINK + if (item instanceof ExternalLink) { + logger.trace("FOUND an ExternalLink..."); + type = WorkspaceItemType.URL_ITEM; + ExternalLink extLink = (ExternalLink) item; + theItem = new URLFile(); + URLFile theURLFile = (URLFile) theItem; + theURLFile.setValue(extLink.getValue()); + } + + // CONVERTING TRASH ITEM + if (item.isTrashed() && item instanceof TrashItem) { type = WorkspaceItemType.TRASH_ITEM; TrashItem trashItem = (TrashItem) item; - itemName = trashItem.getTitle(); //IN CASE OF TRASHED ITEM THE NAME IS THE ID, SO I'M USING THE TITLE + itemName = trashItem.getTitle(); // IN CASE OF TRASHED ITEM THE NAME IS THE ID, SO I'M USING THE TITLE theItem = new org.gcube.common.storagehubwrapper.shared.tohl.impl.TrashItem(); org.gcube.common.storagehubwrapper.shared.tohl.impl.TrashItem theTrashItem = (org.gcube.common.storagehubwrapper.shared.tohl.impl.TrashItem) theItem; theTrashItem.setDeletedBy(trashItem.getDeletedBy()); @@ -309,16 +290,16 @@ public class HLMapper { theTrashItem.setLenght(trashItem.getLenght()); theTrashItem.setMimeType(trashItem.getMimeType()); - isFolder = trashItem.isFolder(); //DO NOT MOVE THIS SET - if(isFolder) //Avoiding null exception on + isFolder = trashItem.isFolder(); // DO NOT MOVE THIS SET + if (isFolder) // Avoiding null exception on type = WorkspaceItemType.TRASH_FOLDER; logger.debug("Wrapped as TrashItem"); } - if(item instanceof GCubeItem){ + if (item instanceof GCubeItem) { type = WorkspaceItemType.FILE_ITEM; - GCubeItem gcubeItem = (GCubeItem) item; //?? + GCubeItem gcubeItem = (GCubeItem) item; // ?? theItem = new GcubeItem(); GcubeItem theGcubeItem = (GcubeItem) theItem; theGcubeItem.setFileItemType(FileItemType.GCUBE_ITEM); @@ -326,7 +307,7 @@ public class HLMapper { theGcubeItem.setCreator(gcubeItem.getCreator()); theGcubeItem.setItemType(gcubeItem.getItemType()); theGcubeItem.setShared(gcubeItem.isShared()); - if(withMapProperties){ + if (withMapProperties) { PropertyMap property = toPropertyMap(gcubeItem.getProperty()); theGcubeItem.setProperty(property); } @@ -334,12 +315,12 @@ public class HLMapper { logger.debug("Wrapped as GcubeItem"); } - if(theItem==null){ + if (theItem == null) { logger.info("Mapping unknown object as simple FileItem"); theItem = new FileItem(); type = WorkspaceItemType.FILE_ITEM; FileItem theFileItem = (FileItem) theItem; - theFileItem.setFileItemType(FileItemType.DOCUMENT); + theFileItem.setFileItemType(FileItemType.DOCUMENT); } theItem.setId(item.getId()); @@ -359,23 +340,19 @@ public class HLMapper { theItem.setAccounting(accountingEntries); theItem.setType(type); theItem.setFolder(isFolder); - theItem.setRoot(isRoot); //THIS IS ALWAYS FALSE. SHUB DOES NOT RETURN THIS INFORMATION + theItem.setRoot(isRoot); // THIS IS ALWAYS FALSE. SHUB DOES NOT RETURN THIS INFORMATION theItem.setPropertyMap(pm); - logger.debug("Wrapped WsItem: "+theItem); - logger.debug("Wrapped WsItem has type: "+theItem.getClass().getName()); + logger.debug("Wrapped WsItem: " + theItem); + logger.debug("Wrapped WsItem has type: " + theItem.getClass().getName()); return (T) theItem; - }catch (Exception e){ - logger.error("Error on converting the item with id: "+item.getId(), e); + } catch (Exception e) { + logger.error("Error on converting the item with id: " + item.getId(), e); throw e; } - } - - - } diff --git a/src/test/.gitignore b/src/test/.gitignore new file mode 100644 index 0000000..cb6eb2c --- /dev/null +++ b/src/test/.gitignore @@ -0,0 +1 @@ +/resources/ diff --git a/src/test/java/WorkspaceInstance.java b/src/test/java/WorkspaceInstance.java index 59ee79e..c7e1c8f 100644 --- a/src/test/java/WorkspaceInstance.java +++ b/src/test/java/WorkspaceInstance.java @@ -14,29 +14,24 @@ import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.WrongItemTypeEx import org.gcube.common.storagehubwrapper.shared.tohl.impl.FileItem; import org.gcube.common.storagehubwrapper.shared.tohl.impl.ImageFile; import org.gcube.common.storagehubwrapper.shared.tohl.impl.PDFFile; -import org.junit.Before; import org.junit.Test; - /** * */ /** * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Jun 20, 2018 + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jun 20, 2018 */ public class WorkspaceInstance { - public static String SCOPE = "/gcube"; - //public static String SCOPE = "/d4science.research-infrastructures.eu"; + // public static String SCOPE = "/gcube"; + public static String SCOPE = "/d4science.research-infrastructures.eu"; - - //public static String USERNAME = "massimiliano.assante"; + // public static String USERNAME = "massimiliano.assante"; public static String USERNAME = "francesco.mangiacrapa"; - public static String TOKEN = System.getenv("TOKEN_gcube"); //YOU MUST SET THIS AS Environment variable - + public static String TOKEN = System.getenv("TOKEN_gcube"); // YOU MUST SET THIS AS Environment variable public static String FIND_FILE_NAME = "francesco"; public static String rootId = null; @@ -44,21 +39,20 @@ public class WorkspaceInstance { private WorkspaceStorageHubClientService workspace; - //@Before - public void init(){ + // @Before + public void init() { - if(TOKEN==null) { + if (TOKEN == null) { System.err.println("You must set the token as Environment variable"); - return; } - System.out.println("Read TOKEN_gcube: "+TOKEN); + System.out.println("Read TOKEN_gcube: " + TOKEN); - //METHOD 1 + // METHOD 1 storageHubWrapper = new StorageHubWrapper(SCOPE, TOKEN, false, false, true); - //workspace = storageHubWrapper.getWorkspace(); + // workspace = storageHubWrapper.getWorkspace(); - //METHOD 2 + // METHOD 2 // StorageHubClientService storageHubClientService = new StorageHubClientService(SCOPE, TOKEN); // workspace = new WorkspaceStorageHubClientService.WorkspaceStorageHubClientServiceBuilder(storageHubClientService). // withAccounting(false). @@ -66,41 +60,41 @@ public class WorkspaceInstance { // build(); } - //@Test + // @Test public void getRoot() { System.out.println("Get Root test"); WorkspaceFolder root; try { root = storageHubWrapper.getWorkspace().getRoot(); rootId = root.getId(); - System.out.println("The root is:"+root); - } - catch (Exception e) { + System.out.println("The root is:" + root); + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } - //@Test - public void getChildren() throws Exception{ + // @Test + public void getChildren() throws Exception { System.out.println("Get children test"); WorkspaceFolder root = storageHubWrapper.getWorkspace().getRoot(); List children = storageHubWrapper.getWorkspace().getChildren(root.getId()); - //WorkspaceFolder root = workspace.getRoot(); - //List children = workspace.getChildren(root.getId()); + // WorkspaceFolder root = workspace.getRoot(); + // List children = workspace.getChildren(root.getId()); int i = 0; for (WorkspaceItem workspaceItem : children) { - System.out.println(++i+")"+workspaceItem.getId() + " name: "+workspaceItem.getName() + " type: "+workspaceItem.getType() +" class: "+workspaceItem.getClass()); + System.out.println(++i + ")" + workspaceItem.getId() + " name: " + workspaceItem.getName() + " type: " + + workspaceItem.getType() + " class: " + workspaceItem.getClass()); } } - - //@Test - public void getPathOnShared() throws Exception{ + + // @Test + public void getPathOnShared() throws Exception { System.out.println("Get Path on Shared"); WorkspaceFolder root = storageHubWrapper.getWorkspace().getRoot(); @@ -108,30 +102,30 @@ public class WorkspaceInstance { int i = 0; for (WorkspaceItem workspaceItem : children) { - if(workspaceItem.isShared()) { - System.out.println(++i+")"+workspaceItem.getId() + " name: "+workspaceItem.getName() + " type: "+workspaceItem.getPath()); + if (workspaceItem.isShared()) { + System.out.println(++i + ")" + workspaceItem.getId() + " name: " + workspaceItem.getName() + " type: " + + workspaceItem.getPath()); } } } - - //@Test - public void getFilteredChildren() throws Exception{ + // @Test + public void getFilteredChildren() throws Exception { System.out.println("Get children test"); WorkspaceFolder root = storageHubWrapper.getWorkspace().getRoot(); Class filterClass = org.gcube.common.storagehub.model.items.FolderItem.class; - List children = storageHubWrapper.getWorkspace().getFilteredChildren(root.getId(), filterClass); + List children = storageHubWrapper.getWorkspace().getFilteredChildren(root.getId(), + filterClass); - //WorkspaceFolder root = workspace.getRoot(); - //List children = workspace.getChildren(root.getId()); + // WorkspaceFolder root = workspace.getRoot(); + // List children = workspace.getChildren(root.getId()); int i = 0; for (WorkspaceItem workspaceItem : children) { - System.out.println(++i+")"+workspaceItem); - System.out.println(workspaceItem.getType() +" "+workspaceItem.getClass()); - + System.out.println(++i + ")" + workspaceItem); + System.out.println(workspaceItem.getType() + " " + workspaceItem.getClass()); // System.out.println(++i+")"); // if(workspaceItem.isFolder()){ @@ -145,33 +139,50 @@ public class WorkspaceInstance { } - public static void printMap(WorkspaceItem workspaceItem){ - if(workspaceItem.getPropertyMap()!=null){ - System.out.println(workspaceItem.getId() + " name: "+workspaceItem.getName() + " isFolder: "+workspaceItem.isFolder()); - System.out.println("Property Map: "+workspaceItem.getPropertyMap().getValues().toString()); + public static void printMap(WorkspaceItem workspaceItem) { + if (workspaceItem.getPropertyMap() != null) { + System.out.println(workspaceItem.getId() + " name: " + workspaceItem.getName() + " isFolder: " + + workspaceItem.isFolder()); + System.out.println("Property Map: " + workspaceItem.getPropertyMap().getValues().toString()); } } - //@Test - public void getWorkspaceId() { + // @Test + public void getWorkspaceId() { System.out.println("Getting the workspaceId test"); try { Workspace ws = storageHubWrapper.getWorkspace(); - //Workspace ws = workspace; + // Workspace ws = workspace; - if(ws!=null){ + if (ws != null) { WorkspaceFolder root = ws.getRoot(); - System.out.println("Retrieve the root "+root.getId()+" correctly"); + System.out.println("Retrieve the root " + root.getId() + " correctly"); } } catch (Exception e) { e.printStackTrace(); } } - //@Test - public void getChildrenPerWorkspaceItem() throws InternalErrorException{ + @Test + public void getSharedFolderMembers() { + System.out.println("Getting getSharedFolderMembers"); + try { + + String sharedFolderId = "b679c6fe-894c-4c4c-aebc-4155321a373a"; + storageHubWrapper = new StorageHubWrapper(SCOPE, TOKEN); + List listLogins = storageHubWrapper.getWorkspace().getSharedFolderMembers(sharedFolderId); + + System.out.println("List: " + listLogins); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + // @Test + public void getChildrenPerWorkspaceItem() throws InternalErrorException { System.out.println("Get children test per workspace item"); try { @@ -183,11 +194,11 @@ public class WorkspaceInstance { // List children = workspace.getChildren(root.getId()); int i = 0; - Map> map = new HashMap>(); + Map> map = new HashMap>(); for (WorkspaceItem workspaceItem : children) { String key = workspaceItem.getType().name(); List list = map.get(key); - if(list==null){ + if (list == null) { list = new ArrayList(); } @@ -204,20 +215,20 @@ public class WorkspaceInstance { switch (fileItem.getFileItemType()) { case PDF_DOCUMENT: PDFFile pdfFile = (PDFFile) fileItem; - System.out.println("PDF_DOCUMENT "+pdfFile); - PDFFile completeItem = (PDFFile) storageHubWrapper.getWorkspace().getItem(fileItem.getId(), true, true, true); + System.out.println("PDF_DOCUMENT " + pdfFile); + PDFFile completeItem = (PDFFile) storageHubWrapper.getWorkspace().getItem(fileItem.getId(), + true, true, true); System.out.println(completeItem); break; case IMAGE_DOCUMENT: ImageFile imageFile = (ImageFile) fileItem; - System.out.println("IMAGE_DOCUMENT "+imageFile); + System.out.println("IMAGE_DOCUMENT " + imageFile); break; default: break; } - System.out.println(fileItem.getFileItemType() +" "+workspaceItem.getClass()); - + System.out.println(fileItem.getFileItemType() + " " + workspaceItem.getClass()); break; default: @@ -225,33 +236,32 @@ public class WorkspaceInstance { } } } - } - catch (Exception e) { + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } - //@Test - public void findFileName() throws InternalErrorException{ + // @Test + public void findFileName() throws InternalErrorException { System.out.println("Find file name test"); getRoot(); try { - //List foundItems = storageHubWrapper.getWorkspace().find(FIND_FILE_NAME,rootId); - List foundItems = workspace.find(FIND_FILE_NAME,rootId); + // List foundItems = + // storageHubWrapper.getWorkspace().find(FIND_FILE_NAME,rootId); + List foundItems = workspace.find(FIND_FILE_NAME, rootId); - if(foundItems==null || foundItems.size()==0){ - System.out.println("No Items found with name: "+FIND_FILE_NAME+ " in the parent: "+rootId); + if (foundItems == null || foundItems.size() == 0) { + System.out.println("No Items found with name: " + FIND_FILE_NAME + " in the parent: " + rootId); return; } - System.out.println("Items found with name: "+FIND_FILE_NAME+ " in the parent: "+rootId +" are:"); + System.out.println("Items found with name: " + FIND_FILE_NAME + " in the parent: " + rootId + " are:"); for (WorkspaceItem workspaceItem : foundItems) { System.out.println(workspaceItem); } - } - catch (ItemNotFoundException | WrongItemTypeException e) { + } catch (ItemNotFoundException | WrongItemTypeException e) { // TODO Auto-generated catch block e.printStackTrace(); }