/** * Given a filename, evaluate the file's contents as a JavaScript program. Return the value of the * program. If the test throws a Java exception or JavaScript runtime or compilation error, return * the string value of the error message. * * @param s full path to the file that will be exectued. * @return test result object. If the test is positive, result should be an instance of * Scriptable. if the test is negative, the result should be a String, whose value is the * message in the JavaScript error or Java exception. */ public Object executeTestFile(String s) { // this bit is stolen from Main.java FileReader in = null; try { in = new FileReader(s); } catch (FileNotFoundException ex) { driver.p("couldn't open file " + s); } Object result = null; try { // Here we evalute the entire contents of the file as // as script. Text is printed only if the print() function // is called. // cx.evaluateReader((Scriptable) global, in, args[i], 1, null); result = ((Scriptable) (((Context) cx).evaluateReader((Scriptable) global, (Reader) in, s, 1, null))); } catch (WrappedException we) { driver.p("Wrapped Exception: " + we.getWrappedException().toString()); result = we.getWrappedException().toString(); } catch (Exception jse) { driver.p("JavaScriptException: " + jse.getMessage()); result = jse.getMessage(); } return (result); }
/** Evaluates script in the given stack frame. */ private static String do_eval(Context cx, StackFrame frame, String expr) { String resultString; Debugger saved_debugger = cx.getDebugger(); Object saved_data = cx.getDebuggerContextData(); int saved_level = cx.getOptimizationLevel(); cx.setDebugger(null, null); cx.setOptimizationLevel(-1); cx.setGeneratingDebug(false); try { Callable script = (Callable) cx.compileString(expr, "", 0, null); Object result = script.call(cx, frame.scope, frame.thisObj, ScriptRuntime.emptyArgs); if (result == Undefined.instance) { resultString = ""; } else { resultString = ScriptRuntime.toString(result); } } catch (Exception exc) { resultString = exc.getMessage(); } finally { cx.setGeneratingDebug(true); cx.setOptimizationLevel(saved_level); cx.setDebugger(saved_debugger, saved_data); } if (resultString == null) { resultString = "null"; } return resultString; }
private Object parseMessage() { // Consume SOAP message, if any // TODO: Add a SOAP handler in Parser.java if (soapAction != null) { try { MimeHeaders mime_headers = new MimeHeaders(); for (Object k : headers.getIds()) { if (k instanceof String) { String name = (String) k; String value = Context.toString(ScriptableObject.getProperty(headers, name)); mime_headers.addHeader(name, value); } } return MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL) .createMessage(mime_headers, request.getInputStream()); } catch (IOException ex) { throw new ESXXException("Unable to read SOAP message stream: " + ex.getMessage()); } catch (SOAPException ex) { throw new ESXXException(400 /* Bad Request */, "Invalid SOAP message: " + ex.getMessage()); } finally { try { request.getInputStream().close(); } catch (Exception ignored) { } } } else if (contentType != null && contentLength > 0) { try { ESXX esxx = ESXX.getInstance(); return esxx.parseStream( contentType, request.getInputStream(), URI.create("urn:x-esxx:incoming-request-entity"), null, new java.io.PrintWriter(request.getErrorWriter()), Context.getCurrentContext(), this); } catch (Exception ex) { throw new ESXXException( 400 /* Bad Request */, "Unable to parse request entity: " + ex.getMessage(), ex); } finally { try { request.getInputStream().close(); } catch (Exception ignored) { } } } else { // Return a dummy object return Context.getCurrentContext().newObject(this); } }
@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); } }
/** * Creates and returns a new Image object that is a rendering of this Image object. The input * object defines the rendering parameters of the new Image object. Imagemagick's convert program * is used to create a scaled bounding box of this Image with the given dimensions. * * @param {Object} input A JavaScript object specifying the rendering parameters. For example, * <code> {maxWidth:200, maxHeight:100} </code> * @returns {Image} The rendered Image object * @throws Exception */ public ImageObject jsFunction_render(Object input) throws Exception { if (input == null || !(input instanceof Scriptable)) { throw new RuntimeException("The first argument to render() must be a scriptable object."); } Scriptable s = (Scriptable) input; int maxWidth = toInt(s.get("maxWidth", s)); int maxHeight = toInt(s.get("maxHeight", s)); int cropWidth = toInt(s.get("cropWidth", s)); int cropHeight = toInt(s.get("cropHeight", s)); int cropXOffset = toInt(s.get("cropXOffset", s)); int cropYOffset = toInt(s.get("cropYOffset", s)); int currentWidth = (int) node.getInteger(WIDTH); int currentHeight = (int) node.getInteger(HEIGHT); String aname = null; if (maxWidth > 0 && maxHeight > 0 && currentWidth > 0 && currentHeight > 0) { int[] dims = this.computeResizedDimensions(maxWidth, maxHeight, currentWidth, currentHeight); aname = dims[0] + "x" + dims[1]; maxWidth = dims[0]; maxHeight = dims[1]; } else if (cropWidth > 0 && cropHeight > 0) { aname = cropWidth + "x" + cropHeight + "_" + cropXOffset + "x" + cropYOffset; } else { throw new RuntimeException("render(), invalid parameter set."); } Object o = this.jsFunction_get(aname); if (o instanceof ImageObject) { return (ImageObject) o; } try { synchronized (this) { while (this.convertOps.contains(aname)) { this.wait(); } this.convertOps.add(aname); } o = ((axiom.objectmodel.db.Node) this.node).getChildElement(aname, true); if (o instanceof Node) { return (ImageObject) Context.toObject(o, this.core.global); } ImageObject computedImg = null; String[] paths = getPaths(); String imgPath = paths[0]; String tmpPath = paths[1]; String fileName = node.getString(FileObject.FILE_NAME); try { File tmpFile = new File(tmpPath); int[] dims = null; if (maxWidth > 0 && maxHeight > 0) { dims = this.resize( maxWidth, maxHeight, (int) this.node.getInteger(WIDTH), (int) this.node.getInteger(HEIGHT), imgPath, tmpPath, true); } else { dims = this.crop(cropWidth, cropHeight, cropXOffset, cropYOffset, imgPath, tmpPath); } if (dims == null) { throw new Exception("ImageObject.render(), resizing the image failed."); } final String protoname = "Image"; INode node = new axiom.objectmodel.db.Node(protoname, protoname, core.app.getWrappedNodeManager()); computedImg = new ImageObject("Image", core, node, core.getPrototype(protoname), true); node.setString(FileObject.FILE_NAME, fileName); node.setString(FileObject.ACCESSNAME, FileObjectCtor.generateAccessName(fileName)); node.setString(FileObject.CONTENT_TYPE, this.node.getString(FileObject.CONTENT_TYPE)); node.setJavaObject(FileObject.SELF, computedImg); node.setInteger(ImageObject.WIDTH, dims[0]); node.setInteger(ImageObject.HEIGHT, dims[1]); node.setString(FileObject.RENDERED_CONTENT, "true"); node.setInteger(FileObject.FILE_SIZE, tmpFile.length()); computedImg.tmpPath = tmpPath; } catch (Exception ex) { throw new RuntimeException( "ImageObject.jsfunction_bound(): Could not write the image to temporary storage, " + ex.getMessage()); } if (computedImg != null) { this.jsFunction_addThumbnail(computedImg, null); return computedImg; } } finally { synchronized (this) { this.convertOps.remove(aname); this.notifyAll(); } } return null; }