Search in sources :

Example 41 with Resource

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;
}
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) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString)

Example 42 with Resource

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;
}
Also used : ParsedParameters(org.apache.sling.resourceresolver.impl.params.ParsedParameters) 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) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString)

Example 43 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 44 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 45 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)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1569 Test (org.junit.Test)695 ValueMap (org.apache.sling.api.resource.ValueMap)338 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)260 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)164 HashMap (java.util.HashMap)160 Node (javax.jcr.Node)151 ArrayList (java.util.ArrayList)137 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)119 PersistenceException (org.apache.sling.api.resource.PersistenceException)114 Test (org.junit.jupiter.api.Test)72 Map (java.util.Map)70 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 LoginException (org.apache.sling.api.resource.LoginException)58 RepositoryException (javax.jcr.RepositoryException)54 Calendar (java.util.Calendar)50 Session (javax.jcr.Session)49