X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FXMLRPC.java;h=13669ca5e0975697a071a727bff1045690720f8a;hb=7e9239a7088d4cd772a31a76e1a53e1c681638bc;hp=4d541fd87d27832ffae28c43b4911b45be026941;hpb=a060cc5025b58e8d3e319aefa6ae44fe7c6182ad;p=org.ibex.core.git diff --git a/src/org/xwt/XMLRPC.java b/src/org/xwt/XMLRPC.java index 4d541fd..13669ca 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.*; @@ -32,17 +32,19 @@ import org.bouncycastle.util.encoders.Base64; */ 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. @@ -67,21 +69,15 @@ class XMLRPC extends JS { */ 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(); - //#switch(c.localName) + //#switch(c.getLocalName()) case "fault": fault = true; case "struct": objects.setElementAt(new JS(), objects.size() - 1); case "array": objects.setElementAt(null, objects.size() - 1); @@ -90,13 +86,13 @@ class XMLRPC extends JS { } public void endElement(XML.Element c) { - //#switch(c.localName) + //#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 Res.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), + 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())) @@ -123,8 +119,8 @@ class XMLRPC extends JS { 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); + if (Log.on) Log.info(this, "error parsing date : " + s); + if (Log.on) Log.info(this, e); } case "member": Object memberValue = objects.elementAt(objects.size() - 1); @@ -154,15 +150,34 @@ class XMLRPC extends JS { public void characters(char[] ch, int start, int length) { try { content.write(ch, start, length); } catch (Exception e) { - if (Log.on) Log.log(this, "Exception in XMLRPC.content() -- this should never happen"); - if (Log.on) Log.log(this, e); + if (Log.on) Log.info(this, "Exception in XMLRPC.content() -- this should never happen"); + if (Log.on) Log.info(this, e); } } public void whitespace(char[] ch, int start, int length) {} } - // Methods to make outbound XML-RPC request /////////////////////////////////////////////////// + // Send /////////////////////////////////////////////////////////////////////////// + + protected String buildRequest(JSArray args) throws JSExn, IOException { + StringBuffer content = new StringBuffer(); + content.append("\r\n"); + content.append("\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 JSExn { @@ -186,10 +201,10 @@ class XMLRPC extends JS { 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); @@ -205,8 +220,8 @@ class XMLRPC extends JS { } 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); + 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"); } @@ -277,142 +292,51 @@ class XMLRPC extends JS { } } - public Object call_(JSArray args) throws JSExn, 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); - 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); - } - - InputStream is = http.POST("text/xml", content); - try { - BufferedReader br = !Log.verbose ? - new BufferedReader(new InputStreamReader(is)) : - 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; - String s; - BufferedReader br2 = new BufferedReader(new StringReader(new String(c, off, ret))); - while ((s = br2.readLine()) != null) Log.log(this, "recv: " + s); - return ret; - } - }); - return null; - } finally { - is.close(); - } - } - - protected String send(JSArray args, HTTP http) throws JSExn, IOException { - StringBuffer content = new StringBuffer(); - content.append("\r\n"); - content.append("\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 JSExn, IOException { - // parse XML reply - 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 JSExn("reply from server was not well-formed XML: " + e); - } - - if (fault) throw new JSExn(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