Search in sources :

Example 11 with SyntheticResource

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

the class ResEditorResourceProvider method getResource.

/** ResourceProvider interface */
public Resource getResource(ResourceResolver resolver, String path) {
    // Synthetic resource for the root, so that /reseditor works
    if ((ABS_ROOT).equals(path)) {
        return new SyntheticResource(resolver, path, ROOT_PATHELEMENT_NAME);
    }
    Resource originalResource = resolver.resolve(path.substring(ROOT_PATHELEMENT_NAME.length() + 1));
    Resource newResource = new ResourceTypeResourceWrapper(originalResource);
    return newResource;
}
Also used : Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource)

Example 12 with SyntheticResource

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

the class ResEditorResourceProvider method getResource.

/** ResourceProvider interface */
public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String path) {
    // Synthetic resource for the root, so that /reseditor works
    if ((ABS_ROOT).equals(path)) {
        return new SyntheticResource(resolver, path, ROOT_PATHELEMENT_NAME);
    }
    Resource originalResource = resolver.resolve(req, path.substring(ROOT_PATHELEMENT_NAME.length()));
    Resource newResource = new ResourceTypeResourceWrapper(originalResource);
    return newResource;
}
Also used : Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource)

Example 13 with SyntheticResource

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

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

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

the class ResourceResolverImplTest method testIsResourceCyclicHierarchyIndirect.

@Test(expected = SlingException.class)
public void testIsResourceCyclicHierarchyIndirect() {
    final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver();
    /**
         * prepare resource type hierarchy
         * /types/1   <----+
         *  +- /types/2    |
         *    +- /types/3 -+
         */
    resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/1", "/types/component", "/types/2"));
    resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/2", "/types/component", "/types/3"));
    resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/3", "/types/component", "/types/1"));
    Resource resource = resolver.add(new SyntheticResource(resolver, "/resourceT1", "/types/1"));
    assertTrue(resolver.isResourceType(resource, "/types/1"));
    assertTrue(resolver.isResourceType(resource, "/types/2"));
    assertTrue(resolver.isResourceType(resource, "/types/3"));
    // this should throw a SlingException when detecting the cyclic hierarchy
    resolver.isResourceType(resource, "/types/unknown");
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Aggregations

SyntheticResource (org.apache.sling.api.resource.SyntheticResource)18 Resource (org.apache.sling.api.resource.Resource)16 Test (org.junit.Test)10 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 AuthenticatedResourceProvider (org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)2 Expectations (org.jmock.Expectations)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 JspException (javax.servlet.jsp.JspException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 IteratorChain (org.apache.commons.collections4.iterators.IteratorChain)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1