use of org.apache.sling.api.resource.Resource in project sling by apache.
the class ResourceResolverImpl method getParent.
@Override
public Resource getParent(final Resource child) {
Resource rsrc = null;
final String parentPath = ResourceUtil.getParent(child.getPath());
if (parentPath != null) {
// if the parent path is relative, resolve using search paths.
if (!parentPath.startsWith("/")) {
rsrc = context.getResourceResolver().getResource(parentPath);
} else {
rsrc = this.control.getParent(this.context, parentPath, child);
if (rsrc != null) {
rsrc.getResourceMetadata().setResolutionPath(rsrc.getPath());
rsrc = this.factory.getResourceDecoratorTracker().decorate(rsrc);
}
}
}
return rsrc;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class ResourceResolverImpl method getResourceInternal.
private Resource getResourceInternal(Resource parent, String path) {
Resource result = null;
if (path != null) {
// if the path is absolute, normalize . and .. segments and get res
if (path.startsWith("/")) {
final ParsedParameters parsedPath = new ParsedParameters(path);
path = ResourceUtil.normalize(parsedPath.getRawPath());
result = (path != null) ? getAbsoluteResourceInternal(parent, path, parsedPath.getParameters(), false) : null;
if (result != null) {
result = this.factory.getResourceDecoratorTracker().decorate(result);
}
} else {
// otherwise we have to apply the search path
// (don't use this.getSearchPath() to save a few cycle for not cloning)
final String[] paths = factory.getSearchPath();
if (paths != null) {
for (final String prefix : factory.getSearchPath()) {
result = getResource(prefix + path);
if (result != null) {
break;
}
}
}
}
}
return result;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MockedResourceResolverImplTest method testGetResource.
/**
* Test getResource for a resource provided by a resource provider.
* @throws LoginException
*/
@Test
public void testGetResource() throws LoginException {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
Assert.assertNotNull(resourceResolver);
Resource singleResource = buildResource("/single/test", EMPTY_RESOURCE_LIST, resourceResolver, resourceProvider);
Resource resource = resourceResolver.getResource("/single/test");
Assert.assertEquals(singleResource, resource);
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MockedResourceResolverImplTest method testResourceResolverGetChildren.
/**
* Tests listing children via the resource resolver getChildren call.
* @throws LoginException
*/
@Test
public void testResourceResolverGetChildren() 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.
Iterable<Resource> resourceIterator = resourceResolver.getChildren(resource);
Assert.assertNotNull(resourceResolver);
int i = 0;
for (Resource r : resourceIterator) {
Assert.assertEquals("m" + i, r.getName());
i++;
}
Assert.assertEquals(5, i);
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class LegacyResourceProviderAdapter method getResource.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Resource getResource(ResolveContext<Object> ctx, String path, ResourceContext resourceContext, Resource parent) {
Resource resourceCandidate;
if (rp instanceof ParametrizableResourceProvider) {
resourceCandidate = ((ParametrizableResourceProvider) rp).getResource(ctx.getResourceResolver(), path, resourceContext.getResolveParameters());
} else {
resourceCandidate = rp.getResource(ctx.getResourceResolver(), path);
}
ResourceProvider<?> parentProvider = ctx.getParentResourceProvider();
ResolveContext parentCtx = ctx.getParentResolveContext();
// Ask the parent provider
if (resourceCandidate == null && !ownsRoot && parentProvider != null) {
return parentProvider.getResource(parentCtx, path, resourceContext, parent);
}
// Support the INTERNAL_CONTINUE_RESOLVING flag
Resource fallbackResource = resourceCandidate;
if (resourceCandidate != null && parentProvider != null && isContinueResolving(resourceCandidate)) {
resourceCandidate = ctx.getParentResourceProvider().getResource(parentCtx, path, resourceContext, parent);
}
if (resourceCandidate != null) {
return resourceCandidate;
} else {
return fallbackResource;
}
}
Aggregations