Пример #1
1
  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);
    }
  }
Пример #2
0
  private static void defineProperties(Context cx, JaggeryContext context, ScriptableObject scope) {
    WebAppContext ctx = (WebAppContext) context;

    JavaScriptProperty request = new JavaScriptProperty("request");
    request.setValue(cx.newObject(scope, "Request", new Object[] {ctx.getServletRequest()}));
    request.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, request);

    JavaScriptProperty response = new JavaScriptProperty("response");
    response.setValue(cx.newObject(scope, "Response", new Object[] {ctx.getServletResponse()}));
    response.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, response);

    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(
        cx.newObject(scope, "Session", new Object[] {ctx.getServletRequest().getSession()}));
    session.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, session);

    JavaScriptProperty application = new JavaScriptProperty("application");
    application.setValue(cx.newObject(scope, "Application", new Object[] {ctx.getServletConext()}));
    application.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, application);

    if (isWebSocket(ctx.getServletRequest())) {
      JavaScriptProperty websocket = new JavaScriptProperty("websocket");
      websocket.setValue(cx.newObject(scope, "WebSocket", new Object[0]));
      websocket.setAttribute(ScriptableObject.READONLY);
      RhinoEngine.defineProperty(scope, websocket);
    }
  }
Пример #3
0
  private static void exposeModule(RhinoEngine engine, JavaScriptModule module) {
    for (JavaScriptHostObject hostObject : module.getHostObjects()) {
      engine.defineHostObject(hostObject);
    }

    for (JavaScriptMethod method : module.getMethods()) {
      engine.defineMethod(method);
    }

    for (JavaScriptScript script : module.getScripts()) {
      engine.defineScript(script);
    }
  }
Пример #4
0
  private static void exposeModule(Context cx, ScriptableObject object, JavaScriptModule module)
      throws ScriptException {
    for (JavaScriptHostObject hostObject : module.getHostObjects()) {
      RhinoEngine.defineHostObject(object, hostObject);
    }

    for (JavaScriptMethod method : module.getMethods()) {
      RhinoEngine.defineMethod(object, method);
    }

    for (JavaScriptScript script : module.getScripts()) {
      script.getScript().exec(cx, object);
    }
  }
Пример #5
0
  @Override
  public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    ServletContext ctx = httpSessionEvent.getSession().getServletContext();

    List<Object> jsListeners =
        (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS);
    JaggeryContext clonedContext = WebAppManager.clonedJaggeryContext(ctx);

    RhinoEngine engine = clonedContext.getEngine();
    Context cx = engine.enterContext();

    ScriptableObject clonedScope = clonedContext.getScope();

    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(
        cx.newObject(clonedScope, "Session", new Object[] {httpSessionEvent.getSession()}));
    session.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(clonedScope, session);

    if (jsListeners != null) {
      for (Object jsListener : jsListeners) {
        CommonManager.getCallstack(clonedContext).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);
        }
      }
    }
  }
Пример #6
0
 private static void exposeDefaultModules(
     RhinoEngine engine, Map<String, JavaScriptModule> modules) throws ScriptException {
   for (JavaScriptModule module : modules.values()) {
     if (module.isExpose()) {
       String namespace = module.getNamespace();
       if (namespace == null || namespace.equals("")) {
         // expose globally
         exposeModule(engine, module);
       } else {
         engine.defineModule(module);
       }
     }
   }
 }
Пример #7
0
 public static void execute(HttpServletRequest request, HttpServletResponse response)
     throws IOException, ServletException {
   String scriptPath = getScriptPath(request);
   InputStream sourceIn = request.getServletContext().getResourceAsStream(scriptPath);
   if (sourceIn == null) {
     response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
     return;
   }
   RhinoEngine engine = null;
   try {
     engine = CommonManager.getInstance().getEngine();
     Context cx = engine.enterContext();
     // Creating an OutputStreamWritter to write content to the servletResponse
     OutputStream out = response.getOutputStream();
     JaggeryContext webAppContext = createJaggeryContext(out, scriptPath, request, response);
     initContext(cx, webAppContext);
     RhinoEngine.putContextProperty(
         FileHostObject.JAVASCRIPT_FILE_MANAGER,
         new WebAppFileManager(request.getServletContext()));
     CommonManager.getInstance()
         .getEngine()
         .exec(
             new ScriptReader(sourceIn),
             webAppContext.getScope(),
             getScriptCachingContext(request, scriptPath));
     // out.flush();
   } catch (ScriptException e) {
     String msg = e.getMessage();
     log.error(msg, e);
     response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
   } finally {
     // Exiting from the context
     if (engine != null) {
       RhinoEngine.exitContext();
     }
   }
 }
Пример #8
0
  public static void deploy(org.apache.catalina.Context context) throws ScriptException {
    ServletContext ctx = context.getServletContext();
    JaggeryContext sharedContext = new JaggeryContext();
    Context cx = Context.getCurrentContext();
    CommonManager.initContext(sharedContext);

    sharedContext.addProperty(Constants.SERVLET_CONTEXT, ctx);
    sharedContext.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(ctx));
    sharedContext.addProperty(
        Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());
    String logLevel = (String) ctx.getAttribute(LogHostObject.LOG_LEVEL);
    if (logLevel != null) {
      sharedContext.addProperty(LogHostObject.LOG_LEVEL, logLevel);
    }
    ScriptableObject sharedScope = sharedContext.getScope();
    JavaScriptProperty application = new JavaScriptProperty("application");
    application.setValue(cx.newObject(sharedScope, "Application", new Object[] {ctx}));
    application.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(sharedScope, application);
    ctx.setAttribute(SHARED_JAGGERY_CONTEXT, sharedContext);
  }
Пример #9
0
  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;
  }
Пример #10
0
 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);
   }
 }
Пример #11
0
 public static void setJaggeryContext(JaggeryContext jaggeryContext) {
   RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, jaggeryContext);
 }
Пример #12
0
 public static JaggeryContext getJaggeryContext() {
   return (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
 }