Saturday, August 2, 2008

My take on REST and SOAP

  1. Soap supports more RPC style of architecture where the use of SOAP is to invoke a remote function carrying in the message the function name and the various parameters. The RPC is basically a function that is sitting on a remote machine.
  2. The function has a specified number of parameters that needs to be provided to make use of the function. The use of SOAP is to make requests to such functions and to get the response back.
  3. SOAP uses XML to transfer the data and so it is heavy in size.
  4. The SOAP message is cryptic in the sense that it contains a header that wil specify the route, the info about the message and sometimes the security aspect for the message
  5. The SOAP message is difficult to create without the tools. So these days the langusgaes provide the library functions which will create the SOAP message
  6. The SOAP is generally sent using HTTP but other protocols are also valid like Java Message service
  7. SOAP is very function (verb ) centric. The way you get the things done is by calling a function so to get the stock prices you will call a function (web method: A webservice will have number of methods) getstock(GE)

Now the function will calculate the price and send it back to you

You must know the names of the function and the way it is going to use the parameters. For that you need to get the WSDL file which is very cryptic.

  1. SOAP syntax is not user friendly and human readable
  2. When SOAP makes the http request its difficult to find out which http method it is actually going to use.

REST

In case of rest the functions are fixed it will be get, put, post and delete. You will mention the name of the stock directly while making the request (each object will have a different URI) so www.stockprice/category/stock/GE

In this case the description of how to make the call is very simple and its intuitive.

No extra tool is required, The call is very simple.

REST is only for http since it uses the protocols methods GET, PUT(Update), POST (create), DELETE. This is good since the user don’t have to find out which method will be used for a specific purpose. Its basically service_name/noun so Stock is a service and GE is the noun

Tuesday, May 13, 2008

Password validator in flex 3


create a validator PwdValidator.as

package valueObjects

{
import mx.validators.ValidationResult;
import mx.validators.Validator;

public class PwdValidator extends Validator {

// Define Array for the return value of doValidation().
private var results:Array;
// this will help in getting the status
public static var VALID:Boolean = true;


// Constructor.
public function PwdValidator() {
// Call base class constructor.
super();


}

// Define the doValidation() method.
override protected function doValidation(value:Object):Array {
//var pwd: Password = value as Password;
var p1: String = value.first;
var p2: String = value.second;

results = [];
results = super.doValidation(value);


if(p1 == p2)
{
VALID=true;
return results;
}
else
{
results.push(new ValidationResult(true, null, "Mismatch",
"Password Dosen't match Retype!"));
VALID=false;
return results;
}
}
}

}


Now the flex code should be passing the value of the password and confirm password textbox. This is done as

<mx:Model id ="pass">
<name>
<passwords>

<first>{txtPassword.text}</first>
<second>{txtConfirmPassword.text}</second>

</passwords>
</name>
</mx:Model>

// this is the way to pass the multiple controls input to the validator.
<val:PwdValidator id="custPass" source="{pass}" property="passwords" listener="{txtPassword}"/>

<mx:Form labelWidth="110">

<mx:FormItem required="true" label="Password">
<mx:TextInput id="txtPassword" displayAsPassword="true" />
</mx:FormItem>
<mx:FormItem required="true" label="Confirm Password">
<mx:TextInput id="txtConfirmPassword" displayAsPassword="true" change="custPass.validate()"/>
</mx:FormItem>

</mx:Form>


Wednesday, May 7, 2008

Five best bollywood lines

1. Border : If every soldier leaves the battlefield to serve his family, who is going to fight the enemy
2. RHTM.. never say i dont eat nonveg, say I have not eaten it since long time
3. DDLJ: Whats there in Zurich , real europe is countryside.
You can not spend the entire life with your friends, you got to have a beloved
4. Zameen: In our country its illegal to kill stray dogs
5. OSO: Every story has a happy ending, if you think the end is not happy it means the story is incomplete

Saturday, May 3, 2008

New Book on Web 2.0



A must read book. The author has explained the Web 2.0 philosophy in a very intuitive way. He talks about how to monetize the websites at the same time attract the users by giving them
some free services. He calls this as Freemium.
The book discusses the success stories of Flickr, Facebook,Linkedin, google. The basis of all the success story is the rich user base.All of them have followed the long trail concept by reaching out to the masses rather than concentrating to a niche segment " Every buck counts".
Since the book talks about the sites which we commonly use it is easy to realize the concepts since we are the part of the revolution happening around the particular site.

I would highly recommend this book to anybody who wants to find out what is this web 2.0 business all about.

