/
JcrResourceProvider.java
680 lines (616 loc) · 28.2 KB
/
JcrResourceProvider.java
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.jcr.resource.internal.helper.jcr;
import java.io.Closeable;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicReference;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.adapter.annotations.Adaptable;
import org.apache.sling.adapter.annotations.Adapter;
import org.apache.sling.api.SlingException;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.external.URIProvider;
import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.apache.sling.jcr.resource.internal.JcrListenerBaseConfig;
import org.apache.sling.jcr.resource.internal.JcrModifiableValueMap;
import org.apache.sling.jcr.resource.internal.JcrResourceListener;
import org.apache.sling.spi.resource.provider.ObserverConfiguration;
import org.apache.sling.spi.resource.provider.ProviderContext;
import org.apache.sling.spi.resource.provider.QueryLanguageProvider;
import org.apache.sling.spi.resource.provider.ResolveContext;
import org.apache.sling.spi.resource.provider.ResourceContext;
import org.apache.sling.spi.resource.provider.ResourceProvider;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.sling.jcr.resource.internal.helper.jcr.ContextUtil.getHelperData;
import static org.apache.sling.jcr.resource.internal.helper.jcr.ContextUtil.getResourceFactory;
import static org.apache.sling.jcr.resource.internal.helper.jcr.ContextUtil.getSession;
@Adaptable(adaptableClass = ResourceProvider.class, adapters = { @Adapter(value=Session.class, condition="If the JcrResourceProvider is loaded"), @Adapter(value=Principal.class, condition="If the underlying java.jcr.Session implements JackrabbitSession") })
@Component(name="org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProviderFactory",
service = ResourceProvider.class,
property = {
ResourceProvider.PROPERTY_NAME + "=JCR",
ResourceProvider.PROPERTY_ROOT + "=/",
ResourceProvider.PROPERTY_MODIFIABLE + ":Boolean=true",
ResourceProvider.PROPERTY_ADAPTABLE + ":Boolean=true",
ResourceProvider.PROPERTY_ATTRIBUTABLE + ":Boolean=true",
ResourceProvider.PROPERTY_REFRESHABLE + ":Boolean=true",
ResourceProvider.PROPERTY_AUTHENTICATE + "=" + ResourceProvider.AUTHENTICATE_REQUIRED,
Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
})
public class JcrResourceProvider extends ResourceProvider<JcrProviderState> {
// due to https://issues.apache.org/jira/browse/SLING-11517 a dedicated class is necessary
@Adaptable(adaptableClass = ResourceResolver.class, adapters = { @Adapter(value=Session.class, condition="If the JcrResourceProvider is loaded"), @Adapter(value=Principal.class, condition="If the underlying java.jcr.Session implements JackrabbitSession") })
private static final class EmptyAdaptableAnnotationCarryingClass {
// just to carry the annotation
}
/** Logger */
private final Logger logger = LoggerFactory.getLogger(JcrResourceProvider.class);
private static final String REPOSITORY_REFERENCE_NAME = "repository";
private static final Set<String> IGNORED_PROPERTIES = new HashSet<>();
static {
IGNORED_PROPERTIES.add(JcrConstants.JCR_MIXINTYPES);
IGNORED_PROPERTIES.add(JcrConstants.JCR_PRIMARYTYPE);
IGNORED_PROPERTIES.add(JcrConstants.JCR_CREATED);
IGNORED_PROPERTIES.add("jcr:createdBy");
}
@Reference(name = REPOSITORY_REFERENCE_NAME, service = SlingRepository.class)
private ServiceReference<SlingRepository> repositoryReference;
/** The JCR listener base configuration. */
private volatile JcrListenerBaseConfig listenerConfig;
/** The JCR observation listeners. */
private final Map<ObserverConfiguration, Closeable> listeners = new HashMap<>();
/**
* Map of bound URIProviders sorted by service ranking in descending order (highest ranking first).
* Key = service reference, value = service implementation
*/
private final SortedMap<ServiceReference<URIProvider>, URIProvider> providers = Collections.synchronizedSortedMap(new TreeMap<>(Collections.reverseOrder()));
private volatile SlingRepository repository;
private volatile JcrProviderStateFactory stateFactory;
private final AtomicReference<DynamicClassLoaderManager> classLoaderManagerReference = new AtomicReference<>();
private final AtomicReference<URIProvider[]> uriProviderReference = new AtomicReference<>();
@Activate
protected void activate(final ComponentContext context) {
SlingRepository slingRepository = context.locateService(REPOSITORY_REFERENCE_NAME,
this.repositoryReference);
if (slingRepository == null) {
// concurrent unregistration of SlingRepository service
// don't care, this component is going to be deactivated
// so we just stop working
logger.warn("activate: Activation failed because SlingRepository may have been unregistered concurrently");
return;
}
this.repository = slingRepository;
this.stateFactory = new JcrProviderStateFactory(repositoryReference, slingRepository,
classLoaderManagerReference, uriProviderReference);
}
@Deactivate
protected void deactivate() {
this.stateFactory = null;
}
@Reference(name = "dynamicClassLoaderManager",
service = DynamicClassLoaderManager.class,
cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
@SuppressWarnings("unused")
protected void bindDynamicClassLoaderManager(final DynamicClassLoaderManager dynamicClassLoaderManager) {
this.classLoaderManagerReference.set(dynamicClassLoaderManager);
}
@SuppressWarnings("unused")
protected void unbindDynamicClassLoaderManager(final DynamicClassLoaderManager dynamicClassLoaderManager) {
this.classLoaderManagerReference.compareAndSet(dynamicClassLoaderManager, null);
}
@Reference(
name = "uriprovider",
service = URIProvider.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
bind = "bindUriProvider",
unbind = "unbindUriProvider"
)
@SuppressWarnings("unused")
private void bindUriProvider(ServiceReference<URIProvider> srUriProvider, URIProvider uriProvider) {
providers.put(srUriProvider, uriProvider);
updateURIProviders();
}
@SuppressWarnings("unused")
private void unbindUriProvider(ServiceReference<URIProvider> srUriProvider) {
providers.remove(srUriProvider);
updateURIProviders();
}
private void updateURIProviders() {
URIProvider[] ups = providers.values().toArray(new URIProvider[0]);
this.uriProviderReference.set(ups);
}
@Override
public void start(final @NotNull ProviderContext ctx) {
super.start(ctx);
this.registerListeners();
}
@Override
public void stop() {
this.unregisterListeners();
super.stop();
}
@Override
public void update(final long changeSet) {
super.update(changeSet);
this.updateListeners();
}
@SuppressWarnings("unused")
private void bindRepository(final ServiceReference<SlingRepository> ref) {
this.repositoryReference = ref;
this.repository = null;
}
@SuppressWarnings("unused")
private void unbindRepository(final ServiceReference<SlingRepository> ref) {
if (this.repositoryReference == ref) {
this.repositoryReference = null;
this.repository = null;
}
}
/**
* Register all observation listeners.
*/
private void registerListeners() {
if (this.repository != null) {
logger.debug("Registering resource listeners...");
try {
this.listenerConfig = new JcrListenerBaseConfig(this.getProviderContext().getObservationReporter(),
this.repository);
for (final ObserverConfiguration config : this.getProviderContext().getObservationReporter().getObserverConfigurations()) {
logger.debug("Registering listener for {}", config.getPaths());
final Closeable listener = new JcrResourceListener(this.listenerConfig, config);
this.listeners.put(config, listener);
}
} catch (final RepositoryException e) {
throw new SlingException("Can't create the JCR event listener.", e);
}
logger.debug("Registered resource listeners");
}
}
/**
* Unregister all observation listeners.
*/
private void unregisterListeners() {
logger.debug("Unregistering resource listeners...");
for (final Closeable c : this.listeners.values()) {
try {
logger.debug("Removing listener for {}", ((JcrResourceListener) c).getConfig().getPaths());
c.close();
} catch (final IOException e) {
// ignore this as the method above does not throw it
}
}
this.listeners.clear();
if (this.listenerConfig != null) {
this.listenerConfig.close();
this.listenerConfig = null;
}
logger.debug("Unregistered resource listeners");
}
/**
* Update observation listeners.
*/
private void updateListeners() {
if (this.listenerConfig == null) {
this.unregisterListeners();
this.registerListeners();
} else {
logger.debug("Updating resource listeners...");
final Map<ObserverConfiguration, Closeable> oldMap = new HashMap<>(this.listeners);
this.listeners.clear();
try {
for (final ObserverConfiguration config : this.getProviderContext().getObservationReporter().getObserverConfigurations()) {
// check if such a listener already exists
Closeable listener = oldMap.remove(config);
if (listener == null) {
logger.debug("Registering listener for {}", config.getPaths());
listener = new JcrResourceListener(this.listenerConfig, config);
} else {
logger.debug("Updating listener for {}", config.getPaths());
((JcrResourceListener) listener).update(config);
}
this.listeners.put(config, listener);
}
} catch (final RepositoryException e) {
throw new SlingException("Can't create the JCR event listener.", e);
}
for (final Closeable c : oldMap.values()) {
try {
logger.debug("Removing listener for {}", ((JcrResourceListener) c).getConfig().getPaths());
c.close();
} catch (final IOException e) {
// ignore this as the method above does not throw it
}
}
logger.debug("Updated resource listeners");
}
}
/**
* Create a new ResourceResolver wrapping a Session object. Carries map of
* authentication info in order to create a new resolver as needed.
*/
@Override
@NotNull
public JcrProviderState authenticate(final @NotNull Map<String, Object> authenticationInfo) throws LoginException {
return stateFactory.createProviderState(authenticationInfo);
}
@Override
public void logout(final @Nullable JcrProviderState state) {
if (state != null) {
state.logout();
}
}
@Override
public boolean isLive(final @NotNull ResolveContext<JcrProviderState> ctx) {
return getSession(ctx).isLive();
}
@Override
@Nullable
public Resource getResource(@NotNull ResolveContext<JcrProviderState> ctx, @NotNull String path,
@NotNull ResourceContext rCtx, @Nullable Resource parent) {
try {
return getResourceFactory(ctx).createResource(ctx.getResourceResolver(), path, parent, rCtx.getResolveParameters());
} catch (RepositoryException e) {
throw new SlingException("Can't get resource", e);
}
}
@Override
@Nullable
public Iterator<Resource> listChildren(@NotNull ResolveContext<JcrProviderState> ctx, @NotNull Resource parent) {
JcrItemResource<?> parentItemResource;
// short cut for known JCR resources
if (parent instanceof JcrItemResource) {
parentItemResource = (JcrItemResource<?>) parent;
} else {
// try to get the JcrItemResource for the parent path to list
// children
try {
parentItemResource = getResourceFactory(ctx).createResource(
parent.getResourceResolver(), parent.getPath(), null,
parent.getResourceMetadata().getParameterMap());
} catch (RepositoryException re) {
throw new SlingException("Can't list children", re);
}
}
// return children if there is a parent item resource, else null
return (parentItemResource != null)
? parentItemResource.listJcrChildren()
: null;
}
@Override
public @Nullable Resource getParent(final @NotNull ResolveContext<JcrProviderState> ctx, final @NotNull Resource child) {
if (child instanceof JcrItemResource<?>) {
String version = null;
if (child.getResourceMetadata().getParameterMap() != null) {
version = child.getResourceMetadata().getParameterMap().get("v");
}
if (version == null) {
String parentPath = ResourceUtil.getParent(child.getPath());
if (parentPath != null) {
Item childItem = ((JcrItemResource) child).getItem();
Node parentNode = getResourceFactory(ctx).getParentOrNull(childItem, parentPath);
if (parentNode != null) {
return new JcrNodeResource(ctx.getResourceResolver(), parentPath, null, parentNode, getHelperData(ctx));
}
}
}
return null;
}
return super.getParent(ctx, child);
}
@Override
@NotNull
public Collection<String> getAttributeNames(final @NotNull ResolveContext<JcrProviderState> ctx) {
final Set<String> names = new HashSet<>();
final String[] sessionNames = getSession(ctx).getAttributeNames();
for (final String name : sessionNames) {
if (isAttributeVisible(name)) {
names.add(name);
}
}
return names;
}
@Override
@Nullable
public Object getAttribute(final @NotNull ResolveContext<JcrProviderState> ctx, final @NotNull String name) {
if (isAttributeVisible(name)) {
if (ResourceResolverFactory.USER.equals(name)) {
return getSession(ctx).getUserID();
}
return getSession(ctx).getAttribute(name);
}
return null;
}
@Override
@NotNull
public Resource create(final @NotNull ResolveContext<JcrProviderState> ctx, @Nullable final String path,
@Nullable final Map<String, Object> properties) throws PersistenceException {
if (path == null) {
throw new PersistenceException("Unable to create node with [path=null]");
}
try {
Node node;
final int lastPos = path.lastIndexOf('/');
final Node parent;
if (lastPos == 0) {
parent = getSession(ctx).getRootNode();
} else {
parent = (Node) getSession(ctx).getItem(path.substring(0, lastPos));
}
final String name = path.substring(lastPos + 1);
// extract the nodetype
final String nodeType = getNodeType(properties, ctx);
if (nodeType != null) {
node = parent.addNode(name, nodeType);
} else {
node = parent.addNode(name);
}
if (properties != null) {
populateProperties(node, properties, ctx, path);
}
return new JcrNodeResource(ctx.getResourceResolver(), path, null, node, getHelperData(ctx));
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to create node at " + path, e, path, null);
}
}
protected static @Nullable String getNodeType(@Nullable Map<String, Object> properties,
@NotNull ResolveContext<JcrProviderState> ctx) {
if (properties == null) {
return null;
}
final Object primaryTypeObj = properties.get(JcrConstants.JCR_PRIMARYTYPE);
if (primaryTypeObj != null) {
return primaryTypeObj.toString();
}
final Object resourceTypeObject = properties.get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY);
if (resourceTypeObject != null) {
String resourceType = resourceTypeObject.toString();
if (looksLikeANodeType(resourceType)) {
try {
// validate if it's really a nodetype
getSession(ctx).getWorkspace().getNodeTypeManager().getNodeType(resourceType);
return resourceType;
} catch (final RepositoryException ignore) {
// we expect this, if this isn't a valid node type, therefore ignoring
}
}
}
return null;
}
private static boolean looksLikeANodeType(final String resourceType) {
return resourceType.indexOf(':') != -1 && resourceType.indexOf('/') == -1;
}
private static void populateProperties(@NotNull Node node, @NotNull Map<String, Object> properties, @NotNull ResolveContext<JcrProviderState> ctx, @NotNull String path) throws PersistenceException {
// create modifiable map
final JcrModifiableValueMap jcrMap = new JcrModifiableValueMap(node, getHelperData(ctx));
// check mixin types first
final Object value = properties.get(JcrConstants.JCR_MIXINTYPES);
if (value != null) {
jcrMap.put(JcrConstants.JCR_MIXINTYPES, value);
}
for (final Map.Entry<String, Object> entry : properties.entrySet()) {
if (!IGNORED_PROPERTIES.contains(entry.getKey())) {
try {
jcrMap.put(entry.getKey(), entry.getValue());
} catch (final IllegalArgumentException iae) {
try {
node.remove();
} catch (final RepositoryException re) {
// we ignore this
}
throw new PersistenceException(iae.getMessage(), iae, path, entry.getKey());
}
}
}
}
@Override
public boolean orderBefore(@NotNull ResolveContext<JcrProviderState> ctx, @NotNull Resource parent, @NotNull String name,
@Nullable String followingSiblingName) throws PersistenceException {
Node node = parent.adaptTo(Node.class);
if (node == null) {
throw new PersistenceException("The resource " + parent.getPath() + " cannot be adapted to Node. It is probably not provided by the JcrResourceProvider");
}
try {
// check if reordering necessary
if (requiresReorder(node, name, followingSiblingName)) {
node.orderBefore(name, followingSiblingName);
return true;
}
return false;
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to reorder children below " + parent.getPath(), e, parent.getPath(), null);
}
}
private static boolean requiresReorder(@NotNull Node node, @NotNull String name, @Nullable String followingSiblingName) throws RepositoryException {
NodeIterator nodeIterator = node.getNodes();
long existingNodePosition = -1;
long index = 0;
while (nodeIterator.hasNext()) {
Node childNode = nodeIterator.nextNode();
if (childNode.getName().equals(name)) {
existingNodePosition = index;
}
if (existingNodePosition >= 0) {
// is existing resource already at the desired position?
if (childNode.getName().equals(followingSiblingName)) {
if (existingNodePosition == index - 1) {
return false;
}
}
// is the existing node already the last one in the list?
else if (followingSiblingName == null && existingNodePosition == nodeIterator.getSize() - 1) {
return false;
}
}
index++;
}
return true;
}
@Override
public void delete(final @NotNull ResolveContext<JcrProviderState> ctx, final @NotNull Resource resource) throws PersistenceException {
// try to adapt to Item
Item item = resource.adaptTo(Item.class);
try {
if (item == null) {
item = getSession(ctx).getItem(resource.getPath());
}
item.remove();
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to delete resource", e, resource.getPath(), null);
}
}
@Override
public void revert(final @NotNull ResolveContext<JcrProviderState> ctx) {
try {
getSession(ctx).refresh(false);
} catch (final RepositoryException ignore) {
logger.warn("Unable to revert pending changes.", ignore);
}
}
@Override
public void commit(final @NotNull ResolveContext<JcrProviderState> ctx) throws PersistenceException {
try {
getSession(ctx).save();
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to commit changes to session.", e);
}
}
@Override
public boolean hasChanges(final @NotNull ResolveContext<JcrProviderState> ctx) {
try {
return getSession(ctx).hasPendingChanges();
} catch (final RepositoryException ignore) {
logger.warn("Unable to check session for pending changes.", ignore);
}
return false;
}
@Override
public void refresh(final @NotNull ResolveContext<JcrProviderState> ctx) {
try {
getSession(ctx).refresh(true);
} catch (final RepositoryException ignore) {
logger.warn("Unable to refresh session.", ignore);
}
}
@SuppressWarnings("unchecked")
@Override
public @Nullable <AdapterType> AdapterType adaptTo(final @NotNull ResolveContext<JcrProviderState> ctx,
final @NotNull Class<AdapterType> type) {
Session session = getSession(ctx);
if (type == Session.class) {
return (AdapterType) session;
} else if (type == Principal.class) {
try {
if (session instanceof JackrabbitSession && session.getUserID() != null) {
JackrabbitSession s = ((JackrabbitSession) session);
final UserManager um = s.getUserManager();
if (um != null) {
final Authorizable auth = um.getAuthorizable(s.getUserID());
if (auth != null) {
return (AdapterType) auth.getPrincipal();
}
}
}
logger.debug("not able to adapto Resource to Principal, let the base class try to adapt");
} catch (RepositoryException e) {
logger.warn("error while adapting Resource to Principal, let the base class try to adapt", e);
}
}
return super.adaptTo(ctx, type);
}
@Override
public boolean copy(final @NotNull ResolveContext<JcrProviderState> ctx,
final @NotNull String srcAbsPath,
final @NotNull String destAbsPath) {
return false;
}
@Override
public boolean move(final @NotNull ResolveContext<JcrProviderState> ctx,
final @NotNull String srcAbsPath,
final @NotNull String destAbsPath) throws PersistenceException {
final String srcNodePath = srcAbsPath;
final String dstNodePath = destAbsPath + '/' + ResourceUtil.getName(srcAbsPath);
try {
getSession(ctx).move(srcNodePath, dstNodePath);
return true;
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to move resource to " + destAbsPath, e, srcAbsPath, null);
}
}
@Override
public @Nullable QueryLanguageProvider<JcrProviderState> getQueryLanguageProvider() {
final ProviderContext ctx = this.getProviderContext();
if (ctx != null) {
return new BasicQueryLanguageProvider(ctx);
}
return null;
}
/**
* Returns <code>true</code> unless the name is
* <code>user.jcr.credentials</code> (
* {@link JcrResourceConstants#AUTHENTICATION_INFO_CREDENTIALS}) or contains
* the string <code>password</code> as in <code>user.password</code> (
* {@link org.apache.sling.api.resource.ResourceResolverFactory#PASSWORD})
*
* @param name The name to check whether it is visible or not
* @return <code>true</code> if the name is assumed visible
* @throws NullPointerException if <code>name</code> is <code>null</code>
*/
private static boolean isAttributeVisible(final String name) {
return !name.equals(JcrResourceConstants.AUTHENTICATION_INFO_CREDENTIALS)
&& !name.contains("password");
}
}