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