1
2 package org.xwt;
3
4 import java.io.*;
5 import java.net.*;
6 import java.util.*;
7 import org.xwt.js.*;
8 import org.xwt.util.*;
9 import org.bouncycastle.util.encoders.Base64;
10
11
22 class SOAP extends XMLRPC {
23
24
25 String action = null;
26
27
28 String nameSpace = null;
29
30
31 public Object get(String name) {
32 return new SOAP(url.toString(), (method.equals("") ? "" : method + ".") + name, http, action, nameSpace);
33 }
34
35
36
37
38 public void startElement(String name, String[] keys, Object[] vals, int line, int col) {
39
40 content.reset();
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;
44
45
46 objects.addElement(new JS());
47
48 for(int i=0; i<keys.length; i++) {
49 String key = keys[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("JSArray") || key.endsWith("arrayType")) {
71 objects.removeElementAt(objects.size() - 1);
72 objects.addElement(new JSArray());
73 }
74 }
75 }
76 }
77
78 public void endElement(String name, int line, int col) {
79
80 if (name.equals("SOAP-ENV:Envelope")) return;
81 if (name.equals("SOAP-ENV:Body")) return;
82
83 if (content.size() > 0 && content.toString().trim().length() > 0) {
84
85
86 Object me = objects.elementAt(objects.size() - 1);
87
88 if (fault || me instanceof String) {
89 objects.removeElementAt(objects.size() - 1);
90 objects.addElement(new String(content.getBuf(), 0, content.size()).intern());
91 content.reset();
92
93 } else if (me instanceof byte[]) {
94 objects.removeElementAt(objects.size() - 1);
95 objects.addElement(new Stream.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), null));
96 content.reset();
97
98 } else if (me instanceof Integer) {
99 objects.removeElementAt(objects.size() - 1);
100 objects.addElement(new Integer(new String(content.getBuf(), 0, content.size())));
101 content.reset();
102
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);
108 content.reset();
109
110 } else if (me instanceof Double) {
111 objects.removeElementAt(objects.size() - 1);
112 objects.addElement(new Double(new String(content.getBuf(), 0, content.size())));
113 content.reset();
114
115 } else {
116
117
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);
125 return;
126 }
127 }
128 if (hasdot) {
129 objects.removeElementAt(objects.size() - 1);
130 objects.addElement(new Double(s));
131 } else {
132 objects.removeElementAt(objects.size() - 1);
133 objects.addElement(new Integer(s));
134 }
135 content.reset();
136 }
137
138 }
139
140
141 Object me = objects.elementAt(objects.size() - 1);
142
143
144 Object parent = objects.size() > 1 ? objects.elementAt(objects.size() - 2) : null;
145
146
147 if (objects.size() < 2) return;
148
149
150 if (parent != null && parent instanceof JSArray) {
151 objects.removeElementAt(objects.size() - 1);
152 ((JSArray)parent).addElement(me);
153
154 } else if (parent != null && parent instanceof JS) {
155 objects.removeElementAt(objects.size() - 1);
156 try {
157 ((JS)parent).put(name, me);
158 } catch (JSExn e) {
159 throw new Error("this should never happen");
160 }
161
162 }
163
164 }
165
166
167 void appendObject(String name, Object o, StringBuffer sb) throws JSExn {
168 if (o instanceof Number) {
169 if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) {
170 sb.append(" <" + name + " xsi:type=\"xsd:int\">");
171 sb.append(((Number)o).intValue());
172 sb.append("</" + name + ">\r\n");
173 } else {
174 sb.append(" <" + name + " xsi:type=\"xsd:double\">");
175 sb.append(o);
176 sb.append("</" + name + ">\r\n");
177 }
178
179 } else if (o instanceof Boolean) {
180 sb.append(" <" + name + " xsi:type=\"xsd:boolean\">");
181 sb.append(((Boolean)o).booleanValue() ? "true" : "false");
182 sb.append("</" + name + ">\r\n");
183
184 } else if (o instanceof Stream) {
185 try {
186 sb.append(" <" + name + " xsi:type=\"SOAP-ENC:base64\">\r\n");
187 InputStream is = ((Stream)o).getInputStream();
188 byte[] buf = new byte[54];
189 while(true) {
190 int numread = is.read(buf, 0, 54);
191 if (numread == -1) break;
192 byte[] writebuf = buf;
193 if (numread < buf.length) {
194 writebuf = new byte[numread];
195 System.arraycopy(buf, 0, writebuf, 0, numread);
196 }
197 sb.append(" ");
198 sb.append(new String(Base64.encode(writebuf)));
199 sb.append("\r\n");
200 }
201 sb.append(((Boolean)o).booleanValue() ? "1" : "0");
202 sb.append("</" + name + ">\r\n");
203 } catch (IOException e) {
204 if (Log.on) Log.info(this, "caught IOException while attempting to send a ByteStream via SOAP");
205 if (Log.on) Log.info(this, e);
206 throw new JSExn("caught IOException while attempting to send a ByteStream via SOAP");
207 }
208
209 } else if (o instanceof String) {
210 sb.append(" <" + name + " xsi:type=\"xsd:string\">");
211 String s = (String)o;
212 if (s.indexOf('<') == -1 && s.indexOf('&') == -1) {
213 sb.append(s);
214 } else {
215 char[] cbuf = s.toCharArray();
216 while(true) {
217 intintdi = 0, i=0;
218 while(i < cbuf.length && cbuf[i] != '<' && cbuf[i] != '&') i++;
219 sb.append(cbuf, oldi, i);
220 if (i == cbuf.length) break;
221 if (cbuf[i] == '<') sb.append("<");
222 else if (cbuf[i] == '&') sb.append("&");
223 i = oldi = i + 1;
224 }
225 }
226 sb.append("</" + name + ">\r\n");
227
228 } else if (o instanceof JSArray) {
229 JSArray a = (JSArray)o;
230 sb.append(" <" + name + " SOAP-ENC:arrayType=\"xsd:ur-type[" + a.length() + "]\">");
231 for(int i=0; i<a.length(); i++) appendObject("item", a.elementAt(i), sb);
232 sb.append("</" + name + ">\r\n");
233
234 } else if (o instanceof JS) {
235 JS j = (JS)o;
236 sb.append(" <" + name + ">");
237 Enumeration e = j.keys();
238 while(e.hasMoreElements()) {
239 Object key = e.nextElement();
240 appendObject((String)key, j.get(key), sb);
241 }
242 sb.append("</" + name + ">\r\n");
243
244 }
245 }
246
247 protected String buildRequest(JSArray args) throws JSExn, IOException {
248
249 StringBuffer content = new StringBuffer();
250 content.append("SOAPAction: " + action + "\r\n\r\n");
251 content.append("<?xml version=\"1.0\"?>\r\n");
252 content.append("<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
253 content.append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n");
254 content.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n");
255 content.append(" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\r\n");
256 content.append(" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\">\r\n");
257 content.append("<SOAP-ENV:Body>\r\n");
258 content.append(" <");
259 content.append(method);
260 content.append(nameSpace != null ? " xmlns=\"" + nameSpace + "\"" : "");
261 content.append(">\r\n");
262 if (args.length() > 0) {
263 Enumeration e = ((JS)args.elementAt(0)).keys();
264 while(e.hasMoreElements()) {
265 Object key = e.nextElement();
266 appendObject((String)key, ((JS)args.elementAt(0)).get(key), content);
267 }
268 }
269 content.append(" </" + method + "></SOAP-ENV:Body></SOAP-ENV:Envelope>\r\n");
270 return content.toString();
271 }
272
273 SOAP(String url, String methodname, String action, String nameSpace) {
274 this(url, methodname, new HTTP(url), action, nameSpace);
275 }
276 SOAP(String url, String methodname, HTTP http, String action, String nameSpace) {
277 super(url, methodname, http);
278 this.action = action;
279 this.nameSpace = nameSpace;
280 }
281
282 }
283