Ejemplo n.º 1
0
 /**
  * Evaluates a script.
  *
  * @param script string representation of the script to evaluate.
  * @param sourceName the name of the evaluated script.
  * @return evaluated object.
  * @throws IOException if the script couldn't be retrieved.
  */
 public Object evaluate(final String script, final String sourceName) {
   if (script == null) {
     throw new IllegalArgumentException("script cannot be null");
   }
   try {
     return context.evaluateString(scope, script, sourceName, 1, null);
   } catch (final JavaScriptException e) {
     LOG.error("JavaScriptException occured: " + e.getMessage());
     throw e;
   }
 }
Ejemplo n.º 2
0
 int getStatus(Exception e) {
   if (e instanceof JavaScriptException) {
     JavaScriptException je = (JavaScriptException) e;
     Object value = ((ScriptableObject) je.getValue()).get("message", null);
     if (value != null) {
       String status = Utility.find(value.toString(), "^\\d\\d\\d\\b");
       if (status != null) return Integer.parseInt(status);
     }
   }
   return 500;
 }
Ejemplo n.º 3
0
 /**
  * Evaluates a script from a reader.
  *
  * @param reader {@link Reader} of the script to evaluate.
  * @param sourceName the name of the evaluated script.
  * @return evaluated object.
  * @throws IOException if the script couldn't be retrieved.
  */
 public Object evaluate(final Reader reader, final String sourceName) throws IOException {
   if (reader == null) {
     throw new IllegalArgumentException("reader cannot be null");
   }
   try {
     return context.evaluateReader(scope, reader, sourceName, 1, null);
   } catch (final JavaScriptException e) {
     LOG.error("JavaScriptException occured: " + e.getMessage());
     throw e;
   } finally {
     reader.close();
   }
 }
Ejemplo n.º 4
0
  @Override
  public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");

    String path = request.getRequestURI();
    // use routes if present
    if (this.routes != null) {
      String route = this.routes.getProperty(path);
      if (route != null) {
        path = route;
        // then we also need to replace the HttpServletRequest.getRequestURI method
        // request = getRequestWrapper(request, path);
      }
    }

    if (path.endsWith("/")) path += "index";
    String source = null;
    // check for changes first
    if (this.debug && contextCache.checkForChanges()) {
      if (this.pageInfoCache != null) this.pageInfoCache.clear();
    }
    ScriptContext context = contextCache.getContext();
    try {
      serve(request, response, context, path, null);
    } catch (Exception e) {
      int status = getStatus(e);
      response.setStatus(status);
      response.setContentType("text/plain");
      System.out.println(e.toString());
      if (errorPath != null && status >= 500) {
        try {
          serve(request, response, context, errorPath, e);
        } catch (Exception f) {
          e.printStackTrace(response.getWriter());
          response.getWriter().write("ERROR IN ERROR HANDLER PAGE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
          f.printStackTrace(response.getWriter());
        }
      } else if (debug && status == 500) {
        e.printStackTrace(response.getWriter());
      } else if (e instanceof JavaScriptException) {
        JavaScriptException je = (JavaScriptException) e;
        response.getWriter().write(je.getValue().toString());
      } else {
        response.getWriter().write(e.getMessage());
      }
    } finally {
      contextCache.returnContext(context);
    }
  }
Ejemplo n.º 5
0
 /**
  * Executes an arbitrary expression.<br>
  * It fails if the expression throws a JsAssertException.<br>
  * It fails if the expression throws a RhinoException.<br>
  * Code from JsTester (http://jstester.sf.net/)
  */
 public Object eval(String expr) {
   Object value = null;
   try {
     value = context.evaluateString(globalScope, expr, "", 1, null);
   } catch (JavaScriptException jse) {
     Scriptable jsAssertException = (Scriptable) globalScope.get("currentException", globalScope);
     jse.printStackTrace();
     String message = (String) jsAssertException.get("message", jsAssertException);
     if (message != null) {
       fail(message);
     }
   } catch (RhinoException re) {
     fail(re.getMessage());
   }
   return value;
 }
