Search in sources :

Example 46 with Resource

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

Example 47 with 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);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 48 with Resource

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;
    }
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) Resource(org.apache.sling.api.resource.Resource) ParametrizableResourceProvider(org.apache.sling.api.resource.ParametrizableResourceProvider)

Example 49 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class ResourceResolverImpl method copy.

@Override
public Resource copy(final String srcAbsPath, final String destAbsPath) throws PersistenceException {
    Resource rsrc = this.control.copy(this.context, srcAbsPath, destAbsPath);
    if (rsrc != null) {
        rsrc.getResourceMetadata().setResolutionPath(rsrc.getPath());
        rsrc = this.factory.getResourceDecoratorTracker().decorate(rsrc);
    }
    return rsrc;
}
Also used : StarResource(org.apache.sling.resourceresolver.impl.helper.StarResource) RedirectResource(org.apache.sling.resourceresolver.impl.helper.RedirectResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource)

Example 50 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class ResourceResolverImpl method getAbsoluteResourceInternal.

/**
     * Creates a resource with the given path if existing
     */
private Resource getAbsoluteResourceInternal(@CheckForNull final Resource parent, @CheckForNull final String path, final Map<String, String> parameters, final boolean isResolve) {
    if (path == null || path.length() == 0 || path.charAt(0) != '/') {
        logger.debug("getResourceInternal: Path must be absolute {}", path);
        // path must be absolute
        return null;
    }
    final Resource parentToUse;
    if (parent != null && path.startsWith(parent.getPath())) {
        parentToUse = parent;
    } else {
        parentToUse = null;
    }
    final Resource resource = this.control.getResource(this.context, path, parentToUse, parameters, isResolve);
    if (resource != null) {
        resource.getResourceMetadata().setResolutionPath(path);
        resource.getResourceMetadata().setParameterMap(parameters);
        return resource;
    }
    logger.debug("getResourceInternal: Cannot resolve path '{}' to a resource", path);
    return null;
}
Also used : StarResource(org.apache.sling.resourceresolver.impl.helper.StarResource) RedirectResource(org.apache.sling.resourceresolver.impl.helper.RedirectResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1151 Test (org.junit.Test)633 ValueMap (org.apache.sling.api.resource.ValueMap)263 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)164 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)159 Node (javax.jcr.Node)116 HashMap (java.util.HashMap)104 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)99 PersistenceException (org.apache.sling.api.resource.PersistenceException)94 ArrayList (java.util.ArrayList)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)55 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)43 Map (java.util.Map)41 InputStream (java.io.InputStream)38 LoginException (org.apache.sling.api.resource.LoginException)37 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)36 Iterator (java.util.Iterator)33