Search in sources :

Example 1 with BindingsValuesProvider

use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.

the class ExportServlet method addScriptBindings.

private void addScriptBindings(SlingScriptHelper scriptHelper, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    SimpleBindings bindings = new SimpleBindings();
    bindings.put(SLING, scriptHelper);
    bindings.put(RESOURCE, request.getResource());
    bindings.put(RESOLVER, request.getResource().getResourceResolver());
    bindings.put(REQUEST, request);
    bindings.put(RESPONSE, response);
    bindings.put(READER, request.getReader());
    bindings.put(OUT, response.getWriter());
    bindings.put(LOG, logger);
    final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProvidersByContext.getBindingsValuesProviders(scriptEngineFactory, BINDINGS_CONTEXT);
    if (!bindingsValuesProviders.isEmpty()) {
        Set<String> protectedKeys = new HashSet<String>();
        protectedKeys.addAll(PROTECTED_KEYS);
        ProtectedBindings protectedBindings = new ProtectedBindings(bindings, protectedKeys);
        for (BindingsValuesProvider provider : bindingsValuesProviders) {
            provider.addBindings(protectedBindings);
        }
    }
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) ProtectedBindings(org.apache.sling.scripting.core.impl.helper.ProtectedBindings) HashSet(java.util.HashSet)

Example 2 with BindingsValuesProvider

use of org.apache.sling.scripting.api.BindingsValuesProvider 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;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 3 with BindingsValuesProvider

use of org.apache.sling.scripting.api.BindingsValuesProvider 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;
}
Also used : BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) Resource(org.apache.sling.api.resource.Resource) ScriptEngine(javax.script.ScriptEngine)

Example 4 with BindingsValuesProvider

use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.

the class BindingsValuesProvidersByContextIT method addBVP.

private void addBVP(final String id, String context, String engineName) {
    final BindingsValuesProvider bvp = new BindingsValuesProvider() {

        @Override
        public String toString() {
            return id;
        }

        @Override
        public void addBindings(Bindings b) {
        }
    };
    regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp, getProperties(context, engineName)));
}
Also used : BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) Bindings(javax.script.Bindings)

Example 5 with BindingsValuesProvider

use of org.apache.sling.scripting.api.BindingsValuesProvider in project sling by apache.

the class BindingsValuesProvidersByContextIT method addBVPWithServiceRanking.

private void addBVPWithServiceRanking(final String id, String context, String engineName, int serviceRanking) {
    final BindingsValuesProvider bvp = new BindingsValuesProvider() {

        @Override
        public String toString() {
            return id;
        }

        @Override
        public void addBindings(Bindings b) {
        }
    };
    final Dictionary<String, Object> properties = getProperties(context, engineName);
    properties.put(Constants.SERVICE_RANKING, serviceRanking);
    regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp, properties));
}
Also used : BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) Bindings(javax.script.Bindings)

Aggregations

BindingsValuesProvider (org.apache.sling.scripting.api.BindingsValuesProvider)7 Bindings (javax.script.Bindings)5 Resource (org.apache.sling.api.resource.Resource)3 SlingBindings (org.apache.sling.api.scripting.SlingBindings)3 HashSet (java.util.HashSet)2 ScriptEngine (javax.script.ScriptEngine)2 SimpleBindings (javax.script.SimpleBindings)2 ProtectedBindings (org.apache.sling.scripting.core.impl.helper.ProtectedBindings)2 PrintWriter (java.io.PrintWriter)1 Map (java.util.Map)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)1 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)1 Result (org.apache.sling.hc.api.Result)1 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)1 Logger (org.slf4j.Logger)1