Search in sources :

Example 56 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class CommonManager method print.

/**
     * JaggeryMethod responsible of writing to the output stream
     */
public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "print";
    JaggeryContext jaggeryContext = getJaggeryContext();
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs("RhinoTopLevel", functionName, argsCount, false);
    }
    OutputStream out = (OutputStream) jaggeryContext.getProperty(CommonManager.JAGGERY_OUTPUT_STREAM);
    if (args[0] instanceof StreamHostObject) {
        InputStream in = ((StreamHostObject) args[0]).getStream();
        if (in instanceof FileInputStream) {
            //if form file we will use channel since it's faster
            ReadableByteChannel inputChannel = null;
            WritableByteChannel outputChannel = null;
            FileChannel fc = null;
            try {
                inputChannel = Channels.newChannel(in);
                outputChannel = Channels.newChannel(out);
                fc = (FileChannel) inputChannel;
                fc.transferTo(0, fc.size(), outputChannel);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new ScriptException(e);
            } finally {
                clearResources(fc, inputChannel, outputChannel, in, out);
            }
        } else {
            try {
                IOUtils.copy(in, out);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new ScriptException(e);
            } finally {
                clearResources(in, out);
            }
        }
    } else {
        try {
            out.write(HostObjectUtil.serializeObject(args[0]).getBytes());
        } catch (IOException e) {
            if (log.isDebugEnabled()) {
                log.debug(e.getMessage(), e);
            }
            throw new ScriptException(e);
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) FileChannel(java.nio.channels.FileChannel) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 57 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException 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 58 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class JaggeryDeployerManager method processJaggeryApp.

/**
     * Process the jaggery.conf file of jaggery apps
     *
     * @param context context of the jaggery app
     */
public static void processJaggeryApp(Context context, Path appBase) {
    JSONObject jaggeryConfigObj = readJaggeryConfig(context, appBase);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setAuthConstraint(true);
    SecurityCollection securityCollection = new SecurityCollection();
    securityCollection.setName(SECURITYCOLLECTION_NAME);
    securityCollection.setDescription(SECURITYCOLLECTION_DESCRIPTION);
    securityConstraint.addCollection(securityCollection);
    initJaggeryappDefaults(context, jaggeryConfigObj, securityConstraint);
    try {
        WebAppManager.getEngine().enterContext();
        WebAppManager.deploy(context);
        setDisplayName(context, jaggeryConfigObj);
        if (jaggeryConfigObj != null) {
            addSessionCreatedListners(context, (JSONArray) jaggeryConfigObj.get(JaggeryCoreConstants.JaggeryConfigParams.SESSION_CREATED_LISTENER_SCRIPTS));
            addSessionDestroyedListners(context, (JSONArray) jaggeryConfigObj.get(JaggeryCoreConstants.JaggeryConfigParams.SESSION_DESTROYED_LISTENER_SCRIPTS));
            executeScripts(context, (JSONArray) jaggeryConfigObj.get(JaggeryCoreConstants.JaggeryConfigParams.INIT_SCRIPTS));
            addUrlMappings(context, jaggeryConfigObj);
        }
    } catch (ScriptException e) {
        log.error(e.getMessage(), e);
        try {
            context.destroy();
        } catch (LifecycleException e1) {
            log.error(e1.getMessage(), e1);
        }
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JSONObject(org.json.simple.JSONObject)

Example 59 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException 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 60 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException 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

ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)83 IOException (java.io.IOException)15 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)13 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)9 URL (java.net.URL)8 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)8 MalformedURLException (java.net.MalformedURLException)7 ServletContext (javax.servlet.ServletContext)6 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)6 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 Context (org.mozilla.javascript.Context)5 File (java.io.File)4 StringReader (java.io.StringReader)4 Callable (java.util.concurrent.Callable)4 ExecutorService (java.util.concurrent.ExecutorService)4 FileItem (org.apache.commons.fileupload.FileItem)4 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)4 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)4