Пример #1
0
 protected static ScriptCachingContext getScriptCachingContext(
     HttpServletRequest request, String scriptPath) throws ScriptException {
   JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
   String tenantId = jaggeryContext.getTenantId();
   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;
 }
Пример #2
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();
     }
   }
 }
Пример #3
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) {
      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 = jaggeryContext.getIncludesCallstack();
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];

    if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
      CommonManager.include(cx, thisObj, args, funObj);
      return;
    }
    executeScript(jaggeryContext, jaggeryContext.getScope(), fileURL, false, false, false);
  }
Пример #4
0
 public static void initContext(Context cx, JaggeryContext context) throws ScriptException {
   CommonManager.initContext(context);
   defineProperties(cx, context, context.getScope());
 }
Пример #5
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;
  }