10
10
11
11
import static biz .netcentric .cq .tools .actool .history .PersistableInstallationLogger .msHumanReadable ;
12
12
13
+ import java .util .Collection ;
13
14
import java .util .HashSet ;
14
15
import java .util .Iterator ;
15
- import java .util .LinkedHashSet ;
16
+ import java .util .LinkedList ;
16
17
import java .util .List ;
17
18
import java .util .Set ;
18
19
import java .util .TreeSet ;
34
35
import org .apache .commons .lang3 .StringUtils ;
35
36
import org .apache .commons .lang3 .time .StopWatch ;
36
37
import org .apache .jackrabbit .api .security .JackrabbitAccessControlList ;
38
+ import org .apache .jackrabbit .util .Text ;
37
39
import org .slf4j .Logger ;
38
40
import org .slf4j .LoggerFactory ;
39
41
@@ -51,7 +53,7 @@ public class QueryHelper {
51
53
* @param session the JCR session
52
54
* @param excludePaths paths which are excluded from search
53
55
* @return all rep:policy nodes delivered by query */
54
- public static Set <Node > getRepPolicyNodes (final Session session ,
56
+ public static Set <String > getRepPolicyNodePaths (final Session session ,
55
57
final List <String > excludePaths ) {
56
58
NodeIterator nodeIt = null ;
57
59
try {
@@ -60,7 +62,7 @@ public static Set<Node> getRepPolicyNodes(final Session session,
60
62
LOG .error ("Exception: {}" , e );
61
63
}
62
64
63
- Set <String > paths = new TreeSet <String >();
65
+ Set <String > rootChildrenPaths = new TreeSet <String >();
64
66
while (nodeIt .hasNext ()) {
65
67
String currentPath = null ;
66
68
Node currentNode = nodeIt .nextNode ();
@@ -73,26 +75,26 @@ public static Set<Node> getRepPolicyNodes(final Session session,
73
75
try {
74
76
if (!currentNode .hasProperty ("rep:AuthorizableFolder" )) {
75
77
if (!excludePaths .contains (currentPath )) {
76
- paths .add (currentPath );
78
+ rootChildrenPaths .add (currentPath );
77
79
}
78
80
}
79
81
} catch (RepositoryException e ) {
80
82
LOG .error ("Exception: {}" , e );
81
83
}
82
84
}
83
- Set <Node > nodes = new LinkedHashSet < Node >();
85
+ Set <String > paths = new HashSet < >();
84
86
try {
85
87
// get the rep:policy node of "/", if existing
86
88
if (session .nodeExists (ROOT_REP_POLICY_NODE )) {
87
- nodes .add (session . getNode ( ROOT_REP_POLICY_NODE ) );
89
+ paths .add (ROOT_REP_POLICY_NODE );
88
90
}
89
91
if (session .nodeExists (ROOT_REPO_POLICY_NODE )) {
90
- nodes .add (session . getNode ( ROOT_REPO_POLICY_NODE ) );
92
+ paths .add (ROOT_REPO_POLICY_NODE );
91
93
}
92
94
93
95
// get the rep:policy node of "/home", if existing
94
96
if (session .nodeExists (HOME_REP_POLICY )) {
95
- nodes .add (session . getNode ( HOME_REP_POLICY ) );
97
+ paths .add (HOME_REP_POLICY );
96
98
}
97
99
98
100
boolean indexForRepACLExists = session .nodeExists (OAK_INDEX_PATH_REP_ACL );
@@ -102,65 +104,69 @@ public static Set<Node> getRepPolicyNodes(final Session session,
102
104
"SELECT ace.* FROM [rep:ACE] AS ace WHERE ace.[rep:principalName] IS NOT NULL AND ISDESCENDANTNODE(ace, [%s])" ;
103
105
LOG .debug ("Query to obtain all ACLs: {}" , queryForAClNodes );
104
106
105
- for (String path : paths ) {
107
+ for (String path : rootChildrenPaths ) {
106
108
if (StringUtils .equals (path , ROOT_REP_POLICY_NODE ) || StringUtils .equals (path , ROOT_REPO_POLICY_NODE )) {
107
109
continue ;
108
110
}
109
111
110
112
String query = String .format (queryForAClNodes , path );
111
113
112
114
long startTime1 = System .currentTimeMillis ();
113
- Set <Node > nodesResult = indexForRepACLExists ?
114
- getNodes (session , query , Query .JCR_SQL2 ):
115
- getDistinctParentNodes (session , query , Query .JCR_SQL2 );
115
+ Set <String > nodesResult = indexForRepACLExists ?
116
+ getNodePathsFromQuery (session , query , Query .JCR_SQL2 ):
117
+ getDistinctParentNodePathsFromQuery (session , query , Query .JCR_SQL2 );
116
118
LOG .debug ("Query to find ACLs under {} ran in {}ms (count ACLs: {})" , path , System .currentTimeMillis ()-startTime1 , nodesResult .size ());
117
- nodes .addAll (nodesResult );
119
+ paths .addAll (nodesResult );
118
120
}
119
121
} catch (Exception e ) {
120
122
throw new IllegalStateException ("Could not query repository for existing ACLs: " +e , e );
121
123
}
122
- return nodes ;
124
+ return paths ;
123
125
}
124
126
125
127
/** Get Nodes with XPATH Query. */
126
- public static Set <Node > getNodes (final Session session ,
128
+ public static Set <String > getNodePathsFromQuery (final Session session ,
127
129
final String xpathQuery ) throws InvalidQueryException ,
128
130
RepositoryException {
129
- Set <Node > nodes = getNodes (session , xpathQuery , Query .XPATH );
130
- return nodes ;
131
+ return getNodePathsFromQuery (session , xpathQuery , Query .XPATH );
131
132
}
132
133
133
134
134
- public static Set <Node > getDistinctParentNodes (final Session session ,final String queryStatement , String queryLanguageType ) throws InvalidQueryException , RepositoryException {
135
- Set <Node > nodes = getNodes (session , queryStatement , queryLanguageType );
136
- Set <Node > parentNodes = new HashSet <>();
135
+ public static Set <String > getDistinctParentNodePathsFromQuery (final Session session ,final String queryStatement , String queryLanguageType ) throws InvalidQueryException , RepositoryException {
136
+ Set <String > paths = getNodePathsFromQuery (session , queryStatement , queryLanguageType );
137
+ Set <String > parentPaths = new HashSet <>();
137
138
138
- for (Node node : nodes ) {
139
- parentNodes .add (node . getParent ( ));
139
+ for (String path : paths ) {
140
+ parentPaths .add (Text . getRelativeParent ( path , 1 ));
140
141
}
141
142
142
- return parentNodes ;
143
+ return parentPaths ;
143
144
}
144
145
145
146
146
147
/** @param session the jcr session
147
148
* @param queryStatement - ex. "SELECT * FROM [rep:ACL]"
148
149
* @param queryLanguageType - ex. Query.JCR_SQL2 */
149
- public static Set <Node > getNodes (final Session session ,
150
- final String queryStatement , String queryLanguageType ) throws InvalidQueryException ,
151
- RepositoryException {
152
- Set <Node > nodes = new HashSet <Node >();
153
-
150
+ public static NodeIterator getNodesFromQuery (final Session session ,
151
+ final String queryStatement , String queryLanguageType ) throws RepositoryException {
154
152
Query query = session .getWorkspace ().getQueryManager ().createQuery (queryStatement , queryLanguageType );
155
153
QueryResult queryResult = query .execute ();
156
- NodeIterator nit = queryResult .getNodes ();
154
+ return queryResult .getNodes ();
155
+ }
157
156
157
+ /** @param session the jcr session
158
+ * @param queryStatement - ex. "SELECT * FROM [rep:ACL]"
159
+ * @param queryLanguageType - ex. Query.JCR_SQL2 */
160
+ public static Set <String > getNodePathsFromQuery (final Session session ,
161
+ final String queryStatement , String queryLanguageType ) throws RepositoryException {
162
+ Set <String > paths = new HashSet <>();
163
+ NodeIterator nit = getNodesFromQuery (session , queryStatement , queryLanguageType );
158
164
while (nit .hasNext ()) {
159
165
// get the next rep:policy node
160
166
Node node = nit .nextNode ();
161
- nodes .add (node );
167
+ paths .add (node . getPath () );
162
168
}
163
- return nodes ;
169
+ return paths ;
164
170
}
165
171
166
172
public static Set <AclBean > getAuthorizablesAcls (final Session session ,
@@ -172,7 +178,7 @@ public static Set<AclBean> getAuthorizablesAcls(final Session session,
172
178
StopWatch sw = new StopWatch ();
173
179
sw .start ();
174
180
175
- Set <Node > nodeSet = new LinkedHashSet < Node >();
181
+ Collection <Node > nodes = new LinkedList < >();
176
182
177
183
Iterator <String > authorizablesIdIterator = authorizableIds .iterator ();
178
184
@@ -186,26 +192,30 @@ public static Set<AclBean> getAuthorizablesAcls(final Session session,
186
192
187
193
String query = queryStringBuilder .toString ();
188
194
189
- Set <Node > resultNodes = getNodes (session , query , Query .JCR_SQL2 );
195
+ NodeIterator nit = getNodesFromQuery (session , query , Query .JCR_SQL2 );
196
+ Collection <Node > resultNodes = new LinkedList <>();
197
+ while (nit .hasNext ()) {
198
+ resultNodes .add (nit .nextNode ());
199
+ }
190
200
LOG .trace ("Querying AclBeans with {} returned {} results" , query , resultNodes .size ());
191
- nodeSet .addAll (resultNodes );
201
+ nodes .addAll (resultNodes );
192
202
}
193
- Set <AclBean > resultBeans = buildAclBeansFromNodeSet (session , nodeSet , principalIdsToBeFilled );
203
+ Set <AclBean > resultBeans = buildAclBeansFromNodes (session , nodes , principalIdsToBeFilled );
194
204
195
205
sw .stop ();
196
206
LOG .debug ("Found {} AclBeans in {}" , resultBeans .size (), msHumanReadable (sw .getTime ()));
197
207
198
208
return resultBeans ;
199
209
}
200
210
201
- private static Set <AclBean > buildAclBeansFromNodeSet (final Session session ,
202
- Set <Node > nodeSet , Set <String > principalIdsToBeFilled ) throws UnsupportedRepositoryOperationException ,
211
+ private static Set <AclBean > buildAclBeansFromNodes (final Session session ,
212
+ Collection <Node > nodes , Set <String > principalIdsToBeFilled ) throws UnsupportedRepositoryOperationException ,
203
213
RepositoryException , PathNotFoundException , AccessDeniedException ,
204
214
ItemNotFoundException {
205
215
AccessControlManager aMgr = session .getAccessControlManager ();
206
216
AccessControlList acl ;
207
217
Set <AclBean > aclSet = new TreeSet <AclBean >(); // use natural ordering
208
- for (Node allowOrDenyNode : nodeSet ) {
218
+ for (Node allowOrDenyNode : nodes ) {
209
219
String principalId = allowOrDenyNode .getProperty ("rep:principalName" ).getValue ().getString ();
210
220
principalIdsToBeFilled .add (principalId );
211
221
Node aclNode = allowOrDenyNode .getParent ();
0 commit comments