5e6d46302f8395537fb0b59a12473385275379ff
[org.ibex.core.git] / src / org / xwt / SOAP.java
1 // FIXME
2 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
3 package org.xwt;
4
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8 import org.xwt.js.*;
9 import org.xwt.util.*;
10 import org.bouncycastle.util.encoders.Base64;
11
12 /**
13  *  A partial RPC-style SOAP 1.1 client. Implemented from the SOAP 1.1
14  *  Spec and Dave Winer's "SOAP for Busy Developers". This class
15  *  extends XMLRPC in order to share some networking logic.
16  *
17  *  Currently unsupported features/hacks:
18  *  <ul><li> Multi-ref data and circular references
19  *      <li> 'Document Style'
20  *      <li> WSDL support
21  *  </ul>
22  */
23 class SOAP /*extends XMLRPC*/ {
24     /*
25     /&* the desired content of the SOAPAction header &/
26     String action = null;
27
28     /&* the namespace to use &/
29     String nameSpace = null;
30
31     /&* When you get a property from an SOAP, it just returns another SOAP with the property name tacked onto methodname. &/
32     public Object get(String name) {
33         return new SOAP(url.toString(), (methodname.equals("") ? "" : methodname + ".") + name, http, action, nameSpace);
34     }
35
36
37     // Methods to Recieve and parse SOAP Responses ////////////////////////////////////////////////////
38
39     public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
40
41         content.reset();
42         if (name.equals("SOAP-ENV:Envelope")) return;
43         if (name.equals("SOAP-ENV:Body")) return;
44         if (name.equals("SOAP-ENV:Fault")) fault = true;
45  
46         // add a generic struct; we'll change this if our type is different
47         objects.addElement(new JS());
48
49         for(int i=0; i<keys.length; i++) {
50             String key = keys[i];
51             String value = vals[i].toString();
52             if (key.endsWith("ype")) {
53                 if (value.endsWith("boolean")) {
54                     objects.removeElementAt(objects.size() - 1);
55                     objects.addElement(Boolean.FALSE);
56                 } else if (value.endsWith("int")) {
57                     objects.removeElementAt(objects.size() - 1);
58                     objects.addElement(new Integer(0));
59                 } else if (value.endsWith("double")) {
60                     objects.removeElementAt(objects.size() - 1);
61                     objects.addElement(new Double(0.0));
62                 } else if (value.endsWith("string")) {
63                     objects.removeElementAt(objects.size() - 1);
64                     objects.addElement("");
65                 } else if (value.endsWith("base64")) {
66                     objects.removeElementAt(objects.size() - 1);
67                     objects.addElement(new byte[] { });
68                 } else if (value.endsWith("null")) {
69                     objects.removeElementAt(objects.size() - 1);
70                     objects.addElement(null);
71                 } else if (value.endsWith("arrayType") || value.endsWith("JSArray") || key.endsWith("arrayType")) {
72                     objects.removeElementAt(objects.size() - 1);
73                     objects.addElement(new JSArray());
74                 }
75             }
76         }
77     }
78
79     public void endElement(String name, int line, int col) {
80
81         if (name.equals("SOAP-ENV:Envelope")) return;
82         if (name.equals("SOAP-ENV:Body")) return;
83
84         if (content.size() > 0 && content.toString().trim().length() > 0) {
85
86             // remove ourselves
87             Object me = objects.elementAt(objects.size() - 1);
88
89             if (fault || me instanceof String) {
90                 objects.removeElementAt(objects.size() - 1);
91                 objects.addElement(new String(content.getBuf(), 0, content.size()).intern());
92                 content.reset();
93
94             } else if (me instanceof byte[]) {
95                 objects.removeElementAt(objects.size() - 1);
96                 objects.addElement(new Res.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), null));
97                 content.reset();                
98
99             } else if (me instanceof Integer) {
100                 objects.removeElementAt(objects.size() - 1);
101                 objects.addElement(new Integer(new String(content.getBuf(), 0, content.size())));
102                 content.reset();
103                 
104             } else if (me instanceof Boolean) {
105                 objects.removeElementAt(objects.size() - 1);
106                 String s = new String(content.getBuf(), 0, content.size()).trim();
107                 if (s.equals("1") || s.equals("true")) objects.addElement(Boolean.TRUE);
108                 else objects.addElement(Boolean.FALSE);
109                 content.reset();
110                 
111             } else if (me instanceof Double) {
112                 objects.removeElementAt(objects.size() - 1);
113                 objects.addElement(new Double(new String(content.getBuf(), 0, content.size())));
114                 content.reset();
115                 
116             } else {
117                 // okay, we got PCDATA for what is supposedly a
118                 // struct... somebody's not adding their type info...
119                 String s = new String(content.getBuf(), 0, content.size()).trim();
120                 boolean hasdot = false;
121                 for(int i=0; i<s.length(); i++) {
122                     if (s.charAt(i) == '.') hasdot = true;
123                     if (!Character.isDigit(s.charAt(i))) {
124                         objects.removeElementAt(objects.size() - 1);
125                         objects.addElement(s);
126                         return;
127                     }
128                 }
129                 if (hasdot) {
130                     objects.removeElementAt(objects.size() - 1);
131                     objects.addElement(new Double(s));
132                 } else {
133                     objects.removeElementAt(objects.size() - 1);
134                     objects.addElement(new Integer(s));
135                 }
136                 content.reset();
137             }
138
139         }
140         
141         // remove ourselves
142         Object me = objects.elementAt(objects.size() - 1);
143
144         // find our parent
145         Object parent = objects.size() > 1 ? objects.elementAt(objects.size() - 2) : null;
146
147         // we want to fold stuff back into the fault object
148         if (objects.size() < 2) return;
149
150         // our parent "should" be an aggregate type -- add ourselves to it.
151         if (parent != null && parent instanceof JSArray) {
152             objects.removeElementAt(objects.size() - 1);
153             ((JSArray)parent).addElement(me);
154
155         } else if (parent != null && parent instanceof JS) {
156             objects.removeElementAt(objects.size() - 1);
157             try {
158                 ((JS)parent).put(name, me);
159             } catch (JSExn e) {
160                 throw new Error("this should never happen");
161             }
162
163         }
164
165     }
166
167     /&* Appends the SOAP representation of <code>o</code> to <code>sb</code> &/
168     void appendObject(String name, Object o, StringBuffer sb) throws JSExn {
169         if (o instanceof Number) {
170             if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) {
171                 sb.append("                <" + name + " xsi:type=\"xsd:int\">");
172                 sb.append(((Number)o).intValue());
173                 sb.append("</" + name + ">\r\n");
174             } else {
175                 sb.append("                <" + name + " xsi:type=\"xsd:double\">");
176                 sb.append(o);
177                 sb.append("</" + name + ">\r\n");
178             }
179
180         } else if (o instanceof Boolean) {
181             sb.append("                <" + name + " xsi:type=\"xsd:boolean\">");
182             sb.append(((Boolean)o).booleanValue() ? "true" : "false");
183             sb.append("</" + name + ">\r\n");
184
185         } else if (o instanceof Res) {
186             try {
187                 sb.append("                <" + name + " xsi:type=\"SOAP-ENC:base64\">\r\n");
188                 InputStream is = ((Res)o).getInputStream();
189                 byte[] buf = new byte[54];
190                 while(true) {
191                     int numread = is.read(buf, 0, 54);
192                     if (numread == -1) break;
193                     byte[] writebuf = buf;
194                     if (numread < buf.length) {
195                         writebuf = new byte[numread];
196                         System.arraycopy(buf, 0, writebuf, 0, numread);
197                     }
198                     sb.append("              ");
199                     sb.append(new String(Base64.encode(writebuf)));
200                     sb.append("\r\n");
201                 }
202                 sb.append(((Boolean)o).booleanValue() ? "1" : "0");
203                 sb.append("</" + name + ">\r\n");
204             } catch (IOException e) {
205                 if (Log.on) Log.log(this, "caught IOException while attempting to send a ByteStream via SOAP");
206                 if (Log.on) Log.log(this, e);
207                 throw new JSExn("caught IOException while attempting to send a ByteStream via SOAP");
208             }
209
210         } else if (o instanceof String) {
211             sb.append("                <" + name + " xsi:type=\"xsd:string\">");
212             String s = (String)o;
213             if (s.indexOf('<') == -1 && s.indexOf('&') == -1) {
214                 sb.append(s);
215             } else {
216                 char[] cbuf = s.toCharArray();
217                 while(true) {
218                     int oldi = 0, i=0;
219                     while(i < cbuf.length && cbuf[i] != '<' && cbuf[i] != '&') i++;
220                     sb.append(cbuf, oldi, i);
221                     if (i == cbuf.length) break;
222                     if (cbuf[i] == '<') sb.append("&lt;");
223                     else if (cbuf[i] == '&') sb.append("&amp;");
224                     i = oldi = i + 1;
225                 }
226             }
227             sb.append("</" + name + ">\r\n");
228
229         } else if (o instanceof JSArray) {
230             JSArray a = (JSArray)o;
231             sb.append("                <" + name + " SOAP-ENC:arrayType=\"xsd:ur-type[" + a.length() + "]\">");
232             for(int i=0; i<a.length(); i++) appendObject("item", a.elementAt(i), sb);
233             sb.append("</" + name + ">\r\n");
234
235         } else if (o instanceof JS) {
236             JS j = (JS)o;
237             sb.append("                <" + name + ">");
238             Enumeration e = j.keys();
239             while(e.hasMoreElements()) {
240                 Object key = e.nextElement();
241                 appendObject((String)key, j.get(key), sb);
242             }
243             sb.append("</" + name + ">\r\n");
244
245         }
246     }
247
248     protected String send(JSArray args, HTTP http) throws JSExn, IOException {
249         // build up the request
250         StringBuffer content = new StringBuffer();
251         content.append("SOAPAction: " + action + "\r\n\r\n");
252         content.append("<?xml version=\"1.0\"?>\r\n");
253         content.append("<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
254         content.append("                   xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
255         content.append("                   xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n");
256         content.append("                   xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\r\n");
257         content.append("                   xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">\r\n");
258         content.append("<SOAP-ENV:Body>\r\n");
259         content.append("    <");
260         content.append(methodname);
261         content.append(nameSpace != null ? " xmlns=\"" + nameSpace + "\"" : "");
262         content.append(">\r\n");
263         if (args.length() > 0) {
264             Enumeration e = ((JS)args.elementAt(0)).keys();
265             while(e.hasMoreElements()) {
266                 Object key = e.nextElement();
267                 appendObject((String)key, ((JS)args.elementAt(0)).get(key), content);
268             }
269         }
270         content.append("    </" + methodname + "></SOAP-ENV:Body></SOAP-ENV:Envelope>\r\n");
271         return content.toString();
272     }
273
274     SOAP(String url, String methodname, String action, String nameSpace) { this(url, methodname, new HTTP(url), action, nameSpace); }
275     SOAP(String url, String methodname, HTTP http, String action, String nameSpace) {
276         super(url, methodname, http);
277         this.action = action;
278         this.nameSpace = nameSpace;
279     }
280 */
281 }