private static void defineProperties(Context cx, JaggeryContext context, ScriptableObject scope) { JavaScriptProperty request = new JavaScriptProperty("request"); HttpServletRequest servletRequest = (HttpServletRequest) context.getProperty(SERVLET_REQUEST); request.setValue(cx.newObject(scope, "Request", new Object[] {servletRequest})); request.setAttribute(ScriptableObject.READONLY); RhinoEngine.defineProperty(scope, request); JavaScriptProperty response = new JavaScriptProperty("response"); HttpServletResponse servletResponse = (HttpServletResponse) context.getProperty(SERVLET_RESPONSE); response.setValue(cx.newObject(scope, "Response", new Object[] {servletResponse})); response.setAttribute(ScriptableObject.READONLY); RhinoEngine.defineProperty(scope, response); JavaScriptProperty session = new JavaScriptProperty("session"); session.setValue(cx.newObject(scope, "Session", new Object[] {servletRequest.getSession()})); session.setAttribute(ScriptableObject.READONLY); RhinoEngine.defineProperty(scope, session); JavaScriptProperty application = new JavaScriptProperty("application"); ServletContext servletConext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT); application.setValue(cx.newObject(scope, "Application", new Object[] {servletConext})); application.setAttribute(ScriptableObject.READONLY); RhinoEngine.defineProperty(scope, application); if (isWebSocket(servletRequest)) { JavaScriptProperty websocket = new JavaScriptProperty("webSocket"); websocket.setValue(cx.newObject(scope, "WebSocket", new Object[0])); websocket.setAttribute(ScriptableObject.READONLY); RhinoEngine.defineProperty(scope, websocket); } }
public static JaggeryContext clonedJaggeryContext(ServletContext context) { JaggeryContext shared = sharedJaggeryContext(context); RhinoEngine engine = shared.getEngine(); Scriptable sharedScope = shared.getScope(); Context cx = Context.getCurrentContext(); ScriptableObject instanceScope = (ScriptableObject) cx.newObject(sharedScope); instanceScope.setPrototype(sharedScope); instanceScope.setParentScope(null); JaggeryContext clone = new JaggeryContext(); clone.setEngine(engine); clone.setTenantId(shared.getTenantId()); clone.setScope(instanceScope); clone.addProperty(Constants.SERVLET_CONTEXT, shared.getProperty(Constants.SERVLET_CONTEXT)); clone.addProperty(LogHostObject.LOG_LEVEL, shared.getProperty(LogHostObject.LOG_LEVEL)); clone.addProperty( FileHostObject.JAVASCRIPT_FILE_MANAGER, shared.getProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER)); clone.addProperty( Constants.JAGGERY_CORE_MANAGER, shared.getProperty(Constants.JAGGERY_CORE_MANAGER)); clone.addProperty(Constants.JAGGERY_INCLUDED_SCRIPTS, new HashMap<String, Boolean>()); clone.addProperty(Constants.JAGGERY_INCLUDES_CALLSTACK, new Stack<String>()); clone.addProperty(Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>()); CommonManager.setJaggeryContext(clone); return clone; }
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; }
public static void clearInterval( 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(); RhinoTopLevel.clearTimeout(cx, thisObj, args, funObj); List<String> taskIds = intervals.get(contextPath); taskIds.remove(String.valueOf(args[0])); }
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( CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false); } if (!(args[0] instanceof String)) { HostObjectUtil.invalidArgsError( CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false); } String moduleId = (String) args[0]; int dotIndex = moduleId.lastIndexOf("."); if (moduleId.length() == dotIndex + 1) { String msg = "Invalid file path for require method : " + moduleId; log.error(msg); throw new ScriptException(msg); } JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); Map<String, ScriptableObject> requiredModules = (Map<String, ScriptableObject>) jaggeryContext.getProperty(Constants.JAGGERY_REQUIRED_MODULES); ScriptableObject object = requiredModules.get(moduleId); if (object != null) { return object; } if (dotIndex == -1) { object = CommonManager.require(cx, thisObj, args, funObj); initModule(cx, jaggeryContext, moduleId, object); } else { object = (ScriptableObject) cx.newObject(thisObj); object.setPrototype(thisObj); object.setParentScope(null); String ext = moduleId.substring(dotIndex + 1); if (ext.equalsIgnoreCase("json")) { object = executeScript(jaggeryContext, object, moduleId, true, true, false); } else if (ext.equalsIgnoreCase("js")) { object = executeScript(jaggeryContext, object, moduleId, false, true, false); } else if (ext.equalsIgnoreCase("jag")) { object = executeScript(jaggeryContext, object, moduleId, false, false, false); } else { String msg = "Unsupported file type for require() method : ." + ext; log.error(msg); throw new ScriptException(msg); } } requiredModules.put(moduleId, object); return object; }
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; }
/** 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); if (servletResponse.getContentType() == null) { servletResponse.setContentType(DEFAULT_CONTENT_TYPE); } if (servletResponse.getCharacterEncoding() == null) { servletResponse.setCharacterEncoding(DEFAULT_CHAR_ENCODING); } CommonManager.print(cx, thisObj, args, funObj); } }
/** 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(); try { byte[] buffer = new byte[BYTE_BUFFER_SIZE]; int count; while ((count = in.read(buffer)) != -1) { out.write(buffer, 0, count); } in.close(); } catch (IOException e) { log.debug(e.getMessage(), e); throw new ScriptException(e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { log.debug(e.getMessage(), e); } } } } else { try { out.write(HostObjectUtil.serializeObject(args[0]).getBytes()); } catch (IOException e) { log.debug(e.getMessage(), e); throw new ScriptException(e); } } }
private static ScriptableObject executeScript( JaggeryContext jaggeryContext, ScriptableObject scope, String fileURL, final boolean isJSON, boolean isBuilt, boolean isIncludeOnce) throws ScriptException { Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext); Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext); ServletContext context = (ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT); String parent = includesCallstack.lastElement(); String keys[] = WebAppManager.getKeys(context.getContextPath(), parent, fileURL); fileURL = getNormalizedScriptPath(keys); if (includesCallstack.search(fileURL) != -1) { return scope; } if (isIncludeOnce && includedScripts.get(fileURL) != null) { return scope; } ScriptReader source; RhinoEngine engine = jaggeryContext.getEngine(); if (isBuilt) { source = new ScriptReader(context.getResourceAsStream(fileURL)) { @Override protected void build() throws IOException { try { if (isJSON) { sourceReader = new StringReader("(" + HostObjectUtil.streamToString(sourceIn) + ")"); } else { sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn)); } } catch (ScriptException e) { throw new IOException(e); } } }; } else { source = new ScriptReader(context.getResourceAsStream(fileURL)); } ScriptCachingContext sctx = new ScriptCachingContext(jaggeryContext.getTenantId(), keys[0], keys[1], keys[2]); sctx.setSecurityDomain(new JaggerySecurityDomain(fileURL, context)); long lastModified = WebAppManager.getScriptLastModified(context, fileURL); sctx.setSourceModifiedTime(lastModified); includedScripts.put(fileURL, true); includesCallstack.push(fileURL); if (isJSON) { scope = (ScriptableObject) engine.eval(source, scope, sctx); } else { engine.exec(source, scope, sctx); } includesCallstack.pop(); return scope; }
public static Stack<String> getCallstack(JaggeryContext jaggeryContext) { return (Stack<String>) jaggeryContext.getProperty(Constants.JAGGERY_INCLUDES_CALLSTACK); }
public static Map<String, Boolean> getIncludes(JaggeryContext jaggeryContext) { return (Map<String, Boolean>) jaggeryContext.getProperty(Constants.JAGGERY_INCLUDED_SCRIPTS); }