X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FXMLRPC.java;h=f78936a18dde6f9cc976e1e20700b148160c0e85;hb=16c24a73c1c1b2955db0bbbaf5a940215329bca1;hp=4da6b1cb723a9b7ada5a5e484b2a5c4d70cddfff;hpb=7d0e914f5223cef3e37b98518ace2520d5210422;p=org.ibex.core.git diff --git a/src/org/xwt/XMLRPC.java b/src/org/xwt/XMLRPC.java index 4da6b1c..f78936a 100644 --- a/src/org/xwt/XMLRPC.java +++ b/src/org/xwt/XMLRPC.java @@ -1,16 +1,16 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt; import java.io.*; import java.net.*; import java.util.*; -import org.mozilla.javascript.*; +import org.xwt.js.*; import org.xwt.util.*; import org.bouncycastle.util.encoders.Base64; /** - * An XML-RPC client implemented as a Rhino JavaScript Host - * Object. See the XWT spec for information on its behavior. + * An XML-RPC client implemented as a JavaScript Host Object. See the + * XWT spec for information on its behavior. * * NOTE: this client is EXTREMELY lenient in the responses it will * accept; there are many, many invalid responses that it will @@ -30,28 +30,21 @@ import org.bouncycastle.util.encoders.Base64; * convert. * */ -class XMLRPC extends XML implements Function { +class XMLRPC extends JS { - /** should we use SSL? */ - protected boolean ssl = false; + public XMLRPC(String url, String method) { this(url, method, new HTTP(url)); } + public XMLRPC(String url, String method, HTTP http) { this.http = http; this.url = url; this.method = method; } + public Object get(Object name) { return new XMLRPC(url, (method.equals("") ? "" : method + ".") + name.toString(), http); } - /** the url to connect to */ - protected URL url = null; - - /** the method name to invoke on the remove server */ - protected String methodname = null; - - /** the host portion of the url; not calculated until first call() */ - protected String host = null; - - /** the filename portion of the url; not calculated until first call() */ - protected String filename = null; - - /** the port to connect to; not calculated until the first call() */ - protected int port = -1; /** this holds character content as we read it in -- since there is only one per instance, we don't support mixed content */ protected AccessibleCharArrayWriter content = new AccessibleCharArrayWriter(100); + protected String url = null; ///< the url to connect to + protected String method = null; ///< the method name to invoke on the remove server + protected HTTP http = null; ///< the HTTP connection to use + private Hash tracker; ///< used to detect multi-ref data + protected boolean fault = false; ///< True iff the return value is a fault (and should be thrown as an exception) + /** The object stack. As we process xml elements, pieces of the * return value are pushed onto and popped off of this stack. @@ -64,10 +57,10 @@ class XMLRPC extends XML implements Function { * * If an <array> tag is encountered, a null is pushed onto the * stack. When a </data> is encountered, we search back on the - * stack to the last null, replace it with a NativeArray, and + * stack to the last null, replace it with a NativeJSArray, and * insert into it all elements above it on the stack. * - * If a <struct> tag is encountered, a JSObject is pushed + * If a <struct> tag is encountered, a JSect is pushed * onto the stack. If a <name> tag is encountered, its CDATA is * pushed onto the stack. When a </member> is encountered, the * name (second element on stack) and value (top of stack) are @@ -76,56 +69,46 @@ class XMLRPC extends XML implements Function { */ protected Vec objects = null; - /** used to detect multi-ref data */ - private Hash tracker; - - /** True iff the return value is a fault (and should be thrown as an exception) */ - protected boolean fault = false; - - - // Methods to Recieve and parse XML-RPC Response //////////////////////////////////////////////////// - - public void startElement(String name, String[] keys, Object[] vals, int line, int col) { - if (name.equals("fault")) fault = true; - else if (name.equals("struct")) objects.setElementAt(new JSObject(false), objects.size() - 1); - else if (name.equals("array")) objects.setElementAt(null, objects.size() - 1); - else if (name.equals("value")) objects.addElement(""); - } - - public void endElement(String name, int line, int col) { - - if (name.equals("int") || name.equals("i4")) - objects.setElementAt(new Integer(new String(content.getBuf(), 0, content.size())), objects.size() - 1); - - else if (name.equals("boolean")) - objects.setElementAt(content.getBuf()[0] == '1' ? Boolean.TRUE : Boolean.FALSE, objects.size() - 1); - else if (name.equals("string")) - objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); + // Recieve //////////////////////////////////////////////////////////////// - else if (name.equals("double")) - objects.setElementAt(new Double(new String(content.getBuf(), 0, content.size())), objects.size() - 1); + private class Helper extends XML { + public Helper() { super(BUFFER_SIZE); } - else if (name.equals("base64")) - objects.setElementAt(new String(Base64.decode(new String(content.getBuf(), 0, content.size()))), objects.size() - 1); - - else if (name.equals("name")) - objects.addElement(new String(content.getBuf(), 0, content.size())); - - else if (name.equals("value") && "".equals(objects.lastElement())) - objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); - - else if (name.equals("dateTime.iso8601")) { - String s = new String(content.getBuf(), 0, content.size()); - - // strip whitespace - int i=0; - while(Character.isWhitespace(s.charAt(i))) i++; - if (i > 0) s = s.substring(i); - - try { - NativeDate nd = (NativeDate)Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date"); - double date = NativeDate.date_msecFromDate(Double.valueOf(s.substring(0, 4)).doubleValue(), + public void startElement(XML.Element c) { + content.reset(); + //#switch(c.getLocalName()) + case "fault": fault = true; + case "struct": objects.setElementAt(new JS(), objects.size() - 1); + case "array": objects.setElementAt(null, objects.size() - 1); + case "value": objects.addElement(""); + //#end + } + + public void endElement(XML.Element c) { + //#switch(c.getLocalName()) + case "int": objects.setElementAt(new Integer(new String(content.getBuf(), 0, content.size())), objects.size() - 1); + case "i4": objects.setElementAt(new Integer(new String(content.getBuf(), 0, content.size())), objects.size() - 1); + case "boolean": objects.setElementAt(content.getBuf()[0] == '1' ? Boolean.TRUE : Boolean.FALSE, objects.size() - 1); + case "string": objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); + case "double": objects.setElementAt(new Double(new String(content.getBuf(), 0, content.size())), objects.size() - 1); + case "base64": + objects.setElementAt(new Stream.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), + null), objects.size() - 1); + case "name": objects.addElement(new String(content.getBuf(), 0, content.size())); + case "value": if ("".equals(objects.lastElement())) + objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); + case "dateTime.iso8601": + String s = new String(content.getBuf(), 0, content.size()); + + // strip whitespace + int i=0; + while(Character.isWhitespace(s.charAt(i))) i++; + if (i > 0) s = s.substring(i); + + try { + JSDate nd = new JSDate(); + double date = JSDate.date_msecFromDate(Double.valueOf(s.substring(0, 4)).doubleValue(), Double.valueOf(s.substring(4, 6)).doubleValue() - 1, Double.valueOf(s.substring(6, 8)).doubleValue(), Double.valueOf(s.substring(9, 11)).doubleValue(), @@ -133,49 +116,75 @@ class XMLRPC extends XML implements Function { Double.valueOf(s.substring(15, 17)).doubleValue(), (double)0 ); - nd.jsFunction_setTime(NativeDate.internalUTC(date)); - objects.setElementAt(nd, objects.size() - 1); - - } catch (Exception e) { - if (Log.on) Log.log(this, "error parsing date : " + s); - if (Log.on) Log.log(this, e); + nd.setTime(JSDate.internalUTC(date)); + objects.setElementAt(nd, objects.size() - 1); + + } catch (Exception e) { + throw new RuntimeException("xwt.net.rpc.xml.recieve.malformedDateTag" + + "the server sent a tag which was malformed: " + s); + } + case "member": + Object memberValue = objects.elementAt(objects.size() - 1); + String memberName = (String)objects.elementAt(objects.size() - 2); + JS struct = (JS)objects.elementAt(objects.size() - 3); + try { + struct.put(memberName, memberValue); + } catch (JSExn e) { + throw new Error("this should never happen"); + } + objects.setSize(objects.size() - 2); + case "data": + int i; + for(i=objects.size() - 1; objects.elementAt(i) != null; i--); + JSArray arr = new JSArray(); + try { + for(int j = i + 1; j\n"); + content.append(" \n"); + content.append(" "); + content.append(method); + content.append("\n"); + content.append(" \n"); + for(int i=0; i\n"); + appendObject(args.elementAt(i), content); + content.append(" \n"); } + content.append(" \n"); + content.append(" "); + return content.toString(); } - // Methods to make outbound XML-RPC request /////////////////////////////////////////////////// - /** Appends the XML-RPC representation of o to sb */ - void appendObject(Object o, StringBuffer sb) throws JavaScriptException { + void appendObject(Object o, StringBuffer sb) throws JSExn { if (o == null) { - throw new JavaScriptException("attempted to send a null value via XML-RPC"); + throw new JSExn("attempted to send a null value via XML-RPC"); } else if (o instanceof Number) { if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) { @@ -193,6 +202,30 @@ class XMLRPC extends XML implements Function { sb.append(((Boolean)o).booleanValue() ? "1" : "0"); sb.append("\n"); + } else if (o instanceof Stream) { + try { + sb.append(" \n"); + InputStream is = ((Stream)o).getInputStream(); + byte[] buf = new byte[54]; + while(true) { + int numread = is.read(buf, 0, 54); + if (numread == -1) break; + byte[] writebuf = buf; + if (numread < buf.length) { + writebuf = new byte[numread]; + System.arraycopy(buf, 0, writebuf, 0, numread); + } + sb.append(" "); + sb.append(new String(Base64.encode(writebuf))); + sb.append("\n"); + } + sb.append("\n \n"); + } catch (IOException e) { + if (Log.on) Log.info(this, "caught IOException while attempting to send a ByteStream via XML-RPC"); + if (Log.on) Log.info(this, e); + throw new JSExn("caught IOException while attempting to send a ByteStream via XML-RPC"); + } + } else if (o instanceof String) { sb.append(" "); String s = (String)o; @@ -213,261 +246,98 @@ class XMLRPC extends XML implements Function { } sb.append("\n"); - } else if (o instanceof NativeArray) { - if (tracker.get(o) != null) throw new JavaScriptException("attempted to send multi-ref data structure via XML-RPC"); + } else if (o instanceof JSDate) { + sb.append(" "); + java.util.Date d = new java.util.Date(((JSDate)o).getRawTime()); + sb.append(d.getYear() + 1900); + if (d.getMonth() + 1 < 10) sb.append('0'); + sb.append(d.getMonth() + 1); + if (d.getDate() < 10) sb.append('0'); + sb.append(d.getDate()); + sb.append('T'); + if (d.getHours() < 10) sb.append('0'); + sb.append(d.getHours()); + sb.append(':'); + if (d.getMinutes() < 10) sb.append('0'); + sb.append(d.getMinutes()); + sb.append(':'); + if (d.getSeconds() < 10) sb.append('0'); + sb.append(d.getSeconds()); + sb.append("\n"); + + } else if (o instanceof JSArray) { + if (tracker.get(o) != null) throw new JSExn("attempted to send multi-ref data structure via XML-RPC"); tracker.put(o, Boolean.TRUE); sb.append(" \n"); - NativeArray na = (NativeArray)o; - for(int i=0; i\n"); - } else if (o instanceof Scriptable && !(o instanceof Undefined)) { - if (tracker.get(o) != null) throw new JavaScriptException("attempted to send multi-ref data structure via XML-RPC"); + } else if (o instanceof JS) { + if (tracker.get(o) != null) throw new JSExn("attempted to send multi-ref data structure via XML-RPC"); tracker.put(o, Boolean.TRUE); - Scriptable s = (Scriptable)o; + JS j = (JS)o; sb.append(" \n"); - Object[] ids = s.getIds(); - for(int i=0; i" + ids[i] + "\n"); - appendObject(s.get(ids[i].toString(), s), sb); + Enumeration e = j.keys(); + while(e.hasMoreElements()) { + Object key = e.nextElement(); + sb.append(" " + key + "\n"); + appendObject(j.get(key), sb); sb.append(" \n"); } sb.append(" \n"); } else { - throw new JavaScriptException("attempt to send object of type " + o.getClass().getName() + " via XML-RPC"); + throw new JSExn("attempt to send object of type " + o.getClass().getName() + " via XML-RPC"); } } - private Object connect(Object[] args) throws JavaScriptException, IOException { - if (filename == null) { - filename = url.getFile(); - host = url.getHost(); - port = url.getPort(); - InetAddress addr; - try { addr = InetAddress.getByName(host); } - catch (UnknownHostException uhe) { throw new JavaScriptException("could not resolve hostname \"" + host + "\""); } - byte[] quadbyte = addr.getAddress(); - - if (quadbyte[0] == 10 || - (quadbyte[0] == 192 && quadbyte[1] == 168) || - (quadbyte[0] == 172 && (quadbyte[1] & 0xF0) == 16) && - !addr.equals(Main.originAddr)) { - filename = null; - throw new JavaScriptException("security violation: " + host + " [" + addr.getHostAddress() + "] is in a firewalled netblock"); - } - } - - Socket s; - if (ssl) s = Platform.getSocket(host, port == -1 ? 443 : port, true); - else if (url.getProtocol().equals("http")) s = Platform.getSocket(host, port == -1 ? 80 : port, false); - else throw new JavaScriptException("only http[s] is supported"); - - s.setTcpNoDelay(true); - OutputStream os = new BufferedOutputStream(s.getOutputStream(), 4000); - InputStream is = new BufferedInputStream(new Filter(s.getInputStream())); - - PrintWriter ps; - if (!Log.verbose) ps = new PrintWriter(os); - else ps = new PrintWriter(new FilterWriter(new OutputStreamWriter(os)) { - public void write(int i) throws IOException { - super.write(i); - if (Log.on) Log.log(this, "send: " + ((char)i)); - } - public void write(String s, int start, int len) throws IOException { - super.write(s, start, len); - if (Log.on) Log.log(this, "send: " + s.substring(start, start + len)); - } - public void write(char[] c, int start, int len) throws IOException { - super.write(c, start, len); - if (Log.on) Log.log(this, "send: " + new String(c, start, len)); - } - }); - - BufferedReader br; - if (!Log.verbose) br = new BufferedReader(new InputStreamReader(is)); - else br = new BufferedReader(new FilterReader(new InputStreamReader(is)) { - public int read() throws IOException { - int i = super.read(); - if (Log.on) Log.log(this, "recv: " + ((char)i)); - return i; - } - public int read(char[] c, int off, int len) throws IOException { - int ret = super.read(c, off, len); - if (ret == -1) return ret; - if (Log.on) Log.log(this, "recv: " + new String(c, off, ret)); - return ret; - } - }); - - if (Log.verbose) Log.log(this, "call to " + url + " : " + methodname); - return send(args, br, ps); - } - - protected Object send(Object[] args, BufferedReader br, PrintWriter ps) throws JavaScriptException, IOException { - - // Construct payload - StringBuffer content = new StringBuffer(); - content.append("\n"); - content.append(" \n"); - content.append(" "); - content.append(methodname); - content.append("\n"); - if (args.length > 0) { - content.append(" \n"); - for(int i=0; i\n"); - appendObject(args[i], content); - content.append(" \n"); - } - content.append(" \n"); - } - content.append(" "); - - // Header - ps.print("POST " + filename + " HTTP/1.0\r\n"); - ps.print("Host: " + host + "\r\n"); - ps.print("User-Agent: XWT (http://www.xwt.org/)\r\n"); - ps.print("Content-Type: text/xml\r\n"); - ps.print("Content-length: " + (content.length() + 1) + "\r\n"); - ps.print("\r\n"); - ps.print(content); - ps.print("\n"); - ps.flush(); - - // throw away HTTP reply headers - while(!br.readLine().equals("")) { } - - // parse XML reply - try { - parse(br); - } catch (XML.SAXException e) { - if (Log.on) Log.log(this, "reply from server was not well-formed XML: " + e); - throw new JavaScriptException("reply from server was not well-formed XML: " + e); - } - - if (fault) throw new JavaScriptException(objects.elementAt(0)); - - if (objects.size() == 0) return null; - return objects.elementAt(0); + // Call Sequence ////////////////////////////////////////////////////////////////////////// + public final Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { + JSArray args = new JSArray(); + for(int i=0; i