Saturday, April 19, 2008

How to create XMLListCollection from XML in flex 3

There are times when the data that you obtain is in the form of XML or a string that contains XML tags. For displaying the data in datagrid, list, trees we desire to convert the data in the XMLListCollection. We need to perform the following steps to get the XMLListCollection.

1. Lets assume that we want to convert the following string to the XMLListCollection

var xmlStr:String="<root>
<person>
<name>Rohit</name>
<surname>Agarwal</surname>
<phone>5551234</phone>
<age>24</age>
</person>
<person>
<name>Richa</name>
<surname>Mittal</surname>
<phone>5552341</phone>
<age>23</age>
</person>
<person>
<name>Puneet</name>
<surname>Jain</surname>
<phone>5553412</phone>
<age>23</age>
</person>
</root>";

2. Convert the string to XML

private var pat:XML ;

pat=new XML(xmlstr);

3. Convert the XML to XMLList. We want to have individual persons as seperate XML objects

private var patList:XMLList;
patList = pat.person;

4. Convert the XMLList to XMLListCollection

[Bindable]
private var patListCol:XMLListCollection;
patListCol = new XMLListCollection(patList);

In this way the XMLListCollection is created from the XML





How to convert a XML to ArrayCollection in Flex

I wanted to use AdvancedDataGrid control of Flex 3, for which the best way to represent data is in the form of an ArrayCollection. The problem was that the data available to me was in the form of an string(XML serialized as string) and not objects. So I wanted to convert the XML to ArrayCollection.

Steps
1. The following is the string that I wanted to convert:

var xmlStr:String="<root>
<person>
<name>Rohit</name>
<surname>Agarwal</surname>
<phone>5551234</phone>
<age>24</age>
</person>
<person>
<name>Richa</name>
<surname>Mittal</surname>
<phone>5552341</phone>
<age>23</age>
</person>
<person>
<name>Puneet</name>
<surname>Jain</surname>
<phone>5553412</phone>
<age>23</age>
</person>
</root>";

2. Convert the string to XMLDocument

var xmlDoc:XMLDocument = new XMLDocument(xmlStr);


3. Convert the XMLDocument to object by using SimpleXMLDecoder

var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
var resultObj:Object = decoder.decodeXML(xmlDoc);

4. Declare an ArrayCollection
[Bindable]
private var patArrayListCollection:ArrayCollection = new ArrayCollection();

5. Add the resultobj to the ArrayCollection

patArrayListCollection.addItem(resultObj.root.person);

The important thing to note here is the composition of the patArrayListCollection is in the form
[0] --main object
[0]-sub object 1
[1]-subobject 2
[2]-subobject 3

This is not in a form that we require to make it a data provider for the AdvancedDataGrid. The Grid requires the ArrayCollection to have
objects in the form
[0]-object 1
[1]-object 2
[2]-object 3


So to get this form the following step is done

6. var tempArray:ArrayCollection=patArrayListCollection.getItemAt(0)as ArrayCollection;

7. Finally assign tempArray to the AdvancedDataGrid
myADG.dataProvider=tempArray;






Sunday, April 13, 2008

ActionScript Error #2148:
SecurityError: Error #2148: SWF file file:///C:/Documents and Settings/UserProfile/Desktop/flexstore/bin-release/flexstore.swf cannot access local resource myFile.swf. Only local-with-filesystem and trusted local SWF files may access local resources.


This runtime error is thrown in Flex 3 if some where in the application you are trying to do a httpservice and use a resource from the local folder.
<mx:HTTPService id="fillTreeByCatRPC" url="categories.xml" resultFormat="e4x" />

I have seen this error only in Flex 3, Flex 2 never gave this error.

I did some google and found some solutions. Almost all the solutions suggests that one should change the flex compiler settings.

"add these arguments to the compiler (via Properties - Flex Compiler) : -use-network=false " http://curtismorley.com/2007/08/31/flash-cs3-flex-2-as3-error-2148/#comment-3714

The problem with this solution is that once the settings are changed then the application will not be able to read any remote resource. so the below request will fail
<mx:HTTPService id="fillTreeByCatRPC" url="http://www.resources.com/categories.xml" resultFormat="e4x" />

After doing further searches on Internet, I found that it is the setting problems of the flash player that cause the problem. Basically one needs to set the security settings of the flash player so that it allows the swf file to access the resources on the local system. This is done by visiting the following page
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html

Make sure that the Global security settings are set to Always Allow

The following screenshot is what we are looking for
Once the settings has been done, Restart the browser and Flex 3 IDE

In all probability the error is gone.