X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FXMLRPC.java;h=f78936a18dde6f9cc976e1e20700b148160c0e85;hb=16c24a73c1c1b2955db0bbbaf5a940215329bca1;hp=43654c7f2a5a6c15168015d5e86258401a7a5739;hpb=a76646eb76b01b5f1b5f0507b1c4bcd4202f1f11;p=org.ibex.core.git diff --git a/src/org/xwt/XMLRPC.java b/src/org/xwt/XMLRPC.java index 43654c7..f78936a 100644 --- a/src/org/xwt/XMLRPC.java +++ b/src/org/xwt/XMLRPC.java @@ -1,4 +1,4 @@ -// Copyright 2003 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.*; @@ -30,19 +30,21 @@ import org.bouncycastle.util.encoders.Base64; * convert. * */ -class XMLRPC extends JSCallable { +class XMLRPC extends JS { - /** the url to connect to */ - protected String url = null; + 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 method name to invoke on the remove server */ - protected String methodname = null; /** 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 HTTP connection to use */ - protected HTTP http = null; /** The object stack. As we process xml elements, pieces of the * return value are pushed onto and popped off of this stack. @@ -58,7 +60,7 @@ class XMLRPC extends JSCallable { * 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 @@ -67,50 +69,36 @@ class XMLRPC extends JSCallable { */ 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 //////////////////////////////////////////////////// + // Recieve //////////////////////////////////////////////////////////////// private class Helper extends XML { public Helper() { super(BUFFER_SIZE); } public void startElement(XML.Element c) { content.reset(); - if (c.localName.equals("fault")) fault = true; - else if (c.localName.equals("struct")) objects.setElementAt(new JSObj(), objects.size() - 1); - else if (c.localName.equals("array")) objects.setElementAt(null, objects.size() - 1); - else if (c.localName.equals("value")) objects.addElement(""); + //#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) { - - if (c.localName.equals("int") || c.localName.equals("i4")) - objects.setElementAt(new Integer(new String(content.getBuf(), 0, content.size())), objects.size() - 1); - - else if (c.localName.equals("boolean")) - objects.setElementAt(content.getBuf()[0] == '1' ? Boolean.TRUE : Boolean.FALSE, objects.size() - 1); - - else if (c.localName.equals("string")) - objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); - - else if (c.localName.equals("double")) - objects.setElementAt(new Double(new String(content.getBuf(), 0, content.size())), objects.size() - 1); - - else if (c.localName.equals("base64")) - objects.setElementAt(new Res.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), null), objects.size() - 1); - - else if (c.localName.equals("name")) - objects.addElement(new String(content.getBuf(), 0, content.size())); - - else if (c.localName.equals("value") && "".equals(objects.lastElement())) + //#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); - - else if (c.localName.equals("dateTime.iso8601")) { + case "dateTime.iso8601": String s = new String(content.getBuf(), 0, content.size()); // strip whitespace @@ -121,59 +109,82 @@ class XMLRPC extends JSCallable { 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(), - Double.valueOf(s.substring(12, 14)).doubleValue(), - Double.valueOf(s.substring(15, 17)).doubleValue(), - (double)0 - ); - nd.jsJSFunction_setTime(JSDate.internalUTC(date)); + Double.valueOf(s.substring(4, 6)).doubleValue() - 1, + Double.valueOf(s.substring(6, 8)).doubleValue(), + Double.valueOf(s.substring(9, 11)).doubleValue(), + Double.valueOf(s.substring(12, 14)).doubleValue(), + Double.valueOf(s.substring(15, 17)).doubleValue(), + (double)0 + ); + nd.setTime(JSDate.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); + throw new RuntimeException("xwt.net.rpc.xml.recieve.malformedDateTag" + + "the server sent a tag which was malformed: " + s); } - - } else if (c.localName.equals("member")) { + 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); - struct.put(memberName, memberValue); + try { + struct.put(memberName, memberValue); + } catch (JSExn e) { + throw new Error("this should never happen"); + } objects.setSize(objects.size() - 2); - - } else if (c.localName.equals("data")) { + case "data": int i; for(i=objects.size() - 1; objects.elementAt(i) != null; i--); JSArray arr = new JSArray(); - 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(); + } /** Appends the XML-RPC representation of o to sb */ - void appendObject(Object o, StringBuffer sb) throws JS.Exn { + void appendObject(Object o, StringBuffer sb) throws JSExn { if (o == null) { - throw new JS.Exn("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()) { @@ -191,10 +202,10 @@ class XMLRPC extends JSCallable { sb.append(((Boolean)o).booleanValue() ? "1" : "0"); sb.append("\n"); - } else if (o instanceof Res) { + } else if (o instanceof Stream) { try { sb.append(" \n"); - InputStream is = ((Res)o).getInputStream(); + InputStream is = ((Stream)o).getInputStream(); byte[] buf = new byte[54]; while(true) { int numread = is.read(buf, 0, 54); @@ -210,9 +221,9 @@ class XMLRPC extends JSCallable { } sb.append("\n \n"); } catch (IOException e) { - if (Log.on) Log.log(this, "caught IOException while attempting to send a ByteStream via XML-RPC"); - if (Log.on) Log.log(this, e); - throw new JS.Exn("caught IOException while attempting to send a ByteStream via XML-RPC"); + 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) { @@ -255,7 +266,7 @@ class XMLRPC extends JSCallable { sb.append("\n"); } else if (o instanceof JSArray) { - if (tracker.get(o) != null) throw new JS.Exn("attempted to send multi-ref data structure via XML-RPC"); + 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"); JSArray a = (JSArray)o; @@ -263,7 +274,7 @@ class XMLRPC extends JSCallable { sb.append(" \n"); } else if (o instanceof JS) { - if (tracker.get(o) != null) throw new JS.Exn("attempted to send multi-ref data structure via XML-RPC"); + if (tracker.get(o) != null) throw new JSExn("attempted to send multi-ref data structure via XML-RPC"); tracker.put(o, Boolean.TRUE); JS j = (JS)o; sb.append(" \n"); @@ -277,122 +288,56 @@ class XMLRPC extends JSCallable { sb.append(" \n"); } else { - throw new JS.Exn("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"); } } - public Object call_(JSArray args) throws JS.Exn, IOException { - if (Log.verbose) Log.log(this, "call to " + url + " : " + methodname); - - if (tracker == null) tracker = new Hash(); - else tracker.clear(); - if (objects == null) objects = new Vec(); - else objects.setSize(0); + // Call Sequence ////////////////////////////////////////////////////////////////////////// - final String content = send(args, http); - if (Log.verbose) { - String s; - BufferedReader br2 = new BufferedReader(new StringReader(content)); - while ((s = br2.readLine()) != null) Log.log(this, "send: " + s); - } + 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\n"); - content.append(" \n"); - content.append(" "); - content.append(methodname); - 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(); - } - - protected Object recieve(BufferedReader br) throws JS.Exn, IOException { - // parse XML reply + final void call(final JS.UnpauseCallback callback, final JSArray args) { try { - new Helper().parse(br); - } catch (XML.XMLException e) { - if (Log.on) Log.log(this, "reply from server was not well-formed XML: " + e); - throw new JS.Exn("reply from server was not well-formed XML: " + e); + if (Log.verbose) Log.info(this, "call to " + url + " : " + method); + if (tracker == null) tracker = new Hash(); + if (objects == null) objects = new Vec(); + String request = buildRequest(args); + if (Log.verbose) Log.info(this, "send:\n" + request); + InputStream is = http.POST("text/xml", request); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + try { + new Helper().parse(br); + final Object result = fault ? new JSExn(objects.elementAt(0)) : objects.size() == 0 ? null : objects.elementAt(0); + Scheduler.add(new Scheduler.Task() { public void perform() throws Exception { callback.unpause(result); }}); + } finally { + tracker.clear(); + objects.setSize(0); + } + } catch (final JSExn e) { + final Exception e2 = e; + Scheduler.add(new Scheduler.Task() { public void perform() throws Exception { callback.unpause(e2); }}); + } catch (final IOException e) { + final Exception e2 = e; + Scheduler.add(new Scheduler.Task() { public void perform() throws Exception { callback.unpause(new JSExn(e2)); }}); + } catch (final XML.Exn e) { + final Exception e2 = e; + Scheduler.add(new Scheduler.Task() { public void perform() throws Exception { callback.unpause(new JSExn(e2)); }}); } - - if (fault) throw new JS.Exn(objects.elementAt(0)); - if (objects.size() == 0) return null; - return objects.elementAt(0); } - - public final Object call(final JSArray args) throws JS.Exn { - final Callback callback = JSContext.pause(); - new java.lang.Thread() { - public void run() { - try { - final Object ret = call_(args); - Scheduler.add(new Scheduler.Task() { public void perform() { callback.call(ret); } }); - } catch (IOException se) { - if (Log.on) Log.log(this, se); - throw new JS.Exn("socket exception: " + se); - } - } }.start(); - return null; - } - - /** When you get a property from an XMLRPC, it just returns another XMLRPC with the property name tacked onto methodname. */ - public Object get(Object name) { - return new XMLRPC(url, (methodname.equals("") ? "" : methodname + ".") + name.toString(), http); - } - - public XMLRPC(String url, String methodname) { - this(url, methodname, new HTTP(url)); - } - - public XMLRPC(String url, String methodname, HTTP http) { - this.http = http; - this.url = url; - this.methodname = methodname; - } - - - // Helper Classes /////////////////////////////////////////////////////////////////////////////////// - - /** CharArrayWriter that lets us touch its buffer */ - protected static class AccessibleCharArrayWriter extends CharArrayWriter { - public char[] getBuf() { return buf; } - public AccessibleCharArrayWriter(int i) { super(i); } - } - }