From 804a6c68f14afc1acef819ad5e06460373a96c4b Mon Sep 17 00:00:00 2001 From: lucio Date: Tue, 6 Jul 2021 11:17:27 +0200 Subject: [PATCH] first commit --- pom.xml | 50 +++++++++++++ src/main/java/GeneralScript.java | 69 ++++++++++++++++++ src/main/java/MoveVREsAndTrash.java | 92 ++++++++++++++++++++++++ src/main/java/RemoveOldUserFromACL.java | 84 ++++++++++++++++++++++ src/main/java/UpdateNodeType.java | 65 +++++++++++++++++ src/main/java/UpdateVersionsOnHomes.java | 40 +++++++++++ src/main/java/UsersSpaceAndItem.java | 88 +++++++++++++++++++++++ src/main/java/VRESItemAndSpace.java | 83 +++++++++++++++++++++ 8 files changed, 571 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/GeneralScript.java create mode 100644 src/main/java/MoveVREsAndTrash.java create mode 100644 src/main/java/RemoveOldUserFromACL.java create mode 100644 src/main/java/UpdateNodeType.java create mode 100644 src/main/java/UpdateVersionsOnHomes.java create mode 100644 src/main/java/UsersSpaceAndItem.java create mode 100644 src/main/java/VRESItemAndSpace.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a8ad4dc --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + org.gcube.data.access + storagehub-scripts + 1.0.0-SNAPSHOT + Storagehub Scripts + + + maven-parent + org.gcube.tools + 1.1.0 + + + + + + org.gcube.data.access + storagehub-scripting-util + [1.0.0-SNAPSHOT,2.0.0) + provided + + + org.gcube.common + storagehub-model + [1.0.0,2.0.0-SNAPSHOT) + provided + + + javax.jcr + jcr + 2.0 + provided + + + org.apache.jackrabbit + jackrabbit-api + 2.19.3 + provided + + + org.apache.jackrabbit + jackrabbit-jcr-commons + 2.20.2 + provided + + + + \ No newline at end of file diff --git a/src/main/java/GeneralScript.java b/src/main/java/GeneralScript.java new file mode 100644 index 0000000..a481cbb --- /dev/null +++ b/src/main/java/GeneralScript.java @@ -0,0 +1,69 @@ +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.Property; +import javax.jcr.PropertyIterator; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GeneralScript implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(GeneralScript.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + try { + + //session.getNodeByIdentifier("6f4b7392-38fc-4c10-bc35-5bad9d21c5d6").remove(); + //session.save(); + + + /* + Node node = session.getNode("/Home/andrea.rossi/InBox"); + NodeIterator it = node.getNodes(); + while (it.hasNext()) { + Node child = it.nextNode(); + Node attachment = child.getNode("hl:attachments"); + visitChildren(stringBuilder, attachment, 1); + }*/ + }catch (Throwable e) { + log.error("error updating nodes",e); + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } + + private void visitChildren(StringBuilder stringBuilder, Node node, int level) throws Exception{ + stringBuilder.append("-").append(node.getPath()).append("\n"); + PropertyIterator props = node.getProperties(); + while (props.hasNext()) { + Property prop = props.nextProperty(); + if (prop.isMultiple()) continue; + for (int i=0 ; i<=level; i++) { + stringBuilder.append("\t"); + } + stringBuilder.append("--").append(prop.getName()).append(",").append(prop.getString()).append("\n"); + } + NodeIterator nodeIt = node.getNodes(); + while(nodeIt.hasNext()) { + Node child = nodeIt.nextNode(); + for (int i=0 ; i<=level; i++) { + stringBuilder.append("\t"); + } + stringBuilder.append("--").append(child.getPath()).append(",").append(child.getPrimaryNodeType().getName()).append("\n"); + visitChildren(stringBuilder, child, level+1); + } + } + +} diff --git a/src/main/java/MoveVREsAndTrash.java b/src/main/java/MoveVREsAndTrash.java new file mode 100644 index 0000000..9c63157 --- /dev/null +++ b/src/main/java/MoveVREsAndTrash.java @@ -0,0 +1,92 @@ +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.common.storagehub.model.Path; +import org.gcube.common.storagehub.model.Paths; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MoveVREsAndTrash implements AbstractScript{ + + //private static Logger log = LoggerFactory.getLogger(MoveVREsAndTrash.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + try { + Node node = session.getNode("/Home"); + + NodeIterator it = node.getNodes(); + + while (it.hasNext()) { + Node home = it.nextNode(); + + long start = System.currentTimeMillis(); + Path workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace"); + Path homePath = Paths.getPath(home.getPath()); + if (!session.nodeExists(workspacePath.toPath())) continue; + + Boolean oldVresExists = session.nodeExists(Paths.append(workspacePath, "MySpecialFolders").toPath()); + Boolean oldTrashExists = session.nodeExists(Paths.append(workspacePath, "Trash").toPath()); + + if (!(oldVresExists && oldTrashExists)) { + stringBuilder.append(home.getPath()).append(" not contains in Workspace folder: "); + if (!oldTrashExists) stringBuilder.append(" Trash "); + if (!oldVresExists) stringBuilder.append(" MySpecialFolders "); + stringBuilder.append("\n"); + continue; + } + + session.getWorkspace().getLockManager().lock(workspacePath.toPath(), true, true, -1, ""); + try { + + if (home.getProperty("hl:version").getLong()>0) { + stringBuilder.append(home.getPath()).append(" already updated\n"); + continue; + } + + home.setProperty("hl:version", 1l); + session.move(Paths.append(workspacePath, "MySpecialFolders").toPath(), Paths.append(homePath, "VREs").toPath()); + session.move(Paths.append(workspacePath, "Trash").toPath(), Paths.append(homePath, "Trash").toPath()); + + Boolean vresExist = session.nodeExists(Paths.append(homePath, "VREs").toPath()); + Boolean trashExist = session.nodeExists(Paths.append(homePath, "Trash").toPath()); + + stringBuilder.append(workspacePath.toPath()).append(",").append(vresExist).append(",") + .append(trashExist).append(",").append(System.currentTimeMillis()-start).append("\n"); + + session.save(); + /* + session.move("/Home/lucio.lelii/Workspace/MySpecialFolders", "/Home/lucio.lelii/VREs"); + + Node vres = session.getNode("/Home/lucio.lelii/VREs"); + + session.move("/Home/lucio.lelii/Workspace/Trash", "/Home/lucio.lelii/Trash"); + + Node trash = session.getNode("/Home/lucio.lelii/Trash"); + session.save();*/ + + + + } finally { + session.getWorkspace().getLockManager().unlock(workspacePath.toPath()); + } + + } + + }catch (Exception e) { + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } +} diff --git a/src/main/java/RemoveOldUserFromACL.java b/src/main/java/RemoveOldUserFromACL.java new file mode 100644 index 0000000..06b144c --- /dev/null +++ b/src/main/java/RemoveOldUserFromACL.java @@ -0,0 +1,84 @@ +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.jcr.security.AccessControlEntry; +import javax.jcr.security.AccessControlManager; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.apache.jackrabbit.api.security.JackrabbitAccessControlList; +import org.apache.jackrabbit.api.security.user.Authorizable; +import org.apache.jackrabbit.api.security.user.UserManager; +import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils; +import org.gcube.common.storagehub.model.Excludes; +import org.gcube.common.storagehub.model.items.Item; +import org.gcube.common.storagehub.model.items.SharedFolder; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RemoveOldUserFromACL implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(RemoveOldUserFromACL.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + Set users = new HashSet<>(); + try { + + Node node = session.getNode("/Share"); + + List shared = scriptUtil.getChildren(null, node, Excludes.GET_ONLY_CONTENT, true, null) + .stream().filter(i -> i instanceof SharedFolder).collect(Collectors.toList()); + + UserManager uMan = session.getUserManager(); + AccessControlManager acm = session.getAccessControlManager(); + + for (Item folder : shared) { + JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(session, folder.getPath() ); + + List acesToDelete = new ArrayList<>(); + for (AccessControlEntry ace : acls.getAccessControlEntries()) { + Authorizable aut = uMan.getAuthorizable(ace.getPrincipal()); + if (aut==null) { + log.info("user {} doesn't exist anymore, it will be removed",ace.getPrincipal().getName()); + users.add(ace.getPrincipal().getName()); + acesToDelete.add(ace); + } + } + + if (acesToDelete.size()>0) { + acesToDelete.forEach(a -> { + try { + acls.removeAccessControlEntry(a); + } catch (RepositoryException e) { + log.warn("cannot remove acl"); + } + }); + } + acm.setPolicy(folder.getPath(), acls); + + } + users.forEach(u -> stringBuilder.append(u).append("\n")); + if (users.isEmpty()) + stringBuilder.append("no invalid users found in shared folders"); + }catch (Throwable e) { + log.error("error updating nodes",e); + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } + +} diff --git a/src/main/java/UpdateNodeType.java b/src/main/java/UpdateNodeType.java new file mode 100644 index 0000000..bdf4cbc --- /dev/null +++ b/src/main/java/UpdateNodeType.java @@ -0,0 +1,65 @@ +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Arrays; + +import javax.jcr.nodetype.NodeType; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.apache.jackrabbit.commons.cnd.CndImporter; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UpdateNodeType implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(UpdateNodeType.class); + + private static final String addVersionToHomes = "\n" + + "\n" + + "\n" + + "\n"+ + "[nthl:home] > nt:folder\n" + + " - hl:scopes (String) multiple\n"+ + " - hl:version (Long)"; + + + //TODO: must be fullfilled + private static final String addExternalFolders = "\n" + + "\n" + + "\n" + + "\n"+ + ""; + + private static final String addSharableToItemSent ="\n" + + "\n" + + "\n" + + "\n" + +"[nthl:itemSentRequestSH] > nthl:itemSentRequest, mix:shareable, mix:created, nt:base\n"; + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + try { + + StringReader reader = new StringReader(addVersionToHomes); + + NodeType[] nodeTypes = CndImporter.registerNodeTypes(reader, session, true); + if (nodeTypes.length>0) + Arrays.asList(nodeTypes).forEach(n -> stringBuilder.append(n.getName()).append("\n")); + else stringBuilder.append("node type not returned"); + + }catch (Throwable e) { + log.error("error updating nodes",e); + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } + +} + diff --git a/src/main/java/UpdateVersionsOnHomes.java b/src/main/java/UpdateVersionsOnHomes.java new file mode 100644 index 0000000..842fe19 --- /dev/null +++ b/src/main/java/UpdateVersionsOnHomes.java @@ -0,0 +1,40 @@ +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UpdateVersionsOnHomes implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(UpdateVersionsOnHomes.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + try { + Node node = session.getNode("/Home"); + + NodeIterator it = node.getNodes(); + + while (it.hasNext()) { + Node home = it.nextNode(); + home.setProperty("hl:version", 0l); + stringBuilder.append(home.getPath()).append(",").append(home.getProperty("hl:version").getLong()).append("\n"); + } + session.save(); + }catch (Exception e) { + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } +} diff --git a/src/main/java/UsersSpaceAndItem.java b/src/main/java/UsersSpaceAndItem.java new file mode 100644 index 0000000..bc8a11c --- /dev/null +++ b/src/main/java/UsersSpaceAndItem.java @@ -0,0 +1,88 @@ + + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.Session; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.common.storagehub.model.Excludes; +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.TrashItem; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UsersSpaceAndItem implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(UsersSpaceAndItem.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + try { + Node node = session.getNode("/Home"); + + NodeIterator it = node.getNodes(); + + while (it.hasNext()) { + Node home = it.nextNode(); + long start = System.currentTimeMillis(); + String workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace").toPath(); + if (!session.nodeExists(workspacePath)) continue; + Node child = session.getNode(workspacePath); + Item childItem = scriptUtil.getItem(child, Excludes.GET_ONLY_CONTENT); + Map val = calculateDiskSpace(childItem, scriptUtil, session); + stringBuilder.append(home.getPath()).append(",").append(val.get("space")).append(",") + .append(val.get("items")).append(",").append(System.currentTimeMillis()-start).append("\n"); + } + }catch (Exception e) { + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } + + + private Map calculateDiskSpace(Item item, ScriptUtil scriptUtil, Session session ) throws Exception { + Map val = new HashMap<>(); + val.put("space", 0l); + val.put("items", 0l); + + if (item instanceof AbstractFileItem) { + val.put("space", ((AbstractFileItem) item).getContent().getSize()); + val.put("items", 1l); + return val; + }else if (item instanceof FolderItem || item instanceof TrashItem) { + + Node node = session.getNodeByIdentifier(item.getId()); + List items = scriptUtil.getChildren(null, node, Excludes.GET_ONLY_CONTENT, true, null); + for (Item child : items) + try { + Map retVal = calculateDiskSpace(child, scriptUtil, session); + long space = retVal.get("space")+val.get("space"); + long itemsCount = retVal.get("items")+val.get("items"); + val.put("space", space); + val.put("items", itemsCount); + }catch (Exception e) { + log.error("cannot calculate disk space for item id {}",child.getId(),e); + } + + } + return val; + } + + +} \ No newline at end of file diff --git a/src/main/java/VRESItemAndSpace.java b/src/main/java/VRESItemAndSpace.java new file mode 100644 index 0000000..3aa0e44 --- /dev/null +++ b/src/main/java/VRESItemAndSpace.java @@ -0,0 +1,83 @@ + + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import javax.jcr.Node; +import javax.jcr.Session; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.common.storagehub.model.Excludes; +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.items.TrashItem; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class VRESItemAndSpace implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(VRESItemAndSpace.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + try { + Node node = session.getNode("/Share"); + + List vres = scriptUtil.getChildren(null, node, Excludes.GET_ONLY_CONTENT, true, null) + .stream().filter(i -> i instanceof SharedFolder && ((SharedFolder) i).isVreFolder()).collect(Collectors.toList()); + for (Item item: vres) { + long start = System.currentTimeMillis(); + Map val = calculateDiskSpace(item, scriptUtil, session); + stringBuilder.append(item.getTitle()).append(",").append(val.get("space")).append(",") + .append(val.get("items")).append(",").append(System.currentTimeMillis()-start).append("\n"); + } + }catch (Exception e) { + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } + + + private Map calculateDiskSpace(Item item, ScriptUtil scriptUtil, Session session ) throws Exception { + Map val = new HashMap<>(); + val.put("space", 0l); + val.put("items", 0l); + + if (item instanceof AbstractFileItem) { + val.put("space", ((AbstractFileItem) item).getContent().getSize()); + val.put("items", 1l); + return val; + }else if (item instanceof FolderItem || item instanceof TrashItem) { + + Node node = session.getNodeByIdentifier(item.getId()); + List items = scriptUtil.getChildren(null, node, Excludes.GET_ONLY_CONTENT, true, null); + for (Item child : items) + try { + Map retVal = calculateDiskSpace(child, scriptUtil, session); + long space = retVal.get("space")+val.get("space"); + long itemsCount = retVal.get("items")+val.get("items"); + val.put("space", space); + val.put("items", itemsCount); + }catch (Exception e) { + log.error("cannot calculate disk space for item id {}",child.getId(),e); + } + + } + return val; + } + + +}