Search in sources :

Example 6 with BindingsValuesProvider

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

the class DefaultSlingScript method verifySlingBindings.

Bindings verifySlingBindings(final SlingBindings slingBindings) throws IOException {
    final Bindings bindings = new SimpleBindings();
    final SlingHttpServletRequest request = slingBindings.getRequest();
    // check sling object
    Object slingObject = slingBindings.get(SLING);
    if (slingObject == null) {
        if (request != null) {
            slingObject = new InternalScriptHelper(this.bundleContext, this, request, slingBindings.getResponse(), this.cache);
        } else {
            slingObject = new InternalScriptHelper(this.bundleContext, this, this.cache);
        }
    } else if (!(slingObject instanceof SlingScriptHelper)) {
        throw fail(SLING, "Wrong type");
    }
    final SlingScriptHelper sling = (SlingScriptHelper) slingObject;
    bindings.put(SLING, sling);
    if (request != null) {
        final SlingHttpServletResponse response = slingBindings.getResponse();
        if (response == null) {
            throw fail(RESPONSE, "Missing or wrong type");
        }
        Object resourceObject = slingBindings.get(RESOURCE);
        if (resourceObject != null && !(resourceObject instanceof Resource)) {
            throw fail(RESOURCE, "Wrong type");
        }
        Object resolverObject = slingBindings.get(RESOLVER);
        if (resolverObject != null && !(resolverObject instanceof ResourceResolver)) {
            throw fail(RESOLVER, "Wrong type");
        }
        Object writerObject = slingBindings.get(OUT);
        if (writerObject != null && !(writerObject instanceof PrintWriter)) {
            throw fail(OUT, "Wrong type");
        }
        // if there is a provided sling script helper, check arguments
        if (slingBindings.get(SLING) != null) {
            if (sling.getRequest() != request) {
                throw fail(REQUEST, "Not the same as request field of SlingScriptHelper");
            }
            if (sling.getResponse() != response) {
                throw fail(RESPONSE, "Not the same as response field of SlingScriptHelper");
            }
            if (resourceObject != null && sling.getRequest().getResource() != resourceObject) {
                throw fail(RESOURCE, "Not the same as resource of the SlingScriptHelper request");
            }
            if (resolverObject != null && sling.getRequest().getResourceResolver() != resolverObject) {
                throw fail(RESOLVER, "Not the same as the resource resolver of the SlingScriptHelper request's resolver");
            }
            if (writerObject != null && sling.getResponse().getWriter() != writerObject) {
                throw fail(OUT, "Not the same as writer of the SlingScriptHelper response");
            }
        }
        // set base variables when executing inside a request
        bindings.put(REQUEST, sling.getRequest());
        bindings.put(READER, sling.getRequest().getReader());
        bindings.put(RESPONSE, sling.getResponse());
        bindings.put(RESOURCE, sling.getRequest().getResource());
        bindings.put(RESOLVER, sling.getRequest().getResourceResolver());
        bindings.put(OUT, sling.getResponse().getWriter());
    }
    Object logObject = slingBindings.get(LOG);
    if (logObject == null) {
        logObject = LoggerFactory.getLogger(getLoggerName());
    } else if (!(logObject instanceof Logger)) {
        throw fail(LOG, "Wrong type");
    }
    bindings.put(LOG, logObject);
    // copy non-base variables
    for (Map.Entry<String, Object> entry : slingBindings.entrySet()) {
        if (!bindings.containsKey(entry.getKey())) {
            bindings.put(entry.getKey(), entry.getValue());
        }
    }
    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);
        }
    }
    return bindings;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Logger(org.slf4j.Logger) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ProtectedBindings(org.apache.sling.scripting.core.impl.helper.ProtectedBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SimpleBindings(javax.script.SimpleBindings) ProtectedBindings(org.apache.sling.scripting.core.impl.helper.ProtectedBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Map(java.util.Map) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 7 with BindingsValuesProvider

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

the class ScriptableHealthCheck method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    resultLog.debug("Checking expression [{}], language extension=[{}]", expression, languageExtension);
    try {
        final ScriptEngine engine = scriptEngineManager.getEngineByExtension(languageExtension);
        if (engine == null) {
            resultLog.healthCheckError("No ScriptEngine available for extension {}", languageExtension);
        } else {
            // Set Bindings, with our ResultLog as a binding first, so that other bindings can use it
            final Bindings b = engine.createBindings();
            b.put(FormattingResultLog.class.getName(), resultLog);
            synchronized (bindingsValuesProviders) {
                for (BindingsValuesProvider bvp : bindingsValuesProviders) {
                    log.debug("Adding Bindings provided by {}", bvp);
                    bvp.addBindings(b);
                }
            }
            log.debug("All Bindings added: {}", b.keySet());
            final Object value = engine.eval(expression, b);
            if (value != null && "true".equals(value.toString().toLowerCase())) {
                resultLog.debug("Expression [{}] evaluates to true as expected", expression);
            } else {
                resultLog.warn("Expression [{}] does not evaluate to true as expected, value=[{}]", expression, value);
            }
        }
    } catch (final Exception e) {
        resultLog.healthCheckError("Exception while evaluating expression [{}] with language extension [{}]: {}", expression, languageExtension, e);
    }
    return new Result(resultLog);
}
Also used : BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) Result(org.apache.sling.hc.api.Result)

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