1 4 5 9 10 package org.openlaszlo.remote.soap; 11 12 import java.io.*; 13 import java.util.*; 14 import javax.xml.namespace.QName ; 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 program.push("__LZstubload"); 42 program.push(1); 43 44 45 program.push("stubinfo"); 48 { 49 program.push("service"); 52 DataCommon.pushStringData(service.getServiceName(), body, dc); 53 54 program.push("port"); 57 DataCommon.pushStringData(service.getPort(), body, dc); 58 59 program.push("wsdl"); 62 DataCommon.pushStringData(service.getWSDL(), body, dc); 63 64 program.push("__LZctypes"); 67 pushComplexTypeInfo(program, service.getSchemaComplexTypes(), dc); 68 69 program.push("__LZnamespace"); 72 program.push(service.getTargetNS()); 73 } 74 program.push(5); 75 body.writeByte(Actions.InitObject); 76 77 78 program.push("stub"); 80 { 81 int count = 0; 82 Map operations = service.getOperations(); 87 Iterator iter = operations.keySet().iterator(); 88 while (iter.hasNext()) { 89 90 String opName = (String )iter.next(); 91 LZSOAPOperation op = (LZSOAPOperation)operations.get(opName); 92 93 if (mLogger.isDebugEnabled()) { 94 mLogger.debug("adding operation: " + opName); 95 } 96 97 count++; 100 DataCommon.pushStringData(opName, body, dc); 101 body.writeByte(Actions.DefineFunction); 102 body.writeWord(5); body.writeByte(0); body.writeWord(0); { 107 108 FlashBuffer fbuf = new FlashBuffer(500); 109 Program fprog = new Program(fbuf); 110 111 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 136 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 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 { 154 DataCommon.pushStringData("parts", fbuf, dc); 160 pushParts(fprog, op.getInputMessage(), op.getStyle(), dc); 161 162 DataCommon.pushStringData("opstyle", fbuf, dc); 164 DataCommon.pushStringData(op.getStyle(), fbuf, dc); 165 166 DataCommon.pushStringData("operation", fbuf, dc); 168 DataCommon.pushStringData(opName, fbuf, dc); 169 170 DataCommon.pushStringData("port", fbuf, dc); 172 DataCommon.pushStringData(service.getPort(), fbuf, dc); 173 174 DataCommon.pushStringData("service", fbuf, dc); 176 DataCommon.pushStringData(service.getServiceName(), fbuf, dc); 177 178 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 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 DataCommon.pushStringData("arguments", fbuf, dc); 196 fprog.getVar(); 197 fprog.push(0); 198 fbuf.writeByte(Actions.GetMember); 199 200 DataCommon.pushStringData("arguments", fbuf, dc); 202 fprog.getVar(); 203 fprog.push(1); 204 fbuf.writeByte(Actions.GetMember); 205 206 fprog.push(6); 208 209 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 fbuf.writeByte(Actions.Return); 220 221 body.writeWord(fbuf.pos); 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 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 byte pooldata[] = DataCommon.makeStringPool(dc); 248 final int MISC = 64; 250 FlashBuffer out = new FlashBuffer(body.getSize() + pooldata.length + MISC); 251 out.writeByte( Actions.ConstantPool ); 253 out.writeWord( pooldata.length + 2 ); out.writeWord( dc.cpool.size() ); out.writeArray( pooldata, 0, pooldata.length); out.writeArray(body.getBuf(), 0, body.getSize()); 258 259 return new Program(out); 260 } 261 262 static void pushParts(Program program, LZSOAPMessage inMesg, String 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 name = part.getName(); 275 String element = part.getElement(); 276 ComplexType type = part.getType(); 277 QName typeName = ( type != null ? type.getName() : null ); 278 279 LZSOAPUtils.pushQName(program, typeName, dc); 280 if (style.equals("rpc")) { 281 LZSOAPUtils.pushString(program, name, dc); 283 } else { 284 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 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 DataCommon.pushStringData("ns", body, dc); 320 DataCommon.pushStringData(ct.getName().getNamespaceURI(), body, dc); 321 322 DataCommon.pushStringData("type", body, dc); 324 DataCommon.pushStringData(ct.getTypeString(), body, dc); 325 326 DataCommon.pushStringData("typeQ", body, dc); 328 LZSOAPUtils.pushQName(program, ct.getArrayItemTypeQName(), dc); 329 330 DataCommon.pushStringData("members", body, dc); 332 pushMembers(program, ct.getMembers(), dc); 333 334 ComplexType base = ct.getBase(); 336 QName 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 key = (String )entry.getKey(); 368 QName value = (QName )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 381 public static FlashFile createObjectFile(LZSOAPService service, int swfnum) 382 throws IOException { 383 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 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 |