2002/08/07 05:03:49
[org.ibex.core.git] / src / org / xwt / XMLRPC.java
index 596151e..6b95b4d 100644 (file)
@@ -41,6 +41,9 @@ class XMLRPC extends XML implements Function {
     /** 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);
 
+    /** 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.
      *
@@ -74,6 +77,7 @@ class XMLRPC extends XML implements Function {
     // Methods to Recieve and parse XML-RPC Response ////////////////////////////////////////////////////
 
     public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
+        content.reset();
         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);
@@ -226,6 +230,26 @@ class XMLRPC extends XML implements Function {
             }
             sb.append("</string></value>\n");
 
+        } else if (o instanceof NativeDate) {
+            sb.append("                <value><dateTime.iso8601>");
+            NativeDate nd = (NativeDate)o;
+            Date d = new Date(nd.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("</dateTime.iso8601></value>\n");
+
         } else if (o instanceof NativeArray) {
             if (tracker.get(o) != null) throw new JavaScriptException("attempted to send multi-ref data structure via XML-RPC");
             tracker.put(o, Boolean.TRUE);
@@ -265,46 +289,36 @@ class XMLRPC extends XML implements Function {
         if (objects == null) objects = new Vec();
         else objects.setSize(0);
 
-        HTTP http = new HTTP(url);
         String content = send(args, http);
-        OutputStream os = new BufferedOutputStream(http.getOutputStream(content.length(), "text/xml"), 4000);
-        PrintWriter ps = !Log.verbose ?
-            new PrintWriter(os) :
-            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));
-                    }
-                });
-        ps.print(content.toString());
-        ps.flush();
-        
-        BufferedReader br = !Log.verbose ?
-            new BufferedReader(new InputStreamReader(new Filter(http.getInputStream()))) :
-            new BufferedReader(new FilterReader(new InputStreamReader(new Filter(http.getInputStream()))) {
-                public int read() throws IOException {
-                    int i = super.read();
-                    System.out.println("X " + i);
-                    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);
-                    System.out.println("Y " + ret);
-                    if (ret == -1) return ret;
-                    if (Log.on) Log.log(this, "recv: " + new String(c, off, ret));
-                    return ret;
-                }
-            });
-        return recieve(br);
+        if (Log.verbose) {
+            String s;
+            BufferedReader br2 = new BufferedReader(new StringReader(content));
+            while ((s = br2.readLine()) != null) Log.log(this, "send: " + s);
+        }
+
+        HTTP.HTTPInputStream is = http.POST("text/xml", content);
+        try {
+            BufferedReader br = !Log.verbose ?
+                new BufferedReader(new InputStreamReader(new Filter(is))) :
+                new BufferedReader(new FilterReader(new InputStreamReader(new Filter(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 recieve(br);
+        } finally {
+            is.close();
+        }
     }
 
     protected String send(Object[] args, HTTP http) throws JavaScriptException, IOException {
@@ -314,15 +328,13 @@ class XMLRPC extends XML implements Function {
         content.append("        <methodName>");
         content.append(methodname);
         content.append("</methodName>\n");
-        if (args.length > 0) {
-            content.append("        <params>\n");
-            for(int i=0; i<args.length; i++) {
-                content.append("            <param>\n");
-                appendObject(args[i], content);
-                content.append("            </param>\n");
-            }
-            content.append("        </params>\n");
+        content.append("        <params>\n");
+        for(int i=0; i<args.length; i++) {
+            content.append("            <param>\n");
+            appendObject(args[i], content);
+            content.append("            </param>\n");
         }
+        content.append("        </params>\n");
         content.append("    </methodCall>");
         return content.toString();
     }
@@ -367,10 +379,15 @@ class XMLRPC extends XML implements Function {
 
     /** When you get a property from an XMLRPC, it just returns another XMLRPC with the property name tacked onto methodname. */
     public Object get(String name, Scriptable start) {
-        return new XMLRPC(url, (methodname.equals("") ? "" : methodname + ".") + name);
+        return new XMLRPC(url, (methodname.equals("") ? "" : methodname + ".") + name, 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;
     }
@@ -389,17 +406,17 @@ class XMLRPC extends XML implements Function {
         public Filter(InputStream is) { super(is); }
         public int read() throws IOException {
             Thread.yield();
-            while(MessageQueue.working) try { Thread.sleep(100); } catch (Exception e) { };
+            while(MessageQueue.nonThreadEventsInQueue > 0) try { Thread.sleep(100); } catch (Exception e) { };
             return super.read();
         }
         public int read(byte[] b) throws IOException {
             Thread.yield();
-            while(MessageQueue.working) try { Thread.sleep(100); } catch (Exception e) { };
+            while(MessageQueue.nonThreadEventsInQueue > 0) try { Thread.sleep(100); } catch (Exception e) { };
             return super.read(b);
         }
         public int read(byte[] b, int i, int j) throws IOException {
             Thread.yield();
-            while(MessageQueue.working) try { Thread.sleep(100); } catch (Exception e) { };
+            while(MessageQueue.nonThreadEventsInQueue > 0) try { Thread.sleep(100); } catch (Exception e) { };
             return super.read(b, i, j);
         }
     }