Ejemplo n.º 6
0
 /**
  * Call a Javascript function, identified by name, on a set of arguments. Optionally, expect it to
  * throw an exception.
  *
  * @param expectingException
  * @param functionName
  * @param args
  * @return
  */
 public Object rhinoCallExpectingException(
     final Object expectingException, final String functionName, final Object... args) {
   Object fObj = rhinoScope.get(functionName, rhinoScope);
   if (!(fObj instanceof Function)) {
     throw new RuntimeException("Missing test function " + functionName);
   }
   Function function = (Function) fObj;
   try {
     return function.call(rhinoContext, rhinoScope, rhinoScope, args);
   } catch (RhinoException angryRhino) {
     if (expectingException != null && angryRhino instanceof JavaScriptException) {
       JavaScriptException jse = (JavaScriptException) angryRhino;
       Assert.assertEquals(jse.getValue(), expectingException);
       return null;
     }
     String trace = angryRhino.getScriptStackTrace();
     Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace);
   } catch (JavaScriptAssertionFailed assertion) {
     Assert.fail(assertion.getMessage());
   }
   return null;
 }
  /** process exception */
  private void processJavaScriptException(JavaScriptException e) {
    Object eValue = e.getValue();

    if (eValue instanceof NativeJavaObject) {
      eValue = ((NativeJavaObject) eValue).unwrap();
    }

    boolean isBuildException = eValue instanceof BuildException;
    if (isBuildException) {
      BuildException be = (BuildException) eValue;
      be.setLocation(getLocation());
      throw be;
    }
  }
  /**
   * Evaluate JavaScript source.
   *
   * @param cx the current context
   * @param filename the name of the file to compile, or null for interactive mode.
   */
  private void processSource(Context cx, String filename) {
    if (filename == null) {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String sourceName = "<stdin>";
      int lineno = 1;
      boolean hitEOF = false;
      do {
        int startline = lineno;
        System.err.print("js> ");
        System.err.flush();
        try {
          String source = "";
          // Collect lines of source to compile.
          while (true) {
            String newline;
            newline = in.readLine();
            if (newline == null) {
              hitEOF = true;
              break;
            }
            source = source + newline + "\n";
            lineno++;
            // Continue collecting as long as more lines
            // are needed to complete the current
            // statement. stringIsCompilableUnit is also
            // true if the source statement will result in
            // any error other than one that might be
            // resolved by appending more source.
            if (cx.stringIsCompilableUnit(source)) {
              break;
            }
          }
          Object result = cx.evaluateString(this, source, sourceName, startline, null);
          if (result != Context.getUndefinedValue()) {
            System.err.println(Context.toString(result));
          }
        } catch (WrappedException we) {
          // Some form of exception was caught by JavaScript and
          // propagated up.
          System.err.println(we.getWrappedException().toString());
          we.printStackTrace();
        } catch (EvaluatorException ee) {
          // Some form of JavaScript error.
          System.err.println("js: " + ee.getMessage());
        } catch (JavaScriptException jse) {
          // Some form of JavaScript error.
          System.err.println("js: " + jse.getMessage());
        } catch (IOException ioe) {
          System.err.println(ioe.toString());
        }
        if (quitting) {
          // The user executed the quit() function.
          break;
        }
      } while (!hitEOF);
      System.err.println();
    } else {
      FileReader in = null;
      try {
        in = new FileReader(filename);
      } catch (FileNotFoundException ex) {
        Context.reportError("Couldn't open file \"" + filename + "\".");
        return;
      }

      try {
        // Here we evalute the entire contents of the file as
        // a script. Text is printed only if the print() function
        // is called.
        cx.evaluateReader(this, in, filename, 1, null);
      } catch (WrappedException we) {
        System.err.println(we.getWrappedException().toString());
        we.printStackTrace();
      } catch (EvaluatorException ee) {
        System.err.println("js: " + ee.getMessage());
      } catch (JavaScriptException jse) {
        System.err.println("js: " + jse.getMessage());
      } catch (IOException ioe) {
        System.err.println(ioe.toString());
      } finally {
        try {
          in.close();
        } catch (IOException ioe) {
          System.err.println(ioe.toString());
        }
      }
    }
  }