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, action, nameSpace);
36 // Methods to Recieve and parse SOAP Responses ////////////////////////////////////////////////////
38 public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
40 if (name.equals("SOAP-ENV:Envelope")) return;
41 if (name.equals("SOAP-ENV:Body")) return;
42 if (name.equals("SOAP-ENV:Fault")) fault = true;
44 // add a generic struct; we'll change this if our type is different
45 objects.addElement(new JSObject(false));
47 for(int i=0; i<keys.length; i++) {
49 String value = vals[i].toString();
50 if (key.endsWith("ype")) {
51 if (value.endsWith("boolean")) {
52 objects.removeElementAt(objects.size() - 1);
53 objects.addElement(Boolean.FALSE);
54 } else if (value.endsWith("int")) {
55 objects.removeElementAt(objects.size() - 1);
56 objects.addElement(new Integer(0));
57 } else if (value.endsWith("double")) {
58 objects.removeElementAt(objects.size() - 1);
59 objects.addElement(new Double(0.0));
60 } else if (value.endsWith("string")) {
61 objects.removeElementAt(objects.size() - 1);
62 objects.addElement("");
63 } else if (value.endsWith("base64")) {
64 objects.removeElementAt(objects.size() - 1);
65 objects.addElement(new byte[] { });
66 } else if (value.endsWith("null")) {
67 objects.removeElementAt(objects.size() - 1);
68 objects.addElement(null);
69 } else if (value.endsWith("arrayType") || value.endsWith("Array") || key.endsWith("arrayType")) {
70 objects.removeElementAt(objects.size() - 1);
71 objects.addElement(Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, new Object[] { } ));
77 public void endElement(String name, int line, int col) {
79 if (name.equals("SOAP-ENV:Envelope")) return;
80 if (name.equals("SOAP-ENV:Body")) return;
82 if (content.size() > 0 && content.toString().trim().length() > 0) {
85 Object me = objects.elementAt(objects.size() - 1);
87 if (fault || me instanceof String) {
88 objects.removeElementAt(objects.size() - 1);
89 objects.addElement(new String(content.getBuf(), 0, content.size()).intern());
92 } else if (me instanceof byte[]) {
93 objects.removeElementAt(objects.size() - 1);
94 objects.addElement(new String(Base64.decode(new String(content.getBuf(), 0, content.size()))));
97 } else if (me instanceof Integer) {
98 objects.removeElementAt(objects.size() - 1);
99 objects.addElement(new Integer(new String(content.getBuf(), 0, content.size())));
102 } else if (me instanceof Boolean) {
103 objects.removeElementAt(objects.size() - 1);
104 String s = new String(content.getBuf(), 0, content.size()).trim();
105 if (s.equals("1") || s.equals("true")) objects.addElement(Boolean.TRUE);
106 else objects.addElement(Boolean.FALSE);
109 } else if (me instanceof Double) {
110 objects.removeElementAt(objects.size() - 1);
111 objects.addElement(new Double(new String(content.getBuf(), 0, content.size())));
115 // okay, we got PCDATA for what is supposedly a
116 // struct... somebody's not adding their type info...
117 String s = new String(content.getBuf(), 0, content.size()).trim();
118 boolean hasdot = false;
119 for(int i=0; i<s.length(); i++) {
120 if (s.charAt(i) == '.') hasdot = true;
121 if (!Character.isDigit(s.charAt(i))) {
122 objects.removeElementAt(objects.size() - 1);
123 objects.addElement(s);
128 objects.removeElementAt(objects.size() - 1);
129 objects.addElement(new Double(s));
131 objects.removeElementAt(objects.size() - 1);
132 objects.addElement(new Integer(s));
140 Object me = objects.elementAt(objects.size() - 1);
143 Object parent = objects.size() > 1 ? objects.elementAt(objects.size() - 2) : null;
145 // we want to fold stuff back into the fault object
146 if (objects.size() < 2) return;
148 // our parent "should" be an aggregate type -- add ourselves to it.
149 if (parent != null && parent instanceof NativeArray) {
150 objects.removeElementAt(objects.size() - 1);
151 ((NativeArray)parent).put((int)((NativeArray)parent).jsGet_length(), (NativeArray)parent, me);
153 } else if (parent != null && parent instanceof Scriptable) {
154 objects.removeElementAt(objects.size() - 1);
155 ((Scriptable)parent).put(name, (Scriptable)parent, me);
161 /** Appends the SOAP representation of <code>o</code> to <code>sb</code> */
162 void appendObject(String name, Object o, StringBuffer sb) {
163 if (o instanceof Number) {
164 if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) {
165 sb.append(" <" + name + " xsi:type=\"xsd:int\">");
166 sb.append(((Number)o).intValue());
167 sb.append("</" + name + ">\n");
169 sb.append(" <" + name + " xsi:type=\"xsd:double\">");
171 sb.append("</" + name + ">\n");
174 } else if (o instanceof Boolean) {
175 sb.append(" <" + name + " xsi:type=\"xsd:boolean\">");
176 sb.append(((Boolean)o).booleanValue() ? "true" : "false");
177 sb.append("</" + name + ">\n");
179 } else if (o instanceof String) {
180 sb.append(" <" + name + " xsi:type=\"xsd:string\">");
181 String s = (String)o;
182 if (s.indexOf('<') == -1 && s.indexOf('&') == -1) {
185 char[] cbuf = s.toCharArray();
188 while(i < cbuf.length && cbuf[i] != '<' && cbuf[i] != '&') i++;
189 sb.append(cbuf, oldi, i);
190 if (i == cbuf.length) break;
191 if (cbuf[i] == '<') sb.append("<");
192 else if (cbuf[i] == '&') sb.append("&");
196 sb.append("</" + name + ">\n");
198 } else if (o instanceof NativeArray) {
199 NativeArray na = (NativeArray)o;
200 sb.append(" <" + name + " SOAP-ENC:arrayType=\"xsd:ur-type[" + na.jsGet_length() + "]\">");
201 for(int i=0; i<na.jsGet_length(); i++)
202 appendObject("item", na.get(i, na), sb);
203 sb.append("</" + name + ">\n");
205 } else if (o instanceof Scriptable && !(o instanceof Undefined)) {
206 Scriptable s = (Scriptable)o;
207 sb.append(" <" + name + ">");
208 Object[] ids = s.getIds();
209 for(int i=0; i<ids.length; i++)
210 appendObject(ids[i].toString(), s.get(ids[i].toString(), s), sb);
211 sb.append("</" + name + ">\n");
215 protected String send(Object[] args, HTTP http) throws JavaScriptException, IOException {
216 // build up the request
217 StringBuffer content = new StringBuffer();
218 content.append("<?xml version=\"1.0\"?>\n");
219 content.append("<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\n");
220 content.append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\n");
221 content.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n");
222 content.append(" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\n");
223 content.append(" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">\n");
224 content.append("<SOAP-ENV:Body>\n");
225 content.append(" <");
226 content.append(methodname);
227 content.append(nameSpace != null ? " xmlns=\"" + nameSpace + "\"" : "");
228 content.append(">\n");
229 if (args.length > 0) {
230 Object[] o = ((Scriptable)args[0]).getIds();
231 for(int i=0; i<o.length; i++)
232 appendObject(o[i].toString(), ((Scriptable)args[0]).get(o[i].toString(), (Scriptable)args[0]), content);
234 content.append(" </" + methodname + "></SOAP-ENV:Body></SOAP-ENV:Envelope>");
235 http.addHeader("SOAPAction", action);
236 return content.toString();
239 SOAP(String url, String methodname, String action, String nameSpace) {
240 super(url, methodname);
241 this.action = action;
242 this.nameSpace = nameSpace;