From 9f283861e60af5610ca6cf19b5cdbf2087d0791c Mon Sep 17 00:00:00 2001 From: lucio Date: Tue, 15 Oct 2024 12:48:08 +0200 Subject: [PATCH] messages removed --- .settings/org.eclipse.wst.common.component | 78 +-- docker/container.ini | 2 +- pom.xml | 4 +- .../data/access/storagehub/StorageHub.java | 8 +- .../gcube/data/access/storagehub/Utils.java | 2 - .../storagehub/handlers/DownloadHandler.java | 21 +- .../handlers/StorageAccountingHandler.java | 117 +++++ .../storagehub/handlers/TrashHandler.java | 21 +- .../handlers/items/ItemHandler.java | 11 +- .../handlers/items/Node2ItemConverter.java | 18 - .../builders/FolderCreationParameters.java | 1 - .../storagehub/services/ItemsManager.java | 1 - .../storagehub/services/MessageManager.java | 450 ------------------ .../access/storagehub/types/ContentPair.java | 8 +- .../storagehub/types/MessageSharable.java | 16 - .../data/access/fs/external/CreateUsers.java | 47 +- 16 files changed, 253 insertions(+), 552 deletions(-) create mode 100644 src/main/java/org/gcube/data/access/storagehub/handlers/StorageAccountingHandler.java delete mode 100644 src/main/java/org/gcube/data/access/storagehub/services/MessageManager.java delete mode 100644 src/main/java/org/gcube/data/access/storagehub/types/MessageSharable.java diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 380cb7e..c840f39 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,5 +1,7 @@ - + + + @@ -40,7 +42,9 @@ - + + + @@ -81,7 +85,9 @@ - + + + @@ -122,62 +128,58 @@ - + + + + + + + - + + + - + + + uses - + + + - - - - - - - uses - - - - - - - - - - uses - - - - - - + + + uses - + + + - + + + @@ -218,7 +220,9 @@ - + + + @@ -259,7 +263,9 @@ - + + + @@ -300,7 +306,9 @@ - + + + diff --git a/docker/container.ini b/docker/container.ini index d1decde..6230c80 100644 --- a/docker/container.ini +++ b/docker/container.ini @@ -1,5 +1,5 @@ [node] -mode = offline +mode = online hostname = dlib29.isti.cnr.it protocol= http port = 8080 diff --git a/pom.xml b/pom.xml index 1eb763a..886c8d7 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ maven-parent org.gcube.tools - 1.1.0 + 1.2.0 4.0.0 @@ -41,7 +41,7 @@ org.gcube.distribution gcube-smartgears-bom - 3.0.1-SNAPSHOT + 4.0.0 pom import diff --git a/src/main/java/org/gcube/data/access/storagehub/StorageHub.java b/src/main/java/org/gcube/data/access/storagehub/StorageHub.java index d14c983..09615e3 100644 --- a/src/main/java/org/gcube/data/access/storagehub/StorageHub.java +++ b/src/main/java/org/gcube/data/access/storagehub/StorageHub.java @@ -3,9 +3,6 @@ package org.gcube.data.access.storagehub; import java.util.HashSet; import java.util.Set; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.core.Application; - import org.gcube.common.gxrest.response.entity.SerializableErrorEntityTextWriter; import org.gcube.data.access.storagehub.services.ACLManager; import org.gcube.data.access.storagehub.services.DocsGenerator; @@ -14,13 +11,15 @@ import org.gcube.data.access.storagehub.services.Impersonable; import org.gcube.data.access.storagehub.services.ItemSharing; import org.gcube.data.access.storagehub.services.ItemsCreator; import org.gcube.data.access.storagehub.services.ItemsManager; -import org.gcube.data.access.storagehub.services.MessageManager; import org.gcube.data.access.storagehub.services.StorageManager; import org.gcube.data.access.storagehub.services.UserManager; import org.gcube.data.access.storagehub.services.WorkspaceManager; import org.gcube.data.access.storagehub.services.admin.ScriptManager; import org.glassfish.jersey.media.multipart.MultiPartFeature; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.Application; + @Path("workspace") public class StorageHub extends Application { @@ -37,7 +36,6 @@ public class StorageHub extends Application { classes.add(UserManager.class); classes.add(GroupManager.class); classes.add(ScriptManager.class); - classes.add(MessageManager.class); classes.add(StorageManager.class); classes.add(DocsGenerator.class); classes.add(MultiPartFeature.class); diff --git a/src/main/java/org/gcube/data/access/storagehub/Utils.java b/src/main/java/org/gcube/data/access/storagehub/Utils.java index 02a11a2..dca260f 100644 --- a/src/main/java/org/gcube/data/access/storagehub/Utils.java +++ b/src/main/java/org/gcube/data/access/storagehub/Utils.java @@ -372,8 +372,6 @@ public class Utils { item.setHidden(false); } - - Node newNode = new Item2NodeConverter().getNode(destinationNode, item); if (accountingHandler!=null) { accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, login, destinationNode, false); diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java b/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java index f18df89..0715c8a 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/DownloadHandler.java @@ -5,6 +5,7 @@ import java.io.OutputStream; import java.util.Deque; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import java.util.zip.Deflater; import java.util.zip.ZipOutputStream; @@ -61,10 +62,16 @@ public class DownloadHandler { @Inject private Node2ItemConverter node2Item; + @Inject + private StorageAccountingHandler storageAccountingHandler; + public Response downloadFolderItem(Session ses, String login, FolderItem item, boolean withAccounting ) throws StorageHubException, RepositoryException { try { final Deque allNodes = compressHandler.getAllNodesForZip((FolderItem)item, login, ses, accountingHandler, Excludes.GET_ONLY_CONTENT); final org.gcube.common.storagehub.model.Path originalPath = Paths.getPath(item.getParentPath()); + + long totalSize = allNodes.stream().filter(i -> i instanceof AbstractFileItem).map(i -> (AbstractFileItem) i).collect(Collectors.summarizingLong(i -> i.getContent().getSize())).getSum(); + StreamingOutput so = new StreamingOutput() { @Override @@ -92,6 +99,7 @@ public class DownloadHandler { if (withAccounting) accountingHandler.createReadObj(item.getTitle(), null, ses, (Node) item.getRelatedNode(), login, false); + storageAccountingHandler.read(login, item.getOwner(), "application/zip", totalSize); return response; }finally { if (ses!=null) ses.save(); @@ -119,8 +127,11 @@ public class DownloadHandler { } accountingHandler.createReadObj(fileItem.getTitle(), versionName, ses, (Node) fileItem.getRelatedNode(), login, true); } + + storageAccountingHandler.read(login, fileItem.getOwner(), content.getMimeType(), content.getSize()); + StreamingOutput so = new SingleFileStreamingOutput(streamToWrite); - + return Response .ok(so) .header("content-disposition","attachment; filename = "+fileItem.getName()) @@ -129,9 +140,9 @@ public class DownloadHandler { .build(); } + public Response downloadVersionedItem(Session ses, String login, AbstractFileItem currentItem, String versionName, boolean withAccounting) throws RepositoryException, StorageHubException{ - List jcrVersions = versionHandler.getContentVersionHistory((Node)currentItem.getRelatedNode()); @@ -165,7 +176,7 @@ public class DownloadHandler { if (withAccounting) accountingHandler.createReadObj(currentItem.getTitle(), versionName, ses, (Node) currentItem.getRelatedNode(), login, true); - + storageAccountingHandler.read(login, currentItem.getOwner(), content.getMimeType(), content.getSize()); StreamingOutput so = new SingleFileStreamingOutput(streamToWrite); return Response @@ -197,6 +208,7 @@ public class DownloadHandler { String title = userMetadata.get("title"); String contentType = userMetadata.get("content-type"); + StreamingOutput so = new SingleFileStreamingOutput(streamToWrite); return Response @@ -206,7 +218,6 @@ public class DownloadHandler { .header("Content-Type", contentType) .build(); - } - + } } diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/StorageAccountingHandler.java b/src/main/java/org/gcube/data/access/storagehub/handlers/StorageAccountingHandler.java new file mode 100644 index 0000000..1a661cc --- /dev/null +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/StorageAccountingHandler.java @@ -0,0 +1,117 @@ +package org.gcube.data.access.storagehub.handlers; + +import java.net.URI; + +import org.gcube.accounting.datamodel.UsageRecord.OperationResult; +import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord; +import org.gcube.accounting.persistence.AccountingPersistence; +import org.gcube.accounting.persistence.AccountingPersistenceFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import jakarta.inject.Singleton; + +@Singleton +public class StorageAccountingHandler { + + private static final Logger log = LoggerFactory.getLogger(StorageAccountingHandler.class); + + public void update(String user, String owner, String fileType, long size) { + AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence(); + + StorageUsageRecord storageUsageRecord = new StorageUsageRecord(); + try { + storageUsageRecord.setConsumerId(user); + storageUsageRecord.setOperationResult(OperationResult.SUCCESS); + storageUsageRecord.setResourceOwner(owner); + storageUsageRecord.setProviderURI(new URI("data.d4science.org")); + storageUsageRecord.setOperationType(AbstractStorageUsageRecord.OperationType.UPDATE); + storageUsageRecord.setDataType(AbstractStorageUsageRecord.DataType.STORAGE); + storageUsageRecord.setDataVolume(size); // Quantity of data in terms of KB + + // Qualifier is not mandatory and is discarded during aggregation so you can + // avoid to set field + storageUsageRecord.setQualifier(fileType); // Qualifies the data in terms of data (e.g. MIME type for the + // Storage) + + accountingPersistence.account(storageUsageRecord); + } catch (Throwable e) { + log.error("error accounting storage read operation",e); + } + } + + public void delete(String user, String owner, String fileType, long size) { + AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence(); + + StorageUsageRecord storageUsageRecord = new StorageUsageRecord(); + try { + storageUsageRecord.setConsumerId(user); + storageUsageRecord.setOperationResult(OperationResult.SUCCESS); + storageUsageRecord.setResourceOwner(owner); + storageUsageRecord.setProviderURI(new URI("data.d4science.org")); + storageUsageRecord.setOperationType(AbstractStorageUsageRecord.OperationType.DELETE); + storageUsageRecord.setDataType(AbstractStorageUsageRecord.DataType.STORAGE); + storageUsageRecord.setDataVolume(size); // Quantity of data in terms of KB + + // Qualifier is not mandatory and is discarded during aggregation so you can + // avoid to set field + storageUsageRecord.setQualifier(fileType); // Qualifies the data in terms of data (e.g. MIME type for the + // Storage) + + accountingPersistence.account(storageUsageRecord); + } catch (Throwable e) { + log.error("error accounting storage read operation",e); + } + } + + public void create(String user, String owner, String fileType, long size) { + AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence(); + + StorageUsageRecord storageUsageRecord = new StorageUsageRecord(); + try { + storageUsageRecord.setConsumerId(user); + storageUsageRecord.setOperationResult(OperationResult.SUCCESS); + storageUsageRecord.setResourceOwner(owner); + storageUsageRecord.setProviderURI(new URI("data.d4science.org")); + storageUsageRecord.setOperationType(AbstractStorageUsageRecord.OperationType.CREATE); + storageUsageRecord.setDataType(AbstractStorageUsageRecord.DataType.STORAGE); + storageUsageRecord.setDataVolume(size); // Quantity of data in terms of KB + + // Qualifier is not mandatory and is discarded during aggregation so you can + // avoid to set field + storageUsageRecord.setQualifier(fileType); // Qualifies the data in terms of data (e.g. MIME type for the + // Storage) + + accountingPersistence.account(storageUsageRecord); + } catch (Throwable e) { + log.error("error accounting storage read operation",e); + } + } + + public void read(String user, String owner, String fileType, long size) { + AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence(); + + StorageUsageRecord storageUsageRecord = new StorageUsageRecord(); + try { + storageUsageRecord.setConsumerId(user); + storageUsageRecord.setOperationResult(OperationResult.SUCCESS); + storageUsageRecord.setResourceOwner(owner); + storageUsageRecord.setProviderURI(new URI("data.d4science.org")); + storageUsageRecord.setOperationType(AbstractStorageUsageRecord.OperationType.READ); + storageUsageRecord.setDataType(AbstractStorageUsageRecord.DataType.STORAGE); + storageUsageRecord.setDataVolume(size); // Quantity of data in terms of KB + + // Qualifier is not mandatory and is discarded during aggregation so you can + // avoid to set field + storageUsageRecord.setQualifier(fileType); // Qualifies the data in terms of data (e.g. MIME type for the + // Storage) + + accountingPersistence.account(storageUsageRecord); + } catch (Throwable e) { + log.error("error accounting storage read operation",e); + } + + } + +} diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/TrashHandler.java b/src/main/java/org/gcube/data/access/storagehub/handlers/TrashHandler.java index c4003f6..9f8a1aa 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/TrashHandler.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/TrashHandler.java @@ -9,8 +9,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; -import jakarta.inject.Inject; -import jakarta.inject.Singleton; import javax.jcr.ItemNotFoundException; import javax.jcr.Node; import javax.jcr.RepositoryException; @@ -19,6 +17,7 @@ import javax.jcr.lock.LockException; import javax.jcr.version.Version; import org.gcube.common.security.AuthorizedTasks; +import org.gcube.common.security.providers.SecretManagerProvider; import org.gcube.common.storagehub.model.Excludes; import org.gcube.common.storagehub.model.Paths; import org.gcube.common.storagehub.model.exceptions.BackendGenericError; @@ -44,6 +43,9 @@ import org.gcube.data.access.storagehub.types.ContentPair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; + @Singleton public class TrashHandler { @@ -72,6 +74,9 @@ public class TrashHandler { @Inject StorageBackendHandler storageBackendHandler; + @Inject + private StorageAccountingHandler storageAccountingHandler; + public void removeNodes(Session ses, List itemsToDelete) throws RepositoryException, StorageHubException{ log.debug("defnitively removing nodes with ids {}",itemsToDelete); for (Item item: itemsToDelete) { @@ -114,7 +119,7 @@ public class TrashHandler { StorageBackend sb = sbf.create(item.getContent().getPayloadBackend()); - contentSet.add(new ContentPair(item.getContent(), sb)); + contentSet.add(new ContentPair(item.getContent(), item.getOwner(), sb)); List versions = versionHandler.getContentVersionHistory((Node)item.getRelatedNode()); @@ -122,7 +127,7 @@ public class TrashHandler { try { Content content = node2Item.getContentFromVersion(version); if (content!= null && content.getStorageId()!=null) - contentSet.add(new ContentPair(content, sb)); + contentSet.add(new ContentPair(content, item.getOwner(), sb)); else log.warn("invalid version {}",version.getName()); }catch (Throwable t) { log.warn("error retrieving version content for {}",version.getName(),t); @@ -150,21 +155,25 @@ public class TrashHandler { } else retrieveItemsToDelete(itemsToDelete, itemToDelete); + Set contentToDelete = retrieveContentToDelete(itemsToDelete); + if (!onlyContent) nodeToDelete.remove(); String ids = itemsToDelete.stream().map((i) -> i.getId()).collect(Collectors.joining(",")); log.debug("content ids to remove are {}",ids); - Set contentToDelete = retrieveContentToDelete(itemsToDelete); Runnable deleteFromStorageRunnable = AuthorizedTasks.bind(new Runnable() { @Override public void run() { + final String login = SecretManagerProvider.get().getOwner().getClientName(); for (ContentPair cp: contentToDelete ) { try { - cp.getStorageBackend().delete(cp.getContent().getStorageId()); + Content content =cp.getContent(); + cp.getStorageBackend().delete(content.getStorageId()); log.debug("file with id {} correctly removed from storage {}",cp.getContent().getStorageId(),cp.getStorageBackend().getClass().getSimpleName()); + storageAccountingHandler.delete(login, cp.getContentOwner(), content.getMimeType(), content.getSize()); }catch(Throwable t) { log.warn("error removing file with id {} from storage {}",cp.getContent().getStorageId(), cp.getStorageBackend().getClass().getSimpleName(), t); } diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java b/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java index beef969..b6e9ebc 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java @@ -7,8 +7,6 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import jakarta.inject.Inject; -import jakarta.inject.Singleton; import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.Session; @@ -40,6 +38,7 @@ import org.gcube.common.storagehub.model.types.ItemAction; import org.gcube.data.access.storagehub.AuthorizationChecker; import org.gcube.data.access.storagehub.Utils; import org.gcube.data.access.storagehub.accounting.AccountingHandler; +import org.gcube.data.access.storagehub.handlers.StorageAccountingHandler; import org.gcube.data.access.storagehub.handlers.VersionHandler; import org.gcube.data.access.storagehub.handlers.content.ContentHandler; import org.gcube.data.access.storagehub.handlers.content.ContentHandlerFactory; @@ -54,6 +53,9 @@ import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; + @Singleton public class ItemHandler { @@ -78,6 +80,9 @@ public class ItemHandler { Node2ItemConverter node2Item; @Inject Item2NodeConverter item2Node; + + @Inject + private StorageAccountingHandler storageAccountingHandler; private static Logger log = LoggerFactory.getLogger(ItemHandler.class); @@ -299,6 +304,7 @@ public class ItemHandler { } accountingHandler.createFileUpdated(item.getTitle(), versionName, ses, newNode, login, false); ses.save(); + storageAccountingHandler.update(login, item.getOwner(), item.getContent().getMimeType(), item.getContent().getSize()); } catch (Throwable t) { log.error("error saving item", t); } finally { @@ -324,6 +330,7 @@ public class ItemHandler { newNode = item2Node.getNode(destinationNode, item); accountingHandler.createEntryCreate(item.getTitle(), ses, newNode, login, false); ses.save(); + storageAccountingHandler.create(login, item.getOwner(), item.getContent().getMimeType(), item.getContent().getSize()); } catch (Throwable t) { log.error("error saving item", t); throw new BackendGenericError(t); diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/items/Node2ItemConverter.java b/src/main/java/org/gcube/data/access/storagehub/handlers/items/Node2ItemConverter.java index 954a83d..85899a8 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/items/Node2ItemConverter.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/items/Node2ItemConverter.java @@ -38,8 +38,6 @@ import org.gcube.common.storagehub.model.items.RootItem; import org.gcube.common.storagehub.model.items.SharedFolder; import org.gcube.common.storagehub.model.items.TrashItem; import org.gcube.common.storagehub.model.items.nodes.Content; -import org.gcube.common.storagehub.model.messages.Message; -import org.gcube.data.access.storagehub.Constants; import org.gcube.data.access.storagehub.handlers.ClassHandler; import org.gcube.data.access.storagehub.predicates.ItemTypePredicate; import org.reflections.Configuration; @@ -86,22 +84,6 @@ public class Node2ItemConverter { return content; } - public Message getMessageItem(Node node) throws RepositoryException{ - if (!(node.getPrimaryNodeType().getName().equals("nthl:itemSentRequest") - || node.getPrimaryNodeType().getName().equals("nthl:itemSentRequestSH"))) - return null; - Message msg = new Message(); - try { - Node attachmentNode = node.getNode(Constants.ATTACHMENTNODE_NAME); - msg.setWithAttachments(attachmentNode.hasNodes()); - }catch (Throwable e) { - msg.setWithAttachments(false); - } - - setRootItemCommonFields(node, Collections.emptyList(), Message.class, msg); - return msg; - } - private T retrieveItem(Node node, List excludes, Class classToHandle) throws RepositoryException, BackendGenericError{ T item; diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/items/builders/FolderCreationParameters.java b/src/main/java/org/gcube/data/access/storagehub/handlers/items/builders/FolderCreationParameters.java index a9ee994..be0215e 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/items/builders/FolderCreationParameters.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/items/builders/FolderCreationParameters.java @@ -5,7 +5,6 @@ import java.util.Objects; import org.gcube.common.storagehub.model.Metadata; import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; -import org.gcube.common.storagehub.model.plugins.PluginParameters; public class FolderCreationParameters extends CreateParameters { diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java index 6274cfd..f5bae8f 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java @@ -1173,7 +1173,6 @@ public class ItemsManager extends Impersonable{ Node node = ses.getNodeByIdentifier(id); Item item = node2Item.getItem(node, Excludes.ALL); if (!(item instanceof FolderItem)) - throw new InvalidCallParameters("the item is not a folder"); return Utils.getFolderInfo(node); } catch (RepositoryException re) { log.error("error getting workspace total size", re); diff --git a/src/main/java/org/gcube/data/access/storagehub/services/MessageManager.java b/src/main/java/org/gcube/data/access/storagehub/services/MessageManager.java deleted file mode 100644 index 7724115..0000000 --- a/src/main/java/org/gcube/data/access/storagehub/services/MessageManager.java +++ /dev/null @@ -1,450 +0,0 @@ -package org.gcube.data.access.storagehub.services; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.UUID; - -import javax.jcr.ItemNotFoundException; -import javax.jcr.Node; -import javax.jcr.NodeIterator; -import javax.jcr.RepositoryException; -import javax.jcr.Session; - -import org.apache.jackrabbit.api.JackrabbitSession; -import org.apache.jackrabbit.api.security.user.User; -import org.gcube.common.gxrest.response.outbound.GXOutboundErrorResponse; -import org.gcube.common.storagehub.model.Excludes; -import org.gcube.common.storagehub.model.Paths; -import org.gcube.common.storagehub.model.exceptions.BackendGenericError; -import org.gcube.common.storagehub.model.exceptions.IdNotFoundException; -import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters; -import org.gcube.common.storagehub.model.exceptions.StorageHubException; -import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException; -import org.gcube.common.storagehub.model.items.AbstractFileItem; -import org.gcube.common.storagehub.model.items.Item; -import org.gcube.common.storagehub.model.items.nodes.Content; -import org.gcube.common.storagehub.model.items.nodes.Owner; -import org.gcube.common.storagehub.model.messages.Message; -import org.gcube.common.storagehub.model.service.ItemList; -import org.gcube.common.storagehub.model.storages.MetaInfo; -import org.gcube.common.storagehub.model.storages.StorageBackend; -import org.gcube.common.storagehub.model.storages.StorageBackendFactory; -import org.gcube.common.storagehub.model.types.ItemAction; -import org.gcube.common.storagehub.model.types.MessageList; -import org.gcube.common.storagehub.model.types.NodeProperty; -import org.gcube.data.access.storagehub.Constants; -import org.gcube.data.access.storagehub.PathUtil; -import org.gcube.data.access.storagehub.StorageHubApplicationManager; -import org.gcube.data.access.storagehub.Utils; -import org.gcube.data.access.storagehub.accounting.AccountingHandler; -import org.gcube.data.access.storagehub.handlers.TrashHandler; -import org.gcube.data.access.storagehub.handlers.items.Item2NodeConverter; -import org.gcube.data.access.storagehub.handlers.items.Item2NodeConverter.Values; -import org.gcube.data.access.storagehub.handlers.items.Node2ItemConverter; -import org.gcube.data.access.storagehub.handlers.plugins.StorageBackendHandler; -import org.gcube.data.access.storagehub.predicates.IncludeTypePredicate; -import org.gcube.data.access.storagehub.predicates.ItemTypePredicate; -import org.gcube.data.access.storagehub.repository.StoragehubRepository; -import org.gcube.data.access.storagehub.types.MessageSharable; -import org.gcube.smartgears.annotations.ManagedBy; -import org.gcube.smartgears.utils.InnerMethodName; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.webcohesion.enunciate.metadata.rs.RequestHeader; -import com.webcohesion.enunciate.metadata.rs.RequestHeaders; - -import jakarta.enterprise.context.RequestScoped; -import jakarta.inject.Inject; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.DELETE; -import jakarta.ws.rs.FormParam; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.PathParam; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.QueryParam; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.core.Response.Status; - -@Path("messages") -@ManagedBy(StorageHubApplicationManager.class) -@RequestHeaders({ - @RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"), - }) -public class MessageManager extends Impersonable{ - - private static final Logger log = LoggerFactory.getLogger(MessageManager.class); - - private final StoragehubRepository repository = StoragehubRepository.repository; - - @Inject - AccountingHandler accountingHandler; - - @RequestScoped - @PathParam("id") - String id; - - @Inject PathUtil pathUtil; - - @Inject Node2ItemConverter node2Item; - @Inject Item2NodeConverter item2Node; - - @Inject TrashHandler trashHandler; - - @Inject - StorageBackendHandler storageBackendHandler; - - @GET - @Path("{id}") - @Produces(MediaType.APPLICATION_JSON) - public Message getById(){ - InnerMethodName.set("getMessageById"); - Session ses = null; - Message toReturn = null; - try{ - ses = repository.getRepository().login(Constants.JCR_CREDENTIALS); - Node messageNode = ses.getNodeByIdentifier(id); - toReturn = node2Item.getMessageItem(messageNode); - checkRights(currentUser, toReturn); - }catch (ItemNotFoundException e) { - log.error("id {} not found",id,e); - GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND); - }catch(RepositoryException re){ - log.error("jcr error getting item", re); - GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error getting item", re)); - }catch(StorageHubException she ){ - log.error(she.getErrorMessage(), she); - GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus())); - }finally{ - if (ses!=null) - ses.logout(); - } - - return toReturn; - } - - @DELETE - @Path("{id}") - public void deleteById(){ - InnerMethodName.set("deleteMessageById"); - Session ses = null; - try{ - ses = repository.getRepository().login(Constants.JCR_CREDENTIALS); - Node messageNode = ses.getNodeByIdentifier(id); - Message message = node2Item.getMessageItem(messageNode); - Node personalNode = checkRights(currentUser, message); - - if (countSharedSet(messageNode)>1) { - log.debug("removing node message "+personalNode.getPath()); - personalNode.remove(); - }else { - if (message.isWithAttachments()) { - Node attachmentNode = messageNode.getNode(Constants.ATTACHMENTNODE_NAME); - ItemTypePredicate itemPredicate = new IncludeTypePredicate(AbstractFileItem.class); - List attachments = Utils.getItemList(attachmentNode, Excludes.GET_ONLY_CONTENT, null, true, itemPredicate); - trashHandler.removeOnlyNodesContent(ses, attachments); - } - messageNode.removeSharedSet(); - } - ses.save(); - log.debug("removing node message saved"); - }catch (ItemNotFoundException e) { - log.error("id {} not found",id,e); - GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND); - }catch(RepositoryException re){ - log.error("jcr error getting item", re); - GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error getting item", re)); - }catch(StorageHubException she ){ - log.error(she.getErrorMessage(), she); - GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus())); - }finally{ - if (ses!=null) - ses.logout(); - } - } - - @GET - @Path("{id}/attachments") - @Produces(MediaType.APPLICATION_JSON) - public ItemList getAttachments(){ - InnerMethodName.set("getAttachmentsByMessageId"); - Session ses = null; - List attachments = new ArrayList<>(); - try{ - ses = repository.getRepository().login(Constants.JCR_CREDENTIALS); - Node messageNode = ses.getNodeByIdentifier(id); - Message messageItem = node2Item.getMessageItem(messageNode); - checkRights(currentUser, messageItem); - Node attachmentNode = messageNode.getNode(Constants.ATTACHMENTNODE_NAME); - ItemTypePredicate itemPredicate = new IncludeTypePredicate(AbstractFileItem.class); - attachments = Utils.getItemList(attachmentNode, Excludes.GET_ONLY_CONTENT, null, true, itemPredicate); - }catch (ItemNotFoundException e) { - log.error("id {} not found",id,e); - GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND); - }catch(RepositoryException re){ - log.error("jcr error getting item", re); - GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error getting item", re)); - }catch(StorageHubException she ){ - log.error(she.getErrorMessage(), she); - GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus())); - }finally{ - if (ses!=null) - ses.logout(); - } - - return new ItemList(attachments); - } - - @GET - @Path("inbox") - @Produces(MediaType.APPLICATION_JSON) - public MessageList getReceivedMessages(@QueryParam("reduceBody") Integer reduceBody){ - InnerMethodName.set("getReceivedMessages"); - Session ses = null; - List toReturn = null; - try{ - ses = repository.getRepository().login(Constants.JCR_CREDENTIALS); - - Node node = ses.getNode(pathUtil.getInboxPath(currentUser).toPath()); - - //return sorted for createdTime - toReturn = getMessages(node, reduceBody); - }catch(RepositoryException re){ - log.error("jcr error getting item", re); - GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error getting item", re)); - }finally{ - if (ses!=null) - ses.logout(); - } - - return new MessageList(toReturn); - } - - @GET - @Path("sent") - @Produces(MediaType.APPLICATION_JSON) - public MessageList getSentMessages(@QueryParam("reduceBody") Integer reduceBody){ - InnerMethodName.set("getSentMessages"); - Session ses = null; - List toReturn = null; - try{ - ses = repository.getRepository().login(Constants.JCR_CREDENTIALS); - - Node node = ses.getNode(pathUtil.getOutboxPath(currentUser).toPath()); - - toReturn = getMessages(node, reduceBody); - }catch(RepositoryException re){ - log.error("jcr error getting item", re); - GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error getting item", re)); - }finally{ - if (ses!=null) - ses.logout(); - } - - return new MessageList(toReturn); - } - - @PUT - @Path("{id}/{prop}") - @Consumes(MediaType.APPLICATION_JSON) - public void setProperty(@PathParam("prop") String property,Object value){ - InnerMethodName.set("setPropertyOnMessage("+property+")"); - Session ses = null; - try{ - ses = repository.getRepository().login(Constants.JCR_CREDENTIALS); - Node messageNode = ses.getNodeByIdentifier(id); - Message messageItem = node2Item.getMessageItem(messageNode); - checkRights(currentUser, messageItem); - Values val = Item2NodeConverter.getObjectValue(value.getClass(), value); - messageNode.setProperty(property, val.getValue()); - ses.save(); - }catch (ItemNotFoundException e) { - log.error("id {} not found",id,e); - GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND); - }catch(StorageHubException she ){ - log.error(she.getErrorMessage(), she); - GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus())); - }catch( Exception re){ - log.error("jcr error getting item", re); - GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error getting item", re)); - }finally{ - if (ses!=null) - ses.logout(); - } - } - - - - @POST - @Path("send") - @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public String sendMessage(@FormParam("to[]") List addresses, - @FormParam("subject") String subject, @FormParam("body") String body, - @FormParam("attachments[]") List attachments){ - InnerMethodName.set("sendMessage"); - JackrabbitSession ses = null; - String messageId = null; - try{ - if (addresses.size()==0 || body==null || subject==null) - throw new InvalidCallParameters(); - - log.debug("attachments send are {}",attachments); - - ses = (JackrabbitSession) repository.getRepository().login(Constants.JCR_CREDENTIALS); - - Message message = new MessageSharable(); - message.setAddresses(addresses.toArray(new String[0])); - message.setSubject(subject); - message.setBody(body); - message.setName(UUID.randomUUID().toString()); - User user = ses.getUserManager().getAuthorizable(currentUser, User.class); - - if (user ==null) - throw new InvalidCallParameters("invalid storagehub user: "+currentUser); - Owner owner = new Owner(); - owner.setUserId(user.getID()); - owner.setUserName(user.getPrincipal().getName()); - message.setSender(owner); - Node outbox = ses.getNode(pathUtil.getOutboxPath(currentUser).toPath()); - Node messageNode = item2Node.getNode(outbox, message); - ses.save(); - if (attachments!=null && !attachments.isEmpty()) { - saveAttachments(ses, messageNode, attachments); - ses.save(); - } - for (String to: addresses) - try { - String userMessagePath = Paths.append(pathUtil.getInboxPath(to), messageNode.getName()).toPath(); - ses.getWorkspace().clone(ses.getWorkspace().getName(), messageNode.getPath(), userMessagePath, false); - }catch (Exception e) { - log.warn("message not send to {}",to,e); - } - - ses.save(); - messageId = messageNode.getIdentifier(); - }catch(RepositoryException re){ - log.error("jcr error getting item", re); - GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error getting item", re)); - }catch(StorageHubException she ){ - log.error(she.getErrorMessage(), she); - GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus())); - }finally{ - if (ses!=null) - ses.logout(); - } - - return messageId; - } - - - - private Node saveAttachments(Session ses, Node messageNode , List attachments) throws RepositoryException, StorageHubException{ - Node attachmentNode = messageNode.getNode(Constants.ATTACHMENTNODE_NAME); - - for (String itemId: attachments) { - Node node = ses.getNodeByIdentifier(itemId); - Item item = node2Item.getItem(node, Excludes.GET_ONLY_CONTENT); - Node newNode = copyNode(ses, attachmentNode, item); - //removes accounting if exists - if (newNode.hasNode(NodeProperty.ACCOUNTING.toString())) - newNode.getNode(NodeProperty.ACCOUNTING.toString()).remove(); - } - return messageNode; - } - - //returns Messages sorted by createdTime - private List getMessages(Node node, Integer reduceBody) throws RepositoryException{ - List messages = new ArrayList(); - NodeIterator nodeIt = node.getNodes(); - while(nodeIt.hasNext()) { - Node child = nodeIt.nextNode(); - log.trace("message type "+child.getPrimaryNodeType().getName()); - Message message = node2Item.getMessageItem(child); - if (message == null) { - log.info("message discarded"); - continue; - } - - if (reduceBody != null && reduceBody>0 && message.getBody().length()>reduceBody ) - message.setBody(message.getBody().substring(0, reduceBody)); - insertOrdered(messages, message); - } - return messages; - } - - private void insertOrdered(List messages, Message toInsert) { - if (messages.isEmpty()) - messages.add(toInsert); - else { - int i; - for ( i=0 ; i System.out.println(u.getUserName())); @@ -104,6 +106,7 @@ public class CreateUsers { this.createRandomFolderForCurrentUser(rootId); } } + */ @Test public void createFileAndFolderUser2() throws Exception { @@ -112,6 +115,14 @@ public class CreateUsers { createRandomFolderAndFileForCurrentUser(); } + + @Test + public void addurl() throws Exception{ + client.impersonate(user2); + FolderContainer folder = client.getWSRoot(); + folder.addUrl(new URL("https://www.google.it"), "google", "ggogle url").get(); + } + @Test public void uploadFileUser2() throws Exception { client.impersonate(user2); @@ -128,7 +139,7 @@ public class CreateUsers { public void createRandomFolderAndFileForCurrentUser() throws Exception{ String rootId = this.getUserRootId(); - this.uploadFileWithRandomNameForCurrentUser(rootId); + this.uploadFileWithRandomNameForCurrentUser(rootId); FolderContainer fc = this.createRandomFolderForCurrentUser(rootId); this.uploadFileWithRandomNameForCurrentUser(fc.getId()); } @@ -205,24 +216,36 @@ public class CreateUsers { } - public void createVRE(String vre, AccessType accesstype, String owner) throws Exception { - client.getVreFolderManager(vre).createVRE(accesstype, owner); + public void createVRE(String vre, AccessType accesstype, String owner, boolean defaultStorage) throws Exception { + //client.getVreFolderManager(vre).createVRE(accesstype, owner, defaultStorage); } + public void setAdmin(String vre, String user) throws Exception { client.getVreFolderManager(vre).setAdmin(user); } @Test public void createAllVREsAndAddUser1AsAdmin() throws Exception { - createVRE(vreWA, AccessType.WRITE_ALL, user1 ); + createVRE(vreWA, AccessType.WRITE_ALL, user1, true ); setAdmin(vreWA, user1); - createVRE(vreRO, AccessType.READ_ONLY, user1 ); + createVRE(vreRO, AccessType.READ_ONLY, user1, true ); setAdmin(vreWA, user1); - createVRE(vreWO, AccessType.WRITE_OWNER, user1 ); + createVRE(vreWO, AccessType.WRITE_OWNER, user1, true ); setAdmin(vreWA, user1); } + @Test + public void createVreNotDefaultStorage() throws Exception { + createVRE(notDefaultVRE, AccessType.WRITE_ALL, user1, false ); + setAdmin(notDefaultVRE, user1); + } + + @Test + public void deleteVreNotDefaultStorage() throws Exception { + client.getVreFolderManager(notDefaultVRE).removeVRE(); + } + @Test public void addUser2ToAllVres() throws Exception { addUserToVre(vreRO, user2); @@ -350,6 +373,16 @@ public class CreateUsers { } } + @Test + public void uploadFileToNotDefault() throws StorageHubException { + client.impersonate(user1); + try (InputStream is = CreateUsers.class.getResourceAsStream("/test1.jpg")) { + retrieveVREbyName(notDefaultVRE).uploadFile(is, "test1.jpg", "a test"); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Test public void removeUser() throws Exception { client.deleteUserAccount(user1);