Search in sources :

Example 6 with SyntheticResource

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

the class ResourceResolverImplTest method testIsResourceTypeWithPaths.

@Test
public void testIsResourceTypeWithPaths() {
    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 SyntheticResource(resolver, "/types/3", "/types/component"));
    Resource resourceT1 = resolver.add(new SyntheticResource(resolver, "/resourceT1", "/types/1"));
    Resource resourceT2 = resolver.add(new SyntheticResource(resolver, "/resourceT2", "/types/2"));
    Resource resourceT3 = resolver.add(new SyntheticResource(resolver, "/resourceT3", "/types/3"));
    assertTrue(resolver.isResourceType(resourceT1, "/types/1"));
    assertTrue(resolver.isResourceType(resourceT1, "/types/2"));
    assertTrue(resolver.isResourceType(resourceT1, "/types/3"));
    assertFalse(resolver.isResourceType(resourceT1, "/types/component"));
    assertFalse(resolver.isResourceType(resourceT1, "/types/unknown"));
    assertFalse(resolver.isResourceType(resourceT2, "/types/1"));
    assertTrue(resolver.isResourceType(resourceT2, "/types/2"));
    assertTrue(resolver.isResourceType(resourceT2, "/types/3"));
    assertFalse(resolver.isResourceType(resourceT2, "/types/component"));
    assertFalse(resolver.isResourceType(resourceT2, "/types/unknown"));
    assertFalse(resolver.isResourceType(resourceT3, "/types/1"));
    assertFalse(resolver.isResourceType(resourceT3, "/types/2"));
    assertTrue(resolver.isResourceType(resourceT3, "/types/3"));
    assertFalse(resolver.isResourceType(resourceT3, "/types/component"));
    assertFalse(resolver.isResourceType(resourceT3, "/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)

Example 7 with SyntheticResource

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

the class ResourceResolverImplTest method testIsResourceTypeWithMixedAbsoluteAndRelativePaths.

/**
     * @see <a href="https://issues.apache.org/jira/browse/SLING-6327">SLING-6327</a>
     */
@Test
public void testIsResourceTypeWithMixedAbsoluteAndRelativePaths() {
    final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver(new String[] { "/apps/", "/libs/" });
    Resource resourceT1 = resolver.add(new SyntheticResource(resolver, "/resourceT1", "types/1"));
    Resource resourceT2 = resolver.add(new SyntheticResource(resolver, "/resourceT2", "/apps/types/2"));
    Resource resourceT3 = resolver.add(new SyntheticResource(resolver, "/resourceT3", "/libs/types/3"));
    Resource resourceT4 = resolver.add(new SyntheticResource(resolver, "/resourceT3", "/someprefix/types/4"));
    assertTrue(resolver.isResourceType(resourceT1, "/libs/types/1"));
    assertTrue(resolver.isResourceType(resourceT1, "/apps/types/1"));
    assertTrue(resolver.isResourceType(resourceT1, "types/1"));
    assertTrue(resolver.isResourceType(resourceT2, "/apps/types/2"));
    assertTrue(resolver.isResourceType(resourceT2, "types/2"));
    assertTrue(resolver.isResourceType(resourceT2, "/libs/types/2"));
    assertTrue(resolver.isResourceType(resourceT3, "/apps/types/3"));
    assertTrue(resolver.isResourceType(resourceT3, "types/3"));
    assertTrue(resolver.isResourceType(resourceT3, "/libs/types/3"));
    assertFalse(resolver.isResourceType(resourceT4, "/apps/types/4"));
    assertFalse(resolver.isResourceType(resourceT4, "types/4"));
    assertFalse(resolver.isResourceType(resourceT4, "/libs/types/4"));
    assertTrue(resolver.isResourceType(resourceT4, "/someprefix/types/4"));
}
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)

Example 8 with SyntheticResource

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

the class ResourceResolverImplTest method testIsResourceCyclicHierarchyDirect.

@Test(expected = SlingException.class)
public void testIsResourceCyclicHierarchyDirect() {
    final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver();
    /**
         * prepare resource type hierarchy
         * /types/1  <---+
         *  +- /types/2 -+
         */
    resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/1", "/types/component", "/types/2"));
    resolver.add(new SyntheticResourceWithSupertype(resolver, "/types/2", "/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"));
    // 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)

Example 9 with SyntheticResource

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

the class ResourceResolverImplTest method test_getResourceSuperType.

@Test
public void test_getResourceSuperType() {
    final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver();
    // the resources to test
    final Resource r = resolver.add(new SyntheticResource(resolver, "/a", "a:b"));
    final Resource r2 = resolver.add(new SyntheticResource(resolver, "/a2", "a:c"));
    resolver.add(new SyntheticResourceWithSupertype(resolver, "/a/b", "x:y", "t:c"));
    assertEquals("t:c", resolver.getParentResourceType(r.getResourceType()));
    assertNull(resolver.getParentResourceType(r2.getResourceType()));
}
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)

Example 10 with SyntheticResource

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

the class EvalTagHandler method doEndTag.

/**
     * Called after the body has been processed.
     *
     * @return whether additional evaluations of the body are desired
     */
public int doEndTag() throws JspException {
    log.debug("EvalTagHandler doEndTag");
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final Servlet servlet;
    if (!this.ignoreResourceTypeHierarchy) {
        // detecte resource
        final Resource resource;
        if (this.resource != null) {
            resource = this.resource;
        } else if (this.resourceType != null) {
            resource = new SyntheticResource(bindings.getRequest().getResourceResolver(), bindings.getResource().getPath(), this.resourceType);
        } else {
            resource = bindings.getResource();
        }
        servlet = servletResolver.resolveServlet(resource, this.script);
    } else {
        final ResourceResolver rr = bindings.getRequest().getResourceResolver();
        final String scriptPath;
        if (!script.startsWith("/")) {
            // resolve relative script
            String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
            // check if parent resides on search path
            for (String sp : rr.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + '/' + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(rr, scriptPath);
    }
    if (servlet == null) {
        throw new JspException("Could not find script '" + script + "' referenced in jsp " + scriptHelper.getScript().getScriptResource().getPath());
    }
    try {
        if (flush && !(pageContext.getOut() instanceof BodyContent)) {
            // might throw an IOException of course
            pageContext.getOut().flush();
        }
        // wrap the response to get the correct output order
        SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
        servlet.service(pageContext.getRequest(), response);
        return EVAL_PAGE;
    } catch (Exception e) {
        log.error("Error while executing script " + script, e);
        throw new JspException("Error while executing script " + script, e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet)

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