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;
}
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();
}
}
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();
}
}
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());
}
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);
}
Aggregations