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);
}
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);
}
}
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;
}
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());
}
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);
}
Aggregations