KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > remote > soap > ClientSOAPService


1 /******************************************************************************
2  * ClientSOAPService.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.remote.soap;
11
12 import java.io.*;
13 import java.util.*;
14 import javax.xml.namespace.QName JavaDoc;
15
16 import org.openlaszlo.iv.flash.api.*;
17 import org.openlaszlo.iv.flash.api.action.*;
18 import org.openlaszlo.iv.flash.util.*;
19 import org.openlaszlo.utils.*;
20 import org.openlaszlo.xml.internal.DataCommon;
21 import org.openlaszlo.xml.internal.DataContext;
22 import org.apache.log4j.Logger;
23
24
25 public class ClientSOAPService
26 {
27     public static Logger mLogger = Logger.getLogger(ClientSOAPService.class);
28
29     public static Program createObjectProgram(LZSOAPService service) {
30
31         int size = 4096;
32         FlashBuffer body = new FlashBuffer(size);
33         Program program = new Program(body);
34         DataContext dc = new DataContext();
35
36
37         // object.__LZstubload
38
//------------------------------------------------------------
39
// Tells client data returned is an object
40
// this.__LZstubload = true
41
program.push("__LZstubload");
42         program.push(1);
43
44
45         // SOAP specific information
46
// object.stubinfo
47
program.push("stubinfo");
48         {
49             //------------------------------------------------------------
50
// this.service = service.getServiceName()
51
program.push("service");
52             DataCommon.pushStringData(service.getServiceName(), body, dc);
53
54             //------------------------------------------------------------
55
// this.port = service.getPort()
56
program.push("port");
57             DataCommon.pushStringData(service.getPort(), body, dc);
58
59             //------------------------------------------------------------
60
// this.wsdl = service.getWSDL()
61
program.push("wsdl");
62             DataCommon.pushStringData(service.getWSDL(), body, dc);
63
64             //------------------------------------------------------------
65
// this.ctypes = <complexTypeInfoObject>
66
program.push("__LZctypes");
67             pushComplexTypeInfo(program, service.getSchemaComplexTypes(), dc);
68
69             //------------------------------------------------------------
70
// this._namespace = namespace /* target namespace */
71
program.push("__LZnamespace");
72             program.push(service.getTargetNS());
73         }
74         program.push(5);
75         body.writeByte(Actions.InitObject);
76
77
78         // object.stub
79
program.push("stub");
80         {
81             int count = 0;
82             //------------------------------------------------------------
83
// Create client-side service operations. Assuming that
84
// operations won't be null.
85
//------------------------------------------------------------
86
Map operations = service.getOperations();
87             Iterator iter = operations.keySet().iterator();
88             while (iter.hasNext()) {
89
90                 String JavaDoc opName = (String JavaDoc)iter.next();
91                 LZSOAPOperation op = (LZSOAPOperation)operations.get(opName);
92
93                 if (mLogger.isDebugEnabled()) {
94                     mLogger.debug("adding operation: " + opName);
95                 }
96
97                 //------------------------------------------------------------
98
//
99
count++;
100                 DataCommon.pushStringData(opName, body, dc);
101                 body.writeByte(Actions.DefineFunction);
102                 body.writeWord(5); // length of header (1 byte + 2 words (2bytes))
103
body.writeByte(0); // function name string (if empty, it is
104
// anonymous)
105
body.writeWord(0); // number of parameters
106
{
107
108                     FlashBuffer fbuf = new FlashBuffer(500);
109                     Program fprog = new Program(fbuf);
110
111                     // arguments.callee.args gets set in the client and includes
112
// secure and secureport information.
113
//
114
// var args = arguments.callee.args;
115
DataCommon.pushStringData("args", fbuf, dc);
116                     {
117                         DataCommon.pushStringData("arguments", fbuf, dc);
118                         fprog.getVar();
119                         DataCommon.pushStringData("callee", fbuf, dc);
120                         fbuf.writeByte(Actions.GetMember);
121                         DataCommon.pushStringData("args", fbuf, dc);
122                         fbuf.writeByte(Actions.GetMember);
123                     }
124                     fprog.setVar();
125
126
127                     // _root.LzSOAP.invoke(
128
// delegate,
129
// args
130
// header
131
// opts, /* wsdl, service, port, operation, parts */
132
// this.secure,
133
// this.secureport,
134
// );
135

136                     // 6. secureport
137
DataCommon.pushStringData("args", fbuf, dc);
138                     fprog.getVar();
139                     DataCommon.pushStringData("superclass", fbuf, dc);
140                     fbuf.writeByte(Actions.GetMember);
141                     DataCommon.pushStringData("secureport", fbuf, dc);
142                     fbuf.writeByte(Actions.GetMember);
143
144                     // 5. secure
145
DataCommon.pushStringData("args", fbuf, dc);
146                     fprog.getVar();
147                     DataCommon.pushStringData("superclass", fbuf, dc);
148                     fbuf.writeByte(Actions.GetMember);
149                     DataCommon.pushStringData("secure", fbuf, dc);
150                     fbuf.writeByte(Actions.GetMember);
151
152                     // 4. opts
153
{
154                         // 6. argument array of parameter type tuples like:
155
// [
156
// [ name1, element1, type1(qname) ],
157
// [ name2, element2, type2(qname) ]
158
// ]
159
DataCommon.pushStringData("parts", fbuf, dc);
160                         pushParts(fprog, op.getInputMessage(), op.getStyle(), dc);
161
162                         // 5. operation type
163
DataCommon.pushStringData("opstyle", fbuf, dc);
164                         DataCommon.pushStringData(op.getStyle(), fbuf, dc);
165
166                         // 4. operation name
167
DataCommon.pushStringData("operation", fbuf, dc);
168                         DataCommon.pushStringData(opName, fbuf, dc);
169
170                         // 3. SOAP port
171
DataCommon.pushStringData("port", fbuf, dc);
172                         DataCommon.pushStringData(service.getPort(), fbuf, dc);
173
174                         // 2. SOAP service
175
DataCommon.pushStringData("service", fbuf, dc);
176                         DataCommon.pushStringData(service.getServiceName(), fbuf, dc);
177
178                         // 1. SOAP wsdl
179
DataCommon.pushStringData("wsdl", fbuf, dc);
180                         DataCommon.pushStringData(service.getWSDL(), fbuf, dc);
181                     }
182                     fprog.push(6);
183                     fbuf.writeByte(Actions.InitObject);
184
185
186                     // 3. requestheaders
187
DataCommon.pushStringData("args", fbuf, dc);
188                     fprog.getVar();
189                     DataCommon.pushStringData("superclass", fbuf, dc);
190                     fbuf.writeByte(Actions.GetMember);
191                     DataCommon.pushStringData("requestheaders", fbuf, dc);
192                     fbuf.writeByte(Actions.GetMember);
193
194                     // 2. arguments[0] (should be array of arguments)
195
DataCommon.pushStringData("arguments", fbuf, dc);
196                     fprog.getVar();
197                     fprog.push(0);
198                     fbuf.writeByte(Actions.GetMember);
199
200                     // 1. arguments[1] (should be delegate)
201
DataCommon.pushStringData("arguments", fbuf, dc);
202                     fprog.getVar();
203                     fprog.push(1);
204                     fbuf.writeByte(Actions.GetMember);
205
206                     // Number of parameters
207
fprog.push(6);
208
209                     // call function
210
DataCommon.pushStringData("_root", fbuf, dc);
211                     fprog.getVar();
212                     DataCommon.pushStringData("LzSOAPService", fbuf, dc);
213                     fbuf.writeByte(Actions.GetMember);
214                     DataCommon.pushStringData("invoke", fbuf, dc);
215
216                     fprog.callMethod();
217
218                     // return seqnum from invoke()
219
fbuf.writeByte(Actions.Return);
220
221                     body.writeWord(fbuf.pos); // length of defined function body
222
body.writeFOB(fbuf);
223                 }
224             }
225             program.push(count);
226             body.writeByte(Actions.InitObject);
227         }
228
229         program.push(3);
230         body.writeByte(Actions.InitObject);
231
232
233         //------------------------------------------------------------
234
// call into the viewsystem
235
program.push("_parent");
236         program.getVar();
237         program.push(2);
238         program.push("_parent");
239         program.getVar();
240         program.push("loader");
241         body.writeByte(Actions.GetMember);
242         program.push("returnData");
243         program.callMethod();
244         program.pop();
245
246         // Collect the string dictionary data
247
byte pooldata[] = DataCommon.makeStringPool(dc);
248         // 'out' is the main FlashBuffer for composing the output file
249
final int MISC = 64;
250         FlashBuffer out = new FlashBuffer(body.getSize() + pooldata.length + MISC);
251         // Write out string constant pool
252
out.writeByte( Actions.ConstantPool );
253         out.writeWord( pooldata.length + 2 ); // number of bytes in pool data + int (# strings)
254
out.writeWord( dc.cpool.size() ); // number of strings in pool
255
out.writeArray( pooldata, 0, pooldata.length); // copy the data
256
// Write out the code to build nodes
257
out.writeArray(body.getBuf(), 0, body.getSize());
258
259         return new Program(out);
260     }
261
262     static void pushParts(Program program, LZSOAPMessage inMesg, String JavaDoc style,
263                           DataContext dc) {
264
265         FlashBuffer body = program.body();
266         if (inMesg == null) {
267             LZSOAPUtils.pushNull(body);
268             return;
269         }
270
271         List parts = inMesg.getParts();
272         for (int i = parts.size()-1; i >= 0; i--) {
273             LZSOAPPart part = (LZSOAPPart)parts.get(i);
274             String JavaDoc name = part.getName();
275             String JavaDoc element = part.getElement();
276             ComplexType type = part.getType();
277             QName JavaDoc typeName = ( type != null ? type.getName() : null );
278
279             LZSOAPUtils.pushQName(program, typeName, dc);
280             if (style.equals("rpc")) {
281                 // rpc calls use the name of the part as the name of element.
282
LZSOAPUtils.pushString(program, name, dc);
283             } else {
284                 // documents use element name
285
LZSOAPUtils.pushString(program, element, dc);
286             }
287             program.push(2);
288             body.writeByte(Actions.InitArray);
289         }
290         program.push(parts.size());
291         body.writeByte(Actions.InitArray);
292     }
293
294     /**
295      * @param program Program
296      * @param ctm complex type map
297      */

