Welcome!

ColdFusion Authors: Yakov Fain, Maureen O'Gara, Nancy Y. Nee, Tad Anderson, Daniel Kaar

Related Topics: ColdFusion, Java, Adobe Flex

ColdFusion: Article

A Complete Application with RPC Communications...

... between Flex and Java

This is a book excerpt from the book RIch Internet applications with Adobe Flex and Java.

Configuring the Server-Side Destination and Proxy

For security reasons (similar to the Java sandbox concept), Flash clients can only access the domains they come from, unless other servers declare, explicitly or implicitly, trust to SWF files downloaded from our domain by a corresponding record in a crossdomain.xml file. But our portfolio SWF wasn't loaded from finance.yahoo.com, and we aren't allowed to install crossdomain.xml on the Yahoo! servers. We'll use another technique called Flex proxy. When the user clicks on the News link in the data grid, the portfolio client will connect to our FDS Web application deployed under Tomcat (JRun, WebLogic), which will proxy our communication with Yahoo!. To configure the Flex proxy service, use the following section of the proxy-config.xml located in the \WEB-INF\flex directory:

   <destination id="YahooFinancialNews">
     <properties>
       <url>http://finance.yahoo.com/rss/headline</url>
     </properties>
   </destination>

Now Flex will contact http://finance.yahoo.com, get the news for the symbol specified, and return it back to the Flash client.

Processing the News Feed
The HTTPService converts the received data according to the value of the resultFormat property: e4x, flashvars, object (this is default), text, or xml.

If we knew that the results were coming back as name/value pairs concatenated with an ampersand, we could have picked the flashvars format. The text format is suitable for any raw text, but it's not easy to parse. The XML format is maintained for compatibility with pre-E4X versions of the ActionScript, so we'll pass on this one as well. Considering the other formats listed above, for our application, we prefer e4X XML. An e4X expression newsFeed.lastResult.channel.item is all you need to populate the XMLListCollection with news headlines. This XMLListCollection will be used as the dataProvider of the news DataGrid.

As usual, we'll separate the code that merely starts the application from the FinancialNews view ( see Listing 1).

<?xml version="1.0" encoding="utf-8"?>
<!--news1.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" >
     <FinancialNews1 id="fn"/>
</mx:Application>

Our newsFeed will initiate the following request from the creationComplete event:

     newsFeed.send({s:"ADBE"});

As part of the send method, Flex converts the object's property into a GET URL parameter using the object's properties as parameter names. Here s:"ADBE" will be concatenated in the form of "?s=ADBE" to the http://finance.yahoo.com/rss/headline specified by the news destination. Since RSS data can be verbose, we'll set the relevant columns of the grid with wordWrap="true" and the height of the Grid row to be flexible:
variableRowHeight="true".

Introducing Item Renderers
To make the data grid's column Link open a news story in a separate browser window, we need to customize the way of displaying (rendering) the cells of this column. We will use the so-called itemRenderer in the <mx:DataGridColumn> tag (see more samples of using item renderers in Chapters 8 and 9). There are two major kinds of item renderers:

•  drop-in, which is an ActionScript class that you specify as the value of the itemRenderer of any list-derived control.

•  inline, where you use an <mx:Component> tag to define a renderer component inside the <mx:itemRenderer> element.

To create an inline renderer, we'd need to change:

<mx:DataGridColumn headerText="Link" width="130" dataField="link" wordWrap="true"/>

into the following code:

<mx:DataGridColumn headerText="Link" width="130">
   <mx:itemRenderer>
     <mx:Component>
       <mx:LinkButton label="{data.link}" click="navigateToURL(new
         URLRequest(data.link), '_blank')"/>
     </mx:Component>
   </mx:itemRenderer>
</mx:DataGridColumn>

The ActionScript function flash.net.navigateToURL opens or replaces a window in the Flash Player's container application - opens it, in our case, in a blank browser.

While inline renderers excel in readability, drop-in renderers are reusable. If you expect to have links with similar presentation and functionality you may decide to create a class out of it.

Let us introduce the write-only security property for the FinancialView2 class. Please note that we provide a setter method to send a newsFeed request every time the security (stock symbol) is updated:

public function set security(value:String):void {
     newsFeed.send({s:value});
}


More Stories By Victor Rasputnis

Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at vrasputnis@faratasystems.com

More Stories By Yakov Fain

Yakov Fain is a co-founder of two software companies: Farata Systems and SuranceBay. He authored several technical books and lots of articles on software development. Yakov is Java Champion (https://java-champions.java.net). He leads leads Princeton Java Users Group. Two of Yakov's books will go in print this year: "Enterprise Web Development" (O'Reilly) and "Java For Kids" (No Starch Press).

More Stories By Anatole Tartakovsky

Anatole Tartakovsky is a Managing Principal of Farata Systems. He's responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at atartakovsky@faratasystems.com

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.