home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / mx / rpc / http / AbstractOperation.as next >
Encoding:
Text File  |  2010-06-23  |  20.8 KB  |  620 lines

  1. package mx.rpc.http
  2. {
  3.    import flash.utils.getQualifiedClassName;
  4.    import flash.xml.XMLDocument;
  5.    import flash.xml.XMLNode;
  6.    import mx.collections.ArrayCollection;
  7.    import mx.core.mx_internal;
  8.    import mx.logging.ILogger;
  9.    import mx.logging.Log;
  10.    import mx.managers.CursorManager;
  11.    import mx.messaging.ChannelSet;
  12.    import mx.messaging.channels.DirectHTTPChannel;
  13.    import mx.messaging.config.LoaderConfig;
  14.    import mx.messaging.events.MessageEvent;
  15.    import mx.messaging.messages.AsyncMessage;
  16.    import mx.messaging.messages.HTTPRequestMessage;
  17.    import mx.messaging.messages.IMessage;
  18.    import mx.netmon.NetworkMonitor;
  19.    import mx.resources.IResourceManager;
  20.    import mx.resources.ResourceManager;
  21.    import mx.rpc.AbstractOperation;
  22.    import mx.rpc.AbstractService;
  23.    import mx.rpc.AsyncDispatcher;
  24.    import mx.rpc.AsyncToken;
  25.    import mx.rpc.Fault;
  26.    import mx.rpc.events.FaultEvent;
  27.    import mx.rpc.mxml.Concurrency;
  28.    import mx.rpc.xml.SimpleXMLDecoder;
  29.    import mx.rpc.xml.SimpleXMLEncoder;
  30.    import mx.utils.ObjectProxy;
  31.    import mx.utils.ObjectUtil;
  32.    import mx.utils.StringUtil;
  33.    import mx.utils.URLUtil;
  34.    
  35.    use namespace mx_internal;
  36.    
  37.    public class AbstractOperation extends mx.rpc.AbstractOperation
  38.    {
  39.       private static var _directChannelSet:ChannelSet;
  40.       
  41.       mx_internal static const RESULT_FORMAT_E4X:String = "e4x";
  42.       
  43.       mx_internal static const RESULT_FORMAT_FLASHVARS:String = "flashvars";
  44.       
  45.       mx_internal static const RESULT_FORMAT_OBJECT:String = "object";
  46.       
  47.       mx_internal static const RESULT_FORMAT_ARRAY:String = "array";
  48.       
  49.       mx_internal static const RESULT_FORMAT_TEXT:String = "text";
  50.       
  51.       mx_internal static const RESULT_FORMAT_XML:String = "xml";
  52.       
  53.       mx_internal static const CONTENT_TYPE_XML:String = "application/xml";
  54.       
  55.       mx_internal static const CONTENT_TYPE_FORM:String = "application/x-www-form-urlencoded";
  56.       
  57.       private static const ERROR_URL_REQUIRED:String = "Client.URLRequired";
  58.       
  59.       private static const ERROR_DECODING:String = "Client.CouldNotDecode";
  60.       
  61.       private static const ERROR_ENCODING:String = "Client.CouldNotEncode";
  62.       
  63.       private var _method:String = "GET";
  64.       
  65.       mx_internal var _log:ILogger;
  66.       
  67.       private var _useProxy:Boolean = false;
  68.       
  69.       public var serializationFilter:SerializationFilter;
  70.       
  71.       public var argumentNames:Array;
  72.       
  73.       public var xmlEncode:Function;
  74.       
  75.       public var request:Object = {};
  76.       
  77.       private var _contentType:String = "application/x-www-form-urlencoded";
  78.       
  79.       public var headers:Object = {};
  80.       
  81.       private var _url:String;
  82.       
  83.       private var _showBusyCursor:Boolean = false;
  84.       
  85.       mx_internal var resourceManager:IResourceManager = ResourceManager.getInstance();
  86.       
  87.       private var _resultFormat:String = "object";
  88.       
  89.       private var _concurrency:String;
  90.       
  91.       public var xmlDecode:Function;
  92.       
  93.       mx_internal var _rootURL:String;
  94.       
  95.       public function AbstractOperation(param1:AbstractService = null, param2:String = null)
  96.       {
  97.          super(param1,param2);
  98.          mx_internal::_log = Log.getLogger("mx.rpc.http.HTTPService");
  99.          concurrency = Concurrency.MULTIPLE;
  100.       }
  101.       
  102.       private function decodeArray(param1:Object) : Object
  103.       {
  104.          var _loc2_:Array = null;
  105.          if(param1 is Array)
  106.          {
  107.             _loc2_ = param1 as Array;
  108.          }
  109.          else
  110.          {
  111.             if(param1 is ArrayCollection)
  112.             {
  113.                return param1;
  114.             }
  115.             _loc2_ = [];
  116.             _loc2_.push(param1);
  117.          }
  118.          if(makeObjectsBindable)
  119.          {
  120.             return new ArrayCollection(_loc2_);
  121.          }
  122.          return _loc2_;
  123.       }
  124.       
  125.       public function get concurrency() : String
  126.       {
  127.          return _concurrency;
  128.       }
  129.       
  130.       public function set showBusyCursor(param1:Boolean) : void
  131.       {
  132.          _showBusyCursor = param1;
  133.       }
  134.       
  135.       public function get showBusyCursor() : Boolean
  136.       {
  137.          return _showBusyCursor;
  138.       }
  139.       
  140.       private function getDirectChannelSet() : ChannelSet
  141.       {
  142.          var _loc1_:ChannelSet = null;
  143.          if(_directChannelSet == null)
  144.          {
  145.             _loc1_ = new ChannelSet();
  146.             _loc1_.addChannel(new DirectHTTPChannel("direct_http_channel"));
  147.             _directChannelSet = _loc1_;
  148.          }
  149.          return _directChannelSet;
  150.       }
  151.       
  152.       public function set concurrency(param1:String) : void
  153.       {
  154.          _concurrency = param1;
  155.       }
  156.       
  157.       public function get useProxy() : Boolean
  158.       {
  159.          return _useProxy;
  160.       }
  161.       
  162.       override mx_internal function preHandle(param1:MessageEvent) : AsyncToken
  163.       {
  164.          if(showBusyCursor)
  165.          {
  166.             CursorManager.removeBusyCursor();
  167.          }
  168.          var _loc2_:Boolean = Boolean(mx_internal::activeCalls.wasLastCall(AsyncMessage(param1.message).correlationId));
  169.          var _loc3_:AsyncToken = super.mx_internal::preHandle(param1);
  170.          if(Concurrency.LAST == concurrency && !_loc2_)
  171.          {
  172.             return null;
  173.          }
  174.          return _loc3_;
  175.       }
  176.       
  177.       public function get rootURL() : String
  178.       {
  179.          if(mx_internal::_rootURL == null)
  180.          {
  181.             mx_internal::_rootURL = LoaderConfig.url;
  182.          }
  183.          return mx_internal::_rootURL;
  184.       }
  185.       
  186.       public function set contentType(param1:String) : void
  187.       {
  188.          _contentType = param1;
  189.       }
  190.       
  191.       public function set method(param1:String) : void
  192.       {
  193.          _method = param1;
  194.       }
  195.       
  196.       private function decodeParameterString(param1:String) : Object
  197.       {
  198.          var _loc6_:String = null;
  199.          var _loc7_:int = 0;
  200.          var _loc8_:String = null;
  201.          var _loc9_:String = null;
  202.          var _loc2_:String = StringUtil.trim(param1);
  203.          var _loc3_:Array = _loc2_.split("&");
  204.          var _loc4_:Object = {};
  205.          var _loc5_:int = 0;
  206.          while(_loc5_ < _loc3_.length)
  207.          {
  208.             _loc6_ = _loc3_[_loc5_];
  209.             _loc7_ = int(_loc6_.indexOf("="));
  210.             if(_loc7_ != -1)
  211.             {
  212.                _loc8_ = _loc6_.substr(0,_loc7_);
  213.                _loc8_ = _loc8_.split("+").join(" ");
  214.                _loc8_ = unescape(_loc8_);
  215.                _loc9_ = _loc6_.substr(_loc7_ + 1);
  216.                _loc9_ = _loc9_.split("+").join(" ");
  217.                _loc9_ = unescape(_loc9_);
  218.                _loc4_[_loc8_] = _loc9_;
  219.             }
  220.             _loc5_++;
  221.          }
  222.          return _loc4_;
  223.       }
  224.       
  225.       public function set useProxy(param1:Boolean) : void
  226.       {
  227.          var _loc2_:ChannelSet = null;
  228.          if(param1 != _useProxy)
  229.          {
  230.             _useProxy = param1;
  231.             _loc2_ = getDirectChannelSet();
  232.             if(!useProxy)
  233.             {
  234.                if(_loc2_ != mx_internal::asyncRequest.channelSet)
  235.                {
  236.                   mx_internal::asyncRequest.channelSet = _loc2_;
  237.                }
  238.             }
  239.             else if(mx_internal::asyncRequest.channelSet == _loc2_)
  240.             {
  241.                mx_internal::asyncRequest.channelSet = null;
  242.             }
  243.          }
  244.       }
  245.       
  246.       protected function getHeaders() : Object
  247.       {
  248.          return headers;
  249.       }
  250.       
  251.       public function sendBody(param1:Object) : AsyncToken
  252.       {
  253.          var _loc3_:Object = null;
  254.          var _loc4_:AsyncToken = null;
  255.          var _loc5_:Fault = null;
  256.          var _loc6_:FaultEvent = null;
  257.          var _loc7_:String = null;
  258.          var _loc11_:String = null;
  259.          var _loc12_:Object = null;
  260.          var _loc13_:SimpleXMLEncoder = null;
  261.          var _loc14_:XMLDocument = null;
  262.          var _loc15_:Array = null;
  263.          var _loc16_:int = 0;
  264.          var _loc17_:Object = null;
  265.          var _loc18_:Object = null;
  266.          var _loc19_:* = undefined;
  267.          var _loc20_:ChannelSet = null;
  268.          var _loc2_:SerializationFilter = getSerializationFilter();
  269.          if(Concurrency.SINGLE == concurrency && Boolean(mx_internal::activeCalls.hasActiveCalls()))
  270.          {
  271.             _loc4_ = new AsyncToken(null);
  272.             _loc11_ = mx_internal::resourceManager.getString("rpc","pendingCallExists");
  273.             _loc5_ = new Fault("ConcurrencyError",_loc11_);
  274.             _loc6_ = FaultEvent.createEvent(_loc5_,_loc4_);
  275.             new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc6_],10);
  276.             return _loc4_;
  277.          }
  278.          var _loc8_:String = contentType;
  279.          var _loc9_:String = url;
  280.          if(_loc2_ != null)
  281.          {
  282.             _loc8_ = _loc2_.getRequestContentType(this,param1,_loc8_);
  283.             _loc9_ = _loc2_.serializeURL(this,param1,_loc9_);
  284.             param1 = _loc2_.serializeBody(this,param1);
  285.          }
  286.          if(_loc8_ == mx_internal::CONTENT_TYPE_XML)
  287.          {
  288.             if(param1 is String && xmlEncode == null)
  289.             {
  290.                _loc3_ = param1 as String;
  291.             }
  292.             else if(!(param1 is XMLNode) && !(param1 is XML))
  293.             {
  294.                if(xmlEncode != null)
  295.                {
  296.                   _loc12_ = xmlEncode(param1);
  297.                   if(null == _loc12_)
  298.                   {
  299.                      _loc4_ = new AsyncToken(null);
  300.                      _loc7_ = mx_internal::resourceManager.getString("rpc","xmlEncodeReturnNull");
  301.                      _loc5_ = new Fault(ERROR_ENCODING,_loc7_);
  302.                      _loc6_ = FaultEvent.createEvent(_loc5_,_loc4_);
  303.                      new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc6_],10);
  304.                      return _loc4_;
  305.                   }
  306.                   if(!(_loc12_ is XMLNode))
  307.                   {
  308.                      _loc4_ = new AsyncToken(null);
  309.                      _loc7_ = mx_internal::resourceManager.getString("rpc","xmlEncodeReturnNoXMLNode");
  310.                      _loc5_ = new Fault(ERROR_ENCODING,_loc7_);
  311.                      _loc6_ = FaultEvent.createEvent(_loc5_,_loc4_);
  312.                      new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc6_],10);
  313.                      return _loc4_;
  314.                   }
  315.                   _loc3_ = XMLNode(_loc12_).toString();
  316.                }
  317.                else
  318.                {
  319.                   _loc13_ = new SimpleXMLEncoder(null);
  320.                   _loc14_ = new XMLDocument();
  321.                   _loc15_ = _loc13_.encodeValue(param1,new QName(null,"encoded"),new XMLNode(1,"top")).childNodes.concat();
  322.                   _loc16_ = 0;
  323.                   while(_loc16_ < _loc15_.length)
  324.                   {
  325.                      _loc14_.appendChild(_loc15_[_loc16_]);
  326.                      _loc16_++;
  327.                   }
  328.                   _loc3_ = _loc14_.toString();
  329.                }
  330.             }
  331.             else
  332.             {
  333.                _loc3_ = XML(param1).toXMLString();
  334.             }
  335.          }
  336.          else if(_loc8_ == mx_internal::CONTENT_TYPE_FORM)
  337.          {
  338.             _loc3_ = {};
  339.             if(typeof param1 == "object")
  340.             {
  341.                _loc18_ = ObjectUtil.getClassInfo(param1);
  342.                for each(_loc19_ in _loc18_.properties)
  343.                {
  344.                   _loc17_ = param1[_loc19_];
  345.                   if(_loc17_ != null)
  346.                   {
  347.                      if(_loc17_ is Array)
  348.                      {
  349.                         _loc3_[_loc19_] = _loc17_;
  350.                      }
  351.                      else
  352.                      {
  353.                         _loc3_[_loc19_] = _loc17_.toString();
  354.                      }
  355.                   }
  356.                }
  357.             }
  358.             else
  359.             {
  360.                _loc3_ = param1;
  361.             }
  362.          }
  363.          else
  364.          {
  365.             _loc3_ = param1;
  366.          }
  367.          var _loc10_:HTTPRequestMessage = new HTTPRequestMessage();
  368.          if(useProxy)
  369.          {
  370.             if(Boolean(_loc9_) && _loc9_ != "")
  371.             {
  372.                _loc10_.url = URLUtil.getFullURL(rootURL,_loc9_);
  373.             }
  374.             if(NetworkMonitor.isMonitoring())
  375.             {
  376.                _loc10_.recordHeaders = true;
  377.             }
  378.          }
  379.          else
  380.          {
  381.             if(!_loc9_)
  382.             {
  383.                _loc4_ = new AsyncToken(null);
  384.                _loc7_ = mx_internal::resourceManager.getString("rpc","urlNotSpecified");
  385.                _loc5_ = new Fault(ERROR_URL_REQUIRED,_loc7_);
  386.                _loc6_ = FaultEvent.createEvent(_loc5_,_loc4_);
  387.                new AsyncDispatcher(mx_internal::dispatchRpcEvent,[_loc6_],10);
  388.                return _loc4_;
  389.             }
  390.             if(!useProxy)
  391.             {
  392.                _loc20_ = getDirectChannelSet();
  393.                if(_loc20_ != mx_internal::asyncRequest.channelSet)
  394.                {
  395.                   mx_internal::asyncRequest.channelSet = _loc20_;
  396.                }
  397.             }
  398.             if(NetworkMonitor.isMonitoring())
  399.             {
  400.                _loc10_.recordHeaders = true;
  401.             }
  402.             _loc10_.url = URLUtil.getFullURL(rootURL,_loc9_);
  403.          }
  404.          _loc10_.contentType = _loc8_;
  405.          _loc10_.method = method.toUpperCase();
  406.          if(_loc8_ == mx_internal::CONTENT_TYPE_XML && _loc10_.method == HTTPRequestMessage.GET_METHOD)
  407.          {
  408.             _loc10_.method = HTTPRequestMessage.POST_METHOD;
  409.          }
  410.          _loc10_.body = _loc3_;
  411.          _loc10_.httpHeaders = getHeaders();
  412.          return mx_internal::invoke(_loc10_);
  413.       }
  414.       
  415.       protected function getSerializationFilter() : SerializationFilter
  416.       {
  417.          return serializationFilter;
  418.       }
  419.       
  420.       public function get contentType() : String
  421.       {
  422.          return _contentType;
  423.       }
  424.       
  425.       public function set url(param1:String) : void
  426.       {
  427.          _url = param1;
  428.       }
  429.       
  430.       public function get method() : String
  431.       {
  432.          return _method;
  433.       }
  434.       
  435.       public function set resultFormat(param1:String) : void
  436.       {
  437.          var _loc2_:SerializationFilter = null;
  438.          var _loc3_:String = null;
  439.          switch(param1)
  440.          {
  441.             case mx_internal::RESULT_FORMAT_OBJECT:
  442.             case mx_internal::RESULT_FORMAT_ARRAY:
  443.             case mx_internal::RESULT_FORMAT_XML:
  444.             case mx_internal::RESULT_FORMAT_E4X:
  445.             case mx_internal::RESULT_FORMAT_TEXT:
  446.             case mx_internal::RESULT_FORMAT_FLASHVARS:
  447.                break;
  448.             default:
  449.                if(param1 != null && (_loc2_ = SerializationFilter.mx_internal::filterForResultFormatTable[param1]) == null)
  450.                {
  451.                   _loc3_ = mx_internal::resourceManager.getString("rpc","invalidResultFormat",[param1,mx_internal::RESULT_FORMAT_OBJECT,mx_internal::RESULT_FORMAT_ARRAY,mx_internal::RESULT_FORMAT_XML,mx_internal::RESULT_FORMAT_E4X,mx_internal::RESULT_FORMAT_TEXT,mx_internal::RESULT_FORMAT_FLASHVARS]);
  452.                   throw new ArgumentError(_loc3_);
  453.                }
  454.                serializationFilter = _loc2_;
  455.                break;
  456.          }
  457.          _resultFormat = param1;
  458.       }
  459.       
  460.       public function set rootURL(param1:String) : void
  461.       {
  462.          mx_internal::_rootURL = param1;
  463.       }
  464.       
  465.       override mx_internal function processResult(param1:IMessage, param2:AsyncToken) : Boolean
  466.       {
  467.          var filter:SerializationFilter;
  468.          var tmp:Object = null;
  469.          var fault:Fault = null;
  470.          var decoded:Object = null;
  471.          var msg:String = null;
  472.          var fault1:Fault = null;
  473.          var decoder:SimpleXMLDecoder = null;
  474.          var fault2:Fault = null;
  475.          var fault3:Fault = null;
  476.          var message:IMessage = param1;
  477.          var token:AsyncToken = param2;
  478.          var body:Object = message.body;
  479.          mx_internal::_log.info("Decoding HTTPService response");
  480.          mx_internal::_log.debug("Processing HTTPService response message:\n{0}",message);
  481.          filter = getSerializationFilter();
  482.          if(filter != null)
  483.          {
  484.             body = filter.deserializeResult(this,body);
  485.          }
  486.          if(body == null || body != null && body is String && StringUtil.trim(String(body)) == "")
  487.          {
  488.             mx_internal::_result = body;
  489.             return true;
  490.          }
  491.          if(body is String)
  492.          {
  493.             if(resultFormat == mx_internal::RESULT_FORMAT_XML || resultFormat == mx_internal::RESULT_FORMAT_OBJECT || resultFormat == mx_internal::RESULT_FORMAT_ARRAY)
  494.             {
  495.                tmp = new XMLDocument();
  496.                XMLDocument(tmp).ignoreWhite = true;
  497.                try
  498.                {
  499.                   XMLDocument(tmp).parseXML(String(body));
  500.                }
  501.                catch(parseError:Error)
  502.                {
  503.                   fault = new Fault(ERROR_DECODING,parseError.message);
  504.                   mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault,token,message));
  505.                   return false;
  506.                }
  507.                if(resultFormat == mx_internal::RESULT_FORMAT_OBJECT || resultFormat == mx_internal::RESULT_FORMAT_ARRAY)
  508.                {
  509.                   if(xmlDecode != null)
  510.                   {
  511.                      decoded = xmlDecode(tmp);
  512.                      if(decoded == null)
  513.                      {
  514.                         msg = mx_internal::resourceManager.getString("rpc","xmlDecodeReturnNull");
  515.                         fault1 = new Fault(ERROR_DECODING,msg);
  516.                         mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault1,token,message));
  517.                      }
  518.                   }
  519.                   else
  520.                   {
  521.                      decoder = new SimpleXMLDecoder(makeObjectsBindable);
  522.                      decoded = decoder.decodeXML(XMLNode(tmp));
  523.                      if(decoded == null)
  524.                      {
  525.                         msg = mx_internal::resourceManager.getString("rpc","defaultDecoderFailed");
  526.                         fault2 = new Fault(ERROR_DECODING,msg);
  527.                         mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault2,token,message));
  528.                      }
  529.                   }
  530.                   if(decoded == null)
  531.                   {
  532.                      return false;
  533.                   }
  534.                   if(makeObjectsBindable && getQualifiedClassName(decoded) == "Object")
  535.                   {
  536.                      decoded = new ObjectProxy(decoded);
  537.                   }
  538.                   else
  539.                   {
  540.                      ;
  541.                   }
  542.                   if(resultFormat == mx_internal::RESULT_FORMAT_ARRAY)
  543.                   {
  544.                      decoded = decodeArray(decoded);
  545.                   }
  546.                   mx_internal::_result = decoded;
  547.                }
  548.                else
  549.                {
  550.                   if(tmp.childNodes.length == 1)
  551.                   {
  552.                      tmp = tmp.firstChild;
  553.                   }
  554.                   mx_internal::_result = tmp;
  555.                }
  556.             }
  557.             else if(resultFormat == mx_internal::RESULT_FORMAT_E4X)
  558.             {
  559.                try
  560.                {
  561.                   mx_internal::_result = new XML(String(body));
  562.                }
  563.                catch(error:Error)
  564.                {
  565.                   fault3 = new Fault(ERROR_DECODING,error.message);
  566.                   mx_internal::dispatchRpcEvent(FaultEvent.createEvent(fault3,token,message));
  567.                   return false;
  568.                }
  569.             }
  570.             else if(resultFormat == mx_internal::RESULT_FORMAT_FLASHVARS)
  571.             {
  572.                mx_internal::_result = decodeParameterString(String(body));
  573.             }
  574.             else
  575.             {
  576.                mx_internal::_result = body;
  577.             }
  578.          }
  579.          else
  580.          {
  581.             if(resultFormat == mx_internal::RESULT_FORMAT_ARRAY)
  582.             {
  583.                body = decodeArray(body);
  584.             }
  585.             mx_internal::_result = body;
  586.          }
  587.          return true;
  588.       }
  589.       
  590.       public function get url() : String
  591.       {
  592.          return _url;
  593.       }
  594.       
  595.       public function get resultFormat() : String
  596.       {
  597.          return _resultFormat;
  598.       }
  599.       
  600.       override mx_internal function invoke(param1:IMessage, param2:AsyncToken = null) : AsyncToken
  601.       {
  602.          if(showBusyCursor)
  603.          {
  604.             CursorManager.setBusyCursor();
  605.          }
  606.          return super.mx_internal::invoke(param1,param2);
  607.       }
  608.       
  609.       override public function cancel(param1:String = null) : AsyncToken
  610.       {
  611.          if(showBusyCursor)
  612.          {
  613.             CursorManager.removeBusyCursor();
  614.          }
  615.          return super.cancel(param1);
  616.       }
  617.    }
  618. }
  619.  
  620.