Search in sources :

Example 31 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject 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 32 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject 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 33 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project jaggery by wso2.

the class CommonManager method require.

public static ScriptableObject require(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
    String functionName = "require";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    String moduleName = (String) args[0];
    JaggeryContext context = getJaggeryContext();
    //RhinoEngine engine = context.getEngine();
    //ScriptableObject scope = context.getScope();
    CommonManager manager = (CommonManager) context.getProperty(Constants.JAGGERY_CORE_MANAGER);
    ModuleManager moduleManager = manager.getModuleManager();
    JavaScriptModule module = moduleManager.getModule(moduleName);
    if (module == null) {
        String msg = "A module cannot be found with the specified name : " + moduleName;
        log.error(msg);
        throw new ScriptException(msg);
    }
    ScriptableObject object = (ScriptableObject) cx.newObject(thisObj);
    object.setPrototype(thisObj);
    object.setParentScope(thisObj);
    exposeModule(cx, object, module);
    return object;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 34 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject 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 35 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject 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)

Aggregations

ScriptableObject (org.mozilla.javascript.ScriptableObject)44 Context (org.mozilla.javascript.Context)20 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)8 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)8 ContextAction (org.mozilla.javascript.ContextAction)6 ContextFactory (org.mozilla.javascript.ContextFactory)6 Scriptable (org.mozilla.javascript.Scriptable)6 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)5 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 IOException (java.io.IOException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 ServletContext (javax.servlet.ServletContext)3 Function (org.mozilla.javascript.Function)3 Function (com.google.common.base.Function)2 PrintWriter (java.io.PrintWriter)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2