use of org.apache.sling.api.resource.Resource 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.Resource 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.Resource in project sling by apache.
the class SlingScriptAdapterFactory method getAdapter.
// ---------- AdapterFactory -----------------------------------------------
@Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
Resource resource = (Resource) adaptable;
String path = resource.getPath();
String ext = path.substring(path.lastIndexOf('.') + 1);
ScriptEngine engine = scriptEngineManager.getEngineByExtension(ext);
if (engine != null) {
final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(engine.getFactory(), BINDINGS_CONTEXT);
// unchecked cast
return (AdapterType) new DefaultSlingScript(this.bundleContext, resource, engine, bindingsValuesProviders, this.serviceCache, scriptCache);
}
return null;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class AbstractDispatcherTagHandler 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("AbstractDispatcherTagHandler.doEndTag");
final SlingHttpServletRequest request = TagUtil.getRequest(pageContext);
// set request dispatcher options according to tag attributes. This
// depends on the implementation, that using a "null" argument
// has no effect
final RequestDispatcherOptions opts = new RequestDispatcherOptions();
opts.setForceResourceType(resourceType);
opts.setReplaceSelectors(replaceSelectors);
opts.setAddSelectors(addSelectors);
opts.setReplaceSuffix(replaceSuffix);
// ensure the path (if set) is absolute and normalized
if (path != null) {
if (!path.startsWith("/")) {
path = request.getResource().getPath() + "/" + path;
}
path = ResourceUtil.normalize(path);
}
// check the resource
if (resource == null) {
if (path == null) {
// neither resource nor path is defined, use current resource
resource = request.getResource();
} else {
// check whether the path (would) resolve, else SyntheticRes.
Resource tmp = request.getResourceResolver().resolve(path);
if (tmp == null && resourceType != null) {
resource = new DispatcherSyntheticResource(request.getResourceResolver(), path, resourceType);
// remove resource type overwrite as synthetic resource
// is correctly typed as requested
opts.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
}
}
}
try {
// create a dispatcher for the resource or path
RequestDispatcher dispatcher;
if (resource != null) {
dispatcher = request.getRequestDispatcher(resource, opts);
} else {
dispatcher = request.getRequestDispatcher(path, opts);
}
if (dispatcher != null) {
SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
dispatch(dispatcher, request, response);
} else {
TagUtil.log(log, pageContext, "No content to include...", null);
}
} catch (final JspTagException jte) {
throw jte;
} catch (final IOException ioe) {
throw new JspTagException(ioe);
} catch (final ServletException ce) {
throw new JspTagException(TagUtil.getRootCause(ce));
}
return EVAL_PAGE;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class DefineObjectsTag method doEndTag.
/**
* Creates Scripting variables for:
* <ul>
* <li><code>SlingHttpServletRequest</code>
* <li><code>SlingHttpServletResponse</code>
* <li>current <code>Resource</code>
* <li>current <code>Node</code> (if resource is adaptable to a node)
* <li>current <code>Logger</code>
* <li>current <code>SlingScriptHelper</code>
* </ul>
*
* @return always {@link #EVAL_PAGE}.
*/
public int doEndTag() {
final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
final SlingScriptHelper scriptHelper = bindings.getSling();
pageContext.setAttribute(requestName, scriptHelper.getRequest());
pageContext.setAttribute(responseName, scriptHelper.getResponse());
final Resource resource = scriptHelper.getRequest().getResource();
pageContext.setAttribute(resourceName, resource);
pageContext.setAttribute(resourceResolverName, scriptHelper.getRequest().getResourceResolver());
pageContext.setAttribute(slingName, scriptHelper);
pageContext.setAttribute(logName, bindings.getLog());
pageContext.setAttribute(bindingsName, bindings);
if (JCR_NODE_CLASS != null) {
final Object node = resource.adaptTo(JCR_NODE_CLASS);
if (node != null) {
pageContext.setAttribute(nodeName, node);
}
}
return EVAL_PAGE;
}
Aggregations