git-svn-id: https://svn.d4science-ii.research-infrastructures.eu/gcube/branches/data-access/storagehub-webapp/1.0@169556 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
75d9f828ee
commit
d5983bca18
5
pom.xml
5
pom.xml
|
@ -1,4 +1,5 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
|
@ -46,7 +47,7 @@
|
|||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>common-authorization</artifactId>
|
||||
|
|
|
@ -7,6 +7,9 @@ public class Constants {
|
|||
|
||||
public static final String VRE_FOLDER_PARENT_NAME = "MySpecialFolders";
|
||||
|
||||
public static final String SHARED_FOLDER_PATH = "/Share";
|
||||
|
||||
|
||||
public static final String TRASH_ROOT_FOLDER_NAME ="Trash";
|
||||
|
||||
public static final String QUERY_LANGUAGE ="JCR-SQL2";
|
||||
|
|
|
@ -9,5 +9,5 @@ public class MetaInfo {
|
|||
|
||||
String storageId;
|
||||
|
||||
|
||||
String remotePath;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import javax.ws.rs.Path;
|
|||
import javax.ws.rs.core.Application;
|
||||
|
||||
import org.gcube.data.access.storagehub.services.ACLManager;
|
||||
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.WorkspaceManager;
|
||||
|
@ -24,6 +25,7 @@ public class StorageHub extends Application {
|
|||
classes.add(ItemsManager.class);
|
||||
classes.add(ItemsCreator.class);
|
||||
classes.add(ACLManager.class);
|
||||
classes.add(ItemSharing.class);
|
||||
return classes;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
package org.gcube.data.access.storagehub;
|
||||
|
||||
import static org.gcube.common.storagehub.model.NodeConstants.ACCOUNTING_NAME;
|
||||
import static org.gcube.common.storagehub.model.NodeConstants.CONTENT_NAME;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
@ -21,6 +25,7 @@ import org.gcube.common.storagehub.model.Paths;
|
|||
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
||||
import org.gcube.common.storagehub.model.items.Item;
|
||||
import org.gcube.common.storagehub.model.items.SharedFolder;
|
||||
import org.gcube.common.storagehub.model.types.NodeProperty;
|
||||
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
|
||||
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
|
||||
|
@ -108,6 +113,10 @@ public class Utils {
|
|||
return Paths.getPath(String.format("/Home/%s/Workspace",AuthorizationProvider.instance.get().getClient().getId()));
|
||||
}
|
||||
|
||||
public static org.gcube.common.storagehub.model.Path getHomePath(String login){
|
||||
return Paths.getPath(String.format("/Home/%s/Workspace",login));
|
||||
}
|
||||
|
||||
public static StorageClient getStorageClient(String login){
|
||||
return new StorageClient(SERVICE_CLASS, SERVICE_NAME, login, AccessType.SHARED, MemoryType.PERSISTENT);
|
||||
|
||||
|
@ -176,4 +185,13 @@ public class Utils {
|
|||
}
|
||||
}
|
||||
|
||||
public static boolean hasSharedChildren(FolderItem item, Session session) throws Exception{
|
||||
Node currentNode = session.getNodeByIdentifier(item.getId());
|
||||
for (Item children : Utils.getItemList(currentNode,Arrays.asList(ACCOUNTING_NAME,CONTENT_NAME), null, false)){
|
||||
if (children instanceof FolderItem)
|
||||
return (children instanceof SharedFolder) || hasSharedChildren((FolderItem)children, session);
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.gcube.data.access.storagehub.accounting;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
@ -27,6 +28,7 @@ public class AccountingHandler {
|
|||
private static final String ITEM_NAME = "hl:itemName";
|
||||
private static final String ITEM_TYPE = "hl:itemType";
|
||||
private static final String MIME_TYPE = "hl:mimeType";
|
||||
private static final String MEMBERS = "hl:members";
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AccountingHandler.class);
|
||||
|
||||
|
@ -87,5 +89,46 @@ public class AccountingHandler {
|
|||
logger.warn("error trying to retrieve accountign node",e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void createFolderRemoveObj(String title, String itemType, String mimeType, Session ses, Node parentNode, boolean saveHistory ) {
|
||||
try {
|
||||
|
||||
if (!parentNode.hasNode(NodeProperty.ACCOUNTING.toString())){
|
||||
parentNode.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
||||
}
|
||||
|
||||
Node accountingNodeParent = parentNode.getNode(NodeProperty.ACCOUNTING.toString());
|
||||
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.REMOVAL.getNodeTypeDefinition());
|
||||
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
||||
accountingNode.setProperty(DATE, Calendar.getInstance());
|
||||
accountingNode.setProperty(ITEM_NAME, title);
|
||||
accountingNode.setProperty(ITEM_TYPE, itemType);
|
||||
if (mimeType!=null)
|
||||
accountingNode.setProperty(MIME_TYPE, mimeType);
|
||||
|
||||
if (saveHistory) ses.save();
|
||||
} catch (RepositoryException e) {
|
||||
logger.warn("error trying to retrieve accountign node",e);
|
||||
}
|
||||
}
|
||||
|
||||
public void shareFolder(String title, Set<String> users, Session ses, Node sharedNode, boolean saveHistory ) {
|
||||
try {
|
||||
|
||||
if (!sharedNode.hasNode(NodeProperty.ACCOUNTING.toString())){
|
||||
sharedNode.addNode(NodeProperty.ACCOUNTING.toString(), NodeProperty.NT_ACCOUNTING.toString());
|
||||
}
|
||||
|
||||
Node accountingNodeParent = sharedNode.getNode(NodeProperty.ACCOUNTING.toString());
|
||||
Node accountingNode = accountingNodeParent.addNode(UUID.randomUUID().toString(),AccountingEntryType.SHARE.getNodeTypeDefinition());
|
||||
accountingNode.setProperty(USER, AuthorizationProvider.instance.get().getClient().getId());
|
||||
accountingNode.setProperty(DATE, Calendar.getInstance());
|
||||
accountingNode.setProperty(ITEM_NAME, title);
|
||||
accountingNode.setProperty(MEMBERS, users.toArray(new String[users.size()]));
|
||||
|
||||
if (saveHistory) ses.save();
|
||||
} catch (RepositoryException e) {
|
||||
logger.warn("error trying to retrieve accountign node",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class ItemHandler {
|
|||
item.setPrimaryType(node.getPrimaryNodeType().getName());
|
||||
Item parent = null ;
|
||||
if (item instanceof SharedFolder) {
|
||||
logger.debug("I'm in a Shared Folder");
|
||||
logger.trace("I'm in a Shared Folder");
|
||||
item.setShared(true);
|
||||
}else {
|
||||
try {
|
||||
|
@ -94,7 +94,7 @@ public class ItemHandler {
|
|||
item.setParentId(node.getParent().getIdentifier());
|
||||
item.setParentPath(node.getParent().getPath());
|
||||
}catch (Throwable e) {
|
||||
logger.info("Root node doesn't have a parent");
|
||||
logger.trace("Root node doesn't have a parent");
|
||||
}
|
||||
|
||||
for (Field field : retrieveAllFields(classToHandle)){
|
||||
|
@ -106,7 +106,7 @@ public class ItemHandler {
|
|||
field.set(item, getPropertyValue(returnType, node.getProperty(attribute.value())));
|
||||
|
||||
}catch(PathNotFoundException e){
|
||||
logger.debug("the current node dosn't contain {} property",attribute.value());
|
||||
logger.trace("the current node dosn't contain {} property",attribute.value());
|
||||
} catch (Exception e ) {
|
||||
logger.warn("error setting value for property {} ",attribute.value());
|
||||
}
|
||||
|
@ -114,14 +114,14 @@ public class ItemHandler {
|
|||
String fieldNodeName = field.getAnnotation(NodeAttribute.class).value();
|
||||
//for now it excludes only first level node
|
||||
if (excludes!=null && excludes.contains(fieldNodeName)) continue;
|
||||
logger.debug("retrieving field node "+field.getName());
|
||||
logger.trace("retrieving field node "+field.getName());
|
||||
field.setAccessible(true);
|
||||
try{
|
||||
Node fieldNode = node.getNode(fieldNodeName);
|
||||
logger.debug("looking in node {} searched with {}",fieldNode.getName(),fieldNodeName);
|
||||
logger.trace("looking in node {} searched with {}",fieldNode.getName(),fieldNodeName);
|
||||
field.set(item, iterateNodeAttributeFields(field.getType(), fieldNode));
|
||||
}catch(PathNotFoundException e){
|
||||
logger.debug("the current node dosn't contain {} node",fieldNodeName);
|
||||
logger.trace("the current node dosn't contain {} node",fieldNodeName);
|
||||
} catch (Exception e ) {
|
||||
logger.warn("error setting value",e);
|
||||
}
|
||||
|
|
|
@ -2,10 +2,8 @@ package org.gcube.data.access.storagehub.handlers;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import javax.jcr.Credentials;
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package org.gcube.data.access.storagehub.handlers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
import javax.jcr.Node;
|
||||
import javax.jcr.Session;
|
||||
|
@ -9,6 +13,7 @@ import javax.jcr.version.VersionIterator;
|
|||
import javax.jcr.version.VersionManager;
|
||||
|
||||
import org.apache.jackrabbit.JcrConstants;
|
||||
import org.apache.jackrabbit.core.version.VersionManagerImplBase;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -46,19 +51,23 @@ public class VersionHandler {
|
|||
}
|
||||
}
|
||||
|
||||
public void getContentVersionHistory(Node node, Session session){
|
||||
public List<Version> getContentVersionHistory(Node node, Session session) {
|
||||
try {
|
||||
Node contentNode = node.getNode("jcr:content");
|
||||
VersionManager versionManager = session.getWorkspace().getVersionManager();
|
||||
VersionHistory history = versionManager.getVersionHistory(contentNode.getPath());
|
||||
VersionIterator iterator = history.getAllVersions();
|
||||
iterator.skip(1);
|
||||
List<Version> versions = new ArrayList<>();
|
||||
while (iterator.hasNext()) {
|
||||
Version version = iterator.nextVersion();
|
||||
versions.add(version);
|
||||
logger.debug("version name {} with nodeType {}",version.getName(),version.getPrimaryNodeType().getName());
|
||||
}
|
||||
return versions;
|
||||
}catch(Exception e ) {
|
||||
logger.warn("cannot get version history content node",e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ import org.gcube.data.access.storagehub.Constants;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Path("item")
|
||||
@Path("items")
|
||||
public class ACLManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ACLManager.class);
|
||||
|
@ -48,6 +48,7 @@ public class ACLManager {
|
|||
@Inject
|
||||
AuthorizationChecker authChecker;
|
||||
|
||||
SimpleCredentials credential = new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray());
|
||||
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@GET
|
||||
|
@ -57,7 +58,7 @@ public class ACLManager {
|
|||
Session ses = null;
|
||||
List<ACL> acls = new ArrayList<>();
|
||||
try{
|
||||
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
String path = ses.getNodeByIdentifier(id).getPath();
|
||||
log.info("checking acces for path {}",path);
|
||||
|
|
|
@ -0,0 +1,175 @@
|
|||
package org.gcube.data.access.storagehub.services;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.jcr.Node;
|
||||
import javax.jcr.Session;
|
||||
import javax.jcr.SimpleCredentials;
|
||||
import javax.jcr.security.AccessControlManager;
|
||||
import javax.jcr.security.Privilege;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.apache.jackrabbit.api.security.JackrabbitAccessControlList;
|
||||
import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
|
||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
||||
import org.gcube.common.storagehub.model.NodeConstants;
|
||||
import org.gcube.common.storagehub.model.Paths;
|
||||
import org.gcube.common.storagehub.model.acls.AccessType;
|
||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
||||
import org.gcube.common.storagehub.model.items.Item;
|
||||
import org.gcube.common.storagehub.model.types.PrimaryNodeType;
|
||||
import org.gcube.data.access.storagehub.AuthorizationChecker;
|
||||
import org.gcube.data.access.storagehub.Constants;
|
||||
import org.gcube.data.access.storagehub.Utils;
|
||||
import org.gcube.data.access.storagehub.accounting.AccountingHandler;
|
||||
import org.gcube.data.access.storagehub.handlers.ItemHandler;
|
||||
import org.gcube.data.access.storagehub.handlers.VersionHandler;
|
||||
import org.glassfish.jersey.media.multipart.FormDataParam;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Path("items")
|
||||
public class ItemSharing {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ItemSharing.class);
|
||||
|
||||
@Inject
|
||||
RepositoryInitializer repository;
|
||||
|
||||
@Inject
|
||||
AccountingHandler accountingHandler;
|
||||
|
||||
@RequestScoped
|
||||
@PathParam("id")
|
||||
String id;
|
||||
|
||||
@Context
|
||||
ServletContext context;
|
||||
|
||||
@Inject
|
||||
AuthorizationChecker authChecker;
|
||||
|
||||
@Inject
|
||||
VersionHandler versionHandler;
|
||||
|
||||
SimpleCredentials credential = new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray());
|
||||
|
||||
@PUT
|
||||
@Path("{id}/share")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||
public String share(@FormDataParam("users") Set<String> users, @FormDataParam("defaultAccessType") AccessType accessType){
|
||||
CalledMethodProvider.instance.set("findChildrenByNamePattern");
|
||||
Session ses = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkWriteAuthorizationControl(ses, id, false);
|
||||
|
||||
Item item = ItemHandler.getItem(ses.getNodeByIdentifier(id), Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.CONTENT_NAME, NodeConstants.METADATA_NAME));
|
||||
|
||||
if (! (item instanceof FolderItem) && ((FolderItem) item).isShared() && Utils.hasSharedChildren((FolderItem)item, ses) && item.getOwner().equals(login))
|
||||
throw new Exception("item with id "+id+" cannot be shared");
|
||||
|
||||
if (accessType==null)
|
||||
accessType = AccessType.READ_ONLY;
|
||||
|
||||
if (users.isEmpty())
|
||||
throw new Exception("users is empty");
|
||||
|
||||
|
||||
String sharedFolderName = item.getId();
|
||||
|
||||
String newNodePath = Constants.SHARED_FOLDER_PATH+"/"+sharedFolderName;
|
||||
|
||||
/*ses.getWorkspace().getLockManager().lock(newNodePath, true, true, 0,login);
|
||||
|
||||
try {
|
||||
*/
|
||||
ses.move(item.getPath(),newNodePath);
|
||||
|
||||
Node sharedFolderNode = ses.getNode(newNodePath);
|
||||
|
||||
sharedFolderNode.setPrimaryType(PrimaryNodeType.NT_WORKSPACE_SHARED_FOLDER);
|
||||
|
||||
Node usersNode =null;
|
||||
if (sharedFolderNode.hasNode("hl:users"))
|
||||
usersNode = sharedFolderNode.getNode("hl:users");
|
||||
else
|
||||
usersNode = sharedFolderNode.addNode("hl:users");
|
||||
|
||||
ses.save();
|
||||
|
||||
ses.getWorkspace().getLockManager().lock(newNodePath, true, true, 0,login);
|
||||
|
||||
try {
|
||||
|
||||
AccessControlManager acm = ses.getAccessControlManager();
|
||||
JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(acm, sharedFolderNode.getPath());
|
||||
|
||||
//setting data for ADMINISTRATOR
|
||||
org.gcube.common.storagehub.model.Path adminFolderPath = Paths.append(Utils.getHomePath(), item.getName());
|
||||
|
||||
log.debug("trying to clone dir from {} to {}", sharedFolderNode.getPath(), adminFolderPath.toPath());
|
||||
|
||||
ses.getWorkspace().clone(ses.getWorkspace().getName(), sharedFolderNode.getPath(), adminFolderPath.toPath(), false);
|
||||
String adminRootWSId = ses.getNode(Utils.getHomePath().toPath()).getIdentifier();
|
||||
|
||||
Privilege[] adminPrivileges = new Privilege[] { acm.privilegeFromName(AccessType.ADMINISTRATOR.getValue()) };
|
||||
acls.addAccessControlEntry(AccessControlUtils.getPrincipal(ses, login), adminPrivileges );
|
||||
|
||||
usersNode.setProperty(login, String.format("%s/%s",adminRootWSId,item.getName()));
|
||||
|
||||
users.remove(login);
|
||||
Privilege[] userPrivileges = new Privilege[] { acm.privilegeFromName(accessType.getValue()) };
|
||||
for (String user : users) {
|
||||
try {
|
||||
org.gcube.common.storagehub.model.Path userFolderPath = Paths.append(Utils.getHomePath(user), item.getName());
|
||||
ses.getWorkspace().clone(ses.getWorkspace().getName(), sharedFolderNode.getPath(), userFolderPath.toPath(), false);
|
||||
String userRootWSId = ses.getNode(Utils.getHomePath(user).toPath()).getIdentifier();
|
||||
acls.addAccessControlEntry(AccessControlUtils.getPrincipal(ses, user), userPrivileges );
|
||||
usersNode.setProperty(user, String.format("%s/%s",userRootWSId,item.getName()));
|
||||
}catch(Throwable t) {
|
||||
log.warn("error sharing folder with user {}",user);
|
||||
}
|
||||
}
|
||||
|
||||
acm.setPolicy(sharedFolderNode.getPath(), acls);
|
||||
|
||||
|
||||
accountingHandler.shareFolder(item.getTitle(), users, ses, sharedFolderNode, false);
|
||||
|
||||
ses.save();
|
||||
|
||||
return sharedFolderNode.getIdentifier();
|
||||
|
||||
} finally {
|
||||
ses.getWorkspace().getLockManager().unlock(newNodePath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(Throwable e){
|
||||
log.error("error sharing node with id {}",id,e);
|
||||
throw new WebApplicationException(e);
|
||||
}finally{
|
||||
if (ses!=null)
|
||||
ses.logout();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
package org.gcube.data.access.storagehub.services;
|
||||
|
||||
import static org.gcube.common.storagehub.model.NodeConstants.ACCOUNTING_NAME;
|
||||
import static org.gcube.common.storagehub.model.NodeConstants.CONTENT_NAME;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
|
@ -35,7 +38,6 @@ import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
|||
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
||||
import org.gcube.common.storagehub.model.items.Item;
|
||||
import org.gcube.common.storagehub.model.service.ItemWrapper;
|
||||
import org.gcube.common.storagehub.model.types.ItemAction;
|
||||
import org.gcube.data.access.storagehub.AuthorizationChecker;
|
||||
import org.gcube.data.access.storagehub.Constants;
|
||||
|
@ -49,9 +51,8 @@ import org.gcube.data.access.storagehub.handlers.content.ContentHandler;
|
|||
import org.gcube.data.access.storagehub.handlers.content.ContentHandlerFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import static org.gcube.common.storagehub.model.NodeConstants.*;
|
||||
|
||||
@Path("item")
|
||||
@Path("items")
|
||||
public class ItemsCreator {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ItemsCreator.class);
|
||||
|
@ -75,6 +76,8 @@ public class ItemsCreator {
|
|||
@Inject
|
||||
AccountingHandler accountingHandler;
|
||||
|
||||
SimpleCredentials credential = new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray());
|
||||
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
|
@ -89,7 +92,7 @@ public class ItemsCreator {
|
|||
long start = System.currentTimeMillis();
|
||||
|
||||
//TODO check if it is possible to change all the ACL on a workspace
|
||||
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
|
||||
//validate input parameters for Item Type
|
||||
|
||||
|
@ -124,7 +127,7 @@ public class ItemsCreator {
|
|||
|
||||
log.debug("content prepared");
|
||||
Node newNode = ItemHandler.createNodeFromItem(ses, destination, item);
|
||||
accountingHandler.createFolderAddObj(name, type, null, ses, newNode, false);
|
||||
accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
|
||||
ses.save();
|
||||
log.info("item with id {} correctly created",newNode.getIdentifier());
|
||||
return Response.ok(newNode.getIdentifier()).build();
|
||||
|
@ -153,22 +156,22 @@ public class ItemsCreator {
|
|||
@Path("/{id}/create/FILE")
|
||||
public Response createFileItem(InputStream stream , @PathParam("id") String id,
|
||||
@QueryParam("name") String name, @QueryParam("description") String description){
|
||||
CalledMethodProvider.instance.set(String.format("createItem(FILE)"));
|
||||
log.info("create file called");
|
||||
CalledMethodProvider.instance.set("createItem(FILE)");
|
||||
|
||||
Session ses = null;
|
||||
Item destinationItem = null;
|
||||
try{
|
||||
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
|
||||
//TODO check if it is possible to change all the ACL on a workspace
|
||||
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
|
||||
//TODO: validate input parameters for Item Type
|
||||
log.info("time to connect to repo {}",(System.currentTimeMillis()-start));
|
||||
Node destination = ses.getNodeByIdentifier(id);
|
||||
|
||||
log.info("create file called with filename {} in dir {} ", name, destination.getPath() );
|
||||
|
||||
destinationItem = ItemHandler.getItem(destination,Arrays.asList(ACCOUNTING_NAME,CONTENT_NAME));
|
||||
log.debug("destination item path is {}",destinationItem.getPath());
|
||||
if (!(destinationItem instanceof FolderItem)) throw new Exception("an Item must be copyed to another directory");
|
||||
|
||||
ses.getWorkspace().getLockManager().lock(destinationItem.getPath(), true, true, 0,login);
|
||||
|
@ -186,7 +189,6 @@ public class ItemsCreator {
|
|||
Node newNode;
|
||||
try {
|
||||
newNode = ses.getNode(org.gcube.common.storagehub.model.Paths.append(org.gcube.common.storagehub.model.Paths.getPath(destinationItem.getPath()), name).toPath());
|
||||
log.info("overwriting the old node");
|
||||
authChecker.checkWriteAuthorizationControl(ses, newNode.getIdentifier(), false);
|
||||
versionHandler.checkoutContentNode(newNode, ses);
|
||||
log.trace("replacing content of class {}",item.getContent().getClass());
|
||||
|
@ -198,7 +200,7 @@ public class ItemsCreator {
|
|||
versionHandler.makeVersionableContent(newNode, ses);
|
||||
}
|
||||
|
||||
accountingHandler.createFolderAddObj(name, "FILE", item.getContent().getMimeType(), ses, newNode, false);
|
||||
accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), item.getContent().getMimeType(), ses, newNode, false);
|
||||
|
||||
ses.save();
|
||||
versionHandler.checkinContentNode(newNode, ses);
|
||||
|
@ -247,7 +249,7 @@ public class ItemsCreator {
|
|||
is1.reset();
|
||||
handler.initiliseSpecificContent(is1);
|
||||
handler.getContent().setMimeType(mimeType);
|
||||
|
||||
|
||||
} catch (Throwable e) {
|
||||
log.error("error retreiving content",e);
|
||||
throw new RuntimeException(e);
|
||||
|
@ -267,6 +269,7 @@ public class ItemsCreator {
|
|||
MetaInfo info = new MetaInfo();
|
||||
info.setSize(size);
|
||||
info.setStorageId(storageId);
|
||||
info.setRemotePath(remotePath);
|
||||
return info;
|
||||
}
|
||||
};
|
||||
|
@ -280,7 +283,7 @@ public class ItemsCreator {
|
|||
handler.getContent().setData("jcr:content");
|
||||
handler.getContent().setStorageId(uploaderF.get().getStorageId());
|
||||
handler.getContent().setSize(uploaderF.get().getSize());
|
||||
|
||||
handler.getContent().setRemotePath(uploaderF.get().getRemotePath());
|
||||
return handler;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,24 +1,30 @@
|
|||
package org.gcube.data.access.storagehub.services;
|
||||
|
||||
import static org.gcube.common.storagehub.model.NodeConstants.*;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Deque;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.jcr.Node;
|
||||
import javax.jcr.NodeIterator;
|
||||
import javax.jcr.Session;
|
||||
import javax.jcr.SimpleCredentials;
|
||||
import javax.jcr.version.Version;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
|
@ -33,6 +39,7 @@ import javax.ws.rs.core.StreamingOutput;
|
|||
|
||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
||||
import org.gcube.common.storagehub.model.NodeConstants;
|
||||
import org.gcube.common.storagehub.model.Paths;
|
||||
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
||||
|
@ -43,6 +50,7 @@ import org.gcube.common.storagehub.model.items.VreFolder;
|
|||
import org.gcube.common.storagehub.model.service.ItemList;
|
||||
import org.gcube.common.storagehub.model.service.ItemWrapper;
|
||||
import org.gcube.common.storagehub.model.types.ItemAction;
|
||||
import org.gcube.contentmanagement.blobstorage.service.IClient;
|
||||
import org.gcube.data.access.storagehub.AuthorizationChecker;
|
||||
import org.gcube.data.access.storagehub.Constants;
|
||||
import org.gcube.data.access.storagehub.Range;
|
||||
|
@ -50,10 +58,11 @@ import org.gcube.data.access.storagehub.SingleFileStreamingOutput;
|
|||
import org.gcube.data.access.storagehub.Utils;
|
||||
import org.gcube.data.access.storagehub.accounting.AccountingHandler;
|
||||
import org.gcube.data.access.storagehub.handlers.ItemHandler;
|
||||
import org.gcube.data.access.storagehub.handlers.VersionHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Path("item")
|
||||
@Path("items")
|
||||
public class ItemsManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ItemsManager.class);
|
||||
|
@ -74,8 +83,10 @@ public class ItemsManager {
|
|||
@Inject
|
||||
AuthorizationChecker authChecker;
|
||||
|
||||
@Inject
|
||||
VersionHandler versionHandler;
|
||||
|
||||
|
||||
SimpleCredentials credential = new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray());
|
||||
|
||||
@GET
|
||||
@Path("{id}")
|
||||
|
@ -86,7 +97,7 @@ public class ItemsManager {
|
|||
Item toReturn = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
toReturn = ItemHandler.getItem(ses.getNodeByIdentifier(id), excludes);
|
||||
}catch(Throwable e){
|
||||
|
@ -100,6 +111,31 @@ public class ItemsManager {
|
|||
return new ItemWrapper<Item>(toReturn);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{id}/items/{name}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public ItemList findChildrenByNamePattern(@QueryParam("exclude") List<String> excludes, @PathParam("name") String name){
|
||||
CalledMethodProvider.instance.set("findChildrenByNamePattern");
|
||||
Session ses = null;
|
||||
List<Item> toReturn = new ArrayList<>();
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
NodeIterator it = ses.getNodeByIdentifier(id).getNodes(name);
|
||||
while (it.hasNext())
|
||||
toReturn.add(ItemHandler.getItem(it.nextNode(), excludes));
|
||||
}catch(Throwable e){
|
||||
log.error("error reading the node children of {} with name pattern",id,name,e);
|
||||
throw new WebApplicationException(e);
|
||||
}finally{
|
||||
if (ses!=null)
|
||||
ses.logout();
|
||||
}
|
||||
|
||||
return new ItemList(toReturn);
|
||||
}
|
||||
|
||||
|
||||
@GET
|
||||
@Path("{id}/children/count")
|
||||
|
@ -110,7 +146,7 @@ public class ItemsManager {
|
|||
Long toReturn = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
toReturn = Utils.getItemCount(ses.getNodeByIdentifier(id), showHidden==null?false:showHidden);
|
||||
}catch(Throwable e){
|
||||
|
@ -131,8 +167,7 @@ public class ItemsManager {
|
|||
Session ses = null;
|
||||
List<? extends Item> toReturn = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, null, showHidden==null?false:showHidden);
|
||||
}catch(Throwable e){
|
||||
|
@ -154,8 +189,7 @@ public class ItemsManager {
|
|||
Session ses = null;
|
||||
List<? extends Item> toReturn = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, new Range(start, limit),showHidden==null?false:showHidden);
|
||||
}catch(Throwable e){
|
||||
|
@ -177,9 +211,16 @@ public class ItemsManager {
|
|||
Session ses = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
String url = Utils.getStorageClient(login).getClient().getHttpsUrl().RFileById(id);
|
||||
|
||||
Item item = ItemHandler.getItem(ses.getNodeByIdentifier(id), Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.METADATA_NAME));
|
||||
|
||||
if (!(item instanceof AbstractFileItem)) throw new Exception("the select item is not a File");
|
||||
|
||||
AbstractFileItem fileItem = (AbstractFileItem) item;
|
||||
|
||||
String url = Utils.getStorageClient(login).getClient().getHttpsUrl().RFileById(fileItem.getContent().getStorageId());
|
||||
return new URL(url);
|
||||
}catch(Throwable e){
|
||||
log.error("error reading the node children of {}",id,e);
|
||||
|
@ -191,6 +232,34 @@ public class ItemsManager {
|
|||
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{id}/rootSharedFolder")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public ItemWrapper<Item> getRootSharedFolder(@QueryParam("exclude") List<String> excludes){
|
||||
CalledMethodProvider.instance.set("getRootSharedFolder");
|
||||
Session ses = null;
|
||||
try{
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
Item currentItem = ItemHandler.getItem(ses.getNodeByIdentifier(id), excludes);
|
||||
if (!currentItem.isShared())
|
||||
throw new RuntimeException("this item is not shared");
|
||||
log.trace("current node is {}",currentItem.getPath());
|
||||
while (!(currentItem instanceof SharedFolder ))
|
||||
currentItem = ItemHandler.getItem(ses.getNodeByIdentifier(currentItem.getParentId()), Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.METADATA_NAME, NodeConstants.CONTENT_NAME));
|
||||
|
||||
return new ItemWrapper<Item>(currentItem);
|
||||
|
||||
}catch(Throwable e){
|
||||
log.error("error retrieving shared root folder of node with id {}",id,e);
|
||||
throw new WebApplicationException(e);
|
||||
}finally{
|
||||
if (ses!=null)
|
||||
ses.logout();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{id}/anchestors")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
|
@ -201,7 +270,7 @@ public class ItemsManager {
|
|||
List<Item> toReturn = new LinkedList<>();
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
Item currentItem = ItemHandler.getItem(ses.getNodeByIdentifier(id), excludes);
|
||||
log.trace("current node is {}",currentItem.getPath());
|
||||
|
@ -233,7 +302,9 @@ public class ItemsManager {
|
|||
return new ItemList(toReturn);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@GET
|
||||
@Path("{id}/download")
|
||||
public Response download(){
|
||||
|
@ -241,7 +312,7 @@ public class ItemsManager {
|
|||
Session ses = null;
|
||||
try{
|
||||
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
final Node node = ses.getNodeByIdentifier(id);
|
||||
authChecker.checkReadAuthorizationControl(ses, id);
|
||||
final Item item = ItemHandler.getItem(node, null);
|
||||
|
@ -311,7 +382,7 @@ public class ItemsManager {
|
|||
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
//ses = RepositoryInitializer.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
//TODO check if it is possible to change all the ACL on a workspace
|
||||
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
|
||||
authChecker.checkWriteAuthorizationControl(ses, destinationId, true);
|
||||
authChecker.checkWriteAuthorizationControl(ses, identifier, false);
|
||||
|
@ -334,7 +405,7 @@ public class ItemsManager {
|
|||
|
||||
if (item instanceof FolderItem){
|
||||
|
||||
if (hasSharedChildren((FolderItem) item, ses)) throw new Exception("folder item with shared children cannot be moved");
|
||||
if (Utils.hasSharedChildren((FolderItem) item, ses)) throw new Exception("folder item with shared children cannot be moved");
|
||||
|
||||
ses.getWorkspace().move(nodeToMove.getPath(), destination.getPath()+"/"+nodeToMove.getName());
|
||||
}else
|
||||
|
@ -356,69 +427,38 @@ public class ItemsManager {
|
|||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("{id}/moveToTrash")
|
||||
public Response moveToTrash(@PathParam("id") String identifier){
|
||||
CalledMethodProvider.instance.set("moveToTrash");
|
||||
@DELETE
|
||||
@Path("{id}")
|
||||
public Response deleteItem(@PathParam("id") String identifier){
|
||||
CalledMethodProvider.instance.set("deleteItem");
|
||||
//TODO: check if identifier is The Workspace root, or the trash folder or the VREFolder root
|
||||
//TODO: check also that is not already trashed
|
||||
Session ses = null;
|
||||
try{
|
||||
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
|
||||
log.info("removing node with id {}", identifier);
|
||||
|
||||
//TODO check if it is possible to change all the ACL on a workspace
|
||||
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
|
||||
authChecker.checkWriteAuthorizationControl(ses, identifier, false);
|
||||
|
||||
final Node nodeToDelete = ses.getNodeByIdentifier(identifier);
|
||||
final Node trashFolder = ses.getNode(Paths.append(Utils.getHomePath(),Constants.TRASH_ROOT_FOLDER_NAME).toPath());
|
||||
|
||||
final Item item = ItemHandler.getItem(nodeToDelete, null);
|
||||
Item itemToDelete = ItemHandler.getItem(nodeToDelete, Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.METADATA_NAME, NodeConstants.OWNER_NAME));
|
||||
|
||||
if (itemToDelete instanceof SharedFolder || itemToDelete instanceof VreFolder || (itemToDelete instanceof FolderItem && Utils.hasSharedChildren((FolderItem) itemToDelete, ses)))
|
||||
throw new Exception("SharedFolder, VreFolder or folders with shared children cannot be deleted");
|
||||
|
||||
log.debug("item is trashed? {}", itemToDelete.isTrashed());
|
||||
|
||||
if (!itemToDelete.isTrashed())
|
||||
moveToTrash(ses, nodeToDelete, itemToDelete);
|
||||
else
|
||||
removeNode(ses, itemToDelete);
|
||||
|
||||
if (item instanceof SharedFolder || item instanceof VreFolder || (item instanceof FolderItem && hasSharedChildren((FolderItem) item, ses)))
|
||||
throw new Exception("SharedFolder, VreFolder or folders with shared children cannot be deleted");
|
||||
|
||||
|
||||
try {
|
||||
ses.getWorkspace().getLockManager().lock(trashFolder.getPath(), true, true, 0,login);
|
||||
ses.getWorkspace().getLockManager().lock(nodeToDelete.getPath(), true, true, 0,login);
|
||||
|
||||
|
||||
TrashItem trashItem = new TrashItem();
|
||||
trashItem.setDeletedBy(AuthorizationProvider.instance.get().getClient().getId());
|
||||
trashItem.setDeletedFrom(nodeToDelete.getParent().getPath());
|
||||
Calendar now = Calendar.getInstance();
|
||||
trashItem.setDeletedTime(now);
|
||||
trashItem.setHidden(false);
|
||||
trashItem.setLastAction(ItemAction.CREATED);
|
||||
trashItem.setDescription("trash item of node " + item.getPath());
|
||||
trashItem.setParentId(nodeToDelete.getParent().getIdentifier());
|
||||
trashItem.setParentPath(nodeToDelete.getParent().getPath());
|
||||
trashItem.setTitle(item.getId());
|
||||
|
||||
trashItem.setOwner(item.getOwner());
|
||||
trashItem.setLastModificationTime(item.getLastModificationTime());
|
||||
trashItem.setLastModifiedBy(item.getLastModifiedBy());
|
||||
|
||||
if (item instanceof FolderItem)
|
||||
trashItem.setFolder(true);
|
||||
else if (item instanceof AbstractFileItem ) {
|
||||
AbstractFileItem file = (AbstractFileItem) item;
|
||||
trashItem.setMimeType(file.getContent().getMimeType());
|
||||
trashItem.setLenght(file.getContent().getSize());
|
||||
}
|
||||
|
||||
Node newTrashItemNode = ItemHandler.createNodeFromItem(ses, trashFolder, trashItem);
|
||||
//TODO: accounting
|
||||
ses.getWorkspace().move(nodeToDelete.getPath(), Paths.append(Paths.getPath(newTrashItemNode.getPath()),nodeToDelete.getName()).toPath());
|
||||
}finally {
|
||||
ses.getWorkspace().getLockManager().unlock(nodeToDelete.getPath());
|
||||
ses.getWorkspace().getLockManager().unlock(trashFolder.getPath());
|
||||
}
|
||||
ses.save();
|
||||
}catch(Exception e){
|
||||
log.error("error moving item with id {} in Thrash",identifier,e);
|
||||
log.error("error removing item with id {} in Thrash",identifier,e);
|
||||
throw new WebApplicationException(e);
|
||||
} finally{
|
||||
if (ses!=null) {
|
||||
|
@ -428,14 +468,124 @@ public class ItemsManager {
|
|||
return Response.ok().build();
|
||||
}
|
||||
|
||||
private boolean hasSharedChildren(FolderItem item, Session session) throws Exception{
|
||||
Node currentNode = session.getNodeByIdentifier(item.getId());
|
||||
for (Item children : Utils.getItemList(currentNode,Arrays.asList(ACCOUNTING_NAME,CONTENT_NAME), null, false)){
|
||||
if (children instanceof FolderItem)
|
||||
return (children instanceof SharedFolder) || hasSharedChildren((FolderItem)item, session);
|
||||
|
||||
private void removeNode(Session ses, Item itemToDelete) throws Exception{
|
||||
log.debug("removing node");
|
||||
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
String parentPath = itemToDelete.getParentPath();
|
||||
try {
|
||||
ses.getWorkspace().getLockManager().lock(parentPath, true, true, 0,login);
|
||||
Set<String> idsToDelete = new HashSet<>();
|
||||
getAllContentIds(ses, idsToDelete, itemToDelete);
|
||||
ses.removeItem(itemToDelete.getPath());
|
||||
new Thread() {
|
||||
|
||||
private String user = AuthorizationProvider.instance.get().getClient().getId();
|
||||
|
||||
public void run() {
|
||||
for (String id: idsToDelete) {
|
||||
try {
|
||||
IClient client = Utils.getStorageClient(user).getClient();
|
||||
client.remove().RFileById(id);
|
||||
log.debug("file with id {} correctly removed on storage",id);
|
||||
}catch(Throwable t) {
|
||||
log.warn("error removing file on storage with id {}",id, t);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}.start();;
|
||||
ses.save();
|
||||
}finally {
|
||||
ses.getWorkspace().getLockManager().unlock(parentPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void getAllContentIds(Session ses, Set<String> idsToDelete, Item itemToDelete) throws Exception{
|
||||
if (itemToDelete instanceof AbstractFileItem) {
|
||||
List<Version> versions = versionHandler.getContentVersionHistory(ses.getNodeByIdentifier(itemToDelete.getId()), ses);
|
||||
|
||||
versions.forEach(v -> {
|
||||
try {
|
||||
String storageId =v.getProperty("hl:storageId").toString();
|
||||
idsToDelete.add(storageId);
|
||||
log.info("retrieved StorageId {} for version {}", storageId, v.getName());
|
||||
} catch (Exception e) {
|
||||
log.warn("error retreiving sotrageId",e);
|
||||
}
|
||||
});
|
||||
|
||||
idsToDelete.add(((AbstractFileItem) itemToDelete).getContent().getStorageId());
|
||||
}else if (itemToDelete instanceof FolderItem) {
|
||||
List<Item> items = Utils.getItemList(ses.getNodeByIdentifier(itemToDelete.getId()), Arrays.asList(NodeConstants.ACCOUNTING_NAME, NodeConstants.METADATA_NAME, NodeConstants.OWNER_NAME) , null, true);
|
||||
for (Item item: items)
|
||||
getAllContentIds(ses, idsToDelete, item);
|
||||
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void moveToTrash(Session ses, Node nodeToDelete, Item item) throws Exception{
|
||||
log.debug("moving node to trash");
|
||||
final Node trashFolder = ses.getNode(Paths.append(Utils.getHomePath(),Constants.TRASH_ROOT_FOLDER_NAME).toPath());
|
||||
|
||||
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
|
||||
try {
|
||||
ses.getWorkspace().getLockManager().lock(trashFolder.getPath(), true, true, 0,login);
|
||||
ses.getWorkspace().getLockManager().lock(nodeToDelete.getPath(), true, true, 0,login);
|
||||
|
||||
log.debug("preparing thrash item");
|
||||
|
||||
TrashItem trashItem = new TrashItem();
|
||||
trashItem.setDeletedBy(AuthorizationProvider.instance.get().getClient().getId());
|
||||
trashItem.setDeletedFrom(nodeToDelete.getParent().getPath());
|
||||
Calendar now = Calendar.getInstance();
|
||||
trashItem.setDeletedTime(now);
|
||||
trashItem.setHidden(false);
|
||||
trashItem.setLastAction(ItemAction.CREATED);
|
||||
trashItem.setDescription("trash item of node " + nodeToDelete.getPath());
|
||||
trashItem.setParentId(trashFolder.getIdentifier());
|
||||
trashItem.setParentPath(trashFolder.getPath());
|
||||
String pathUUid= UUID.randomUUID().toString();
|
||||
trashItem.setTitle(pathUUid);
|
||||
trashItem.setName(pathUUid);
|
||||
trashItem.setOriginalParentId(nodeToDelete.getParent().getIdentifier());
|
||||
|
||||
trashItem.setOwner(item.getOwner());
|
||||
trashItem.setLastModificationTime(item.getLastModificationTime());
|
||||
trashItem.setLastModifiedBy(item.getLastModifiedBy());
|
||||
|
||||
trashItem.setLenght(0);
|
||||
|
||||
if (item instanceof FolderItem)
|
||||
trashItem.setFolder(true);
|
||||
else if (item instanceof AbstractFileItem ) {
|
||||
AbstractFileItem file = (AbstractFileItem) item;
|
||||
trashItem.setMimeType(file.getContent().getMimeType());
|
||||
trashItem.setLenght(file.getContent().getSize());
|
||||
}
|
||||
|
||||
log.debug("creating node");
|
||||
|
||||
Node newTrashItemNode = ItemHandler.createNodeFromItem(ses, trashFolder, trashItem);
|
||||
|
||||
ses.save();
|
||||
log.debug("calling move into jcr");
|
||||
ses.getWorkspace().move(nodeToDelete.getPath(), Paths.append(Paths.getPath(newTrashItemNode.getPath()),nodeToDelete.getName()).toPath());
|
||||
String mimetype = null;
|
||||
if (item instanceof AbstractFileItem)
|
||||
mimetype = ((AbstractFileItem) item).getContent().getMimeType();
|
||||
accountingHandler.createFolderRemoveObj(item.getName(), item.getClass().getSimpleName(), mimetype, ses, ses.getNodeByIdentifier(item.getParentId()), true);
|
||||
}finally {
|
||||
ses.getWorkspace().getLockManager().unlock(nodeToDelete.getPath());
|
||||
ses.getWorkspace().getLockManager().unlock(trashFolder.getPath());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -73,6 +73,9 @@ public class WorkspaceManager {
|
|||
@QueryParam("exclude")
|
||||
private List<String> excludes = Collections.emptyList();
|
||||
|
||||
SimpleCredentials credential = new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray());
|
||||
|
||||
|
||||
|
||||
@Path("")
|
||||
@GET
|
||||
|
@ -89,7 +92,7 @@ public class WorkspaceManager {
|
|||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
long start = System.currentTimeMillis();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
log.trace("time to connect to repo {}",(System.currentTimeMillis()-start));
|
||||
Node node = ses.getNode(absolutePath.toPath());
|
||||
authChecker.checkReadAuthorizationControl(ses, node.getIdentifier());
|
||||
|
@ -135,7 +138,7 @@ public class WorkspaceManager {
|
|||
Session ses = null;
|
||||
try {
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
return new ItemWrapper<Item>(getVreFolderItem(ses).getVreFolder());
|
||||
}catch(Throwable e){
|
||||
log.error("error reading vreNode for context {}",ScopeProvider.instance.get(),e);
|
||||
|
@ -155,7 +158,7 @@ public class WorkspaceManager {
|
|||
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
|
||||
VRE vre = getVreFolderItem(ses);
|
||||
log.trace("VRE retrieved {}",vre.getVreFolder().getTitle());
|
||||
|
@ -184,7 +187,7 @@ public class WorkspaceManager {
|
|||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
long start = System.currentTimeMillis();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
log.info("time to connect to repo {}",(System.currentTimeMillis()-start));
|
||||
|
||||
|
||||
|
@ -214,7 +217,7 @@ public class WorkspaceManager {
|
|||
List<? extends Item> toReturn = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
toReturn = Utils.getItemList(ses.getNode(vrePath.toPath()) , excludes, null, false);
|
||||
}catch(Throwable e){
|
||||
log.error("error reading the node children of {}",vrePath,e);
|
||||
|
@ -238,7 +241,7 @@ public class WorkspaceManager {
|
|||
List<? extends Item> toReturn = null;
|
||||
try{
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
toReturn = Utils.getItemList(ses.getNode(vrePath.toPath()) , excludes, new Range(start, limit), false);
|
||||
}catch(Throwable e){
|
||||
log.error("(paged) error reading the node children of {}",vrePath,e);
|
||||
|
@ -279,7 +282,7 @@ public class WorkspaceManager {
|
|||
|
||||
|
||||
String login = AuthorizationProvider.instance.get().getClient().getId();
|
||||
ses = repository.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
|
||||
ses = repository.getRepository().login(credential);
|
||||
Query jcrQuery = ses.getWorkspace().getQueryManager().createQuery(sql2Query, Constants.QUERY_LANGUAGE);
|
||||
|
||||
if (limit!=null && limit!=-1 )
|
||||
|
|
|
@ -16,9 +16,14 @@ import org.gcube.common.storagehub.model.types.ItemAction;
|
|||
import org.gcube.data.access.storagehub.handlers.ItemHandler;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class TestFields {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(TestFields.class);
|
||||
|
||||
@Test
|
||||
public void replace(){
|
||||
System.out.println("/Home/Giancarlo".replaceAll("^/(.*)/?$", "$1").replaceAll("/", "-"));
|
||||
|
@ -62,4 +67,5 @@ public class TestFields {
|
|||
Assert.assertTrue(item.isShared());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue