Esempio n. 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(
          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 param = (String) args[0];
    int dotIndex = param.lastIndexOf(".");
    if (param.length() == dotIndex + 1) {
      String msg = "Invalid file path for require method : " + param;
      log.error(msg);
      throw new ScriptException(msg);
    }

    if (dotIndex == -1) {
      ScriptableObject object = CommonManager.require(cx, thisObj, args, funObj);
      initModule(cx, CommonManager.getJaggeryContext(), param, object);
      return object;
    }

    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    ScriptableObject object = (ScriptableObject) cx.newObject(thisObj);
    object.setPrototype(thisObj);
    object.setParentScope(null);
    String ext = param.substring(dotIndex + 1);
    if (ext.equalsIgnoreCase("json")) {
      return executeScript(jaggeryContext, object, param, true, true, false);
    } else if (ext.equalsIgnoreCase("js")) {
      return executeScript(jaggeryContext, object, param, false, true, false);
    } else if (ext.equalsIgnoreCase("jag")) {
      return executeScript(jaggeryContext, object, param, false, false, false);
    } else {
      String msg = "Unsupported file type for require() method : ." + ext;
      log.error(msg);
      throw new ScriptException(msg);
    }
  }
Esempio n. 2
0
 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]));
 }
Esempio n. 3
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;
  }
Esempio n. 4
0
  /** 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
      if (((WebAppContext) jaggeryContext).getServletResponse().getContentType() == null) {
        ((WebAppContext) CommonManager.getJaggeryContext())
            .getServletResponse()
            .setContentType(DEFAULT_CONTENT_TYPE);
      }

      if (((WebAppContext) jaggeryContext).getServletResponse().getCharacterEncoding() == null) {
        ((WebAppContext) CommonManager.getJaggeryContext())
            .getServletResponse()
            .setCharacterEncoding(DEFAULT_CHAR_ENCODING);
      }

      CommonManager.print(cx, thisObj, args, funObj);
    }
  }
Esempio n. 5
0
 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;
 }
Esempio n. 6
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;
 }
Esempio n. 7
0
  /** 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);
    }
  }
Esempio n. 8
0
  public static void include_once(Context cx, Scriptable thisObj, Object[] args, Function funObj)
      throws ScriptException {
    String functionName = "include_once";
    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 = CommonManager.getCallstack(jaggeryContext);
    String parent = includesCallstack.lastElement();
    String fileURL = (String) args[0];

    if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
      CommonManager.include_once(cx, thisObj, args, funObj);
      return;
    }
    executeScript(jaggeryContext, jaggeryContext.getScope(), fileURL, false, false, true);
  }