Search in sources :

Example 36 with ScriptException

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;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Locale(java.util.Locale) IContext(org.thymeleaf.context.IContext) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ServletContext(javax.servlet.ServletContext) Writer(java.io.Writer)

Example 37 with ScriptException

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;
}
Also used : ScriptException(javax.script.ScriptException) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException)

Example 38 with ScriptException

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);
    }
}
Also used : ScriptException(javax.script.ScriptException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 39 with ScriptException

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);
    }
}
Also used : NashornScriptEngineFactory(jdk.nashorn.api.scripting.NashornScriptEngineFactory) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) ScriptException(javax.script.ScriptException) Activate(org.apache.felix.scr.annotations.Activate)

Example 40 with ScriptException

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;
}
Also used : ScriptException(javax.script.ScriptException) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror)

Aggregations

ScriptException (javax.script.ScriptException)106 ScriptEngine (javax.script.ScriptEngine)42 IOException (java.io.IOException)41 Bindings (javax.script.Bindings)30 ScriptEngineManager (javax.script.ScriptEngineManager)20 InputStreamReader (java.io.InputStreamReader)12 CompiledScript (javax.script.CompiledScript)12 Map (java.util.Map)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)11 SimpleBindings (javax.script.SimpleBindings)10 File (java.io.File)8 Reader (java.io.Reader)7 Writer (java.io.Writer)7 HashMap (java.util.HashMap)7 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5