Search in sources :

Example 51 with Resource

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

the class ResourceDecoratorTracker method decorate.

/**
     * Decorate a resource.
     */
public Resource decorate(final Resource resource) {
    Resource result = resource;
    final ResourceDecorator[] decorators = this.resourceDecoratorsArray;
    for (final ResourceDecorator decorator : decorators) {
        final Resource original = result;
        result = decorator.decorate(original);
        if (result == null) {
            result = original;
        }
    }
    // make resource metadata read-only
    result.getResourceMetadata().lock();
    return result;
}
Also used : Resource(org.apache.sling.api.resource.Resource) ResourceDecorator(org.apache.sling.api.resource.ResourceDecorator)

Example 52 with Resource

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

the class ResourceResolverControl method checkSourceAndDest.

private AuthenticatedResourceProvider checkSourceAndDest(final ResourceResolverContext context, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
    // check source
    final Node<ResourceProviderHandler> srcNode = getResourceProviderStorage().getTree().getBestMatchingNode(srcAbsPath);
    if (srcNode == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    AuthenticatedResourceProvider srcProvider = null;
    try {
        srcProvider = context.getProviderManager().getOrCreateProvider(srcNode.getValue(), this);
    } catch (LoginException e) {
    // ignore
    }
    if (srcProvider == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    final Resource srcResource = srcProvider.getResource(srcAbsPath, null, null);
    if (srcResource == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    // check destination
    final Node<ResourceProviderHandler> destNode = getResourceProviderStorage().getTree().getBestMatchingNode(destAbsPath);
    if (destNode == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    AuthenticatedResourceProvider destProvider = null;
    try {
        destProvider = context.getProviderManager().getOrCreateProvider(destNode.getValue(), this);
    } catch (LoginException e) {
    // ignore
    }
    if (destProvider == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    final Resource destResource = destProvider.getResource(destAbsPath, null, null);
    if (destResource == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    // check for sub providers of src and dest
    if (srcProvider == destProvider && !collectProviders(context, srcNode) && !collectProviders(context, destNode)) {
        return srcProvider;
    }
    return null;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) PersistenceException(org.apache.sling.api.resource.PersistenceException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) LoginException(org.apache.sling.api.resource.LoginException) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Example 53 with Resource

use of org.apache.sling.api.resource.Resource 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 54 with Resource

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

the class ResourceResolverControl method copy.

private void copy(final ResourceResolverContext context, final Resource src, final String dstPath, final List<Resource> newNodes) throws PersistenceException {
    final ValueMap vm = src.getValueMap();
    final String createPath = new PathBuilder(dstPath).append(src.getName()).toString();
    newNodes.add(this.create(context, createPath, vm));
    for (final Resource c : src.getChildren()) {
        copy(context, c, createPath, newNodes);
    }
}
Also used : PathBuilder(org.apache.sling.api.resource.path.PathBuilder) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource)

Example 55 with Resource

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

the class MockedResourceResolverImplTest method testResourceResolverListChildren.

/**
     * Test listing children via the resource resolver listChildren call.
     * @throws LoginException
     */
@Test
public void testResourceResolverListChildren() 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.
    Iterator<Resource> resourceIterator = resourceResolver.listChildren(resource);
    Assert.assertNotNull(resourceResolver);
    int i = 0;
    while (resourceIterator.hasNext()) {
        Assert.assertEquals("m" + i, resourceIterator.next().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)

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