Search in sources :

Example 6 with JaggeryContext

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

the class WebAppManager method exec.

private static void exec(HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStream sourceIn;
    Context cx;
    JaggeryContext context;
    RhinoEngine engine = null;
    ServletContext servletContext = request.getServletContext();
    try {
        engine = CommonManager.getInstance().getEngine();
        cx = engine.enterContext();
        String scriptPath = getScriptPath(request);
        OutputStream out = response.getOutputStream();
        context = createJaggeryContext(cx, out, scriptPath, request, response);
        context.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(request.getServletContext()));
        if (ModuleManager.isModuleRefreshEnabled()) {
            //reload init scripts
            refreshServletContext(servletContext);
            InputStream jaggeryConf = servletContext.getResourceAsStream(JaggeryCoreConstants.JAGGERY_CONF_FILE);
            if (jaggeryConf != null) {
                StringWriter writer = new StringWriter();
                IOUtils.copy(jaggeryConf, writer, null);
                String jsonString = writer.toString();
                JSONObject conf = (JSONObject) JSONValue.parse(jsonString);
                JSONArray initScripts = (JSONArray) conf.get(JaggeryCoreConstants.JaggeryConfigParams.INIT_SCRIPTS);
                if (initScripts != null) {
                    JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(servletContext);
                    ScriptableObject sharedScope = sharedContext.getScope();
                    Object[] scripts = initScripts.toArray();
                    for (Object script : scripts) {
                        if (!(script instanceof String)) {
                            log.error("Invalid value for initScripts in jaggery.conf : " + script);
                            continue;
                        }
                        String path = (String) script;
                        path = path.startsWith("/") ? path : "/" + path;
                        Stack<String> callstack = CommonManager.getCallstack(sharedContext);
                        callstack.push(path);
                        engine.exec(new ScriptReader(servletContext.getResourceAsStream(path)) {

                            @Override
                            protected void build() throws IOException {
                                try {
                                    sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                                } catch (ScriptException e) {
                                    throw new IOException(e);
                                }
                            }
                        }, sharedScope, null);
                        callstack.pop();
                    }
                }
            }
        }
        Object serveFunction = request.getServletContext().getAttribute(SERVE_FUNCTION_JAGGERY);
        if (serveFunction != null) {
            Function function = (Function) serveFunction;
            ScriptableObject scope = context.getScope();
            function.call(cx, scope, scope, new Object[] { scope.get("request", scope), scope.get("response", scope), scope.get("session", scope) });
        } else {
            //resource rendering model proceeding
            sourceIn = request.getServletContext().getResourceAsStream(scriptPath);
            if (sourceIn == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
                return;
            }
            CommonManager.getInstance().getEngine().exec(new ScriptReader(sourceIn), context.getScope(), getScriptCachingContext(request, scriptPath));
        }
    } catch (ScriptException e) {
        String msg = e.getMessage();
        log.error(msg, e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
    } catch (Error e) {
        //Rhino doesn't catch Error instances and it is been used to stop the script execution
        //from any specific place. Hence, Error exception propagates up to Java and we silently
        //ignore it, assuming it was initiated by an exit() method call.
        log.debug("Script has called exit() method", e);
    } finally {
        // Exiting from the context
        if (engine != null) {
            RhinoEngine.exitContext();
        }
    }
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) ServletContext(javax.servlet.ServletContext) WebAppFileManager(org.jaggeryjs.jaggery.core.plugins.WebAppFileManager) JSONArray(org.json.simple.JSONArray) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JSONObject(org.json.simple.JSONObject) ServletContext(javax.servlet.ServletContext) JSONObject(org.json.simple.JSONObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 7 with JaggeryContext

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

the class WebAppManager 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(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);
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];
    if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
        CommonManager.include(cx, thisObj, args, funObj);
        return;
    }
    if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) {
        HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false);
    }
    ScriptableObject scope;
    if (argsCount == 2) {
        scope = (ScriptableObject) args[1];
    } else {
        scope = jaggeryContext.getScope();
    }
    executeScript(jaggeryContext, scope, fileURL, false, false, false);
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 8 with JaggeryContext

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

the class WebAppManager method setInterval.

public static String setInterval(final Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    JaggeryContext context = CommonManager.getJaggeryContext();
    ServletContext servletContext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT);
    String contextPath = servletContext.getContextPath();
    String taskId = RhinoTopLevel.setInterval(cx, thisObj, args, funObj);
    List<String> taskIds = intervals.get(contextPath);
    if (taskIds == null) {
        taskIds = new ArrayList<String>();
        intervals.put(contextPath, taskIds);
    }
    taskIds.add(taskId);
    return taskId;
}
Also used : ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 9 with JaggeryContext

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

the class WebAppManager method print.

/**
     * JaggeryMethod responsible of writing to the output stream
     */
public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    if (!isWebSocket) {
        JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
        //If the script itself havent set the content type we set the default content type to be text/html
        HttpServletResponse servletResponse = (HttpServletResponse) jaggeryContext.getProperty(SERVLET_RESPONSE);
        //a sessionDestroyedListeners
        if (servletResponse != null) {
            if (servletResponse.getContentType() == null) {
                servletResponse.setContentType(DEFAULT_CONTENT_TYPE);
            }
            if (servletResponse.getCharacterEncoding() == null) {
                servletResponse.setCharacterEncoding(DEFAULT_CHAR_ENCODING);
            }
            CommonManager.print(cx, thisObj, args, funObj);
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 10 with JaggeryContext

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

the class WebAppManager method getScriptCachingContext.

protected static ScriptCachingContext getScriptCachingContext(HttpServletRequest request, String scriptPath) throws ScriptException {
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    String tenantId = jaggeryContext.getTenantDomain();
    String[] parts = getKeys(request.getContextPath(), scriptPath, scriptPath);
    /**
         * tenantId = tenantId
         * context = webapp context
         * path = relative path to the directory of *.js file
         * cacheKey = name of the *.js file being cached
         */
    ScriptCachingContext sctx = new ScriptCachingContext(tenantId, parts[0], parts[1], parts[2]);
    ServletContext servletContext = request.getServletContext();
    sctx.setSecurityDomain(new JaggerySecurityDomain(getNormalizedScriptPath(parts), servletContext));
    long lastModified = getScriptLastModified(servletContext, scriptPath);
    sctx.setSourceModifiedTime(lastModified);
    return sctx;
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) ServletContext(javax.servlet.ServletContext) 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