Search in sources :

Example 1 with SlingScript

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

the class ScriptUseProvider method evalScript.

private ProviderOutcome evalScript(Resource scriptResource, Bindings bindings) {
    SlingScript slingScript = scriptResource.adaptTo(SlingScript.class);
    if (slingScript == null) {
        return ProviderOutcome.failure();
    }
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    Object scriptEval = slingScript.eval(slingBindings);
    return ProviderOutcome.notNullOrFailure(scriptEval);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScript(org.apache.sling.api.scripting.SlingScript)

Example 2 with SlingScript

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

the class ScriptConsolePlugin method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final String contentType = getContentType(req);
    resp.setContentType(contentType);
    if (contentType.startsWith("text/")) {
        resp.setCharacterEncoding("UTF-8");
    }
    final String script = getCodeValue(req);
    final SlingBindings bindings = new SlingBindings();
    final PrintWriter pw = resp.getWriter();
    //Populate bindings
    bindings.put(SlingBindings.REQUEST, req);
    bindings.put(SlingBindings.READER, new StringReader(script));
    bindings.put(SlingBindings.RESPONSE, resp);
    bindings.put(SlingBindings.OUT, pw);
    //Also expose the bundleContext to simplify scripts interaction with the
    //enclosing OSGi container
    bindings.put("bundleContext", bundleContext);
    final String lang = WebConsoleUtil.getParameter(req, "lang");
    final Resource resource = new RuntimeScriptResource(lang, script);
    final boolean webClient = "webconsole".equals(WebConsoleUtil.getParameter(req, "client"));
    SlingScript slingScript = resource.adaptTo(SlingScript.class);
    try {
        log.debug("Executing script {}", script);
        slingScript.eval(bindings);
    } catch (Throwable t) {
        if (!webClient) {
            resp.setStatus(500);
        }
        pw.println(exceptionToString(t));
        log.warn("Error in executing script", t);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScript(org.apache.sling.api.scripting.SlingScript) StringReader(java.io.StringReader) Resource(org.apache.sling.api.resource.Resource) PrintWriter(java.io.PrintWriter)

Example 3 with SlingScript

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

the class JavaScriptEngineFactory method getWrapperAdapter.

private ServletWrapper getWrapperAdapter(final SlingScriptHelper scriptHelper) throws SlingException {
    SlingScript script = scriptHelper.getScript();
    final String scriptName = script.getScriptResource().getPath();
    ServletWrapper wrapper = this.ioProvider.getServletCache().getWrapper(scriptName);
    if (wrapper != null) {
        return wrapper;
    }
    wrapper = new ServletWrapper(servletConfig, ioProvider, scriptName, scriptHelper);
    wrapper = this.ioProvider.getServletCache().addWrapper(scriptName, wrapper);
    return wrapper;
}
Also used : SlingScript(org.apache.sling.api.scripting.SlingScript)

Example 4 with SlingScript

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

the class StandaloneScriptExecutionServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final SlingScript script = request.getResource().adaptTo(SlingScript.class);
    if (script == null) {
        throw new ServletException("Resource does not adapt to a SlingScript:" + request.getResource().getPath());
    }
    // Execute the script without providing a request or response, in the simplest possible way
    final SlingBindings bindings = new SlingBindings();
    final StringWriter sw = new StringWriter();
    bindings.put("StandaloneScriptExecutionServletOutput", sw);
    script.eval(bindings);
    response.setContentType("text/plain");
    response.getWriter().write(sw.toString());
}
Also used : ServletException(javax.servlet.ServletException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) StringWriter(java.io.StringWriter) SlingScript(org.apache.sling.api.scripting.SlingScript)

Example 5 with SlingScript

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

the class JspScriptEngineFactory method getJspWrapper.

private JspServletWrapper getJspWrapper(final SlingScriptHelper scriptHelper, final SlingBindings bindings) throws SlingException {
    final SlingScript script = scriptHelper.getScript();
    final String scriptName = script.getScriptResource().getPath();
    return getJspWrapper(scriptName, bindings);
}
Also used : SlingScript(org.apache.sling.api.scripting.SlingScript)

Aggregations

SlingScript (org.apache.sling.api.scripting.SlingScript)5 SlingBindings (org.apache.sling.api.scripting.SlingBindings)3 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 ServletException (javax.servlet.ServletException)1 Resource (org.apache.sling.api.resource.Resource)1