use of org.apache.sling.api.resource.SyntheticResource 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.SyntheticResource in project sling by apache.
the class ResourceProviderEntryTest method testRemoveTheOnlyProvider.
@Test
public void testRemoveTheOnlyProvider() throws LoginException {
long counter = 1;
providers.clear();
for (String path : new String[] { "/foo", "/", "/foo/bar" }) {
final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
final ResourceProvider<?> p = Mockito.mock(ResourceProvider.class);
Mockito.when(p.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(path), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(resolver));
++counter;
ResourceProviderHandler h = createRPHandler(p, "rp" + counter, counter, path);
providers.add(h);
this.providersBasedResolver = null;
{
final Resource r = getResource(path);
assertEqualsResolver(resolver, r);
assertFalse(r instanceof SyntheticResource);
}
providers.remove(h);
this.providersBasedResolver = null;
{
final Resource r = getResource(path);
// If our provider is indeed gone, we should get one of the following conditions
if (r == null) {
//fine
} else if (!p.equals(r.getResourceResolver())) {
//fine
} else {
fail("Expecting inactive provider after removing it for " + path);
}
}
}
}
use of org.apache.sling.api.resource.SyntheticResource in project sling by apache.
the class ResourceResolverImplTest method testIsResourceTypeWithPaths.
@Test
public void testIsResourceTypeWithPaths() {
final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver();
/**
* prepare resource type hierarchy
* /types/1
* +- /types/2
* +- /types/3
*/
resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/1", "/types/component", "/types/2"));
resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/2", "/types/component", "/types/3"));
resolver.add(new SyntheticResource(resolver, "/types/3", "/types/component"));
Resource resourceT1 = resolver.add(new SyntheticResource(resolver, "/resourceT1", "/types/1"));
Resource resourceT2 = resolver.add(new SyntheticResource(resolver, "/resourceT2", "/types/2"));
Resource resourceT3 = resolver.add(new SyntheticResource(resolver, "/resourceT3", "/types/3"));
assertTrue(resolver.isResourceType(resourceT1, "/types/1"));
assertTrue(resolver.isResourceType(resourceT1, "/types/2"));
assertTrue(resolver.isResourceType(resourceT1, "/types/3"));
assertFalse(resolver.isResourceType(resourceT1, "/types/component"));
assertFalse(resolver.isResourceType(resourceT1, "/types/unknown"));
assertFalse(resolver.isResourceType(resourceT2, "/types/1"));
assertTrue(resolver.isResourceType(resourceT2, "/types/2"));
assertTrue(resolver.isResourceType(resourceT2, "/types/3"));
assertFalse(resolver.isResourceType(resourceT2, "/types/component"));
assertFalse(resolver.isResourceType(resourceT2, "/types/unknown"));
assertFalse(resolver.isResourceType(resourceT3, "/types/1"));
assertFalse(resolver.isResourceType(resourceT3, "/types/2"));
assertTrue(resolver.isResourceType(resourceT3, "/types/3"));
assertFalse(resolver.isResourceType(resourceT3, "/types/component"));
assertFalse(resolver.isResourceType(resourceT3, "/types/unknown"));
}
use of org.apache.sling.api.resource.SyntheticResource in project sling by apache.
the class ResourceResolverImplTest method testIsResourceTypeWithMixedAbsoluteAndRelativePaths.
/**
* @see <a href="https://issues.apache.org/jira/browse/SLING-6327">SLING-6327</a>
*/
@Test
public void testIsResourceTypeWithMixedAbsoluteAndRelativePaths() {
final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver(new String[] { "/apps/", "/libs/" });
Resource resourceT1 = resolver.add(new SyntheticResource(resolver, "/resourceT1", "types/1"));
Resource resourceT2 = resolver.add(new SyntheticResource(resolver, "/resourceT2", "/apps/types/2"));
Resource resourceT3 = resolver.add(new SyntheticResource(resolver, "/resourceT3", "/libs/types/3"));
Resource resourceT4 = resolver.add(new SyntheticResource(resolver, "/resourceT3", "/someprefix/types/4"));
assertTrue(resolver.isResourceType(resourceT1, "/libs/types/1"));
assertTrue(resolver.isResourceType(resourceT1, "/apps/types/1"));
assertTrue(resolver.isResourceType(resourceT1, "types/1"));
assertTrue(resolver.isResourceType(resourceT2, "/apps/types/2"));
assertTrue(resolver.isResourceType(resourceT2, "types/2"));
assertTrue(resolver.isResourceType(resourceT2, "/libs/types/2"));
assertTrue(resolver.isResourceType(resourceT3, "/apps/types/3"));
assertTrue(resolver.isResourceType(resourceT3, "types/3"));
assertTrue(resolver.isResourceType(resourceT3, "/libs/types/3"));
assertFalse(resolver.isResourceType(resourceT4, "/apps/types/4"));
assertFalse(resolver.isResourceType(resourceT4, "types/4"));
assertFalse(resolver.isResourceType(resourceT4, "/libs/types/4"));
assertTrue(resolver.isResourceType(resourceT4, "/someprefix/types/4"));
}
use of org.apache.sling.api.resource.SyntheticResource in project sling by apache.
the class ResourceResolverImplTest method testIsResourceCyclicHierarchyDirect.
@Test(expected = SlingException.class)
public void testIsResourceCyclicHierarchyDirect() {
final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver();
/**
* prepare resource type hierarchy
* /types/1 <---+
* +- /types/2 -+
*/
resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/1", "/types/component", "/types/2"));
resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/2", "/types/component", "/types/1"));
Resource resource = resolver.add(new SyntheticResource(resolver, "/resourceT1", "/types/1"));
assertTrue(resolver.isResourceType(resource, "/types/1"));
assertTrue(resolver.isResourceType(resource, "/types/2"));
// this should throw a SlingException when detecting the cyclic hierarchy
resolver.isResourceType(resource, "/types/unknown");
}
Aggregations