298     public static void pushComplexTypeInfo(Program program, Map ctm, DataContext dc) {
299         FlashBuffer body = program.body();
300         if (ctm == null) {
301             LZSOAPUtils.pushNull(body);
302             return;
303         }
304
305         Iterator iter = ctm.entrySet().iterator();
306         if (! iter.hasNext()) {
307             LZSOAPUtils.pushNull(body);
308             return;
309         }
310
311         int nattrs = 0;
312         while (iter.hasNext()) {
313             Map.Entry entry = (Map.Entry)iter.next();
314             ComplexType ct = (ComplexType)entry.getValue();
315
316             DataCommon.pushStringData(ct.getName().getLocalPart(), body, dc);
317             {
318                 // namespace
319
DataCommon.pushStringData("ns", body, dc);
320                 DataCommon.pushStringData(ct.getName().getNamespaceURI(), body, dc);
321
322                 // type is one of simple, complex, array
323
DataCommon.pushStringData("type", body, dc);
324                 DataCommon.pushStringData(ct.getTypeString(), body, dc);
325
326                 // QName for array type; null if complex type is not an array
327
DataCommon.pushStringData("typeQ", body, dc);
328                 LZSOAPUtils.pushQName(program, ct.getArrayItemTypeQName(), dc);
329
330                 // push members
331
DataCommon.pushStringData("members", body, dc);
332                 pushMembers(program, ct.getMembers(), dc);
333
334                 // push base
335
ComplexType base = ct.getBase();
336                 QName JavaDoc baseQName = (base != null ? base.getArrayItemTypeQName() : null);
337                 DataCommon.pushStringData("base", body, dc);
338                 LZSOAPUtils.pushQName(program, baseQName, dc);
339
340                 program.push(5);
341                 body.writeByte(Actions.InitObject);
342             }
343             nattrs++;
344         }
345
346         program.push(nattrs);
347         body.writeByte(Actions.InitObject);
348     }
349
350
351     static void pushMembers(Program program, Map members, DataContext dc) {
352         FlashBuffer body = program.body();
353         if (members == null) {
354             LZSOAPUtils.pushNull(body);
355             return;
356         }
357
358         Iterator iter = members.entrySet().iterator();
359         if (! iter.hasNext()) {
360             LZSOAPUtils.pushNull(body);
361             return;
362         }
363
364         int count = 0;
365         while (iter.hasNext()) {
366             Map.Entry entry = (Map.Entry)iter.next();
367             String JavaDoc key = (String JavaDoc)entry.getKey();
368             QName JavaDoc value = (QName JavaDoc)entry.getValue();
369             DataCommon.pushStringData(key, body, dc);
370             LZSOAPUtils.pushQName(program, value, dc);
371             count++;
372         }
373
374         program.push(count);
375         body.writeByte(Actions.InitObject);
376     }
377
378
379     /**
380      */

381     public static FlashFile createObjectFile(LZSOAPService service, int swfnum)
382         throws IOException {
383         // Create FlashFile object nd include action bytes
384
FlashFile file = FlashFile.newFlashFile();
385         Script s = new Script(1);
386         file.setMainScript(s);
387         file.setVersion(swfnum);
388         Frame frame = s.newFrame();
389         Program program = createObjectProgram(service);
390         frame.addFlashObject(new DoAction(program));
391         return file;
392     }
393
394     /**
395      */

396     public static byte[] createObject(LZSOAPService service, int swfnum)
397         throws IOException {
398
399         int i = 0;
400         try {
401             FlashFile file = createObjectFile(service, swfnum);
402             FlashOutput fob = file.generate();
403             byte[] buf = new byte[fob.getSize()];
404             System.arraycopy(fob.getBuf(), 0, buf, 0, fob.getSize());
405             return buf;
406         } catch (IVException e) {
407             throw new ChainedException(e);
408         } catch (IOException e) {
409             mLogger.error("io error creating object SWF: " + e.getMessage());
410             throw e;
411         }
412     }
413
414 }
415
Popular Tags