Example #1
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 #2
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 #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 && 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 #4
0
 public static void initContext(Context cx, JaggeryContext context) throws ScriptException {
   CommonManager.initContext(context);
   defineProperties(cx, context, context.getScope());
 }