From 35e4aa7f51b9cb1eb727f53f6e2be8626a7e8083 Mon Sep 17 00:00:00 2001
From: Sonia Zorba <sonia.zorba@inaf.it>
Date: Mon, 8 Feb 2021 17:51:30 +0100
Subject: [PATCH] Split getNodePropertyByURI in 2 methods (String and List
 results); Added checkIfReadable util method

---
 .../vospace/datamodel/NodeProperties.java     | 83 ++++++++---------
 .../oats/vospace/datamodel/NodeUtils.java     | 90 +++++++++++--------
 2 files changed, 91 insertions(+), 82 deletions(-)

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 e2f21d8..66744d2 100644
--- a/src/main/java/it/inaf/oats/vospace/datamodel/NodeProperties.java
+++ b/src/main/java/it/inaf/oats/vospace/datamodel/NodeProperties.java
@@ -1,8 +1,3 @@
-/*
- * 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.List;
@@ -10,56 +5,56 @@ import java.util.stream.Collectors;
 import net.ivoa.xml.vospace.v2.Node;
 import net.ivoa.xml.vospace.v2.Property;
 
-/**
- *
- * @author bertocco
- */
-public class NodeProperties {
-    
-    
-    private NodeProperties() { }
-    
-    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
-    public static final String COVERAGE_URI  = "ivo://ivoa.net/vospace/core#coverage";    // a spatial or temporal topic of the resource, the spatial applicability of the resource, or the jurisdiction under which the resource is relevant      
-    public static final String CREATOR_URI  = "ivo://ivoa.net/vospace/core#creator";     // an entity primarily responsible for making the resource
-    public static final String STATUS_CHANGE_TIME_URI  = "ivo://ivoa.net/vospace/core#ctime";       // the status change (aka metadata modification) time    
-    public static final String DATE_URI  = "ivo://ivoa.net/vospace/core#date";        // a point or period of time associated with an event in the lifecycle of the resource
-    public static final String DESCRIPTION_URI  = "ivo://ivoa.net/vospace/core#description"; // an account of the resource    
-    public static final String FORMAT_URI  = "ivo://ivoa.net/vospace/core#format";      // the file format, physical medium or dimensions of the resource
-    public static final String GROUP_READ_URI  = "ivo://ivoa.net/vospace/core#groupread";   // the list of groups which can only read this resource  delimiter-separated 
-    public static final String GROUP_WRITE_URI  = "ivo://ivoa.net/vospace/core#groupwrite";  // the list of groups which can read and write to this resource  delimiter-separated 
-    public static final String IDENTIFIER_URI  = "ivo://ivoa.net/vospace/core#identifier";  // an unambiguous reference to the resource within a given context
-    public static final String LANGUAGE_URI  = "ivo://ivoa.net/vospace/core#language";    // a language of the resource    
-    public static final String CORE_URI  = "ivo://ivoa.net/vospace/core#length";      // the length or size of a resource      
-    public static final String MODIFICATION_TIME_URI  = "ivo://ivoa.net/vospace/core#mtime";       // the data modification time    
-    public static final String PUBLIC_READ_URI  = "ivo://ivoa.net/vospace/core#publicread";  // whether this resource is world readable
-    public static final String PUBLISHER_URI  = "ivo://ivoa.net/vospace/core#publisher";   // an entity responsible for making the resource available
-    public static final String QUOTA_URI  = "ivo://ivoa.net/vospace/core#quota";       // the value of a system quota on the resource   
-    public static final String RELATED_RESOURCE_URI  = "ivo://ivoa.net/vospace/core#relation";    // a related resource    
-    public static final String RIGHTS_ON_URI  = "ivo://ivoa.net/vospace/core#rights";      // information about rights held in and over the resource
-    public static final String RESOURCE_RELATED_URI  = "ivo://ivoa.net/vospace/core#source";      // a related resource from which the described resource is derived
-    public static final String SUBJECT_URI  = "ivo://ivoa.net/vospace/core#subject";     // the topic of the resource     
-    public static final String TITLE_URI  = "ivo://ivoa.net/vospace/core#title";       // a name given to the resource  
-    public static final String TYPE_URI  = "ivo://ivoa.net/vospace/core#type";        // the nature or genre of the resource
+public abstract class NodeProperties {
 
+    private NodeProperties() {
+    }
 
-    
+    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
+    public static final String COVERAGE_URI = "ivo://ivoa.net/vospace/core#coverage";    // a spatial or temporal topic of the resource, the spatial applicability of the resource, or the jurisdiction under which the resource is relevant      
+    public static final String CREATOR_URI = "ivo://ivoa.net/vospace/core#creator";     // an entity primarily responsible for making the resource
+    public static final String STATUS_CHANGE_TIME_URI = "ivo://ivoa.net/vospace/core#ctime";       // the status change (aka metadata modification) time    
+    public static final String DATE_URI = "ivo://ivoa.net/vospace/core#date";        // a point or period of time associated with an event in the lifecycle of the resource
+    public static final String DESCRIPTION_URI = "ivo://ivoa.net/vospace/core#description"; // an account of the resource    
+    public static final String FORMAT_URI = "ivo://ivoa.net/vospace/core#format";      // the file format, physical medium or dimensions of the resource
+    public static final String GROUP_READ_URI = "ivo://ivoa.net/vospace/core#groupread";   // the list of groups which can only read this resource  delimiter-separated 
+    public static final String GROUP_WRITE_URI = "ivo://ivoa.net/vospace/core#groupwrite";  // the list of groups which can read and write to this resource  delimiter-separated 
+    public static final String IDENTIFIER_URI = "ivo://ivoa.net/vospace/core#identifier";  // an unambiguous reference to the resource within a given context
+    public static final String LANGUAGE_URI = "ivo://ivoa.net/vospace/core#language";    // a language of the resource    
+    public static final String CORE_URI = "ivo://ivoa.net/vospace/core#length";      // the length or size of a resource      
+    public static final String MODIFICATION_TIME_URI = "ivo://ivoa.net/vospace/core#mtime";       // the data modification time    
+    public static final String PUBLIC_READ_URI = "ivo://ivoa.net/vospace/core#publicread";  // whether this resource is world readable
+    public static final String PUBLISHER_URI = "ivo://ivoa.net/vospace/core#publisher";   // an entity responsible for making the resource available
+    public static final String QUOTA_URI = "ivo://ivoa.net/vospace/core#quota";       // the value of a system quota on the resource   
+    public static final String RELATED_RESOURCE_URI = "ivo://ivoa.net/vospace/core#relation";    // a related resource    
+    public static final String RIGHTS_ON_URI = "ivo://ivoa.net/vospace/core#rights";      // information about rights held in and over the resource
+    public static final String RESOURCE_RELATED_URI = "ivo://ivoa.net/vospace/core#source";      // a related resource from which the described resource is derived
+    public static final String SUBJECT_URI = "ivo://ivoa.net/vospace/core#subject";     // the topic of the resource     
+    public static final String TITLE_URI = "ivo://ivoa.net/vospace/core#title";       // a name given to the resource  
+    public static final String TYPE_URI = "ivo://ivoa.net/vospace/core#type";        // the nature or genre of the resource
+    //
+    // Non-standard properties
+    public static final String ASYNC_TRANS_URN = "urn:async_trans";
+    public static final String STICKY_URN = "urn:sticky";
 
-    public static String getProperty(Node node, String propertyName) {
+    public static String getStandardNodePropertyByName(Node node, String propertyName) {
+        return getNodePropertyByURI(node, "ivo://ivoa.net/vospace/core#".concat(propertyName));
+    }
 
+    public static String getNodePropertyByURI(Node node, String propertyURI) {
         for (Property property : node.getProperties()) {
-            if (property.getUri().equals("ivo://ivoa.net/vospace/core#".concat(propertyName))) {
+            if (property.getUri().equals(propertyURI)) {
                 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> getNodePropertyAsListByURI(Node node, String propertyURI) {
 
         List<String> propertyList = node.getProperties().stream()
                 .filter((i) -> i.getUri()
@@ -81,7 +76,5 @@ public class NodeProperties {
         }
 
         return List.of(trimmedProperty.split(separator));
-
     }
-    
 }
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 543b974..fe8e7d5 100644
--- a/src/main/java/it/inaf/oats/vospace/datamodel/NodeUtils.java
+++ b/src/main/java/it/inaf/oats/vospace/datamodel/NodeUtils.java
@@ -1,28 +1,19 @@
-/*
- * 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.security.Principal;
 import java.util.ArrayList;
 import java.util.List;
 import net.ivoa.xml.vospace.v2.Node;
 
-
 public class NodeUtils {
- 
-    
-    
+
     /**
      * Slash is a special character in defining REST endpoints and trying to
      * define a PathVariable containing slashes doesn't work, so the endpoint
      * has been defined using "/nodes/**" instead of "/nodes/{path}" and the
      * path is extracted manually parsing the request URL.
      */
-    public static String getPathFromRequestURLString(String requestUrlString ) {
-        
+    public static String getPathFromRequestURLString(String requestUrlString) {
+
         String[] split = requestUrlString.split("/nodes/");
 
         String path = "/";
@@ -31,7 +22,7 @@ public class NodeUtils {
         }
         return path;
     }
-    
+
     // This method assumes that URL is in the format /node1/node2/...
     // multiple slashes as a single separator are allowed
     // But the output has only single slash separators
@@ -55,43 +46,69 @@ public class NodeUtils {
 
         return sb.toString();
     }
-    
-    
+
     public static List<String> subPathComponents(String path) {
-        
+
         List resultList = new ArrayList<String>();
-        
+
         String[] pathComponents = path.split("[/]+");
-        
-        if (pathComponents.length == 0) { 
-                       
+
+        if (pathComponents.length == 0) {
+
             // Manage root node
             resultList.add("/");
-            
+
         } else {
-            
+
             // Manage all precursors in full path
-            String parentPath="/";
-            for (int i = 1; i < pathComponents.length; i++) { 
+            String parentPath = "/";
+            for (int i = 1; i < pathComponents.length; i++) {
                 parentPath = parentPath + pathComponents[i] + "/";
                 // "I'm managing path = " + parentPath.substring(0, parentPath.length()-1));
-                resultList.add(parentPath.substring(0, parentPath.length()-1));
-                            
+                resultList.add(parentPath.substring(0, parentPath.length() - 1));
             }
-                    
         }
-        
+
         return resultList;
-        
+
+    }
+
+    public static boolean checkIfReadable(Node myNode, String userName, List<String> userGroups) {
+
+        if ("true".equals(NodeProperties.getNodePropertyByURI(myNode, NodeProperties.PUBLIC_READ_URI))) {
+            return true;
+        }
+
+        String creator = NodeProperties.getNodePropertyByURI(myNode, NodeProperties.CREATOR_URI);
+        if (creator != null && creator.equals(userName)) {
+            return true;
+        }
+
+        if (userGroups == null || userGroups.isEmpty()) {
+            return false;
+        }
+
+        List<String> groupReadValues = NodeProperties.getNodePropertyAsListByURI(myNode, NodeProperties.GROUP_READ_URI);
+
+        if (groupReadValues == null) {
+            return false;
+        }
+
+        for (String group : groupReadValues) {
+            if (userGroups.contains(group)) {
+                return true;
+            }
+        }
+
+        return false;
     }
-    
+
     public static boolean checkIfWritable(Node myNode, String userName, List<String> userGroups) {
-        
+
         // First check if parent node creator is == userid
         List<String> nodeOwner
-                = NodeProperties.getNodePropertyByURI(myNode, NodeProperties.CREATOR_URI);
-                        
-        
+                = NodeProperties.getNodePropertyAsListByURI(myNode, NodeProperties.CREATOR_URI);
+
         if (nodeOwner == null
                 || nodeOwner.isEmpty()
                 || !nodeOwner.get(0).equals(userName)) {
@@ -104,13 +121,13 @@ public class NodeUtils {
             }
 
             List<String> groupWritePropValues
-                    = NodeProperties.getNodePropertyByURI(myNode,
+                    = NodeProperties.getNodePropertyAsListByURI(myNode,
                             NodeProperties.GROUP_WRITE_URI);
 
             // If groupwrite property is absent in Parent Node throw exception
             if (groupWritePropValues == null
                     || groupWritePropValues.isEmpty()) {
-                return false;                
+                return false;
             }
 
             List<String> nodeGroups
@@ -126,5 +143,4 @@ public class NodeUtils {
 
         return true;
     }
-    
 }
-- 
GitLab