1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
10 import org.ibex.util.*;
11 import org.ibex.crypto.*;
14 * A partial RPC-style SOAP 1.1 client. Implemented from the SOAP 1.1
15 * Spec and Dave Winer's "SOAP for Busy Developers". This class
16 * extends XMLRPC in order to share some networking logic.
18 * Currently unsupported features/hacks:
19 * <ul><li> Multi-ref data and circular references
20 * <li> 'Document Style'
24 public class SOAP extends XMLRPC {
26 /** the desired content of the SOAPAction header */
29 /** the namespace to use */
30 String nameSpace = null;
32 /** When you get a property from an SOAP, it just returns another SOAP with the property name tacked onto methodname. */
33 public Object get(Object name) {
34 return new SOAP(url, (method.equals("") ? "" : method + ".") + name.toString(), this, action, nameSpace); }
37 // Methods to Recieve and parse SOAP Responses ////////////////////////////////////////////////////
39 public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
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;
46 // add a generic struct; we'll change this if our type is different
47 objects.addElement(new JS.O());
49 for(int i=0; i<keys.length; 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(Script.B(true));
56 } else if (value.endsWith("int")) {
57 objects.removeElementAt(objects.size() - 1);
58 objects.addElement(Script.N(0));
59 } else if (value.endsWith("double")) {
60 objects.removeElementAt(objects.size() - 1);
61 objects.addElement(Script.N(0.0));
62 } else if (value.endsWith("string")) {
63 objects.removeElementAt(objects.size() - 1);
64 objects.addElement(Script.S(""));
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());
79 public void endElement(String name, int line, int col) {
81 if (name.equals("SOAP-ENV:Envelope")) return;
82 if (name.equals("SOAP-ENV:Body")) return;
84 if (content.size() > 0 && content.toString().trim().length() > 0) {
87 Object me = objects.elementAt(objects.size() - 1);
89 if (fault || me instanceof String) {
90 objects.removeElementAt(objects.size() - 1);
91 objects.addElement(new String(content.getBuf(), 0, content.size()).intern());
94 } else if (me instanceof byte[]) {
95 objects.removeElementAt(objects.size() - 1);
96 objects.addElement(new Fountain.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), null));
99 } else if (me instanceof Integer) {
100 objects.removeElementAt(objects.size() - 1);
101 objects.addElement(new Integer(new String(content.getBuf(), 0, content.size())));
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);
111 } else if (me instanceof Double) {
112 objects.removeElementAt(objects.size() - 1);
113 objects.addElement(new Double(new String(content.getBuf(), 0, content.size())));
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);
130 objects.removeElementAt(objects.size() - 1);
131 objects.addElement(new Double(s));
133 objects.removeElementAt(objects.size() - 1);
134 objects.addElement(new Integer(s));
142 JS me = (JS)objects.elementAt(objects.size() - 1);
145 JS parent = objects.size() > 1 ? (JS)objects.elementAt(objects.size() - 2) : (JS)null;
147 // we want to fold stuff back into the fault object
148 if (objects.size() < 2) return;
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);
155 } else if (parent != null && parent instanceof JS) {
156 objects.removeElementAt(objects.size() - 1);
158 ((JS)parent).put(Script.S(name), me);
160 throw new Error("this should never happen");
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");
175 sb.append(" <" + name + " xsi:type=\"xsd:double\">");
177 sb.append("</" + name + ">\r\n");
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");
185 } else if (o instanceof Fountain) {
187 sb.append(" <" + name + " xsi:type=\"SOAP-ENC:base64\">\r\n");
188 InputStream is = ((Fountain)o).getInputStream();
189 byte[] buf = new byte[54];
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);
199 sb.append(new String(Base64.encode(writebuf)));
202 sb.append(((Boolean)o).booleanValue() ? "1" : "0");
203 sb.append("</" + name + ">\r\n");
204 } catch (IOException e) {
205 if (Log.on) Log.info(this, "caught IOException while attempting to send a Fountain via SOAP");
206 if (Log.on) Log.info(this, e);
207 throw new JSExn("caught IOException while attempting to send a Fountain via SOAP");
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) {
216 char[] cbuf = s.toCharArray();
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("<");
223 else if (cbuf[i] == '&') sb.append("&");
227 sb.append("</" + name + ">\r\n");
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");
235 } else if (o instanceof JS) {
237 sb.append(" <" + name + ">");
238 Enumeration e = j.keys();
239 while(e.hasMoreElements()) {
240 Object key = e.nextElement();
241 appendObject((String)key, j.get((JS)key), sb);
243 sb.append("</" + name + ">\r\n");
248 protected String buildRequest(JSArray args) 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(method);
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 JS key = (JS)e.nextElement();
267 appendObject(((JSString)key).coerceToString(), ((JS)args.elementAt(0)).get(key), content);
270 content.append(" </" + method + "></SOAP-ENV:Body></SOAP-ENV:Envelope>\r\n");
271 return content.toString();
274 public SOAP(String url, String methodname, String action, String nameSpace) {
275 super(url, methodname);
276 this.action = action;
277 this.nameSpace = nameSpace;
279 public SOAP(String url, String methodname, SOAP httpSource, String action, String nameSpace) {
280 super(url, methodname, httpSource);
281 this.action = action;
282 this.nameSpace = nameSpace;