Example #1
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(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;
  }
Example #2
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;
 }
Example #3
0
  /** 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);
      }
    }
  }
Example #4
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();
     }
   }
 }
Example #5
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);
  }
Example #6
0
  public static void initContext(JaggeryContext context) throws ScriptException {
    context.setEngine(manager.engine);
    context.setScope(manager.engine.getRuntimeScope());
    context.setTenantId(
        Integer.toString(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()));

    context.addProperty(Constants.JAGGERY_CORE_MANAGER, manager);
    context.addProperty(Constants.JAGGERY_INCLUDED_SCRIPTS, new HashMap<String, Boolean>());
    context.addProperty(Constants.JAGGERY_INCLUDES_CALLSTACK, new Stack<String>());
  }
Example #7
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);
   }
 }
Example #8
0
 public static Stack<String> getCallstack(JaggeryContext jaggeryContext) {
   return (Stack<String>) jaggeryContext.getProperty(Constants.JAGGERY_INCLUDES_CALLSTACK);
 }
Example #9
0
 public static Map<String, Boolean> getIncludes(JaggeryContext jaggeryContext) {
   return (Map<String, Boolean>) jaggeryContext.getProperty(Constants.JAGGERY_INCLUDED_SCRIPTS);
 }
Example #10
0
 public static void initContext(Context cx, JaggeryContext context) throws ScriptException {
   CommonManager.initContext(context);
   defineProperties(cx, context, context.getScope());
 }
Example #11
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;
  }