1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
7 import org.mozilla.javascript.*;
9 import org.bouncycastle.util.encoders.Base64;
12 * A partial RPC-style SOAP 1.1 client. Implemented from the SOAP 1.1
13 * Spec and Dave Winer's "SOAP for Busy Developers". This class
14 * extends XMLRPC in order to share some networking logic.
16 * Currently unsupported features/hacks:
17 * <ul><li> Multi-ref data and circular references
18 * <li> 'Document Style'
22 class SOAP extends XMLRPC {
24 /** the desired content of the SOAPAction header */
27 /** the namespace to use */
28 String nameSpace = null;
30 /** When you get a property from an SOAP, it just returns another SOAP with the property name tacked onto methodname. */
31 public Object get(String name, Scriptable start) {
32 return new SOAP(url.toString(), (methodname.equals("") ? "" : methodname + ".") + name, http, action, nameSpace);
36 // Methods to Recieve and parse SOAP Responses ////////////////////////////////////////////////////
38 public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
41 if (name.equals("SOAP-ENV:Envelope")) return;
42 if (name.equals("SOAP-ENV:Body")) return;
43 if (name.equals("SOAP-ENV:Fault")) fault = true;
45 // add a generic struct; we'll change this if our type is different
46 objects.addElement(new JSObject(false));
48 for(int i=0; i<keys.length; i++) {
50 String value = vals[i].toString();
51 if (key.endsWith("ype")) {
52 if (value.endsWith("boolean")) {
53 objects.removeElementAt(objects.size() - 1);
54 objects.addElement(Boolean.FALSE);
55 } else if (value.endsWith("int")) {
56 objects.removeElementAt(objects.size() - 1);
57 objects.addElement(new Integer(0));
58 } else if (value.endsWith("double")) {
59 objects.removeElementAt(objects.size() - 1);
60 objects.addElement(new Double(0.0));
61 } else if (value.endsWith("string")) {
62 objects.removeElementAt(objects.size() - 1);
63 objects.addElement("");
64 } else if (value.endsWith("base64")) {
65 objects.removeElementAt(objects.size() - 1);
66 objects.addElement(new byte[] { });
67 } else if (value.endsWith("null")) {
68 objects.removeElementAt(objects.size() - 1);
69 objects.addElement(null);
70 } else if (value.endsWith("arrayType") || value.endsWith("Array") || key.endsWith("arrayType")) {
71 objects.removeElementAt(objects.size() - 1);
72 objects.addElement(Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, new Object[] { } ));
78 public void endElement(String name, int line, int col) {
80 if (name.equals("SOAP-ENV:Envelope")) return;
81 if (name.equals("SOAP-ENV:Body")) return;
83 if (content.size() > 0 && content.toString().trim().length() > 0) {
86 Object me = objects.elementAt(objects.size() - 1);
88 if (fault || me instanceof String) {
89 objects.removeElementAt(objects.size() - 1);
90 objects.addElement(new String(content.getBuf(), 0, content.size()).intern());
93 } else if (me instanceof byte[]) {
94 objects.removeElementAt(objects.size() - 1);
95 objects.addElement(new ByteStream(Base64.decode(new String(content.getBuf(), 0, content.size()))));
98 } else if (me instanceof Integer) {
99 objects.removeElementAt(objects.size() - 1);
100 objects.addElement(new Integer(new String(content.getBuf(), 0, content.size())));
103 } else if (me instanceof Boolean) {
104 objects.removeElementAt(objects.size() - 1);
105 String s = new String(content.getBuf(), 0, content.size()).trim();
106 if (s.equals("1") || s.equals("true")) objects.addElement(Boolean.TRUE);
107 else objects.addElement(Boolean.FALSE);
110 } else if (me instanceof Double) {
111 objects.removeElementAt(objects.size() - 1);
112 objects.addElement(new Double(new String(content.getBuf(), 0, content.size())));
116 // okay, we got PCDATA for what is supposedly a
117 // struct... somebody's not adding their type info...
118 String s = new String(content.getBuf(), 0, content.size()).trim();
119 boolean hasdot = false;
120 for(int i=0; i<s.length(); i++) {
121 if (s.charAt(i) == '.') hasdot = true;
122 if (!Character.isDigit(s.charAt(i))) {
123 objects.removeElementAt(objects.size() - 1);
124 objects.addElement(s);
129 objects.removeElementAt(objects.size() - 1);
130 objects.addElement(new Double(s));
132 objects.removeElementAt(objects.size() - 1);
133 objects.addElement(new Integer(s));
141 Object me = objects.elementAt(objects.size() - 1);
144 Object parent = objects.size() > 1 ? objects.elementAt(objects.size() - 2) : null;
146 // we want to fold stuff back into the fault object
147 if (objects.size() < 2) return;
149 // our parent "should" be an aggregate type -- add ourselves to it.
150 if (parent != null && parent instanceof NativeArray) {
151 objects.removeElementAt(objects.size() - 1);
152 ((NativeArray)parent).put((int)((NativeArray)parent).jsGet_length(), (NativeArray)parent, me);
154 } else if (parent != null && parent instanceof Scriptable) {
155 objects.removeElementAt(objects.size() - 1);
156 ((Scriptable)parent).put(name, (Scriptable)parent, me);
162 /** Appends the SOAP representation of <code>o</code> to <code>sb</code> */
163 void appendObject(String name, Object o, StringBuffer sb) throws JavaScriptException {
164 if (o instanceof Number) {
165 if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) {
166 sb.append(" <" + name + " xsi:type=\"xsd:int\">");
167 sb.append(((Number)o).intValue());
168 sb.append("</" + name + ">\r\n");
170 sb.append(" <" + name + " xsi:type=\"xsd:double\">");
172 sb.append("</" + name + ">\r\n");
175 } else if (o instanceof Boolean) {
176 sb.append(" <" + name + " xsi:type=\"xsd:boolean\">");
177 sb.append(((Boolean)o).booleanValue() ? "true" : "false");
178 sb.append("</" + name + ">\r\n");
180 } else if (o instanceof ByteStream) {
182 sb.append(" <" + name + " xsi:type=\"SOAP-ENC:base64\">\r\n");
183 InputStream is = ((ByteStream)o).getInputStream();
184 byte[] buf = new byte[54];
186 int numread = is.read(buf, 0, 54);
187 if (numread == -1) break;
188 byte[] writebuf = buf;
189 if (numread < buf.length) {
190 writebuf = new byte[numread];
191 System.arraycopy(buf, 0, writebuf, 0, numread);
194 sb.append(new String(Base64.encode(writebuf)));
197 sb.append(((Boolean)o).booleanValue() ? "1" : "0");
198 sb.append("</" + name + ">\r\n");
199 } catch (IOException e) {
200 if (Log.on) Log.log(this, "caught IOException while attempting to send a ByteStream via SOAP");
201 if (Log.on) Log.log(this, e);
202 throw new JavaScriptException("caught IOException while attempting to send a ByteStream via SOAP");
205 } else if (o instanceof String) {
206 sb.append(" <" + name + " xsi:type=\"xsd:string\">");
207 String s = (String)o;
208 if (s.indexOf('<') == -1 && s.indexOf('&') == -1) {
211 char[] cbuf = s.toCharArray();
214 while(i < cbuf.length && cbuf[i] != '<' && cbuf[i] != '&') i++;
215 sb.append(cbuf, oldi, i);
216 if (i == cbuf.length) break;
217 if (cbuf[i] == '<') sb.append("<");
218 else if (cbuf[i] == '&') sb.append("&");
222 sb.append("</" + name + ">\r\n");
224 } else if (o instanceof NativeArray) {
225 NativeArray na = (NativeArray)o;
226 sb.append(" <" + name + " SOAP-ENC:arrayType=\"xsd:ur-type[" + na.jsGet_length() + "]\">");
227 for(int i=0; i<na.jsGet_length(); i++)
228 appendObject("item", na.get(i, na), sb);
229 sb.append("</" + name + ">\r\n");
231 } else if (o instanceof Scriptable && !(o instanceof Undefined)) {
232 Scriptable s = (Scriptable)o;
233 sb.append(" <" + name + ">");
234 Object[] ids = s.getIds();
235 for(int i=0; i<ids.length; i++)
236 appendObject(ids[i].toString(), s.get(ids[i].toString(), s), sb);
237 sb.append("</" + name + ">\r\n");
241 protected String send(Object[] args, HTTP http) throws JavaScriptException, IOException {
242 // build up the request
243 StringBuffer content = new StringBuffer();
244 content.append("SOAPAction: " + action + "\r\n\r\n");
245 content.append("<?xml version=\"1.0\"?>\r\n");
246 content.append("<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
247 content.append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
248 content.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n");
249 content.append(" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\r\n");
250 content.append(" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">\r\n");
251 content.append("<SOAP-ENV:Body>\r\n");
252 content.append(" <");
253 content.append(methodname);
254 content.append(nameSpace != null ? " xmlns=\"" + nameSpace + "\"" : "");
255 content.append(">\r\n");
256 if (args.length > 0) {
257 Object[] o = ((Scriptable)args[0]).getIds();
258 for(int i=0; i<o.length; i++)
259 appendObject(o[i].toString(), ((Scriptable)args[0]).get(o[i].toString(), (Scriptable)args[0]), content);
261 content.append(" </" + methodname + "></SOAP-ENV:Body></SOAP-ENV:Envelope>\r\n");
262 return content.toString();
265 SOAP(String url, String methodname, String action, String nameSpace) { this(url, methodname, new HTTP(url), action, nameSpace); }
266 SOAP(String url, String methodname, HTTP http, String action, String nameSpace) {
267 super(url, methodname, http);
268 this.action = action;
269 this.nameSpace = nameSpace;