2002/08/24 22:45:14
[org.ibex.core.git] / src / org / xwt / SOAP.java
index e055809..1a279f4 100644 (file)
@@ -6,6 +6,7 @@ import java.net.*;
 import java.util.*;
 import org.mozilla.javascript.*;
 import org.xwt.util.*;
+import org.bouncycastle.util.encoders.Base64;
 
 /**
  *  A partial RPC-style SOAP 1.1 client. Implemented from the SOAP 1.1
@@ -28,7 +29,7 @@ class SOAP extends XMLRPC {
 
     /** When you get a property from an SOAP, it just returns another SOAP with the property name tacked onto methodname. */
     public Object get(String name, Scriptable start) {
-        return new SOAP(url.toString(), (methodname.equals("") ? "" : methodname + ".") + name, action, nameSpace);
+        return new SOAP(url.toString(), (methodname.equals("") ? "" : methodname + ".") + name, http, action, nameSpace);
     }
 
 
@@ -36,6 +37,7 @@ class SOAP extends XMLRPC {
 
     public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
 
+        content.reset();
         if (name.equals("SOAP-ENV:Envelope")) return;
         if (name.equals("SOAP-ENV:Body")) return;
         if (name.equals("SOAP-ENV:Fault")) fault = true;
@@ -59,6 +61,9 @@ class SOAP extends XMLRPC {
                 } else if (value.endsWith("string")) {
                     objects.removeElementAt(objects.size() - 1);
                     objects.addElement("");
+                } else if (value.endsWith("base64")) {
+                    objects.removeElementAt(objects.size() - 1);
+                    objects.addElement(new byte[] { });
                 } else if (value.endsWith("null")) {
                     objects.removeElementAt(objects.size() - 1);
                     objects.addElement(null);
@@ -84,6 +89,11 @@ class SOAP extends XMLRPC {
                 objects.removeElementAt(objects.size() - 1);
                 objects.addElement(new String(content.getBuf(), 0, content.size()).intern());
                 content.reset();
+
+            } else if (me instanceof byte[]) {
+                objects.removeElementAt(objects.size() - 1);
+                objects.addElement(new ByteStream(Base64.decode(new String(content.getBuf(), 0, content.size()))));
+                content.reset();                
                 
             } else if (me instanceof Integer) {
                 objects.removeElementAt(objects.size() - 1);
@@ -150,22 +160,47 @@ class SOAP extends XMLRPC {
     }
 
     /** Appends the SOAP representation of <code>o</code> to <code>sb</code> */
-    void appendObject(String name, Object o, StringBuffer sb) {
+    void appendObject(String name, Object o, StringBuffer sb) throws JavaScriptException {
         if (o instanceof Number) {
             if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) {
                 sb.append("                <" + name + " xsi:type=\"xsd:int\">");
                 sb.append(((Number)o).intValue());
-                sb.append("</" + name + ">\n");
+                sb.append("</" + name + ">\r\n");
             } else {
                 sb.append("                <" + name + " xsi:type=\"xsd:double\">");
                 sb.append(o);
-                sb.append("</" + name + ">\n");
+                sb.append("</" + name + ">\r\n");
             }
 
         } else if (o instanceof Boolean) {
             sb.append("                <" + name + " xsi:type=\"xsd:boolean\">");
             sb.append(((Boolean)o).booleanValue() ? "true" : "false");
-            sb.append("</" + name + ">\n");
+            sb.append("</" + name + ">\r\n");
+
+        } else if (o instanceof ByteStream) {
+            try {
+                sb.append("                <" + name + " xsi:type=\"SOAP-ENC:base64\">\r\n");
+                InputStream is = ((ByteStream)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("\r\n");
+                }
+                sb.append(((Boolean)o).booleanValue() ? "1" : "0");
+                sb.append("</" + name + ">\r\n");
+            } catch (IOException e) {
+                if (Log.on) Log.log(this, "caught IOException while attempting to send a ByteStream via SOAP");
+                if (Log.on) Log.log(this, e);
+                throw new JavaScriptException("caught IOException while attempting to send a ByteStream via SOAP");
+            }
 
         } else if (o instanceof String) {
             sb.append("                <" + name + " xsi:type=\"xsd:string\">");
@@ -184,14 +219,14 @@ class SOAP extends XMLRPC {
                     i = oldi = i + 1;
                 }
             }
-            sb.append("</" + name + ">\n");
+            sb.append("</" + name + ">\r\n");
 
         } else if (o instanceof NativeArray) {
             NativeArray na = (NativeArray)o;
             sb.append("                <" + name + " SOAP-ENC:arrayType=\"xsd:ur-type[" + na.jsGet_length() + "]\">");
             for(int i=0; i<na.jsGet_length(); i++)
                 appendObject("item", na.get(i, na), sb);
-            sb.append("</" + name + ">\n");
+            sb.append("</" + name + ">\r\n");
 
         } else if (o instanceof Scriptable && !(o instanceof Undefined)) {
             Scriptable s = (Scriptable)o;
@@ -199,62 +234,37 @@ class SOAP extends XMLRPC {
             Object[] ids = s.getIds();
             for(int i=0; i<ids.length; i++)
                 appendObject(ids[i].toString(), s.get(ids[i].toString(), s), sb);
-            sb.append("</" + name + ">\n");
+            sb.append("</" + name + ">\r\n");
         }
     }
 
-    protected Object send(Object[] args, BufferedReader br, PrintWriter ps) throws IOException, JavaScriptException {
-
+    protected String send(Object[] args, HTTP http) throws JavaScriptException, IOException {
         // build up the request
         StringBuffer content = new StringBuffer();
-        content.append("<?xml version=\"1.0\"?>\n");
-        content.append("<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\n");
-        content.append("                   xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\n");
-        content.append("                   xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n");
-        content.append("                   xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\n");
-        content.append("                   xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">\n");
-        content.append("<SOAP-ENV:Body>\n");
+        content.append("SOAPAction: " + action + "\r\n\r\n");
+        content.append("<?xml version=\"1.0\"?>\r\n");
+        content.append("<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
+        content.append("                   xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
+        content.append("                   xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n");
+        content.append("                   xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\r\n");
+        content.append("                   xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">\r\n");
+        content.append("<SOAP-ENV:Body>\r\n");
         content.append("    <");
         content.append(methodname);
         content.append(nameSpace != null ? " xmlns=\"" + nameSpace + "\"" : "");
-        content.append(">\n");
+        content.append(">\r\n");
         if (args.length > 0) {
             Object[] o = ((Scriptable)args[0]).getIds();
             for(int i=0; i<o.length; i++)
                 appendObject(o[i].toString(), ((Scriptable)args[0]).get(o[i].toString(), (Scriptable)args[0]), content);
         }
-        content.append("    </" + methodname + "></SOAP-ENV:Body></SOAP-ENV:Envelope>");
-        
-        // send it
-        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; charset=\"us-ascii\"\r\n");
-        ps.print("Content-length: " + (content.length() + 1) + "\r\n");
-        ps.print("SOAPAction: " + (action == null ? filename : action) + "\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((Scriptable)objects.elementAt(0));
-        if (objects.size() == 0) return null;
-        return objects.elementAt(0);
+        content.append("    </" + methodname + "></SOAP-ENV:Body></SOAP-ENV:Envelope>\r\n");
+        return content.toString();
     }
 
-    SOAP(String urlstr, String methodname, String action, String nameSpace) {
-        super(urlstr, methodname);
+    SOAP(String url, String methodname, String action, String nameSpace) { this(url, methodname, new HTTP(url), action, nameSpace); }
+    SOAP(String url, String methodname, HTTP http, String action, String nameSpace) {
+        super(url, methodname, http);
         this.action = action;
         this.nameSpace = nameSpace;
     }