refactoring of some classes

This commit is contained in:
Lucio Lelii 2020-04-14 18:52:59 +02:00
parent e82d695bbf
commit 94d9307b4c
11 changed files with 198 additions and 54 deletions

View File

@ -0,0 +1,51 @@
package org.gcube.data.access.storagehub;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
import org.gcube.data.access.storagehub.handlers.ClassHandler;
public class NodeChildrenFilterIterator implements Iterator<Node> {
private NodeIterator it;
public NodeChildrenFilterIterator(Node node) throws BackendGenericError{
super();
try {
it = node.getNodes();
} catch (RepositoryException e) {
throw new BackendGenericError(e);
}
}
public NodeChildrenFilterIterator(NodeIterator iterator) throws BackendGenericError{
it = iterator;
}
private Node currentNode = null;
@Override
public boolean hasNext() {
try {
while (it.hasNext()) {
currentNode=it.nextNode();
if (ClassHandler.instance().get(currentNode.getPrimaryNodeType().getName())!=null)
return true;
}
return false;
}catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
@Override
public Node next() {
return currentNode;
}
}

View File

@ -119,21 +119,21 @@ public class Utils {
logger.debug("query for search is {}",xpath);
long start = System.currentTimeMillis();
Query jcrQuery = ses.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);
NodeIterator it = jcrQuery.execute().getNodes();
NodeChildrenFilterIterator iterator = new NodeChildrenFilterIterator(jcrQuery.execute().getNodes());
logger.debug("[SEARCH] real search took {} millis",(System.currentTimeMillis()-start));
return getItemListFromNodeIterator(authChecker, it, excludes, range, showHidden, excludeTrashed, nodeTypeToInclude);
return getItemListFromNodeIterator(authChecker, iterator , excludes, range, showHidden, excludeTrashed, nodeTypeToInclude);
}
public static <T extends Item> List<T> getItemList(Node parent, List<String> excludes, Range range, boolean showHidden, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
logger.trace("getting children of node {}", parent.getIdentifier());
long start = System.currentTimeMillis();
NodeIterator iterator = parent.getNodes();
NodeChildrenFilterIterator iterator = new NodeChildrenFilterIterator(parent);
logger.trace("time to get iterator {}",(System.currentTimeMillis()-start));
return getItemListFromNodeIterator(null, iterator, excludes, range, showHidden, false, nodeTypeToInclude);
}
private static <T extends Item> List<T> getItemListFromNodeIterator(AuthorizationChecker authChecker, NodeIterator iterator, List<String> excludes, Range range, boolean showHidden, boolean excludeTrashed, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
private static <T extends Item> List<T> getItemListFromNodeIterator(AuthorizationChecker authChecker, NodeChildrenFilterIterator iterator, List<String> excludes, Range range, boolean showHidden, boolean excludeTrashed, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
List<T> returnList = new ArrayList<T>();
logger.trace("nodeType is {}",nodeTypeToInclude);
@ -142,7 +142,7 @@ public class Utils {
Node2ItemConverter node2Item= new Node2ItemConverter();
Set<String> duplicateId = new HashSet<String>();
while (iterator.hasNext()){
Node current = iterator.nextNode();
Node current = iterator.next();
logger.debug("[SEARCH] evaluating node {} ",current.hasProperty(NodeProperty.TITLE.toString())? current.getProperty(NodeProperty.TITLE.toString()):current.getName());
@ -152,7 +152,7 @@ public class Utils {
continue;
}
//ECLUDES node not authorized, in case the indexes are not working
//EXCLUDES node not authorized, in case the indexes are not working
if (authChecker!=null)
try {
authChecker.checkReadAuthorizationControl(current.getSession(), current.getIdentifier());
@ -269,10 +269,10 @@ public class Utils {
public static boolean hasSharedChildren(Node node) throws RepositoryException, BackendGenericError{
Node2ItemConverter node2Item = new Node2ItemConverter();
NodeIterator children = node.getNodes();
NodeChildrenFilterIterator children = new NodeChildrenFilterIterator(node);
while (children.hasNext()) {
Node child= children.nextNode();
Node child= children.next();
if (node2Item.checkNodeType(child, SharedFolder.class)) return true;
if (node2Item.checkNodeType(child, FolderItem.class) && hasSharedChildren(child)) return true;
}

View File

@ -1,6 +1,8 @@
package org.gcube.data.access.storagehub.handlers;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -24,6 +26,10 @@ public class ClassHandler {
private Reflections reflection = new Reflections();
private List<String> deprecatedNode = Arrays.asList("nthl:query", "nthl:aquamapsItem", "nthl:timeSeriesItem", "nthl:report", "nthl:reportTemplate", "nthl:workflowReport",
"nthl:workflowTemplate", "nthl:gCubeMetadata", "nthl:gCubeDocument", "nthl:gCubeDocumentLink", "nthl:gCubeImageDocumentLink", "nthl:gCubePDFDocumentLink",
"nthl:gCubeImageDocument", "nthl:gCubePDFDocument", "nthl:gCubeURLDocument", "nthl:gCubeAnnotation", "nthl:externalResourceLink", "nthl:tabularDataLink");
private Map<String, Class<? extends Item>> classMap = new HashMap<String, Class<? extends Item>>();
private Map<Class<? extends Item>, String> typeMap = new HashMap<Class<? extends Item>, String>();
@ -44,7 +50,8 @@ public class ClassHandler {
public Class<? extends Item> get(String nodeType){
if (classMap.containsKey(nodeType)) return classMap.get(nodeType);
else return Item.class;
if (deprecatedNode.contains(nodeType)) return Item.class;
return null;
//throw new RuntimeException("mapping not found for nodetype "+ nodeType);
}

View File

@ -20,6 +20,7 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider;
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.InvalidCallParameters;
import org.gcube.common.storagehub.model.exceptions.ItemLockedException;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException;
@ -43,7 +44,7 @@ public class TrashHandler {
private static Logger log = LoggerFactory.getLogger(TrashHandler.class);
ExecutorService executor = Executors.newFixedThreadPool(100);
@Inject
VersionHandler versionHandler;
@ -55,13 +56,13 @@ public class TrashHandler {
@Inject
Item2NodeConverter item2Node;
@Inject
Node2ItemConverter node2Item;
@Inject
StorageBackendHandler storageHandler;
public void removeNodes(Session ses, List<Item> itemsToDelete) throws RepositoryException, StorageHubException{
log.debug("defnitively removing nodes with ids {}",itemsToDelete);
for (Item item: itemsToDelete) {
@ -70,7 +71,7 @@ public class TrashHandler {
}
private void removeNodesInternally(Session ses, Item itemToDelete) throws RepositoryException, StorageHubException {
try {
Set<String> contentIdsToDelete = new HashSet<>();
@ -84,12 +85,12 @@ public class TrashHandler {
Utils.getAllContentIds(ses, contentIdsToDelete, itemToDelete, versionHandler);
}
nodeToDelete.remove();
log.debug("content ids to remove are {}",contentIdsToDelete);
//String user = AuthorizationProvider.instance.get().getClient().getId();
Runnable deleteFromStorageRunnable = AuthorizedTasks.bind(new Runnable() {
@Override
public void run() {
for (String id: contentIdsToDelete) {
@ -100,11 +101,11 @@ public class TrashHandler {
log.warn("error removing file on storage with id {}",id, t);
}
}
}
});
executor.execute(deleteFromStorageRunnable);
ses.save();
}catch (LockException e) {
throw new ItemLockedException("the selected node or his parent is locked", e);
@ -181,51 +182,66 @@ public class TrashHandler {
}
public String restoreItem(Session ses, TrashItem item) throws RepositoryException, BackendGenericError, UserNotAuthorizedException{
public String restoreItem(Session ses, TrashItem item, FolderItem destination) throws RepositoryException, StorageHubException, BackendGenericError{
log.debug("restoring node from trash");
final String login = AuthorizationProvider.instance.get().getClient().getId();
//final Node trashFolder = ses.getNode(Paths.append(Utils.getHomePath(),Constants.TRASH_ROOT_FOLDER_NAME).toPath());
boolean originalParentExists = true;
boolean originalParentTrashed = false;
Node originalParent = null;
try {
originalParent = ses.getNodeByIdentifier(item.getOriginalParentId());
}catch (ItemNotFoundException e) {
originalParentExists = false;
}
Item originalParentItem = null;
if (originalParentExists) {
originalParentItem = node2Item.getItem(originalParent, Excludes.ALL);
originalParentTrashed = originalParentItem.isTrashed();
}
Node destinationNode= null;
if(originalParentExists && !originalParentTrashed) {
authChecker.checkWriteAuthorizationControl(ses, originalParentItem, originalParent, false );
destinationNode = originalParent;
}else {
String homeWS = Utils.getWorkspacePath(login).toPath();
Node node = ses.getNode(homeWS);
if (destination==null) {
boolean originalParentExists = true;
boolean originalParentTrashed = false;
Node originalParent = null;
try {
originalParent = ses.getNodeByIdentifier(item.getOriginalParentId());
}catch (ItemNotFoundException e) {
originalParentExists = false;
}
Item originalParentItem = null;
if (originalParentExists) {
originalParentItem = node2Item.getItem(originalParent, Excludes.ALL);
originalParentTrashed = originalParentItem.isTrashed();
}
if(originalParentExists && !originalParentTrashed) {
authChecker.checkWriteAuthorizationControl(ses, originalParentItem, originalParent, false );
destinationNode = originalParent;
}else {
String homeWS = Utils.getWorkspacePath(login).toPath();
Node node = ses.getNode(homeWS);
authChecker.checkWriteAuthorizationControl(ses, node.getIdentifier(), false );
destinationNode = node;
}
} else {
Node node = ses.getNodeByIdentifier(destination.getId());
if (!node2Item.checkNodeType(node, FolderItem.class))
throw new InvalidCallParameters("destination Node is not a folder");
authChecker.checkWriteAuthorizationControl(ses, node.getIdentifier(), false );
destinationNode = node;
}
ses.getWorkspace().getLockManager().lock(destinationNode.getPath(), true, true, 0,login);
ses.getWorkspace().getLockManager().lock(originalParent.getPath(), true, true, 0,login);
//the real node is a child of the Trash node
List<Item> items = Utils.getItemList(ses.getNodeByIdentifier(item.getId()), Excludes.ALL, null, false, null);
if (items.size()!=1) {
log.warn("a problem occurred restoring item from trash");
throw new BackendGenericError("An error occurred on trash item");
}
Item itemToMove = items.get(0);
item2Node.updateOwnerOnSubTree(ses.getNodeByIdentifier(itemToMove.getId()), login);
String newNodePath = Paths.append(Paths.getPath(destinationNode.getPath()), itemToMove.getName()).toPath();
ses.move(itemToMove.getPath(), newNodePath);
Utils.setPropertyOnChangeNode(ses.getNode(newNodePath), login, ItemAction.MOVED);
ses.removeItem(item.getPath());
ses.save();
return ses.getNode(newNodePath).getIdentifier();
}
}

View File

@ -24,6 +24,7 @@ import org.apache.jackrabbit.util.ISO9075;
import org.gcube.common.storagehub.model.Excludes;
import org.gcube.common.storagehub.model.NodeConstants;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.data.access.storagehub.NodeChildrenFilterIterator;
import org.gcube.data.access.storagehub.handlers.items.Node2ItemConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -73,10 +74,10 @@ public class VREQueryRetriever implements Callable<List<Item>> {
jcrQuery.setLimit(CACHE_DIMENSION);
lastTimestamp = System.currentTimeMillis();
NodeIterator it = jcrQuery.execute().getNodes();
NodeChildrenFilterIterator it = new NodeChildrenFilterIterator(jcrQuery.execute().getNodes());
logger.debug("query for recents took {}",System.currentTimeMillis()-start);
while (it.hasNext()) {
Node node = it.nextNode();
Node node = it.next();
//long lastModifiedTimeItem = node.getProperty(NodeProperty.LAST_MODIFIED.toString()).getLong();

View File

@ -13,6 +13,7 @@ import java.util.Set;
import javax.inject.Singleton;
import javax.jcr.ItemExistsException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
@ -31,10 +32,13 @@ import org.gcube.common.storagehub.model.annotations.ListNodes;
import org.gcube.common.storagehub.model.annotations.MapAttribute;
import org.gcube.common.storagehub.model.annotations.NodeAttribute;
import org.gcube.common.storagehub.model.annotations.RootNode;
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
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.types.ItemAction;
import org.gcube.common.storagehub.model.types.NodeProperty;
import org.gcube.data.access.storagehub.NodeChildrenFilterIterator;
import org.gcube.data.access.storagehub.Utils;
import org.gcube.data.access.storagehub.handlers.ClassHandler;
import org.slf4j.Logger;
@ -222,6 +226,28 @@ public class Item2NodeConverter {
node.setProperty(NodeProperty.HIDDEN.toString(), hidden);
}
public void updateOwnerOnSubTree(Node node, String owner) throws RepositoryException, BackendGenericError {
Class<? extends Item> classToHandle = (Class<? extends Item>)ClassHandler.instance().get(node.getPrimaryNodeType().getName());
if (classToHandle==null) return;
if (classToHandle.isAssignableFrom(FolderItem.class)) {
NodeChildrenFilterIterator iterator = new NodeChildrenFilterIterator(node);
while (iterator.hasNext()) {
Node nextNode = iterator.next();
updateOwnerOnSubTree(nextNode, owner);
}
}
updateOwner(node, owner);
}
public void updateOwner(Node node, String owner) throws RepositoryException {
//Utils.setPropertyOnChangeNode(node, login, ItemAction.UPDATED);
if (node.hasProperty(NodeProperty.PORTAL_LOGIN.toString()))
node.setProperty(NodeProperty.PORTAL_LOGIN.toString(), owner);
else logger.debug("cannot set new owner to {} "+node.getPath());
}
public <I extends Item> void updateMetadataNode(Node node, Map<String, Object> meta, String login){
try {

View File

@ -53,6 +53,7 @@ public class Node2ItemConverter {
public <T extends Item> T getFilteredItem(Node node, List<String> excludes, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
@SuppressWarnings("unchecked")
Class<T> classToHandle = (Class<T>)ClassHandler.instance().get(node.getPrimaryNodeType().getName());
if (classToHandle==null) return null;
if (nodeTypeToInclude!=null && !(nodeTypeToInclude.isAssignableFrom(classToHandle))) return null;
else return retrieveItem(node, excludes, classToHandle);
}
@ -65,6 +66,7 @@ public class Node2ItemConverter {
public <T extends Item> T getItem(Node node, List<String> excludes) throws RepositoryException, BackendGenericError{
@SuppressWarnings("unchecked")
Class<T> classToHandle = (Class<T>)ClassHandler.instance().get(node.getPrimaryNodeType().getName());
if (classToHandle==null) return null;
/*Node nodeToRetrieve= node;
if (SharedFolder.class.isAssignableFrom(classToHandle)) {
NodeIterator it= node.getSharedSet();
@ -323,6 +325,7 @@ public class Node2ItemConverter {
return values;
}
//Checks if a node is a subtype od classToCompare
public boolean checkNodeType(Node node, Class<? extends Item> classToCompare) throws BackendGenericError{
try {

View File

@ -17,6 +17,7 @@ import javax.jcr.query.QueryResult;
import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@ -30,12 +31,14 @@ 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.InvalidCallParameters;
import org.gcube.common.storagehub.model.exceptions.InvalidItemException;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException;
import org.gcube.common.storagehub.model.expressions.Expression;
import org.gcube.common.storagehub.model.expressions.logical.And;
import org.gcube.common.storagehub.model.expressions.logical.ISDescendant;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.items.TrashItem;
import org.gcube.common.storagehub.model.service.ItemList;
@ -273,22 +276,22 @@ public class WorkspaceManager {
}
@PUT
@Consumes(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("trash/restore")
public String restoreItem(String identifier){
public String restoreItem(@FormParam("trashedItemId") String trashedItemId,@FormParam("destinationId") String destinationFolderId){
InnerMethodName.instance.set("restoreItem");
Session ses = null;
String toReturn = null;
try{
log.info("restoring node with id {}", identifier);
log.info("restoring node with id {}", trashedItemId);
//TODO check if it is possible to change all the ACL on a workspace
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
//authChecker.checkWriteAuthorizationControl(ses, identifier, false);
final Node nodeToRestore = ses.getNodeByIdentifier(identifier);
final Node nodeToRestore = ses.getNodeByIdentifier(trashedItemId);
Item itemToRestore = node2Item.getItem(nodeToRestore, Excludes.ALL);
@ -299,11 +302,21 @@ public class WorkspaceManager {
org.gcube.common.storagehub.model.Path trashPath = Paths.append(Utils.getWorkspacePath(), Constants.TRASH_ROOT_FOLDER_NAME);
if (!itemToRestore.getPath().startsWith(trashPath.toPath()))
throw new UserNotAuthorizedException("this item is not in the user "+user+" trash");
Item destinationItem = null;
if (destinationFolderId!=null ) {
destinationItem = node2Item.getItem(ses.getNodeByIdentifier(destinationFolderId), Excludes.ALL);
if (!(destinationItem instanceof FolderItem))
throw new InvalidCallParameters("destintation item is not a folder");
toReturn = trashHandler.restoreItem(ses, (TrashItem)itemToRestore, (FolderItem) destinationItem);
} else {
toReturn = trashHandler.restoreItem(ses, (TrashItem)itemToRestore, null);
}
toReturn = trashHandler.restoreItem(ses, (TrashItem)itemToRestore);
}catch(RepositoryException re ){
log.error("error restoring item with id {}",identifier, re);
log.error("error restoring item with id {}",trashedItemId, re);
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
}catch(StorageHubException she ){
log.error(she.getErrorMessage(), she);

View File

@ -0,0 +1,22 @@
package org.gcube.data.access.storagehub.services.admin;
import javax.jcr.Item;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
@Path("admin/items")
public class ItemManager {
@POST
@Consumes(MediaType.APPLICATION_JSON)
private String createItem(Item item) {
return null;
}
}

View File

@ -25,7 +25,7 @@ The projects leading to this software have received funding from a series of
Version
--------------------------------------------------
1.1.1-SNAPSHOT (2020-04-09)
1.1.1-SNAPSHOT (2020-04-14)
Please see the file named "changelog.xml" in this directory for the release notes.

View File

@ -9,6 +9,7 @@ import org.gcube.common.storagehub.model.expressions.GenericSearchableItem;
import org.gcube.common.storagehub.model.expressions.date.Before;
import org.gcube.common.storagehub.model.expressions.logical.And;
import org.gcube.common.storagehub.model.expressions.text.Contains;
import org.gcube.data.access.storagehub.Constants;
import org.gcube.data.access.storagehub.query.sql2.evaluators.Evaluators;
import org.junit.Test;
import org.slf4j.Logger;
@ -37,9 +38,13 @@ public class Expressions {
@Test
public void test2() {
String groupId ="/gcube/devsec/devVre";
String title = groupId.substring(groupId.lastIndexOf("/")+1);
System.out.println(title);
String workspace ="/Share/22228279-0793-4b68-b7c9-81489b68a355/Frosini - Resource Registry.MP4";
String rpl1 = workspace.replaceAll("/Home/[^/]*/"+Constants.WORKSPACE_ROOT_FOLDER_NAME,"");
String rpl2 = workspace.replaceAll("/Share", "");
System.out.println(rpl1+" ---- "+rpl2 );
}