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);
}
use of javax.script.Bindings in project JMRI by JMRI.
the class Jdk9Application method getContext.
private ScriptContext getContext(Object handler) {
Bindings bindings = new SimpleBindings();
// NOI18N
bindings.put("handler", handler);
ScriptContext context = new SimpleScriptContext();
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
return context;
}
use of javax.script.Bindings in project sling by apache.
the class ThymeleafScriptEngine method eval.
@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
final SlingHttpServletRequest request = helper.getRequest();
final SlingHttpServletResponse response = helper.getResponse();
// only used by Thymeleaf's ServletContextResourceResolver (TODO check if still true for 3.0)
final ServletContext servletContext = null;
final Locale locale = helper.getResponse().getLocale();
final String scriptName = helper.getScript().getScriptResource().getPath();
final Writer writer = scriptContext.getWriter();
try {
final ResourceResolver resourceResolver = thymeleafScriptEngineFactory.getRequestScopedResourceResolver();
final IContext context = new SlingWebContext(request, response, servletContext, resourceResolver, locale, bindings);
thymeleafScriptEngineFactory.getTemplateEngine().process(scriptName, context, writer);
} catch (Exception e) {
logger.error("Failure rendering Thymeleaf template '{}': {}", scriptName, e.getMessage());
throw new ScriptException(e);
}
return null;
}
use of javax.script.Bindings in project sling by apache.
the class FreemarkerScriptEngine method eval.
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
// ensure GET request
if (!"GET".equals(helper.getRequest().getMethod())) {
throw new ScriptException("FreeMarker templates only support GET requests");
}
freemarkerScriptEngineFactory.getTemplateModels().forEach(bindings::put);
final String scriptName = helper.getScript().getScriptResource().getPath();
try {
final Template template = new Template(scriptName, reader, configuration);
template.process(bindings, scriptContext.getWriter());
} catch (Throwable t) {
final String message = String.format("Failure processing FreeMarker template %s.", scriptName);
logger.error(message, t);
throw new ScriptException(message);
}
return null;
}
use of javax.script.Bindings in project sling by apache.
the class GSPScriptEngine method eval.
public Object eval(Reader reader, ScriptContext ctx) throws ScriptException {
Template template = null;
try {
template = templateEngine.createTemplate(reader);
} catch (IOException e) {
throw new ScriptException("Unable to compile GSP script: " + e.getMessage());
} catch (ClassNotFoundException e) {
throw new ScriptException("Unable to compile GSP script: " + e.getMessage());
}
Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
Writable result = template.make(bindings);
try {
result.writeTo(ctx.getWriter());
} catch (IOException e) {
throw new ScriptException("Unable to write result of script execution: " + e.getMessage());
}
return null;
}
Aggregations