Ejemplo n.º 1
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);
    }
  }
Ejemplo n.º 2
0
  private static ScriptableObject executeScript(
      JaggeryContext jaggeryContext,
      ScriptableObject scope,
      String fileURL,
      final boolean isJSON,
      boolean isBuilt,
      boolean isIncludeOnce)
      throws ScriptException {
    WebAppContext webAppContext = (WebAppContext) jaggeryContext;
    Stack<String> includesCallstack = jaggeryContext.getIncludesCallstack();
    Map<String, Boolean> includedScripts = jaggeryContext.getIncludedScripts();
    ServletContext context = webAppContext.getServletConext();
    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(webAppContext.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;
  }