Search in sources :

Example 6 with SightlyException

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

the class RenderContextImpl method call.

@Override
public Object call(String functionName, Object... arguments) {
    Map<String, RuntimeExtension> extensions = extensionRegistryService.extensions();
    RuntimeExtension extension = extensions.get(functionName);
    if (extension == null) {
        throw new SightlyException("Runtime extension is not available: " + functionName);
    }
    return extension.call(this, arguments);
}
Also used : RuntimeExtension(org.apache.sling.scripting.sightly.extension.RuntimeExtension) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 7 with SightlyException

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

the class SightlyCompiledScript method eval.

@Override
public Object eval(ScriptContext context) throws ScriptException {
    Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    SlingHttpServletRequest request = slingBindings.getRequest();
    if (request == null) {
        throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
    }
    Object oldBindings = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        RenderContext renderContext = new RenderContextImpl(context);
        PrintWriter out = new PrintWriter(context.getWriter());
        renderUnit.render(out, renderContext, new SimpleBindings());
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldBindings);
    }
    return null;
}
Also used : RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings) RenderContextImpl(org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 8 with SightlyException

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

the class IncludeRuntimeExtension method includeScript.

private void includeScript(final Bindings bindings, String script, PrintWriter out) {
    if (StringUtils.isEmpty(script)) {
        throw new SightlyException("Path for data-sly-include is empty");
    } else {
        LOG.debug("Attempting to include script {}.", script);
        SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
        ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class);
        if (servletResolver != null) {
            SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
            Servlet servlet = servletResolver.resolveServlet(request.getResource(), script);
            if (servlet != null) {
                try {
                    SlingHttpServletResponse response = BindingsUtils.getResponse(bindings);
                    PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response);
                    servlet.service(request, resWrapper);
                } catch (Exception e) {
                    throw new SightlyException("Failed to include script " + script, e);
                }
            } else {
                throw new SightlyException("Failed to locate script " + script);
            }
        } else {
            throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletResolver(org.apache.sling.api.servlets.ServletResolver) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Servlet(javax.servlet.Servlet) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 9 with SightlyException

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

the class DependencyResolver method resolve.

/**
     * Resolve a dependency
     * @param dependency the dependency identifier
     * @param callback the callback that will receive the resolved dependency
     */
public void resolve(String dependency, UnaryCallback callback) {
    if (!Utils.isJsScript(dependency)) {
        throw new SightlyException("Only JS scripts are allowed as dependencies. Invalid dependency: " + dependency);
    }
    Resource scriptResource = Utils.getScriptResource(caller, dependency, globalBindings);
    jsEnvironment.runResource(scriptResource, globalBindings, Utils.EMPTY_BINDINGS, callback);
}
Also used : SightlyException(org.apache.sling.scripting.sightly.SightlyException) Resource(org.apache.sling.api.resource.Resource)

Example 10 with SightlyException

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

the class AsyncExtractor method decodeJSPromise.

private void decodeJSPromise(final Scriptable promise, final UnaryCallback callback) {
    try {
        Context context = Context.enter();
        final AsyncContainer errorContainer = new AsyncContainer();
        final Function errorHandler = createErrorHandler(errorContainer);
        final Function successHandler = convertCallback(callback);
        EventLoopInterop.schedule(context, new Runnable() {

            @Override
            public void run() {
                ScriptableObject.callMethod(promise, THEN_METHOD, new Object[] { successHandler, errorHandler });
            }
        });
        if (errorContainer.isCompleted()) {
            throw new SightlyException("Promise has completed with failure: " + Context.toString(errorContainer.getResult()));
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function) SightlyException(org.apache.sling.scripting.sightly.SightlyException) ScriptableObject(org.mozilla.javascript.ScriptableObject)

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