home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / com / facebook / delegates / WebDelegate.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  5.0 KB  |  165 lines

  1. package com.facebook.delegates
  2. {
  3.    import com.facebook.data.FacebookData;
  4.    import com.facebook.data.XMLDataParser;
  5.    import com.facebook.errors.FacebookError;
  6.    import com.facebook.events.FacebookEvent;
  7.    import com.facebook.facebook_internal;
  8.    import com.facebook.net.FacebookCall;
  9.    import com.facebook.session.IFacebookSession;
  10.    import com.facebook.session.WebSession;
  11.    import flash.events.ErrorEvent;
  12.    import flash.events.Event;
  13.    import flash.events.EventDispatcher;
  14.    import flash.events.HTTPStatusEvent;
  15.    import flash.events.IOErrorEvent;
  16.    import flash.events.SecurityErrorEvent;
  17.    import flash.net.FileReference;
  18.    import flash.net.URLLoader;
  19.    import flash.net.URLLoaderDataFormat;
  20.    import flash.net.URLRequest;
  21.    import flash.net.URLRequestMethod;
  22.    
  23.    use namespace facebook_internal;
  24.    
  25.    public class WebDelegate extends EventDispatcher implements IFacebookCallDelegate
  26.    {
  27.       protected var _call:FacebookCall;
  28.       
  29.       protected var loader:URLLoader;
  30.       
  31.       protected var _session:WebSession;
  32.       
  33.       protected var parser:XMLDataParser;
  34.       
  35.       protected var fileRef:FileReference;
  36.       
  37.       public function WebDelegate(param1:FacebookCall, param2:WebSession)
  38.       {
  39.          super();
  40.          this.call = param1;
  41.          this.session = param2;
  42.          this.parser = new XMLDataParser();
  43.          this.execute();
  44.       }
  45.       
  46.       protected function onDataComplete(param1:Event) : void
  47.       {
  48.          this.handleResult(param1.target.data as String);
  49.       }
  50.       
  51.       public function set call(param1:FacebookCall) : void
  52.       {
  53.          this._call = param1;
  54.       }
  55.       
  56.       protected function createURLLoader() : void
  57.       {
  58.          this.loader = new URLLoader();
  59.          this.loader.addEventListener(Event.COMPLETE,this.onDataComplete);
  60.          this.loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,this.onHTTPStatus);
  61.          this.loader.addEventListener(IOErrorEvent.IO_ERROR,this.onError);
  62.          this.loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onError);
  63.       }
  64.       
  65.       protected function clean() : void
  66.       {
  67.          if(this.loader == null)
  68.          {
  69.             return;
  70.          }
  71.          this.loader.removeEventListener(Event.COMPLETE,this.onDataComplete);
  72.          this.loader.removeEventListener(IOErrorEvent.IO_ERROR,this.onError);
  73.          this.loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onError);
  74.       }
  75.       
  76.       protected function addOptionalArguments() : void
  77.       {
  78.          this.call.facebook_internal::setRequestArgument("ss",true);
  79.       }
  80.       
  81.       public function get session() : IFacebookSession
  82.       {
  83.          return this._session;
  84.       }
  85.       
  86.       public function get call() : FacebookCall
  87.       {
  88.          return this._call;
  89.       }
  90.       
  91.       protected function post() : void
  92.       {
  93.          this.addOptionalArguments();
  94.          RequestHelper.formatRequest(this.call);
  95.          this.sendRequest();
  96.       }
  97.       
  98.       protected function sendRequest() : void
  99.       {
  100.          this.createURLLoader();
  101.          var _loc1_:URLRequest = new URLRequest(this._session.rest_url);
  102.          _loc1_.contentType = "application/x-www-form-urlencoded";
  103.          _loc1_.method = URLRequestMethod.POST;
  104.          _loc1_.data = this.call.args;
  105.          trace(_loc1_.url + "?" + unescape(this.call.args.toString()));
  106.          this.loader.dataFormat = URLLoaderDataFormat.TEXT;
  107.          this.loader.load(_loc1_);
  108.       }
  109.       
  110.       protected function onError(param1:ErrorEvent) : void
  111.       {
  112.          this.clean();
  113.          var _loc2_:FacebookError = this.parser.createFacebookError(param1,this.loader.data);
  114.          this.call.facebook_internal::handleError(_loc2_);
  115.          dispatchEvent(new FacebookEvent(FacebookEvent.COMPLETE,false,false,false,null,_loc2_));
  116.       }
  117.       
  118.       public function set session(param1:IFacebookSession) : void
  119.       {
  120.          this._session = param1 as WebSession;
  121.       }
  122.       
  123.       protected function handleResult(param1:String) : void
  124.       {
  125.          var _loc3_:FacebookData = null;
  126.          this.clean();
  127.          var _loc2_:FacebookError = this.parser.validateFacebookResponce(param1);
  128.          if(_loc2_ == null)
  129.          {
  130.             _loc3_ = this.parser.parse(param1,this.call.method);
  131.             this.call.facebook_internal::handleResult(_loc3_);
  132.          }
  133.          else
  134.          {
  135.             this.call.facebook_internal::handleError(_loc2_);
  136.          }
  137.       }
  138.       
  139.       protected function execute() : void
  140.       {
  141.          if(this.call == null)
  142.          {
  143.             throw new Error("No call defined.");
  144.          }
  145.          this.post();
  146.       }
  147.       
  148.       public function close() : void
  149.       {
  150.          try
  151.          {
  152.             this.loader.close();
  153.          }
  154.          catch(e:*)
  155.          {
  156.          }
  157.       }
  158.       
  159.       protected function onHTTPStatus(param1:HTTPStatusEvent) : void
  160.       {
  161.       }
  162.    }
  163. }
  164.  
  165.