Search in sources :

Example 21 with JaggeryContext

use of org.jaggeryjs.scriptengine.engine.JaggeryContext 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 22 with JaggeryContext

use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.

the class FileHostObject method jsConstructor.

public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
    }
    FileHostObject fho = new FileHostObject();
    JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
    Object obj = context.getProperty(JAVASCRIPT_FILE_MANAGER);
    if (obj instanceof JavaScriptFileManager) {
        fho.manager = (JavaScriptFileManager) obj;
    } else {
        fho.manager = new JavaScriptFileManagerImpl();
    }
    fho.file = fho.manager.getJavaScriptFile(args[0]);
    fho.file.construct();
    fho.context = cx;
    return fho;
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 23 with JaggeryContext

use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.

the class FileHostObject method jsFunction_unZip.

/**
     * To unzip a zip file
     *
     * @param cx      Context
     * @param thisObj FileHostObject to be unzipped
     * @param args    Path to unzip the zip file
     * @param funObj  Function Object
     * @throws ScriptException
     */
@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
public static boolean jsFunction_unZip(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
    String functionName = "unZip";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    FileHostObject fho = (FileHostObject) thisObj;
    ZipInputStream zin = null;
    BufferedOutputStream out = null;
    if (fho.file.isExist()) {
        JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
        Object obj = context.getProperty(JAVASCRIPT_FILE_MANAGER);
        if (obj instanceof JavaScriptFileManager) {
            fho.manager = (JavaScriptFileManager) obj;
        } else {
            fho.manager = new JavaScriptFileManagerImpl();
        }
        File zipfile = new File(fho.manager.getFile(fho.file.getPath()).getAbsolutePath());
        File outdir = new File(fho.manager.getDirectoryPath(args[0].toString()));
        if (outdir.getParentFile().exists() || outdir.getParentFile().mkdirs()) {
            if (outdir.exists() || outdir.mkdir()) {
                try {
                    zin = new ZipInputStream(new FileInputStream(zipfile));
                    ZipEntry entry;
                    String name, dir;
                    byte[] buffer = new byte[1024];
                    while ((entry = zin.getNextEntry()) != null) {
                        name = entry.getName();
                        if (entry.isDirectory()) {
                            mkdirs(outdir, name);
                            continue;
                        }
                        int hasParentDirs = name.lastIndexOf(File.separatorChar);
                        dir = (hasParentDirs == -1) ? null : name.substring(0, hasParentDirs);
                        if (dir != null) {
                            mkdirs(outdir, dir);
                        }
                        try {
                            out = new BufferedOutputStream(new FileOutputStream(new File(outdir, name)));
                            int count;
                            while ((count = zin.read(buffer)) != -1) {
                                out.write(buffer, 0, count);
                            }
                        } catch (Exception ex) {
                            log.error("Unable to perform unZip operation for file : " + fho.file.getName(), ex);
                            return false;
                        } finally {
                            if (out != null) {
                                try {
                                    out.close();
                                } catch (IOException er) {
                                    log.error("Unable to close the output stream " + er);
                                }
                            }
                        }
                    }
                    return true;
                } catch (IOException ex) {
                    log.error("Cannot unzip the file " + ex);
                    throw new IOException(ex);
                } finally {
                    if (zin != null) {
                        try {
                            zin.close();
                        } catch (IOException er) {
                            log.error("Unable to close the zip input stream " + er);
                        }
                    }
                }
            } else {
                log.error("Unable to create directories to handle file : " + fho.file.getName());
            }
        } else {
            log.error("Unable to create directories to handle file : " + fho.file.getName());
        }
    } else {
        log.error("Zip file not exists");
    }
    return false;
}
Also used : ZipEntry(java.util.zip.ZipEntry) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ZipInputStream(java.util.zip.ZipInputStream) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 24 with JaggeryContext

use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.

the class FileHostObject method jsFunction_zip.

/**
     * To zip a folder
     *
     * @param cx      Context
     * @param thisObj FileHostObject
     * @param args    Zip file path to zip the folder
     * @param funObj  Function
     * @throws ScriptException
     */
@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_OUT", "PATH_TRAVERSAL_IN" })
public static boolean jsFunction_zip(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
    String functionName = "zip";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    FileHostObject fho = (FileHostObject) thisObj;
    ZipOutputStream zip = null;
    if (fho.file.isExist()) {
        JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
        Object obj = context.getProperty(JAVASCRIPT_FILE_MANAGER);
        if (obj instanceof JavaScriptFileManager) {
            fho.manager = (JavaScriptFileManager) obj;
        } else {
            fho.manager = new JavaScriptFileManagerImpl();
        }
        String destinationPath = fho.manager.getFile(args[0].toString()).getAbsolutePath();
        String sourcePath = fho.manager.getDirectoryPath(fho.file.getPath());
        File destinationFile = new File(destinationPath);
        if (destinationFile.getParentFile().exists() || destinationFile.getParentFile().mkdirs()) {
            try {
                zip = new ZipOutputStream(new FileOutputStream(destinationPath));
                File folder = new File(sourcePath);
                if (folder.list() != null) {
                    for (String fileName : folder.list()) {
                        addFileToZip("", sourcePath + File.separator + fileName, zip);
                    }
                }
                return true;
            } catch (IOException ex) {
                log.error("Cannot zip the folder. " + ex);
                throw new IOException(ex);
            } finally {
                if (zip != null) {
                    try {
                        zip.flush();
                        zip.close();
                    } catch (IOException er) {
                        log.error("Unable to close the zip output stream " + er);
                    }
                }
            }
        } else {
            log.error("Unable to create the directory path for file : " + fho.file.getName());
        }
    } else {
        log.error("Zip operation cannot be done. Folder not found");
    }
    return false;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) StreamHostObject(org.jaggeryjs.hostobjects.stream.StreamHostObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 25 with JaggeryContext

use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.

the class RequestHostObject method jsFunction_getMappedPath.

public static String jsFunction_getMappedPath(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "getMappedPath";
    int argsCount = args.length;
    if (argsCount != 0) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    RequestHostObject rho = (RequestHostObject) thisObj;
    JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
    Stack stack = (Stack) context.getProperty(LogHostObject.JAGGERY_INCLUDES_CALLSTACK);
    String path = (String) stack.firstElement();
    if (rho.request.getRequestURI().equals(path)) {
        return null;
    }
    return path;
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Aggregations

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