Search in sources :

Example 6 with ScriptReader

use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.

the class CommonManager method include.

public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "include";
    int argsCount = args.length;
    if (argsCount != 1 && argsCount != 2) {
        HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) {
        HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false);
    }
    JaggeryContext jaggeryContext = getJaggeryContext();
    RhinoEngine engine = jaggeryContext.getEngine();
    if (engine == null) {
        log.error("Rhino Engine in Jaggery context is null");
        throw new ScriptException("Rhino Engine in Jaggery context is null");
    }
    Stack<String> includesCallstack = getCallstack(jaggeryContext);
    Map<String, Boolean> includedScripts = getIncludes(jaggeryContext);
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];
    if (isHTTP(fileURL) || isHTTP(parent)) {
        if (!isHTTP(fileURL)) {
            fileURL = parent + fileURL;
        }
        if (includesCallstack.search(fileURL) != -1) {
            return;
        }
        ScriptReader source;
        ScriptableObject scope;
        if (argsCount == 2) {
            scope = (ScriptableObject) args[1];
        } else {
            scope = jaggeryContext.getScope();
        }
        //this is a remote file url
        try {
            URL url = new URL(fileURL);
            url.openConnection();
            source = new ScriptReader(url.openStream());
            includedScripts.put(fileURL, true);
            includesCallstack.push(fileURL);
            engine.exec(source, scope, null);
            includesCallstack.pop();
        } catch (MalformedURLException e) {
            String msg = "Malformed URL. function : import, url : " + fileURL;
            log.warn(msg, e);
            throw new ScriptException(msg, e);
        } catch (IOException e) {
            String msg = "IO exception while importing content from url : " + fileURL;
            log.warn(msg, e);
            throw new ScriptException(msg, e);
        }
    } else {
        String msg = "Unsupported file include : " + fileURL;
        throw new ScriptException(msg);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ScriptableObject(org.mozilla.javascript.ScriptableObject) URL(java.net.URL) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 7 with ScriptReader

use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.

the class CommonManager method include_once.

public static void include_once(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "include_once";
    int argsCount = args.length;
    if (argsCount != 1 && argsCount != 2) {
        HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) {
        HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false);
    }
    JaggeryContext jaggeryContext = getJaggeryContext();
    RhinoEngine engine = jaggeryContext.getEngine();
    if (engine == null) {
        log.error("Rhino Engine in Jaggery context is null");
        throw new ScriptException("Rhino Engine in Jaggery context is null");
    }
    Stack<String> includesCallstack = getCallstack(jaggeryContext);
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];
    if (isHTTP(fileURL) || isHTTP(parent)) {
        if (!isHTTP(fileURL)) {
            fileURL = parent + fileURL;
        }
        if (includesCallstack.search(fileURL) != -1) {
            return;
        }
        Map<String, Boolean> includedScripts = getIncludes(jaggeryContext);
        if (includedScripts.get(fileURL)) {
            return;
        }
        ScriptReader source;
        ScriptableObject scope;
        if (argsCount == 2) {
            scope = (ScriptableObject) args[1];
        } else {
            scope = jaggeryContext.getScope();
        }
        //this is a remote file url
        try {
            URL url = new URL(fileURL);
            url.openConnection();
            source = new ScriptReader(url.openStream());
            includedScripts.put(fileURL, true);
            includesCallstack.push(fileURL);
            engine.exec(source, scope, null);
            includesCallstack.pop();
        } catch (MalformedURLException e) {
            String msg = "Malformed URL. function : import, url : " + fileURL;
            log.warn(msg, e);
            throw new ScriptException(msg, e);
        } catch (IOException e) {
            String msg = "IO exception while importing content from url : " + fileURL;
            log.warn(msg, e);
            throw new ScriptException(msg, e);
        }
    } else {
        String msg = "Unsupported file include : " + fileURL;
        throw new ScriptException(msg);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ScriptableObject(org.mozilla.javascript.ScriptableObject) URL(java.net.URL) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 8 with ScriptReader

use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.

the class WebAppSessionListener method sessionCreated.

@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
    ServletContext ctx = httpSessionEvent.getSession().getServletContext();
    List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_CREATED_LISTENERS);
    if (jsListeners == null) {
        return;
    }
    JaggeryContext shared = WebAppManager.sharedJaggeryContext(ctx);
    Context cx = shared.getEngine().enterContext();
    JaggeryContext context = CommonManager.getJaggeryContext();
    if (CommonManager.getJaggeryContext() == null) {
        context = WebAppManager.clonedJaggeryContext(ctx);
        CommonManager.setJaggeryContext(context);
    }
    RhinoEngine engine = context.getEngine();
    ScriptableObject clonedScope = context.getScope();
    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(cx.newObject(clonedScope, "Session", new Object[] { httpSessionEvent.getSession() }));
    session.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(clonedScope, session);
    for (Object jsListener : jsListeners) {
        CommonManager.getCallstack(context).push((String) jsListener);
        try {
            ScriptReader sr = new ScriptReader(ctx.getResourceAsStream((String) jsListener)) {

                @Override
                protected void build() throws IOException {
                    try {
                        sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                    } catch (ScriptException e) {
                        throw new IOException(e);
                    }
                }
            };
            engine.exec(sr, clonedScope, null);
        } catch (ScriptException e) {
            log.error(e.getMessage(), e);
        } finally {
            CommonManager.getCallstack(context).pop();
        }
    }
    Context.exit();
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) Context(org.mozilla.javascript.Context) ServletContext(javax.servlet.ServletContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) IOException(java.io.IOException) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JavaScriptProperty(org.jaggeryjs.scriptengine.engine.JavaScriptProperty) StringReader(java.io.StringReader) ServletContext(javax.servlet.ServletContext) List(java.util.List) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 9 with ScriptReader

