Search in sources :

Example 56 with Bindings

use of javax.script.Bindings 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 57 with Bindings

use of javax.script.Bindings in project sling by apache.

the class ResourceRuntimeExtension method provideResource.

private String provideResource(final RenderContext renderContext, Object pathObj, Map<String, Object> options) {
    Map<String, Object> opts = new HashMap<>(options);
    final Bindings bindings = renderContext.getBindings();
    SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
    Map originalAttributes = ExtensionUtils.setRequestAttributes(request, (Map) options.remove(OPTION_REQUEST_ATTRIBUTES));
    RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
    String resourceType = runtimeObjectModel.toString(getAndRemoveOption(opts, OPTION_RESOURCE_TYPE));
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    if (pathObj instanceof Resource) {
        Resource includedResource = (Resource) pathObj;
        Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
        String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
        includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
    } else {
        String includePath = runtimeObjectModel.toString(pathObj);
        // build path completely
        includePath = buildPath(includePath, options, request.getResource());
        if (includePath != null) {
            // check if path identifies an existing resource
            Resource includedResource = request.getResourceResolver().getResource(includePath);
            PathInfo pathInfo;
            if (includedResource != null) {
                Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
                String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
                includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
            } else {
                // analyse path and decompose potential selectors from the path
                pathInfo = new PathInfo(includePath);
                Map<String, String> dispatcherOptionsMap = handleSelectors(request, pathInfo.selectors, opts, runtimeObjectModel);
                String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
                includeResource(bindings, printWriter, pathInfo.path, dispatcherOptions, resourceType);
            }
        } else {
            // use the current resource
            Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
            String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
            includeResource(bindings, printWriter, request.getResource(), dispatcherOptions, resourceType);
        }
    }
    ExtensionUtils.setRequestAttributes(request, originalAttributes);
    return writer.toString();
}
Also used : HashMap(java.util.HashMap) RuntimeObjectModel(org.apache.sling.scripting.sightly.render.RuntimeObjectModel) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Bindings(javax.script.Bindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 58 with Bindings

use of javax.script.Bindings in project sling by apache.

the class RenderUnit method render.

/**
     * Render the main script template
     *
     * @param out           the {@link PrintWriter} to which the commands are written
     * @param renderContext the rendering context
     * @param arguments     the arguments for this unit
     */
public final void render(PrintWriter out, RenderContext renderContext, Bindings arguments) {
    Bindings globalBindings = renderContext.getBindings();
    render(out, buildGlobalScope(globalBindings), new CaseInsensitiveBindings(arguments), renderContext);
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 59 with Bindings

use of javax.script.Bindings in project sling by apache.

the class JsEnvironment method runResource.

public void runResource(Resource scriptResource, Bindings globalBindings, Bindings arguments, UnaryCallback callback) {
    ScriptContext scriptContext = new SimpleScriptContext();
    CommonJsModule module = new CommonJsModule();
    Bindings scriptBindings = buildBindings(scriptResource, globalBindings, arguments, module);
    scriptContext.setBindings(scriptBindings, ScriptContext.ENGINE_SCOPE);
    scriptContext.setAttribute(ScriptEngine.FILENAME, scriptResource.getPath(), ScriptContext.ENGINE_SCOPE);
    runScript(scriptResource, scriptContext, callback, module);
}
Also used : CommonJsModule(org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 60 with Bindings

use of javax.script.Bindings in project java8-tutorial by winterbe.

the class Nashorn11 method test5.

private static void test5() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("var obj = { foo: 'foo' };");
    engine.eval("function printFoo() { print(obj.foo) };");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context1 = new SimpleScriptContext();
    context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context2 = new SimpleScriptContext();
    context2.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    engine.eval("obj.foo = 'bar';", context1);
    engine.eval("printFoo();", context1);
    engine.eval("printFoo();", context2);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Aggregations

Bindings (javax.script.Bindings)142 SimpleBindings (javax.script.SimpleBindings)74 Test (org.junit.Test)36 ScriptException (javax.script.ScriptException)27 ScriptContext (javax.script.ScriptContext)24 SimpleScriptContext (javax.script.SimpleScriptContext)21 SlingBindings (org.apache.sling.api.scripting.SlingBindings)18 Test (org.testng.annotations.Test)17 ScriptEngine (javax.script.ScriptEngine)15 CompiledScript (javax.script.CompiledScript)14 Resource (org.apache.sling.api.resource.Resource)11 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)7 ArrayList (java.util.ArrayList)7 PrintWriter (java.io.PrintWriter)6 ScriptEngineManager (javax.script.ScriptEngineManager)6