Search in sources :

Example 6 with AuthenticatedResourceProvider

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

the class ResourceResolverControl method listChildren.

/**
     * This method asks all matching resource providers for the children iterators,
     * merges them, adds {@link SyntheticResource}s (see
     * {@link #getResource(String, Resource, Map, boolean)} for more details),
     * filters out the duplicates and returns the resulting iterator. All
     * transformations are done lazily, during the {@link Iterator#hasNext()}
     * invocation on the result.
     */
@SuppressWarnings("unchecked")
public Iterator<Resource> listChildren(final ResourceResolverContext context, final Resource parent) {
    final String parentPath = parent.getPath();
    // 3 sources are combined: children of the provider which owns 'parent',
    // providers which are directly mounted at a child path,
    // synthetic resources for providers mounted at a lower level
    // children of the 'parent' provider
    Iterator<Resource> realChildren = null;
    final AuthenticatedResourceProvider provider = this.getBestMatchingProvider(context, parentPath);
    if (provider != null) {
        realChildren = provider.listChildren(parent);
    }
    final Set<String> visitedNames = new HashSet<>();
    IteratorChain chain = new IteratorChain();
    if (realChildren != null) {
        chain.addIterator(realChildren);
    }
    // synthetic and providers are done in one loop
    final Node<ResourceProviderHandler> node = getResourceProviderStorage().getTree().getNode(parent.getPath());
    if (node != null) {
        final List<Resource> syntheticList = new ArrayList<>();
        final List<Resource> providerList = new ArrayList<>();
        for (final Entry<String, Node<ResourceProviderHandler>> entry : node.getChildren().entrySet()) {
            final String name = entry.getKey();
            final ResourceProviderHandler handler = entry.getValue().getValue();
            PathBuilder pathBuilder = new PathBuilder(parent.getPath());
            pathBuilder.append(name);
            final String childPath = pathBuilder.toString();
            if (handler == null) {
                syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath, ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
            } else {
                Resource rsrc = null;
                try {
                    final AuthenticatedResourceProvider rp = context.getProviderManager().getOrCreateProvider(handler, this);
                    rsrc = rp == null ? null : rp.getResource(childPath, parent, null);
                } catch (final LoginException ignore) {
                // ignore
                }
                if (rsrc != null) {
                    providerList.add(rsrc);
                } else {
                    // otherwise we need to make sure that no one else is providing this child
                    if (entry.getValue().getChildren().isEmpty()) {
                        syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath, ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
                    } else {
                        visitedNames.add(name);
                    }
                }
            }
        }
        if (!providerList.isEmpty()) {
            chain.addIterator(providerList.iterator());
        }
        if (!syntheticList.isEmpty()) {
            chain.addIterator(syntheticList.iterator());
        }
    }
    if (chain.size() == 0) {
        return Collections.EMPTY_LIST.iterator();
    }
    return new UniqueResourceIterator(visitedNames, chain);
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) Node(org.apache.sling.resourceresolver.impl.providers.tree.Node) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ArrayList(java.util.ArrayList) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider) IteratorChain(org.apache.commons.collections4.iterators.IteratorChain) PathBuilder(org.apache.sling.api.resource.path.PathBuilder) LoginException(org.apache.sling.api.resource.LoginException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 7 with AuthenticatedResourceProvider

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

the class ResourceResolverControl method queryResources.

/**
     * Queries all resource providers and combines the results.
     */
public Iterator<Map<String, Object>> queryResources(final ResourceResolverContext context, final String query, final String language) {
    final List<AuthenticatedResourceProvider> queryableRP = getQueryableProviders(context, language);
    final List<Iterator<Map<String, Object>>> iterators = new ArrayList<>(queryableRP.size());
    for (AuthenticatedResourceProvider p : queryableRP) {
        iterators.add(p.queryResources(query, language));
    }
    return new ChainedIterator<>(iterators.iterator());
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Example 8 with AuthenticatedResourceProvider

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

the class ResourceResolverControl method copy.

/**
     * Tries to find a resource provider accepting both paths and invokes
     * {@link AuthenticatedResourceProvider#copy(String, String)} method on it.
     * Returns false if there's no such provider.
     */
public Resource copy(final ResourceResolverContext context, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
    final AuthenticatedResourceProvider optimizedSourceProvider = checkSourceAndDest(context, srcAbsPath, destAbsPath);
    if (optimizedSourceProvider != null && optimizedSourceProvider.copy(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);
        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)

Example 9 with AuthenticatedResourceProvider

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

the class ResourceResolverControl method findResources.

/**
     * Queries all resource providers and combines the results.
     */
public Iterator<Resource> findResources(final ResourceResolverContext context, final String query, final String language) {
    final List<AuthenticatedResourceProvider> queryableRP = getQueryableProviders(context, language);
    final List<Iterator<Resource>> iterators = new ArrayList<>(queryableRP.size());
    for (AuthenticatedResourceProvider p : queryableRP) {
        iterators.add(p.findResources(query, language));
    }
    return new ChainedIterator<>(iterators.iterator());
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) 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