use of org.apache.sling.api.resource.Resource in project sling by apache.
the class ResourceDecoratorTracker method decorate.
/**
* Decorate a resource.
*/
public Resource decorate(final Resource resource) {
Resource result = resource;
final ResourceDecorator[] decorators = this.resourceDecoratorsArray;
for (final ResourceDecorator decorator : decorators) {
final Resource original = result;
result = decorator.decorate(original);
if (result == null) {
result = original;
}
}
// make resource metadata read-only
result.getResourceMetadata().lock();
return result;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class ResourceResolverControl method checkSourceAndDest.
private AuthenticatedResourceProvider checkSourceAndDest(final ResourceResolverContext context, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
// check source
final Node<ResourceProviderHandler> srcNode = getResourceProviderStorage().getTree().getBestMatchingNode(srcAbsPath);
if (srcNode == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
AuthenticatedResourceProvider srcProvider = null;
try {
srcProvider = context.getProviderManager().getOrCreateProvider(srcNode.getValue(), this);
} catch (LoginException e) {
// ignore
}
if (srcProvider == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
final Resource srcResource = srcProvider.getResource(srcAbsPath, null, null);
if (srcResource == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
// check destination
final Node<ResourceProviderHandler> destNode = getResourceProviderStorage().getTree().getBestMatchingNode(destAbsPath);
if (destNode == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
AuthenticatedResourceProvider destProvider = null;
try {
destProvider = context.getProviderManager().getOrCreateProvider(destNode.getValue(), this);
} catch (LoginException e) {
// ignore
}
if (destProvider == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
final Resource destResource = destProvider.getResource(destAbsPath, null, null);
if (destResource == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
// check for sub providers of src and dest
if (srcProvider == destProvider && !collectProviders(context, srcNode) && !collectProviders(context, destNode)) {
return srcProvider;
}
return null;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class ResourceResolverControl method getResource.
/**
* Returns resource from the most appropriate resource provider.
* <br/><br/>
* If there's no such provider and the path is a part of some resource
* provider path, then the {@link SyntheticResource} will be returned. For
* instance, if we have resource provider under
* {@code /libs/sling/servlet/default/GET.servlet} and no resource provider
* returns a resource for {@code /libs/sling/servlet/default}, then the
* {@link SyntheticResource} will be returned to provide a consistent
* resource tree.
* <br/><br/>
* The same behaviour occurs in {@link #getParent(Resource)} and
* {@link #listChildren(Resource)}.
*/
public Resource getResource(final ResourceResolverContext context, String path, Resource parent, Map<String, String> parameters, boolean isResolve) {
if (path == null || path.length() == 0 || path.charAt(0) != '/') {
logger.debug("Not absolute {}", path);
// path must be absolute
return null;
}
final AuthenticatedResourceProvider provider = this.getBestMatchingProvider(context, path);
if (provider != null) {
final Resource resourceCandidate = provider.getResource(path, parent, parameters);
if (resourceCandidate != null) {
return resourceCandidate;
}
}
// to get the parent resource for resource traversal.
if (!isResolve && isIntermediatePath(path)) {
logger.debug("Resolved Synthetic {}", path);
return new SyntheticResource(context.getResourceResolver(), path, ResourceProvider.RESOURCE_TYPE_SYNTHETIC);
}
logger.debug("Resource null {} ", path);
return null;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class ResourceResolverControl method copy.
private void copy(final ResourceResolverContext context, final Resource src, final String dstPath, final List<Resource> newNodes) throws PersistenceException {
final ValueMap vm = src.getValueMap();
final String createPath = new PathBuilder(dstPath).append(src.getName()).toString();
newNodes.add(this.create(context, createPath, vm));
for (final Resource c : src.getChildren()) {
copy(context, c, createPath, newNodes);
}
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MockedResourceResolverImplTest method testResourceResolverListChildren.
/**
* Test listing children via the resource resolver listChildren call.
* @throws LoginException
*/
@Test
public void testResourceResolverListChildren() throws LoginException {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
buildResource("/single/test/withchildren", buildChildResources("/single/test/withchildren"), resourceResolver, resourceProvider);
Resource resource = resourceResolver.getResource("/single/test/withchildren");
Assert.assertNotNull(resource);
// test via the resource list children itself, this really just tests this test case.
Iterator<Resource> resourceIterator = resourceResolver.listChildren(resource);
Assert.assertNotNull(resourceResolver);
int i = 0;
while (resourceIterator.hasNext()) {
Assert.assertEquals("m" + i, resourceIterator.next().getName());
i++;
}
Assert.assertEquals(5, i);
}
Aggregations