Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* 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 ) {
String[] split = requestUrlString.split("/nodes/");
String path = "/";
if (split.length == 2) {
path += split[1];
}
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
public static String getParentPath(String path) {
String[] parsedPath = path.split("[/]+");
if (parsedPath.length < 2 || !parsedPath[0].isEmpty()) {
throw new IllegalArgumentException();
}
StringBuilder sb = new StringBuilder();
sb.append("/");
for (int i = 1; i < parsedPath.length - 1; i++) {
sb.append(parsedPath[i]);
if (i < parsedPath.length - 2) {
sb.append("/");
}
}
return sb.toString();
}
public static List<String> subPathComponents(String path) {
List resultList = new ArrayList<String>();
String[] pathComponents = path.split("[/]+");
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++) {
parentPath = parentPath + pathComponents[i] + "/";
// "I'm managing path = " + parentPath.substring(0, parentPath.length()-1));
resultList.add(parentPath.substring(0, parentPath.length()-1));
}
}
return resultList;
}
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);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
if (nodeOwner == null
|| nodeOwner.isEmpty()
|| !nodeOwner.get(0).equals(userName)) {
// Node owner check has failed: let's check if user can write
// due to group privileges
// If the user doesn't belong to any groups throw exception
if (userGroups == null || userGroups.isEmpty()) {
return false;
}
List<String> groupWritePropValues
= NodeProperties.getNodePropertyByURI(myNode,
NodeProperties.GROUP_WRITE_URI);
// If groupwrite property is absent in Parent Node throw exception
if (groupWritePropValues == null
|| groupWritePropValues.isEmpty()) {
return false;
}
List<String> nodeGroups
= NodeProperties.parsePropertyStringToList(groupWritePropValues.get(0));
if (nodeGroups.isEmpty()
|| !nodeGroups.stream()
.anyMatch((i) -> userGroups.contains(i))) {
return false;
}
}
return true;
}
}