Search in sources :

Example 16 with SightlyException

use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.

the class ResourceRuntimeExtension method includeResource.

private void includeResource(final Bindings bindings, PrintWriter out, String path, String dispatcherOptions, String resourceType) {
    if (StringUtils.isEmpty(path)) {
        throw new SightlyException("Resource path cannot be empty");
    } else {
        SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
        Resource includeRes = request.getResourceResolver().resolve(path);
        if (ResourceUtil.isNonExistingResource(includeRes)) {
            includeRes = new SyntheticResource(request.getResourceResolver(), path, resourceType);
        }
        includeResource(bindings, out, includeRes, dispatcherOptions, resourceType);
    }
}
Also used : SightlyException(org.apache.sling.scripting.sightly.SightlyException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 17 with SightlyException

use of org.apache.sling.scripting.sightly.SightlyException in project sling by apache.

the class ResourceRuntimeExtension method includeResource.

private void includeResource(final Bindings bindings, PrintWriter out, Resource includeRes, String dispatcherOptions, String resourceType) {
    if (includeRes == null) {
        throw new SightlyException("Resource cannot be null");
    } else {
        SlingHttpServletResponse customResponse = new PrintWriterResponseWrapper(out, BindingsUtils.getResponse(bindings));
        SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
        RequestDispatcherOptions opts = new RequestDispatcherOptions(dispatcherOptions);
        if (StringUtils.isNotEmpty(resourceType)) {
            opts.setForceResourceType(resourceType);
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher(includeRes, opts);
        try {
            if (dispatcher != null) {
                dispatcher.include(request, customResponse);
            } else {
                throw new SightlyException("Failed to include resource " + includeRes.getPath());
            }
        } catch (Exception e) {
            throw new SightlyException("Failed to include resource " + includeRes.getPath(), e);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestDispatcher(javax.servlet.RequestDispatcher) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Aggregations

SightlyException (org.apache.sling.scripting.sightly.SightlyException)17 Bindings (javax.script.Bindings)6 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)6 Resource (org.apache.sling.api.resource.Resource)5 SimpleBindings (javax.script.SimpleBindings)3 SlingBindings (org.apache.sling.api.scripting.SlingBindings)3 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)3 Function (org.mozilla.javascript.Function)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ScriptException (javax.script.ScriptException)2 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)2 RenderUnit (org.apache.sling.scripting.sightly.java.compiler.RenderUnit)2 AsyncContainer (org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer)2 TimingFunction (org.apache.sling.scripting.sightly.js.impl.async.TimingFunction)2 HybridObject (org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject)2 Context (org.mozilla.javascript.Context)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1