Code I/O

A topnotch WordPress.com site

5 minutes on Adobe Flex: A service proxy pattern for applications

Leave a comment

The process of communication involved between Rich Internet Applications and Server components is very repeatable:

  • Sending request to server
  • handling its result or fault, and
  • finally processing the result for the application to provide desired functionality

The process involves invoking services and handling responses individually, One can think of simplifying the process by introducing the Service Proxy pattern, which becomes handy to manage the entire application specific communications.

The benefit of this pattern is that, one can process results specifically with respect to business logic at the same time having a unified interface to manage server side interactions.

package service
{
 import mx.controls.Alert;
 import mx.rpc.events.FaultEvent;
 import mx.rpc.events.ResultEvent;
 import mx.rpc.http.HTTPService;

 public class ServiceProxy
 {
 private static var SERVICE_URL:String;
 public static function getURL():String{
 return SERVICE_URL;
 }
 public static const TERMS_AND_CONDITIONS_PAGE:String = "/sub_tac.html";

 public function ServiceProxy()
 {
 throw new Error("ServiceProxy cannot be instantiated");
 }

 public static const CHECK_SUBSCRIBER:String     = "CHECK_SUBSCRIBER";
 public static const LOAD_DICTIONARY:String         = "LOAD_DICTIONARY";
 public static const SUBSCRIBE:String             = "SUBSCRIBE";
 public static const UNSUBSCRIBE:String             = "UNSUBSCRIBE";
 public static const CHANGE_SUBSCRIPTIONS:String = "CHANGE_SUBSCRIPTIONS";

 private static function init():void {
 register(LOAD_DICTIONARY,
 {script:"/php/load_dictionary.php",         method:"GET",    success:"", failure:""});

 register(CHANGE_SUBSCRIPTIONS,
 {script:"/php/change_subscriptions.php",     method:"POST",    success:"/sub_success.html", failure:""});

 register(CHECK_SUBSCRIBER,
 {script:"/php/check_subscriber.php",         method:"POST",    success:"", failure:""});

 register(SUBSCRIBE,
 {script:"/php/subscribe.php",                 method:"POST",     success:"/sub_success.html", failure:""});

 register(UNSUBSCRIBE,
 {script:"/php/unsubscribe.php",             method:"POST",    success:"/unsub_success.html", failure:""});
 }

 private static var registry:Object = null;
 public static function getObjectByTag(tag:String):Object{
 if(registry == null){
 registry = {};

 init();
 }

 return registry[tag];
 }

 public static function register(handle:String, object:Object):Boolean {
 if(!registry[handle]){
 registry[handle] = object;
 return true;
 }

 return false;
 }

 public static function execute(tag:String, xml:XML=null, callback:Function=null):void {
 var o:Object = getObjectByTag(tag);
 if(o == null){
 throw new Error(tag + " is not supported currently!!");
 return;
 }

 var s:HTTPService = new HTTPService();

 s.url = SERVICE_URL + o.script;
 s.method = o.method;
 s.contentType = HTTPService.CONTENT_TYPE_XML;
 s.resultFormat = HTTPService.RESULT_FORMAT_E4X;

 if(xml != null){
 s.request = xml;
 }

 s.addEventListener(ResultEvent.RESULT,
 function resultHandler(event:ResultEvent):void
 {
 var xml:XML = XML(event.result);

 trace("RESULTn" + xml);
 if(callback != null){
 callback(xml);
 }
 }
 );

 s.addEventListener(FaultEvent.FAULT,
 function faultHandler(event:FaultEvent):void
 {
 trace("ERROR: " + event.message);
 Alert.show("Error communicating to server, try again later", "Communication Fault");
 }
 );

 s.send();
 }

 public static function initFromURL():void
 {
 var url:String = FlexGlobals.topLevelApplication.url;
 var protocol:String = "http";
 var server:String = "192.168.1.100";
 var port:String = "8085";
 var prefix:String = "/referral-network";

 if (url.search("file://") >= 0)
 {
 }
 else
 {
 var i:int = url.indexOf("://");
 if (i >= 0) {
 protocol = url.substring(0, i);
 }

 url = url.replace(protocol + "://", "");
 url = url.substring(0, url.search("/"));

 var array:Array = url.split(":");
 if (array.length >= 1)
 {
 server = array[0];
 }
 if (array.length >= 2)
 {
 port = array[1];
 }
 else
 {
 if (protocol != null && protocol == "https")
 port = "443";
 else
 port = "80";
 }
 }

 SERVICE_URL = protocol + "://" + server + ":" + port + prefix;
 }
 }
}

Here is a simple usage of the pattern

public function unSubscribe():void{
	var xml:XML = <input />;

	var profile:XML = <profile />;
	xml.appendChild(profile);

	trace("DATA TO SEND: " + xml);
	ServiceProxy.execute(ServiceProxy.UNSUBSCRIBE, xml, unsubscribeResultHandler);
}

private function unsubscribeResultHandler(xml:XML):void{
	// Your Business Logic Here ...
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s