Using the Flex Proxy Service


In this task, you will use the Proxy Service to overcome the security restrictions of Flash Player that apply when you try to retrieve data or other assets from remote servers at run time.

1.

Open Flex Builder 2 and be sure FDS is running. Be sure the TfsDataServices file is open, and add an <mx:HTTPService> tag. Assign the tag an id of myHTTPCall, and specify the URL as http://www.flexgrocer.com/categorizedProducts.xml. Specify the resultFormat as e4x, as follows:

<mx:HTTPService     url ="http://www.flexgrocer.com/categorizedProducts.xml"    resultFormat="e4x"/> 


This will make an HTTP call to the flexgrocer.com server to access the XML file. If a cross-domain file is installed at the web root of the server, this will work fine without the use of the FPS.

2.

Add a creationComplete event to the <mx:Application> tag and call the send() method of the myHTTPCall HTTPService tag, as follows:

<mx:Application    xmlns:mx= "http://www.adobe.com/2006/mxml"    layout= "absolute" creationComplete="myHTTPCall.send();"> 


This will actually execute the HTTPService call when all the controls have been built within the application.

3.

After the <mx:HTTPService> tag, add a Tree control to the application and specify the dataProvider as myHTTPCall.lastResult.category. Set the labelField property to @name and set showRoot to false. Run the application.

<mx:Tree dataProvider="{myHTTPCall.lastResult.category}"    labelField="@name"    showRoot="false"/> 


The Tree control is populated with the data from the HTTPService call because a cross-domain security file has been installed at the web root of the flexgrocer.com server, and any SWF file can access the URL without violating the security sandbox restrictions of Flash Player.

4.

Modify the <mx:HTTPService> tag so that the URL property is pointing to the http://www.flex2.net/categorizedProducts.xml server and then run the application.

<mx:HTTPService    url="http://www.flex2.net/categorizedProducts.xml"   resultFormat="e4x"/> 


You should see that the Tree control is no longer populated, and the following run-time error should appear:

This error appears because no cross-domain security file is installed at the flex2.net server, and the security sandbox restriction in Flash Player is violated. In the next steps, you will use the proxy service of the FDS to access this URL and bypass the sandbox restrictions.

5.

From the Flex Builder menu, choose File > New > File. Click the Advanced button to display more options and click the Link to file in the file system check box. Click Browse and navigate to the WEB-INF/flex directory under the Flex context root and select the file proxy-config.xml file. Enter the file name, proxy-config.xml, in the filename text box.

You should now see the linked file in the Navigator view, which creates a link to the external default Proxy Service configuration file in Flex Builder that you can access. The Proxy Service relays HTTP and web service requests from the Flex application in the browser to the server that's hosting the remote service. Because the communication with the remote server now comes from the FDS instead of directly from Flash Player, you no longer need to create a cross-domain security file. In the proxy-config.xml file, you can configure which servers the proxy can access.

6.

In the proxy-config.xml file, locate the DefaultHTTP <destination> tag. Add a properties tag pair inside of the destination. Then, add a dynamic-url tag pair inside of properties. Add the string http://www.flex2.net/* inside of the dynamic-url tag, as shown following:

<destination >   <properties>     <dynamic-url>       http://www.flex2.net/*     </dynamic-url>   </properties> </destination> 


When setting up a Proxy Service destination, you choose between the default destination and a specially named destination. The default destination always has an id of DefaultHTTP. Within the properties element, you can create one or more <dynamic-url> elements. Each element represents a URL that has permission to use the Proxy Service to communicate with the remote server. Each URL is essentially a whitelist for addresses that are acceptable to use with the Proxy Service.

7.

Return to the TfsDataServices.mxml file and locate the <mx:HTTPService> tag. Add a useProxy attribute to the tag and set the value to true, as shown here.

<mx:HTTPService     url= "http://www.flex2.net/categorizedProducts.xml"    resultFormat="e4x" useProxy="true" /> 


The request will use the default destination because the url property matches one of the default destination's dynamic URL patterns specified in the proxy-config.xml file.

8.

Save and run the application.

You should see the application load and the data display correctly.




Adobe Flex 2.Training from the Source
Adobe Flex 2: Training from the Source
ISBN: 032142316X
EAN: 2147483647
Year: 2006
Pages: 225

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net