home *** CD-ROM | disk | FTP | other *** search
- package mx.rpc.soap
- {
- import flash.utils.getTimer;
- import flash.xml.XMLDocument;
- import flash.xml.XMLNode;
- import mx.logging.ILogger;
- import mx.logging.Log;
- import mx.rpc.soap.types.ICustomSOAPType;
- import mx.rpc.wsdl.WSDLEncoding;
- import mx.rpc.wsdl.WSDLMessagePart;
- import mx.rpc.wsdl.WSDLOperation;
- import mx.rpc.xml.ContentProxy;
- import mx.rpc.xml.DecodingContext;
- import mx.rpc.xml.SchemaConstants;
- import mx.rpc.xml.SchemaDatatypes;
- import mx.rpc.xml.TypeIterator;
- import mx.rpc.xml.XMLDecoder;
- import mx.utils.StringUtil;
- import mx.utils.XMLUtil;
- import mx.utils.object_proxy;
-
- use namespace object_proxy;
-
- public class SOAPDecoder extends XMLDecoder implements ISOAPDecoder
- {
- public static var PI_WHITESPACE_PATTERN:RegExp = /[\?][>]\s*[<]/g;
-
- private var _multiplePartsFormat:String;
-
- private var _forcePartArrays:Boolean;
-
- private var _headerFormat:String;
-
- private var log:ILogger;
-
- private var _resultFormat:String;
-
- private var _ignoreWhitespace:Boolean = true;
-
- private var _referencesResolved:Boolean;
-
- public var supportGenericCompoundTypes:Boolean = false;
-
- private var _wsdlOperation:WSDLOperation;
-
- private var _elementsWithId:XMLList;
-
- public function SOAPDecoder()
- {
- super();
- log = Log.getLogger("mx.rpc.soap.SOAPDecoder");
- }
-
- public function get soapConstants() : SOAPConstants
- {
- return wsdlOperation.soapConstants;
- }
-
- override public function decodeComplexRestriction(param1:XML, param2:*, param3:QName, param4:*) : void
- {
- var _loc8_:ICustomSOAPType = null;
- var _loc5_:SchemaConstants = schemaManager.schemaConstants;
- var _loc6_:String = param1.@base;
- var _loc7_:QName = schemaManager.getQNameForPrefixedName(_loc6_,param1);
- if(_loc7_ == soapConstants.soapencArrayQName)
- {
- _loc8_ = SOAPConstants.getCustomSOAPType(_loc7_);
- if(_loc8_ != null)
- {
- _loc8_.decode(this,param2,param3,param4,param1);
- return;
- }
- }
- super.decodeComplexRestriction(param1,param2,param3,param4);
- }
-
- public function set multiplePartsFormat(param1:String) : void
- {
- _multiplePartsFormat = param1;
- }
-
- private function resolveReferences(param1:XML, param2:Boolean = true) : void
- {
- var index:uint;
- var child:XML = null;
- var element:XML = null;
- var href:String = null;
- var hashPosition:int = 0;
- var matches:XMLList = null;
- var referent:XML = null;
- var root:XML = param1;
- var cleanupElementsWithIdCache:Boolean = param2;
- if(_referencesResolved)
- {
- return;
- }
- index = 0;
- if(_elementsWithId == null)
- {
- _elementsWithId = document..*.(attribute("id").length() > 0);
- }
- for each(child in root.children())
- {
- if(child.nodeKind() == "element")
- {
- element = child;
- href = getAttributeFromNode("href",element);
- if(href != null)
- {
- hashPosition = int(href.indexOf("#"));
- if(hashPosition >= 0)
- {
- href = href.substring(hashPosition + 1);
- }
- matches = _elementsWithId.(@id == href);
- if(matches.length() <= 0)
- {
- throw new Error("The element referenced by id \'" + href + "\' was not found.");
- }
- referent = matches[0];
- referent.setName(element.name());
- if(referent.hasComplexContent())
- {
- resolveReferences(referent,false);
- }
- root.replace(index,referent);
- }
- else if(element.hasComplexContent())
- {
- resolveReferences(element,false);
- }
- }
- index++;
- }
- if(cleanupElementsWithIdCache)
- {
- _elementsWithId = null;
- _referencesResolved = true;
- }
- }
-
- protected function decodeFaults(param1:XMLList) : Array
- {
- var _loc3_:XML = null;
- var _loc4_:QName = null;
- var _loc5_:String = null;
- var _loc6_:String = null;
- var _loc7_:XML = null;
- var _loc8_:String = null;
- var _loc9_:XMLList = null;
- var _loc10_:XML = null;
- var _loc11_:SOAPFault = null;
- log.debug("SOAP: Decoding SOAP response fault");
- var _loc2_:Array = [];
- for each(_loc3_ in param1)
- {
- _loc7_ = _loc3_;
- _loc9_ = _loc3_.children();
- for each(_loc10_ in _loc9_)
- {
- if(_loc10_.localName() == "faultcode")
- {
- _loc4_ = schemaManager.getQNameForPrefixedName(_loc10_.toString(),_loc10_);
- }
- else if(_loc10_.localName() == "faultstring")
- {
- _loc5_ = _loc10_.toString();
- }
- else if(_loc10_.localName() == "faultactor")
- {
- _loc8_ = _loc10_.toString();
- }
- else if(_loc10_.localName() == "detail")
- {
- if(_loc10_.hasComplexContent())
- {
- _loc6_ = _loc10_.children().toXMLString();
- }
- else
- {
- _loc6_ = _loc10_.toString();
- }
- }
- }
- _loc11_ = new SOAPFault(_loc4_,_loc5_,_loc6_,_loc7_,_loc8_);
- _loc2_.push(_loc11_);
- }
- return _loc2_;
- }
-
- override protected function preProcessXML(param1:XML) : void
- {
- if(outputEncoding.useStyle == SOAPConstants.USE_ENCODED)
- {
- resolveReferences(param1);
- }
- }
-
- public function get schemaConstants() : SchemaConstants
- {
- return schemaManager.schemaConstants;
- }
-
- public function set forcePartArrays(param1:Boolean) : void
- {
- _forcePartArrays = param1;
- }
-
- protected function decodeHeaders(param1:XML) : Array
- {
- var _loc4_:XML = null;
- var _loc5_:QName = null;
- var _loc6_:XML = null;
- var _loc7_:Object = null;
- var _loc8_:SOAPHeader = null;
- var _loc9_:String = null;
- var _loc10_:String = null;
- log.debug("Decoding SOAP response headers");
- var _loc2_:Array = [];
- var _loc3_:XMLList = param1.elements();
- for each(_loc4_ in _loc3_)
- {
- if(headerFormat == "object")
- {
- _loc5_ = getXSIType(_loc4_);
- _loc6_ = null;
- _loc7_ = null;
- if(_loc5_ != null)
- {
- _loc6_ = schemaManager.getNamedDefinition(_loc5_,constants.complexTypeQName,constants.simpleTypeQName);
- }
- if(_loc6_ != null)
- {
- schemaManager.releaseScope();
- _loc7_ = decode(_loc4_,null,_loc5_);
- }
- else
- {
- _loc7_ = decode(_loc4_,_loc4_.name());
- }
- _loc8_ = new SOAPHeader(_loc4_.name(),_loc7_);
- _loc9_ = XMLUtil.getAttributeByQName(_loc4_,soapConstants.mustUnderstandQName).toString();
- if(_loc9_ == "1")
- {
- _loc8_.mustUnderstand = true;
- }
- _loc10_ = XMLUtil.getAttributeByQName(_loc4_,soapConstants.actorQName).toString();
- if(_loc10_ != "")
- {
- _loc8_.role = _loc10_;
- }
- _loc2_.push(_loc8_);
- }
- else if(headerFormat == "e4x")
- {
- _loc2_.push(_loc4_);
- }
- else if(headerFormat == "xml")
- {
- _loc2_.push(new XMLDocument(_loc4_.toString()));
- }
- }
- return _loc2_;
- }
-
- override public function decodeComplexType(param1:XML, param2:*, param3:QName, param4:*, param5:XML = null, param6:DecodingContext = null) : void
- {
- var _loc7_:XML = null;
- if(param4 is XML)
- {
- _loc7_ = param4 as XML;
- if(_loc7_.elements(SOAPConstants.diffgramQName).length() > 0 && _loc7_.elements(schemaConstants.schemaQName).length() > 0)
- {
- decodeType(SOAPConstants.diffgramQName,param2,_loc7_.name(),param4);
- return;
- }
- }
- super.decodeComplexType(param1,param2,param3,param4,param5,param6);
- }
-
- protected function get inputEncoding() : WSDLEncoding
- {
- var _loc1_:WSDLEncoding = null;
- if(_wsdlOperation.inputMessage != null)
- {
- _loc1_ = _wsdlOperation.inputMessage.encoding;
- }
- else
- {
- _loc1_ = new WSDLEncoding();
- }
- return _loc1_;
- }
-
- protected function decodeBody(param1:XML, param2:SOAPResult) : void
- {
- var _loc3_:* = undefined;
- var _loc6_:WSDLMessagePart = null;
- var _loc7_:XMLList = null;
- var _loc8_:XML = null;
- var _loc9_:* = undefined;
- var _loc10_:QName = null;
- var _loc11_:QName = null;
- var _loc12_:XML = null;
- var _loc13_:String = null;
- var _loc14_:uint = 0;
- var _loc15_:QName = null;
- var _loc16_:uint = 0;
- log.debug("Decoding SOAP response body");
- document = param1;
- preProcessXML(param1);
- if(wsdlOperation.outputMessage == null)
- {
- param2.result = undefined;
- return;
- }
- var _loc4_:Array = wsdlOperation.outputMessage.parts;
- if(_loc4_ == null || _loc4_.length == 0)
- {
- param2.result = undefined;
- return;
- }
- var _loc5_:XML = param1;
- if(wsdlOperation.style == SOAPConstants.RPC_STYLE)
- {
- _loc5_ = _loc5_.elements()[0];
- }
- else if(outputEncoding.useStyle == SOAPConstants.USE_LITERAL && wsdlOperation.outputMessage.isWrapped == true)
- {
- _loc5_ = _loc5_.elements()[0];
- }
- for each(_loc6_ in _loc4_)
- {
- if(_loc6_.element != null)
- {
- if(_loc5_.hasComplexContent())
- {
- _loc7_ = _loc5_.elements(_loc6_.element);
- }
- else
- {
- _loc7_ = _loc5_.text();
- }
- _loc10_ = _loc6_.element;
- _loc11_ = null;
- }
- else
- {
- _loc11_ = _loc6_.type;
- _loc12_ = _loc6_.definition;
- if(_loc5_.hasComplexContent())
- {
- if(outputEncoding.useStyle == SOAPConstants.USE_ENCODED)
- {
- _loc10_ = new QName("",_loc6_.name.localName);
- _loc7_ = _loc5_.elements(_loc10_);
- if(_loc7_.length() == 0)
- {
- _loc13_ = outputEncoding.namespaceURI;
- _loc10_ = new QName(_loc13_,_loc6_.name.localName);
- _loc7_ = _loc5_.elements(_loc10_);
- if(_loc7_.length() == 0)
- {
- _loc13_ = inputEncoding.namespaceURI;
- _loc10_ = new QName(_loc13_,_loc6_.name.localName);
- _loc7_ = _loc5_.elements(_loc10_);
- }
- }
- }
- else
- {
- _loc7_ = _loc5_.elements(_loc6_.name);
- }
- }
- else
- {
- _loc7_ = _loc5_.text();
- }
- }
- for each(_loc8_ in _loc7_)
- {
- _loc9_ = decode(_loc8_,_loc10_,_loc11_,_loc12_);
- if(_loc4_.length > 1)
- {
- if(multiplePartsFormat == "object")
- {
- if(_loc3_ == null)
- {
- _loc3_ = createContent(_loc5_.name());
- _loc3_.isSimple = false;
- }
- if(_loc3_[_loc6_.name.localName] == null)
- {
- _loc14_ = getMaxOccurs(_loc12_);
- if(_loc14_ > 1 && forcePartArrays || _loc7_.length() > 1)
- {
- _loc3_[_loc6_.name.localName] = createIterableValue(_loc6_.type);
- }
- }
- if(TypeIterator.isIterable(_loc3_[_loc6_.name.localName]))
- {
- TypeIterator.push(_loc3_[_loc6_.name.localName],_loc9_);
- }
- else
- {
- _loc3_[_loc6_.name.localName] = _loc9_;
- }
- }
- else if(multiplePartsFormat == "array")
- {
- if(_loc3_ == null)
- {
- _loc3_ = createIterableValue(_loc5_.name());
- }
- TypeIterator.push(_loc3_,_loc9_);
- }
- }
- else
- {
- if(_loc3_ == null)
- {
- _loc15_ = _loc11_;
- if(_loc15_ == null)
- {
- _loc15_ = _loc6_.element;
- }
- if(_loc15_ == null)
- {
- _loc15_ = _loc6_.name;
- }
- _loc16_ = getMaxOccurs(_loc12_);
- if(_loc16_ > 1 && forcePartArrays || _loc7_.length() > 1)
- {
- _loc3_ = createIterableValue(_loc15_);
- }
- else
- {
- _loc3_ = createContent();
- }
- }
- if(TypeIterator.isIterable(_loc3_))
- {
- TypeIterator.push(_loc3_,_loc9_);
- }
- else
- {
- _loc3_ = _loc9_;
- }
- }
- }
- }
- if(_loc3_ is ContentProxy)
- {
- _loc3_ = ContentProxy(_loc3_).object_proxy::content;
- }
- param2.result = _loc3_;
- }
-
- public function set wsdlOperation(param1:WSDLOperation) : void
- {
- _wsdlOperation = param1;
- schemaManager = _wsdlOperation.schemaManager;
- }
-
- public function get multiplePartsFormat() : String
- {
- return _multiplePartsFormat;
- }
-
- public function set headerFormat(param1:String) : void
- {
- _headerFormat = param1;
- }
-
- public function decodeResponse(param1:*) : SOAPResult
- {
- var startTime:int;
- var soapResult:SOAPResult = null;
- var responseString:String = null;
- var oldIgnoreWhitespace:Boolean = false;
- var responseXML:XML = null;
- var response:* = param1;
- if(response is XML)
- {
- responseString = XML(response).toXMLString();
- }
- else
- {
- responseString = String(response);
- }
- startTime = getTimer();
- log.info("Decoding SOAP response");
- reset();
- if(responseString != null)
- {
- log.debug("Encoded SOAP response {0}",responseString);
- oldIgnoreWhitespace = Boolean(XML.ignoreWhitespace);
- try
- {
- responseString = responseString.replace(PI_WHITESPACE_PATTERN,"?><");
- responseString = StringUtil.trim(responseString);
- XML.ignoreWhitespace = ignoreWhitespace;
- responseXML = new XML(responseString);
- soapResult = decodeEnvelope(responseXML);
- }
- finally
- {
- XML.ignoreWhitespace = oldIgnoreWhitespace;
- }
- }
- log.info("Decoded SOAP response into result [{0} millis]",getTimer() - startTime);
- return soapResult;
- }
-
- override public function decodeType(param1:QName, param2:*, param3:QName, param4:*, param5:XML = null) : void
- {
- var _loc9_:SchemaDatatypes = null;
- var _loc10_:String = null;
- var _loc11_:SchemaConstants = null;
- var _loc12_:XML = null;
- var _loc6_:QName = param1;
- var _loc7_:QName = getXSIType(param4);
- if(_loc7_ != null)
- {
- param1 = _loc7_;
- }
- if(outputEncoding.useStyle == SOAPConstants.USE_ENCODED)
- {
- if(SOAPConstants.isSOAPEncodedType(param1))
- {
- _loc9_ = schemaManager.schemaDatatypes;
- if(param1 == soapConstants.soapBase64QName)
- {
- param1 = _loc9_.base64BinaryQName;
- }
- else
- {
- _loc10_ = param1.localName;
- if(_loc10_ != "Array" && _loc10_ != "arrayType")
- {
- param1 = schemaConstants.getQName(_loc10_);
- }
- }
- }
- }
- var _loc8_:ICustomSOAPType = SOAPConstants.getCustomSOAPType(param1);
- if(_loc8_ != null)
- {
- _loc8_.decode(this,param2,param3,param4,param5);
- setXSIType(param2,param1);
- }
- else
- {
- _loc11_ = schemaManager.schemaConstants;
- if(isBuiltInType(param1))
- {
- super.decodeType(param1,param2,param3,param4,param5);
- }
- else
- {
- _loc12_ = schemaManager.getNamedDefinition(param1,_loc11_.complexTypeQName,_loc11_.simpleTypeQName,_loc11_.elementTypeQName);
- if(_loc12_ != null)
- {
- schemaManager.releaseScope();
- super.decodeType(param1,param2,param3,param4,param5);
- }
- else
- {
- super.decodeType(_loc6_,param2,param3,param4,param5);
- }
- }
- }
- }
-
- public function get forcePartArrays() : Boolean
- {
- return _forcePartArrays;
- }
-
- public function set ignoreWhitespace(param1:Boolean) : void
- {
- _ignoreWhitespace = param1;
- }
-
- public function get wsdlOperation() : WSDLOperation
- {
- return _wsdlOperation;
- }
-
- public function get headerFormat() : String
- {
- return _headerFormat;
- }
-
- public function set resultFormat(param1:String) : void
- {
- _resultFormat = param1;
- }
-
- protected function decodeEnvelope(param1:XML) : SOAPResult
- {
- var _loc3_:Namespace = null;
- var _loc4_:SchemaConstants = null;
- var _loc5_:Array = null;
- var _loc6_:Namespace = null;
- var _loc7_:XML = null;
- var _loc8_:XML = null;
- var _loc9_:XMLList = null;
- var _loc10_:Array = null;
- var _loc11_:XMLList = null;
- var _loc12_:XML = null;
- var _loc13_:String = null;
- var _loc14_:XMLDocument = null;
- var _loc15_:XMLNode = null;
- log.debug("Decoding SOAP response envelope");
- var _loc2_:SOAPResult = new SOAPResult();
- if(param1 != null)
- {
- _loc3_ = param1.namespace();
- }
- if(_loc3_ == null)
- {
- throw new Error("SOAP Response cannot be decoded. Raw response: " + param1);
- }
- if(_loc3_.uri != SOAPConstants.SOAP_ENVELOPE_URI)
- {
- throw new Error("SOAP Response Version Mismatch");
- }
- _loc5_ = param1.inScopeNamespaces();
- for each(_loc6_ in _loc5_)
- {
- schemaManager.namespaces[_loc6_.prefix] = _loc6_;
- }
- _loc7_ = param1[soapConstants.headerQName][0];
- if(_loc7_ != null)
- {
- _loc2_.headers = decodeHeaders(_loc7_);
- }
- _loc8_ = param1[soapConstants.bodyQName][0];
- if(_loc8_ == null || _loc8_.hasComplexContent() == false || _loc8_.children().length() <= 0)
- {
- _loc2_.result = undefined;
- }
- else
- {
- _loc9_ = _loc8_[soapConstants.faultQName];
- if(_loc9_.length() > 0)
- {
- _loc2_.isFault = true;
- _loc2_.result = decodeFaults(_loc9_);
- }
- else if(resultFormat == "object")
- {
- decodeBody(_loc8_,_loc2_);
- }
- else if(resultFormat == "e4x")
- {
- _loc2_.result = _loc8_.children();
- }
- else if(resultFormat == "xml")
- {
- _loc10_ = [];
- _loc11_ = _loc8_.children();
- for each(_loc12_ in _loc11_)
- {
- _loc13_ = _loc12_.nodeKind();
- if(_loc13_ == "element")
- {
- _loc14_ = new XMLDocument(_loc12_.toString());
- _loc15_ = _loc14_.firstChild;
- _loc10_.push(_loc15_);
- }
- else if(_loc13_ == "text")
- {
- _loc10_.push(_loc12_.toString());
- }
- }
- _loc2_.result = _loc10_;
- }
- }
- return _loc2_;
- }
-
- override public function reset() : void
- {
- super.reset();
- _referencesResolved = false;
- _elementsWithId = null;
- }
-
- protected function get outputEncoding() : WSDLEncoding
- {
- var _loc1_:WSDLEncoding = null;
- if(_wsdlOperation.outputMessage != null)
- {
- _loc1_ = _wsdlOperation.outputMessage.encoding;
- }
- else
- {
- _loc1_ = new WSDLEncoding();
- }
- return _loc1_;
- }
-
- public function get ignoreWhitespace() : Boolean
- {
- return _ignoreWhitespace;
- }
-
- public function get resultFormat() : String
- {
- return _resultFormat;
- }
-
- override protected function parseValue(param1:*, param2:XMLList) : *
- {
- var _loc3_:QName = null;
- var _loc4_:XMLList = null;
- if(supportGenericCompoundTypes && outputEncoding.useStyle == SOAPConstants.USE_LITERAL && param2.length() > 0)
- {
- _loc3_ = new QName(param2[0].name().uri,"item");
- _loc4_ = param2.elements(_loc3_);
- if(_loc4_.length() > 0)
- {
- param2 = _loc4_;
- }
- }
- return super.parseValue(param1,param2);
- }
- }
- }
-
-