use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.

the class CommandLineManager method include_once.

@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public static void include_once(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "include_once";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
    Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];
    if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
        CommonManager.include_once(cx, thisObj, args, funObj);
        return;
    }
    ScriptableObject scope = jaggeryContext.getScope();
    RhinoEngine engine = jaggeryContext.getEngine();
    if (fileURL.startsWith("/")) {
        fileURL = includesCallstack.firstElement() + fileURL;
    } else {
        fileURL = FilenameUtils.getFullPath(parent) + fileURL;
    }
    fileURL = FilenameUtils.normalize(fileURL);
    if (includesCallstack.search(fileURL) != -1) {
        return;
    }
    if (includedScripts.get(fileURL) != null) {
        return;
    }
    try {
        ScriptReader source = new ScriptReader(new FileInputStream(fileURL));
        includedScripts.put(fileURL, true);
        includesCallstack.push(fileURL);
        engine.exec(source, scope, null);
        includesCallstack.pop();
    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 10 with ScriptReader

use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.

the class CommandLineManager method include.

@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "include";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
    Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];
    if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
        CommonManager.include(cx, thisObj, args, funObj);
        return;
    }
    ScriptableObject scope = jaggeryContext.getScope();
    RhinoEngine engine = jaggeryContext.getEngine();
    if (fileURL.startsWith("/")) {
        fileURL = includesCallstack.firstElement() + fileURL;
    } else {
        fileURL = FilenameUtils.getFullPath(parent) + fileURL;
    }
    fileURL = FilenameUtils.normalize(fileURL);
    if (includesCallstack.search(fileURL) != -1) {
        return;
    }
    ScriptReader source = null;
    try {
        source = new ScriptReader(new FileInputStream(fileURL));
        includedScripts.put(fileURL, true);
        includesCallstack.push(fileURL);
        engine.exec(source, scope, null);
        includesCallstack.pop();
    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)10 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)9 ScriptableObject (org.mozilla.javascript.ScriptableObject)8 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)6 ServletContext (javax.servlet.ServletContext)5 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)5 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 List (java.util.List)2 LogHostObject (org.jaggeryjs.hostobjects.log.LogHostObject)2 JavaScriptProperty (org.jaggeryjs.scriptengine.engine.JavaScriptProperty)2 JSONObject (org.json.simple.JSONObject)2 Context (org.mozilla.javascript.Context)2 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)1 WebAppFileManager (org.jaggeryjs.jaggery.core.plugins.WebAppFileManager)1 JSONArray (org.json.simple.JSONArray)1