use of javax.script.ScriptException 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.ScriptException in project sling by apache.
the class Module method createModuleScript.
/**
*
* @param file
* @param type
* @return
* @throws ScriptException
*/
private ModuleScript createModuleScript(Resource file, int type) throws ScriptException {
log.debug("module created. " + file.getPath());
Node currentNode = file.adaptTo(Node.class);
if (currentNode != null) {
log.debug("currentNode !) null = " + (currentNode != null));
try {
boolean isFile = currentNode.isNodeType(NodeType.NT_FILE);
log.debug("isFile: " + isFile);
if (isFile) {
return new ModuleScript(type, file);
}
log.debug("not a file " + currentNode.getMixinNodeTypes().toString());
} catch (RepositoryException ex) {
throw new ScriptException("cannot load file " + file.getPath());
}
}
return null;
}
use of javax.script.ScriptException in project sling by apache.
the class Module method readScript.
/**
*
* @param script
* @return
*/
public String readScript(Resource script) throws ScriptException {
InputStream is = script.getChild("jcr:content").adaptTo(InputStream.class);
BufferedReader esxScript = new BufferedReader(new InputStreamReader(is));
StringBuilder buffer = new StringBuilder();
String temp;
try {
while ((temp = esxScript.readLine()) != null) {
buffer.append(temp).append("\r\n");
}
return buffer.toString();
} catch (IOException ioex) {
throw new ScriptException(ioex);
}
}
use of javax.script.ScriptException in project sling by apache.
the class ScriptSandboxServiceImpl method activate.
/**
*
* @param context
*/
@Activate
protected void activate(ComponentContext context) {
try {
this.slingBabelSource = "//@sourceURL=" + getClass().getCanonicalName() + "\n load( { name : \"" + getClass().getCanonicalName() + "\", script: \"" + StringEscapeUtils.escapeEcmaScript(new String(IOUtils.toByteArray(context.getBundleContext().getBundle().getEntry(this.SLING_BABEL_SOURCE_CODE).openStream()))) + "\"} )";
} catch (Exception ex) {
log.error("failed to load babel source", ex);
}
String options = "var config = { " + " presets: [\"es2015\"], " + " compact: 'true'," + " plugins: [[\"transform-react-jsx\", { pragma : \"SlingEsx.createElement\"}]] " + //" \"plugins\": [\"SlingSandbox, [\"transform-react-jsx\", { \"pragma\" : \"SlingEsx.createElement\"}]] \n" +
" }";
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
this.scriptEngine = factory.getScriptEngine();
try {
log.info("trying to load babel into the system");
this.scriptEngine.eval(options);
this.babelOptions = this.scriptEngine.get("config");
scriptEngine.eval(slingBabelSource);
this.babel = this.scriptEngine.get("SlingBabel");
log.info("Babel loaded");
} catch (ScriptException ex) {
log.error("coudlnt load babel options", ex);
}
}
use of javax.script.ScriptException in project sling by apache.
the class Module method decorateScript.
/**
*
* @param source
* @return
* @throws ScriptException
*/
private ScriptObjectMirror decorateScript(String source, boolean es6) throws ScriptException {
String filename = (String) get("filename");
if (filename.indexOf("node_modules") == -1 && es6) {
try {
source = factory.getSandboxService().compileSource(source);
} catch (ScriptException e) {
log.error("Could not transpile script", e);
throw new ScriptException("could not load " + get("filename"));
}
}
// TODO: refactor polyfill for window, global and make require outside the wrapper as function parameter
source = "//@sourceURL=" + (String) get("filename") + "\n" + "(function (exports, Require, module, __filename," + " __dirname, currentNode, console, properties, sling, simpleResource) { " + "var window = (this.window == 'undefined' || this.window == null) ? this : this.window;" + "var global = (global == 'undefined') ? this : global;" + "function require(id) { return Require.require(id); } require.resolve = function (id) { return Require.resolve(id, currentNode.resource, 1); };" + source + "})";
// use load + filenane for older JDK versions, @sourceURL is working for latest JDK version
source = "load( { name : \"" + get("filename") + "\"," + " script: \"" + StringEscapeUtils.escapeEcmaScript(source) + "\" } )";
ScriptObjectMirror function = null;
try {
function = (ScriptObjectMirror) factory.getNashornEngine().eval(source);
if (function == null) {
log.error("Function is null !");
}
} catch (ScriptException ex) {
// todo: better handling in future
throw ex;
}
return function;
}
Aggregations