messages removed

This commit is contained in:
lucio 2024-10-15 12:48:08 +02:00
parent 413b5cdfdf
commit 9f283861e6
16 changed files with 253 additions and 552 deletions

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
@ -40,7 +42,9 @@
<wb-module deploy-name="storagehub">
@ -81,7 +85,9 @@
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
@ -122,62 +128,58 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<dependent-module archiveName="common-smartgears-app-3.0.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/common-smartgears-app/common-smartgears-app">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="authorization-control-library-2.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/authorization-control-library/authorization-control-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="storagehub-model-2.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/storagehub-model/storagehub-model">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="storagehub-script-utils-2.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/storagehub-scripting-util/storagehub-scripting-util">
<dependency-type>uses</dependency-type>
</dependent-module>
@ -218,7 +220,9 @@
<property name="context-root" value="storagehub"/>
@ -259,7 +263,9 @@
<property name="java-output-path" value="/storagehub-webapp_BRANCH/target/classes"/>
@ -300,7 +306,9 @@
</wb-module>

View File

@ -1,5 +1,5 @@
[node]
mode = offline
mode = online
hostname = dlib29.isti.cnr.it
protocol= http
port = 8080

View File

@ -4,7 +4,7 @@
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.1.0</version>
<version>1.2.0</version>
<relativePath />
</parent>
<modelVersion>4.0.0</modelVersion>
@ -41,7 +41,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>4.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>

View File

@ -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);

View File

@ -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);

View File

@ -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<Item> 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<Version> 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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<Item> 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<Version> 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<ContentPair> 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<ContentPair> 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);
}

View File

@ -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);

View File

@ -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 extends Item> T retrieveItem(Node node, List<String> excludes, Class<T> classToHandle) throws RepositoryException, BackendGenericError{
T item;

View File

@ -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 {

View File

@ -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);

View File

@ -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<Item> 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<Item> 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<Message> 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<Message> 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<String> addresses,
@FormParam("subject") String subject, @FormParam("body") String body,
@FormParam("attachments[]") List<String> 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<String> 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<Message> getMessages(Node node, Integer reduceBody) throws RepositoryException{
List<Message> messages = new ArrayList<Message>();
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<Message> messages, Message toInsert) {
if (messages.isEmpty())
messages.add(toInsert);
else {
int i;
for ( i=0 ; i<messages.size(); i++)
if (messages.get(i).getCreationTime().getTimeInMillis()<=toInsert.getCreationTime().getTimeInMillis())
break;
messages.add(i, toInsert);
}
}
private Node checkRights(String user, Message messageItem) throws RepositoryException, StorageHubException{
Node personalNode = null;
Node messageNode = (Node) messageItem.getRelatedNode();
if (messageNode.getPath().startsWith(pathUtil.getInboxPath(user).toPath()))
return messageNode;
NodeIterator nodeIt = messageNode.getSharedSet();
while (nodeIt.hasNext()) {
Node node = nodeIt.nextNode();
if (node.getPath().startsWith(pathUtil.getInboxPath(user).toPath()))
personalNode = node;
}
if (personalNode == null &&
!messageItem.getSender().getUserName().equals(user) && !Arrays.asList(messageItem.getAddresses()).contains(user))
throw new UserNotAuthorizedException("user "+currentUser+"cannot read message with id "+id);
return personalNode== null ? messageNode : personalNode;
}
private Node copyNode(Session session, Node destination, Item itemToCopy) throws RepositoryException, StorageHubException{
//it needs to be locked ??
Node nodeToCopy = ((Node)itemToCopy.getRelatedNode());
String uniqueName = Utils.checkExistanceAndGetUniqueName(session, destination,itemToCopy.getName() );
String newPath= String.format("%s/%s", destination.getPath(), uniqueName);
session.getWorkspace().copy(nodeToCopy.getPath(), newPath);
Node newNode = session.getNode(newPath);
if (itemToCopy instanceof AbstractFileItem) {
AbstractFileItem newNodeItem = node2Item.getItem(newNode, Excludes.EXCLUDE_ACCOUNTING);
Content contentToCopy = newNodeItem.getContent();
StorageBackendFactory sbf = storageBackendHandler.get(contentToCopy.getPayloadBackend());
StorageBackend sb = sbf.create(contentToCopy.getPayloadBackend());
MetaInfo contentInfo = sb.onCopy(contentToCopy, destination.getPath(), uniqueName);
Utils.setContentFromMetaInfo(newNodeItem, contentInfo);
item2Node.replaceContent(newNode, newNodeItem, ItemAction.CLONED);
}
Utils.setPropertyOnChangeNode(newNode, currentUser, ItemAction.CLONED);
newNode.setProperty(NodeProperty.PORTAL_LOGIN.toString(), currentUser);
newNode.setProperty(NodeProperty.IS_PUBLIC.toString(), false);
newNode.setProperty(NodeProperty.TITLE.toString(), uniqueName);
return newNode;
}
private int countSharedSet(Node node) throws RepositoryException{
int count =0;
NodeIterator it = node.getSharedSet();
while (it.hasNext()) {
count ++;
it.next();
}
return count;
}
}

View File

@ -7,10 +7,13 @@ public class ContentPair {
private Content content;
private StorageBackend storageBackend;
public ContentPair(Content content, StorageBackend storageBackend) {
private String contentOwner;
public ContentPair(Content content, String contentOwner, StorageBackend storageBackend) {
super();
this.content = content;
this.storageBackend = storageBackend;
this.contentOwner = contentOwner;
}
public Content getContent() {
return content;
@ -18,6 +21,9 @@ public class ContentPair {
public StorageBackend getStorageBackend() {
return storageBackend;
}
public String getContentOwner() {
return this.contentOwner;
}
@Override
public int hashCode() {

View File

@ -1,16 +0,0 @@
package org.gcube.data.access.storagehub.types;
import org.gcube.common.storagehub.model.annotations.RootNode;
import org.gcube.common.storagehub.model.messages.Message;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@RootNode("nthl:itemSentRequestSH")
public class MessageSharable extends Message {
}

View File

@ -39,7 +39,8 @@ public class CreateUsers {
private String vreWA = "vre-write-all";
private String vreRO = "vre-read-only";
private String vreWO = "vre-write-owner";
private String notDefaultVRE = "notdefault";
private static final int TEST_USERS_NUMBER = 5000;
@BeforeClass
@ -76,6 +77,7 @@ public class CreateUsers {
}
/*
@Test
public void getUsers() throws Exception {
client.getUsers().forEach(u -> 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);