Jojit Soriano's Blog

July 1, 2009

Flex Workaround: ActionScript Does Not Support Method Overloading

Filed under: Uncategorized — jojitsoriano @ 8:43 am
Tags: ,

C#, VB.Net, and Java support method overloading, i.e. one can define two methods of the same name but with different parameters. For example, you can have a method named ShowName with a string parameter named pstrFirstName and you can have another method with the same name but with two string parameters named pstrFirstName and pstrLastName. This is method overloading and this is available also to class constructors. A class may be defined with multiple constructors. For example, you can have a default constructor, i.e. no parameters, and you can have two other constructors but with varying number of parameters depending on your requirement.

Unfortunately, ActionScript does not support method overloading! You cannot have two methods or two constructors having the same name even though they have different sets of parameters. The good news is that ActionScript provides …(rest) parameter to allow the developer to define a method that can accept any number of parameters. For example, if you want to create a single method that can add two, three, four, or more numbers, you can use the …(rest) parameter. This parameter declaration must be the last parameter specified.

The following code snippet shows a constructor for a class named AppBase that inherits from Canvas. My goal for this constructor with …(rest) parameter is to be able to use this class as a control in an MXML but at the same time to be able to instantiate the class with certain parameters. When you create a control and you want that control to be declared in MXML, the constructor of the control must have no parameter. In this snippet, I named the …(rest) parameter as …p_Args and is an array from which you can obtain the elements by specifying its index.

private var mstrAppId : String;
private var mstrJSessionId : String;
private var mstrServerUrl : String;
private var mstrWsdl : String;
public function AppBase(…p_Args)
{
    super();
    if(p_Args.length == 4)
    {
         mstrAppId = p_Args[0];
         mstrServerUrl = p_Args[1];
         mstrJSessionId = p_Args[2];
         mstrWsdl = p_Args[3];
    }
}

Flex Workaround: HTTPService Does Not Return Response Headers

Filed under: Uncategorized — jojitsoriano @ 5:03 am
Tags: ,

I am surprised that HTTPService of Adobe Flex does not return response headers!

What if an application written in Flex would want to communicate with a system written in J2EE and was using the Set-Cookie response header to get the JSESSIONID for login validation? This is what I have experienced when I started to work on a project that aims to evaluate the capability of Adobe Flex. Luckily I was able to do a workaround but I had to reuse the .Net WCF-based service I created in another project. My WCF-based service contains an OperationContract or a web method named MakeRequest. It accepts the parameters target url, request method, i.e. POST or GET, an array of header names, an array of header values, an array of parameter names, and an array of the values to post. The key to this web method is the WebClient
class from the System.Net namespace because it returns response headers together with the response body. The output string of this web method is the response body plus the response headers delimited by “@@@@@”.

[OperationContract]

public
string MakeRequest(string pstrTargetUrl,


string pstrMethod,


string[] pstrHeaderNames,


string[] pstrHeaderValues,


string[] pstrPostValueNames,


string[] pstrPostValues)

{


string returnString = “Bad Request”;


// This is a GET request with no form values and optional Basic auth


if (pstrMethod.ToUpper() == “GET”)

{


using (WebClient wc = new
WebClient())

{

wc.Proxy = null;


if (pstrHeaderNames != null && pstrHeaderNames.Length > 0)

{


for (int i = 0; i < pstrHeaderNames.Length; i++)

{

wc.Headers.Add(pstrHeaderNames[i], pstrHeaderValues[i]);

}

}

returnString = wc.DownloadString(pstrTargetUrl);

}

}


// This is a POST request with form values and optional Basic auth


if (pstrMethod.ToUpper() == “POST” && pstrPostValueNames != null)

{


using (WebClient wc = new
WebClient())

{

wc.Proxy = null;


if (pstrHeaderNames != null && pstrHeaderValues != null)

{


for (int i = 0; i < pstrHeaderNames.Length; i++)

{

wc.Headers.Add(pstrHeaderNames[i], pstrHeaderValues[i]);

}

}


var data = new
NameValueCollection();


for (int i = 0; i < pstrPostValueNames.Length; i++)

{

data.Add(pstrPostValueNames[i], pstrPostValues[i]);

}

 


byte[] result = wc.UploadValues(pstrTargetUrl, “POST”, data);


byte[] responseHeader = wc.ResponseHeaders.ToByteArray();

 

returnString = System.Text.Encoding.UTF8.GetString(responseHeader) + “@@@@@” + System.Text.Encoding.UTF8.GetString(result);

 

}

}


return returnString;

}

 

At the Flex side of the code, use the mx.rpc.soap.WebService to call the web method above with the required parameters. A sample of the header and parameter key-value pairs is shown below. Note that headerNames and headerValues should have a one-to-one correspondence, i.e. each element of the headerNames array should correspond to each element of the headerValues array. paramNames and paramValues should also have a one-to-one correspondence.

var headerNames : Array = new Array(“Content-Type”);

var headerValues : Array = new Array(“application/x-www-form-urlencoded”);

 

var paramNames : Array = new Array(“USER”, “CLIENT_PASSWORD”, “DATABASE”); var paramValues: Array = new Array(“username”, “password”, “dbname”);


 

Blog at WordPress.com.