Search in sources :

Example 6 with NonExistingResource

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

the class SlingBindingsVariablesListJsonServlet method getBindingsByEngine.

/**
     * Gets the {@link Bindings} object for the given {@link ScriptEngineFactory}.
     * It only considers the default context "request".
     *
     * @see <a href="https://issues.apache.org/jira/browse/SLING-3038">binding contexts(SLING-3083)</a>
     *
     * @param scriptEngineFactory the factory of the script engine, for which to retrieve the bindings
     * @param request the current request (necessary to create the bindings)
     * @param response the current response (necessary to create the bindings)
     * @return the bindings (list of key/value pairs) as defined by {@link Bindings} for the given script engine.
     * @throws IOException
     */
private Bindings getBindingsByEngine(ScriptEngineFactory scriptEngineFactory, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    // use default context only
    String context = SlingScriptAdapterFactory.BINDINGS_CONTEXT;
    final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(scriptEngineFactory, context);
    Resource invalidScriptResource = new NonExistingResource(request.getResourceResolver(), "some/invalid/scriptpath");
    DefaultSlingScript defaultSlingScript = new DefaultSlingScript(bundleContext, invalidScriptResource, scriptEngineFactory.getScriptEngine(), bindingsValuesProviders, null, null);
    // prepare the bindings (similar as in DefaultSlingScript#service)
    final SlingBindings initalBindings = new SlingBindings();
    initalBindings.setRequest(request);
    initalBindings.setResponse(response);
    final Bindings bindings = defaultSlingScript.verifySlingBindings(initalBindings);
    // only thing being added in {DefaultSlingScript#call(...)} is resource resolver
    bindings.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, request.getResourceResolver());
    return bindings;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 7 with NonExistingResource

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

the class ResourceResolverTest method testResolveRemovedesourceAliasJcrContent.

@Test
public void testResolveRemovedesourceAliasJcrContent() throws Exception {
    // define an alias for the rootPath in the jcr:content child node
    String alias = "testAlias";
    Node content = rootNode.addNode("jcr:content", "nt:unstructured");
    content.setProperty("sling:alias", alias);
    try {
        saveMappings(session);
        String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
        HttpServletRequest request = new FakeSlingHttpServletRequest(path);
        Resource res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(rootPath, res.getPath());
        assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
        assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
        assertNotNull(res.adaptTo(Node.class));
        assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html/suffix.pdf");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(rootPath, res.getPath());
        assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
        assertEquals(".print.html/suffix.pdf", res.getResourceMetadata().getResolutionPathInfo());
        assertNotNull(res.adaptTo(Node.class));
        assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + "/" + alias + ".print.html");
        res = resResolver.resolve(request, path);
        assertEquals("GET request resolution does not go up the path", Resource.RESOURCE_TYPE_NON_EXISTING, res.getResourceType());
        //remove alias property
        content.getProperty("sling:alias").remove();
        saveMappings(session);
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertTrue(res instanceof NonExistingResource);
        assertEquals("/" + alias + ".print.html", res.getPath());
    } finally {
        content.remove();
        session.save();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 8 with NonExistingResource

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveRemovedesourceAliasJcrContent.

@Test
public void testResolveRemovedesourceAliasJcrContent() throws Exception {
    // define an alias for the rootPath in the jcr:content child node
    String alias = "testAlias";
    Node content = rootNode.addNode("jcr:content", "nt:unstructured");
    content.setProperty("sling:alias", alias);
    try {
        saveMappings(session);
        String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
        HttpServletRequest request = new FakeSlingHttpServletRequest(path);
        Resource res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(rootPath, res.getPath());
        assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
        assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
        assertNotNull(res.adaptTo(Node.class));
        assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html/suffix.pdf");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(rootPath, res.getPath());
        assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
        assertEquals(".print.html/suffix.pdf", res.getResourceMetadata().getResolutionPathInfo());
        assertNotNull(res.adaptTo(Node.class));
        assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + "/" + alias + ".print.html");
        res = resResolver.resolve(request, path);
        assertEquals("GET request resolution does not go up the path", Resource.RESOURCE_TYPE_NON_EXISTING, res.getResourceType());
        //remove alias property
        content.getProperty("sling:alias").remove();
        saveMappings(session);
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertTrue(res instanceof NonExistingResource);
        assertEquals("/" + alias + ".print.html", res.getPath());
    } finally {
        content.remove();
        session.save();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 9 with NonExistingResource

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveRemovedResourceAlias.

@Test
public void testResolveRemovedResourceAlias() throws Exception {
    // define an alias for the rootPath
    String alias = "testAlias";
    rootNode.setProperty("sling:alias", alias);
    saveMappings(session);
    String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
    HttpServletRequest request = new FakeSlingHttpServletRequest(path);
    Resource res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath, res.getPath());
    assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html/suffix.pdf");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath, res.getPath());
    assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html/suffix.pdf", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
    //remove alias property
    rootNode.getProperty("sling:alias").remove();
    saveMappings(session);
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertTrue(res instanceof NonExistingResource);
    assertEquals("/" + alias + ".print.html", res.getPath());
    //create new child with alias
    String childNodeName = "rootChildAlias";
    Node childNode = maybeCreateNode(rootNode, childNodeName, "nt:unstructured");
    childNode.setProperty("sling:alias", "childAlias");
    saveMappings(session);
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + rootPath + "/childAlias.print.html");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath + "/" + childNodeName, res.getPath());
    assertEquals(childNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(childNode.isSame(res.adaptTo(Node.class)));
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + rootPath + "/childAlias" + ".print.html/suffix.pdf");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath + "/" + childNodeName, res.getPath());
    assertEquals(childNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html/suffix.pdf", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(childNode.isSame(res.adaptTo(Node.class)));
    //remove the child node with the alias
    childNode.remove();
    saveMappings(session);
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + rootPath + "/childAlias" + ".print.html");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertTrue(res instanceof NonExistingResource);
    assertEquals(rootPath + "/childAlias.print.html", res.getPath());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 10 with NonExistingResource

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

the class MergingResourcePicker method getFromBaseResource.

/**
     * @return <code>null</code> if it did not try to resolve the resource. {@link NonExistingResource} if it could not
     * find the resource.
     */
private Resource getFromBaseResource(final ResourceResolver resolver, final Resource baseResource, final String path) {
    final Resource resource;
    final String baseResourcePath = baseResource.getPath();
    // Check if the path is a child of the base resource
    if (path.startsWith(baseResourcePath + '/')) {
        String relPath = path.substring(baseResourcePath.length() + 1);
        resource = baseResource.getChild(relPath);
    } else // Check if the path is a direct parent of the base resource
    if (baseResourcePath.startsWith(path) && baseResourcePath.lastIndexOf('/') == path.length()) {
        resource = baseResource.getParent();
    } else // The two resources are not related enough, retrieval cannot be optimised
    {
        return null;
    }
    return (resource != null) ? resource : new NonExistingResource(resolver, path);
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) MergedResource(org.apache.sling.resourcemerger.impl.MergedResource)

Aggregations

NonExistingResource (org.apache.sling.api.resource.NonExistingResource)24 Resource (org.apache.sling.api.resource.Resource)22 Test (org.junit.Test)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Node (javax.jcr.Node)10 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)6 ValidationResult (org.apache.sling.validation.ValidationResult)3 ChildResource (org.apache.sling.validation.model.ChildResource)3 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)3 MergedResource (org.apache.sling.resourcemerger.impl.MergedResource)2 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)2 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)2 ValidationModel (org.apache.sling.validation.model.ValidationModel)2 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)2 Expectations (org.jmock.Expectations)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1