JcrSocialGraph.java
14.2 KB
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
90
91
92
93
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.security.user.UserProperties
* com.adobe.granite.security.user.UserPropertiesManager
* com.adobe.granite.security.user.UserPropertiesService
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.ArrayUtils
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.Group
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.socialgraph.impl;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.security.user.UserPropertiesService;
import com.adobe.granite.socialgraph.GraphNode;
import com.adobe.granite.socialgraph.Relationship;
import com.adobe.granite.socialgraph.SocialGraph;
import com.adobe.granite.socialgraph.SocialGraphException;
import com.adobe.granite.socialgraph.impl.JcrGraphNode;
import com.adobe.granite.socialgraph.impl.SocialGraphConfig;
import com.adobe.granite.socialgraph.impl.VirtualRelationship;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.ArrayUtils;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class JcrSocialGraph
implements SocialGraph {
private static final Logger log = LoggerFactory.getLogger(JcrSocialGraph.class);
private static final String USER_PROPERTIES_NODE = "profile";
private final ResourceResolver resolver;
private final Session session;
private final UserPropertiesManager upMgr;
private final UserPropertiesService upSvc;
private final UserManager uMgr;
private final SocialGraphConfig config;
public JcrSocialGraph(UserPropertiesService upSvc, ResourceResolver resolver, SocialGraphConfig config) throws RepositoryException {
this.upSvc = upSvc;
this.upMgr = upSvc.createUserPropertiesManager(resolver);
this.resolver = resolver;
this.config = config;
this.session = (Session)resolver.adaptTo(Session.class);
this.uMgr = ((JackrabbitSession)this.session).getUserManager();
}
@Override
public GraphNode getNode(String id) {
if (id.startsWith("/")) {
return new JcrGraphNode(this, id, null, false);
}
try {
UserProperties up = this.getUserProperties(id);
if (up != null) {
return new JcrGraphNode(this, up.getAuthorizablePath(), id, up.isGroupProperties());
}
log.debug("Unable to get graph node. ID '{}' refers neither to a user nor a group.", (Object)id);
return null;
}
catch (RepositoryException e) {
log.error("Error while reading authorizable " + id, (Throwable)e);
throw new SocialGraphException("Error while reading authorizable", (Throwable)e);
}
}
protected UserProperties getUserProperties(String id) throws RepositoryException {
UserProperties up = this.upMgr.getUserProperties(id, "profile");
if (up == null) {
up = this.upMgr.getUserProperties(id, null);
}
return up;
}
protected UserProperties getUserPropertiesByPath(String path) throws RepositoryException {
String id = this.upSvc.getAuthorizableId(path);
return id == null ? null : this.getUserProperties(id);
}
public Session getSession() {
return this.session;
}
@Override
public void save() {
try {
this.session.save();
}
catch (RepositoryException e) {
log.error("Error while saving changes", (Throwable)e);
throw new SocialGraphException("Error while saving changes", (Throwable)e);
}
}
@Override
public void refresh(boolean keepChanges) {
try {
this.session.refresh(keepChanges);
}
catch (RepositoryException e) {
log.error("Error while refresh changes", (Throwable)e);
throw new SocialGraphException("Error while refresh changes", (Throwable)e);
}
}
protected Resource getResource(String path) {
return this.resolver.getResource(path);
}
public /* varargs */ void fillOutgoingVirtualRelationships(String id, Map<String, Relationship> relations, String ... types) throws RepositoryException {
try {
Authorizable auth = this.uMgr.getAuthorizable(id);
if (auth instanceof Group) {
this.fillOutgoingGroupRelationships((Group)auth, relations, types);
} else {
UserProperties up = this.getUserProperties(id);
if (up != null && !up.isGroupProperties()) {
this.fillOutgoingRelationships(id, relations, types);
}
}
}
catch (RepositoryException e) {
log.error("Error while creating virtual relationships", (Throwable)e);
}
}
private void fillOutgoingGroupRelationships(Group grp, Map<String, Relationship> relations, String[] types) throws RepositoryException {
UserProperties a;
VirtualRelationship rel;
if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupOutgoingRelationship()) < 0) {
return;
}
if (this.config.getOutgoingExcludedGroups().contains(grp.getID())) {
return;
}
Iterator iter = this.upMgr.getMemberUserProperties(grp, null, false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (a.isGroupProperties()) continue;
rel = new VirtualRelationship(this, grp.getID(), a.getAuthorizableID(), this.config.getGroupOutgoingRelationship());
relations.put(rel.signature(), rel);
}
iter = this.upMgr.getMemberUserProperties(grp, "profile", false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (a.isGroupProperties()) continue;
rel = new VirtualRelationship(this, grp.getID(), a.getAuthorizableID(), this.config.getGroupOutgoingRelationship());
relations.put(rel.signature(), rel);
}
}
private void fillOutgoingRelationships(String userId, Map<String, Relationship> relations, String[] types) throws RepositoryException {
UserProperties a;
VirtualRelationship rel;
String groupId;
if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupIncomingRelationship()) < 0) {
return;
}
Iterator iter = this.upMgr.getMemberOfUserProperties(userId, null, false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
groupId = a.getAuthorizableID();
if (this.config.getIncomingExcludedGroups().contains(groupId)) continue;
rel = new VirtualRelationship(this, userId, groupId, this.config.getGroupIncomingRelationship());
relations.put(rel.signature(), rel);
}
iter = this.upMgr.getMemberOfUserProperties(userId, "profile", false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
groupId = a.getAuthorizableID();
if (this.config.getIncomingExcludedGroups().contains(groupId)) continue;
rel = new VirtualRelationship(this, userId, groupId, this.config.getGroupIncomingRelationship());
relations.put(rel.signature(), rel);
}
}
public /* varargs */ void fillIncomingVirtualRelationships(String id, Map<String, Relationship> relations, String ... types) throws RepositoryException {
try {
Authorizable auth = this.uMgr.getAuthorizable(id);
if (auth instanceof Group) {
this.fillIncomingGroupRelationships((Group)auth, relations, types);
} else {
UserProperties up = this.getUserProperties(id);
if (up != null && !up.isGroupProperties()) {
this.fillIncomingRelationships(id, relations, types);
}
}
}
catch (RepositoryException e) {
log.error("Error while creating virtual relationships", (Throwable)e);
}
}
public Relationship getVirtualRelationship(String fromId, String toId, String type) {
try {
Authorizable from = this.uMgr.getAuthorizable(fromId);
Authorizable to = this.uMgr.getAuthorizable(toId);
if (from != null && to != null) {
if (from instanceof Group && !(to instanceof Group)) {
return this.getOutgoingRelationship((Group)from, toId, type);
}
if (!(from instanceof Group) && to instanceof Group) {
return this.getIncomingRelationship(fromId, (Group)to, type);
}
}
}
catch (RepositoryException e) {
log.error("Error while creating virtual relationships", (Throwable)e);
}
return null;
}
private void fillIncomingGroupRelationships(Group grp, Map<String, Relationship> relations, String[] types) throws RepositoryException {
UserProperties a;
VirtualRelationship rel;
if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupIncomingRelationship()) < 0) {
return;
}
if (this.config.getIncomingExcludedGroups().contains(grp.getID())) {
return;
}
Iterator iter = this.upMgr.getMemberUserProperties(grp, null, false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (a.isGroupProperties()) continue;
rel = new VirtualRelationship(this, a.getAuthorizableID(), grp.getID(), this.config.getGroupIncomingRelationship());
relations.put(rel.signature(), rel);
}
iter = this.upMgr.getMemberUserProperties(grp, "profile", false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (a.isGroupProperties()) continue;
rel = new VirtualRelationship(this, a.getAuthorizableID(), grp.getID(), this.config.getGroupIncomingRelationship());
relations.put(rel.signature(), rel);
}
}
private void fillIncomingRelationships(String userId, Map<String, Relationship> relations, String[] types) throws RepositoryException {
UserProperties a;
VirtualRelationship rel;
String groupId;
if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupOutgoingRelationship()) < 0) {
return;
}
Iterator iter = this.upMgr.getMemberOfUserProperties(userId, null, false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
groupId = a.getAuthorizableID();
if (this.config.getOutgoingExcludedGroups().contains(groupId)) continue;
rel = new VirtualRelationship(this, groupId, userId, this.config.getGroupOutgoingRelationship());
relations.put(rel.signature(), rel);
}
iter = this.upMgr.getMemberOfUserProperties(userId, "profile", false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
groupId = a.getAuthorizableID();
if (this.config.getOutgoingExcludedGroups().contains(groupId)) continue;
rel = new VirtualRelationship(this, groupId, userId, this.config.getGroupOutgoingRelationship());
relations.put(rel.signature(), rel);
}
}
private Relationship getIncomingRelationship(String fromUserId, Group to, String type) throws RepositoryException {
UserProperties a;
if (type == null || !type.equals(this.config.getGroupIncomingRelationship())) {
return null;
}
if (this.config.getIncomingExcludedGroups().contains(to.getID())) {
return null;
}
Iterator iter = this.upMgr.getMemberUserProperties(to, null, false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (!a.getAuthorizableID().equals(fromUserId)) continue;
return new VirtualRelationship(this, fromUserId, to.getID(), type);
}
iter = this.upMgr.getMemberUserProperties(to, "profile", false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (!a.getAuthorizableID().equals(fromUserId)) continue;
return new VirtualRelationship(this, fromUserId, to.getID(), type);
}
return null;
}
private Relationship getOutgoingRelationship(Group from, String toUserId, String type) throws RepositoryException {
UserProperties a;
if (type == null || !type.equals(this.config.getGroupOutgoingRelationship())) {
return null;
}
if (this.config.getOutgoingExcludedGroups().contains(from.getID())) {
return null;
}
Iterator iter = this.upMgr.getMemberUserProperties(from, null, false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (!a.getAuthorizableID().equals(toUserId)) continue;
return new VirtualRelationship(this, from.getID(), toUserId, type);
}
iter = this.upMgr.getMemberUserProperties(from, "profile", false);
while (iter.hasNext()) {
a = (UserProperties)iter.next();
if (!a.getAuthorizableID().equals(toUserId)) continue;
return new VirtualRelationship(this, from.getID(), toUserId, type);
}
return null;
}
}