first commit

This commit is contained in:
lucio 2021-07-06 11:17:27 +02:00
commit 804a6c68f1
8 changed files with 571 additions and 0 deletions

50
pom.xml Normal file
View File

@ -0,0 +1,50 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.data.access</groupId>
<artifactId>storagehub-scripts</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Storagehub Scripts</name>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.1.0</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.gcube.data.access</groupId>
<artifactId>storagehub-scripting-util</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-model</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-api</artifactId>
<version>2.19.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-jcr-commons</artifactId>
<version>2.20.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

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

View File

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

View File

@ -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<String> users = new HashSet<>();
try {
Node node = session.getNode("/Share");
List<Item> 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<AccessControlEntry> 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();
}
}

View File

@ -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 = "<mix = 'http://www.jcp.org/jcr/mix/1.0'>\n"
+ "<nt = 'http://www.jcp.org/jcr/nt/1.0'>\n"
+ "<nthl = 'http://ip-server:port/hl/nthl'>\n"
+ "<hl = 'http://ip-server:port/hl'>\n"+
"[nthl:home] > nt:folder\n"
+ " - hl:scopes (String) multiple\n"+
" - hl:version (Long)";
//TODO: must be fullfilled
private static final String addExternalFolders = "<mix = 'http://www.jcp.org/jcr/mix/1.0'>\n"
+ "<nt = 'http://www.jcp.org/jcr/nt/1.0'>\n"
+ "<nthl = 'http://ip-server:port/hl/nthl'>\n"
+ "<hl = 'http://ip-server:port/hl'>\n"+
"";
private static final String addSharableToItemSent ="<mix = 'http://www.jcp.org/jcr/mix/1.0'>\n"
+ "<nt = 'http://www.jcp.org/jcr/nt/1.0'>\n"
+ "<nthl = 'http://ip-server:port/hl/nthl'>\n"
+ "<hl = 'http://ip-server:port/hl'>\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();
}
}

View File

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

View File

@ -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<String, Long> 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<String, Long> calculateDiskSpace(Item item, ScriptUtil scriptUtil, Session session ) throws Exception {
Map<String, Long> 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<? extends Item> items = scriptUtil.getChildren(null, node, Excludes.GET_ONLY_CONTENT, true, null);
for (Item child : items)
try {
Map<String, Long> 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;
}
}

View File

@ -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<Item> 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<String, Long> 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<String, Long> calculateDiskSpace(Item item, ScriptUtil scriptUtil, Session session ) throws Exception {
Map<String, Long> 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<? extends Item> items = scriptUtil.getChildren(null, node, Excludes.GET_ONLY_CONTENT, true, null);
for (Item child : items)
try {
Map<String, Long> 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;
}
}