diff --git a/src/main/java/it/inaf/oats/vospace/datamodel/NodeProperties.java b/src/main/java/it/inaf/oats/vospace/datamodel/NodeProperties.java index e2f21d82580b67886b81317b3c71a0fef3e02794..100ba0c9f342c60fcd43eddbcd54ecf6160370d4 100644 --- a/src/main/java/it/inaf/oats/vospace/datamodel/NodeProperties.java +++ b/src/main/java/it/inaf/oats/vospace/datamodel/NodeProperties.java @@ -19,6 +19,8 @@ public class NodeProperties { private NodeProperties() { } + + private static final String PROPERTY_BASE_URI = "ivo://ivoa.net/vospace/core#"; public static final String AVAILABLE_SPACE_URI = "ivo://ivoa.net/vospace/core#availableSpace"; // the amount of space available within a container public static final String INITIAL_CREATION_TIME_URI = "ivo://ivoa.net/vospace/core#btime"; // the initial creation time public static final String CONTRIBUTOR_URI = "ivo://ivoa.net/vospace/core#contributor"; // an entity responsible for making contributions to this resource @@ -47,19 +49,30 @@ public class NodeProperties { - public static String getProperty(Node node, String propertyName) { + public static String getPropertiesStringByName(Node node, String propertyName) { for (Property property : node.getProperties()) { - if (property.getUri().equals("ivo://ivoa.net/vospace/core#".concat(propertyName))) { + if (property.getUri().equals(PROPERTY_BASE_URI.concat(propertyName))) { return property.getValue(); } } return null; } + + public static String getPropertiesStringByURI(Node node, String uri) { + + for (Property property : node.getProperties()) { + if (uri.equals(property.getUri())) { + return property.getValue(); + } + } + return null; + + } // Returns all properties stored inside the node under the requested // property URI. - public static List<String> getNodePropertyByURI(Node node, String propertyURI) { + public static List<String> getNodePropertiesListByURI(Node node, String propertyURI) { List<String> propertyList = node.getProperties().stream() .filter((i) -> i.getUri() @@ -83,5 +96,10 @@ public class NodeProperties { return List.of(trimmedProperty.split(separator)); } + + + public static String getPropertyURI(String propertyName) { + return PROPERTY_BASE_URI.concat(propertyName); + } } diff --git a/src/main/java/it/inaf/oats/vospace/datamodel/NodeUtils.java b/src/main/java/it/inaf/oats/vospace/datamodel/NodeUtils.java index 543b97445140be47e606f4daf95c055bf21060eb..e878de1cb3dd3dad4b5dfc8d292a1203327996e5 100644 --- a/src/main/java/it/inaf/oats/vospace/datamodel/NodeUtils.java +++ b/src/main/java/it/inaf/oats/vospace/datamodel/NodeUtils.java @@ -8,7 +8,10 @@ package it.inaf.oats.vospace.datamodel; import java.security.Principal; import java.util.ArrayList; import java.util.List; +import net.ivoa.xml.vospace.v2.ContainerNode; +import net.ivoa.xml.vospace.v2.DataNode; import net.ivoa.xml.vospace.v2.Node; +import net.ivoa.xml.vospace.v2.StructuredDataNode; public class NodeUtils { @@ -87,9 +90,25 @@ public class NodeUtils { public static boolean checkIfWritable(Node myNode, String userName, List<String> userGroups) { + return checkAccessPropery(myNode, userName, userGroups, NodeProperties.GROUP_WRITE_URI); + + } + + + public static boolean checkIfRedeable(Node myNode, String userName, List<String> userGroups) { + + return checkAccessPropery(myNode, userName, userGroups, NodeProperties.GROUP_READ_URI); + + } + + + + public static boolean checkAccessPropery(Node myNode, String userName, + List<String> userGroups, String accessPropertyName) { + // First check if parent node creator is == userid List<String> nodeOwner - = NodeProperties.getNodePropertyByURI(myNode, NodeProperties.CREATOR_URI); + = NodeProperties.getNodePropertiesListByURI(myNode, NodeProperties.CREATOR_URI); if (nodeOwner == null @@ -103,18 +122,18 @@ public class NodeUtils { return false; } - List<String> groupWritePropValues - = NodeProperties.getNodePropertyByURI(myNode, - NodeProperties.GROUP_WRITE_URI); + List<String> groupAccessPropValues + = NodeProperties.getNodePropertiesListByURI(myNode, + accessPropertyName); // If groupwrite property is absent in Parent Node throw exception - if (groupWritePropValues == null - || groupWritePropValues.isEmpty()) { + if (groupAccessPropValues == null + || groupAccessPropValues.isEmpty()) { return false; } List<String> nodeGroups - = NodeProperties.parsePropertyStringToList(groupWritePropValues.get(0)); + = NodeProperties.parsePropertyStringToList(groupAccessPropValues.get(0)); if (nodeGroups.isEmpty() || !nodeGroups.stream() @@ -125,6 +144,60 @@ public class NodeUtils { } return true; + } + + + public static String getDbNodeType(Node node) { + if (node instanceof ContainerNode) { + return "container"; + } else if (node instanceof DataNode) { + return "data"; + } + throw new UnsupportedOperationException("Unable to retrieve database node type for class " + node.getClass().getCanonicalName()); + } + + + public static String getNodeName(String path) { + String[] parsedPath = path.split("/"); + + return parsedPath[parsedPath.length - 1]; + } + + + public static String getNodeName(Node myNode) { + String uri = myNode.getUri(); + return getNodeName(uri); + } + + + public static boolean getIsBusy(Node myNode) { + + if (myNode instanceof DataNode) { + + DataNode dataNode = (DataNode) myNode; + return dataNode.isBusy(); + } + + return false; + } + + + public static Node getTypedNode(String type) { + Node node; + switch (type) { + case "container": + node = new ContainerNode(); + break; + case "data": + node = new DataNode(); + break; + case "structured": + node = new StructuredDataNode(); + break; + default: + throw new UnsupportedOperationException("Node type " + type + " not supported yet"); + } + return node; } } diff --git a/src/test/java/it/inaf/oats/vospace/datamodel/NodeUtilsTest.java b/src/test/java/it/inaf/oats/vospace/datamodel/NodeUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..54c78d3e8c2bf4a238d7635f485581729f4225c7 --- /dev/null +++ b/src/test/java/it/inaf/oats/vospace/datamodel/NodeUtilsTest.java @@ -0,0 +1,93 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package it.inaf.oats.vospace.datamodel; + + +import java.util.ArrayList; +import java.util.List; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + + +public class NodeUtilsTest { + + + //@Test + public void getPathFromRequestURLStringTest() { + + + String urlForTest = "http://server.example.com/vospace/"; + String result = NodeUtils.getPathFromRequestURLString(urlForTest); + assertEquals("/", result); + + urlForTest = "http://server.example.com/vospace/nodes"; + result = NodeUtils.getPathFromRequestURLString(urlForTest); + assertEquals("/", result); + + urlForTest = "http://server.example.com/vospace/nodes/mydata"; + result = NodeUtils.getPathFromRequestURLString(urlForTest); + assertEquals("/mydata", result); + + urlForTest = "http://server.example.com/vospace/nodes/mydata1/uffi/nonso/pappa.txt"; + result = NodeUtils.getPathFromRequestURLString(urlForTest); + assertEquals("/mydata1/uffi/nonso/pappa.txt", result); + } + + /* Is it a possible case? + @Test + public void subPathComponentsTest1() { + + //assertArrayEquals(expected, actual) + String pathForTest = ""; + List result = NodeUtils.subPathComponents(pathForTest); + List expected = new ArrayList(); // expected empty + assertArrayEquals(expected.toArray(), result.toArray()); + + } + */ + + @Test + public void subPathComponentsTest2() { + + String pathForTest = "/"; + List result = NodeUtils.subPathComponents(pathForTest); + List expected = new ArrayList(); + expected.add("/"); + assertArrayEquals(expected.toArray(), result.toArray()); + + + } + + + @Test + public void subPathComponentsTest3() { + + String pathForTest = "/mynode1"; + List result = NodeUtils.subPathComponents(pathForTest); + List expected = new ArrayList(); + expected.add("/mynode1"); + assertArrayEquals(expected.toArray(), result.toArray()); + + } + + + @Test + public void subPathComponentsTest4() { + + //assertArrayEquals(expected, actual) + String pathForTest = "/mydata1/uffi/nonso/pappa.txt"; + List result = NodeUtils.subPathComponents(pathForTest); + List expected = new ArrayList(); + expected.add("/mydata1"); + expected.add("/mydata1/uffi"); + expected.add("/mydata1/uffi/nonso"); + expected.add("/mydata1/uffi/nonso/pappa.txt"); + assertArrayEquals(expected.toArray(), result.toArray()); + + } + +}