Search in sources :

Example 1 with SyntheticResource

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;
}
Also used : Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Example 2 with SyntheticResource

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);
            }
        }
    }
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) AbstractResource(org.apache.sling.api.resource.AbstractResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 3 with SyntheticResource

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"));
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 4 with SyntheticResource

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"));
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 5 with SyntheticResource

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");
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Aggregations

SyntheticResource (org.apache.sling.api.resource.SyntheticResource)18 Resource (org.apache.sling.api.resource.Resource)16 Test (org.junit.Test)10 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 AuthenticatedResourceProvider (org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)2 Expectations (org.jmock.Expectations)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 JspException (javax.servlet.jsp.JspException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 IteratorChain (org.apache.commons.collections4.iterators.IteratorChain)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1