Search in sources :

Example 1 with AuthenticatedResourceProvider

use of org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider 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 2 with AuthenticatedResourceProvider

use of org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider 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 3 with AuthenticatedResourceProvider

use of org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider in project sling by apache.

the class ResourceResolverControl method delete.

/**
     * Delete the resource. Iterate over all modifiable ResourceProviders
     * giving each an opportunity to delete the resource if they are able.
     *
     * @throws NullPointerException
     *             if resource is null
     * @throws UnsupportedOperationException
     *             If deletion is not allowed/possible
     * @throws PersistenceException
     *             If deletion fails
     */
public void delete(final ResourceResolverContext context, final Resource resource) throws PersistenceException {
    final String path = resource.getPath();
    final AuthenticatedResourceProvider provider = getBestMatchingModifiableProvider(context, path);
    if (provider != null) {
        provider.delete(resource);
        return;
    }
    throw new UnsupportedOperationException("delete at '" + path + "'");
}
Also used : AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Example 4 with AuthenticatedResourceProvider

use of org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider in project sling by apache.

the class ResourceResolverControl method getParent.

/**
     * Returns parent from the most appropriate resource provider accepting the
     * given children.
     *
     * In some cases the {@link SyntheticResource} can be returned if no
     * resource provider returns parent for this child. See
     * {@link #getResource(String, Resource, Map, boolean)} for more details
     */
public Resource getParent(@Nonnull final ResourceResolverContext context, @Nonnull final String parentPath, @Nonnull final Resource child) {
    final AuthenticatedResourceProvider childProvider = getBestMatchingProvider(context, child.getPath());
    final AuthenticatedResourceProvider parentProvider = getBestMatchingProvider(context, parentPath);
    if (parentProvider != null) {
        final Resource parentCandidate;
        if (childProvider == parentProvider) {
            parentCandidate = parentProvider.getParent(child);
        } else {
            parentCandidate = parentProvider.getResource(parentPath, null, null);
        }
        if (parentCandidate != null) {
            return parentCandidate;
        }
    }
    if (isIntermediatePath(parentPath)) {
        return new SyntheticResource(context.getResourceResolver(), parentPath, ResourceProvider.RESOURCE_TYPE_SYNTHETIC);
    }
    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 5 with AuthenticatedResourceProvider

use of org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider in project sling by apache.

the class ResourceResolverControl method move.

/**
     * Tries to find a resource provider accepting both paths and invokes
     * {@link AuthenticatedResourceProvider#move(String, String)} method on it.
     * Returns false if there's no such provider.
     */
public Resource move(final ResourceResolverContext context, String srcAbsPath, String destAbsPath) throws PersistenceException {
    final AuthenticatedResourceProvider optimizedSourceProvider = checkSourceAndDest(context, srcAbsPath, destAbsPath);
    if (optimizedSourceProvider != null && optimizedSourceProvider.move(srcAbsPath, destAbsPath)) {
        return this.getResource(context, destAbsPath + '/' + ResourceUtil.getName(srcAbsPath), null, null, false);
    }
    final Resource srcResource = this.getResource(context, srcAbsPath, null, null, false);
    final List<Resource> newResources = new ArrayList<>();
    boolean rollback = true;
    try {
        this.copy(context, srcResource, destAbsPath, newResources);
        this.delete(context, srcResource);
        rollback = false;
        return newResources.get(0);
    } finally {
        if (rollback) {
            for (final Resource rsrc : newResources) {
                this.delete(context, rsrc);
            }
        }
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ArrayList(java.util.ArrayList) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Aggregations

AuthenticatedResourceProvider (org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)9 Resource (org.apache.sling.api.resource.Resource)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)2 LoginException (org.apache.sling.api.resource.LoginException)2 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 IteratorChain (org.apache.commons.collections4.iterators.IteratorChain)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 PathBuilder (org.apache.sling.api.resource.path.PathBuilder)1 Node (org.apache.sling.resourceresolver.impl.providers.tree.Node)1