Exploring Macromedia s Developer Exchange

 < Day Day Up > 



Exploring Macromedia's Developer Exchange

You can take advantage of other developers' work by visiting Macromedia's Developer Exchange at http://devex.macromedia.com/developer/ gallery/. There you'll find hundreds of custom tags created to handle common Web tasks, such as managing banner ads, creating Web stores, and building online knowledge bases. Many tags are free of charge; others may offer a trial download but require a fee for registration.

You can often save hours, days, or weeks of development work by making use of a prewritten custom tag. For this reason, it's important to familiarize yourself with the contents of the Developer Exchange and check it regularly for updates.

Caution 

Not all custom tags are written by experienced developers who take care in their work. It's always important to test all downloaded tags thoroughly before using them in a live environment. It's also a good idea to look for tags written by established developers, and those that have a good track record with other users.

start sidebar

Web Services: Revolutionizing Application Integration, by Samuel Neff

Web services are the latest technology that allow one application to remotely call and utilize the functionality and data of another application. The development of SOAP-based Web services enables more than simply integrating functionality but provides the ability to describe that functionality and the programmatic interfaces. In this tutorial, I describe how to use ColdFusion MX to create and consume Web services.

Creating a Web service

To create a Web service in ColdFusion MX, start by creating a component. If you're not familiar with ColdFusion Components, refer to the components tutorial in Chapter 54. With a completed component, change the access level for the function you want to expose to remote consumption. For example, review the following code, which demonstrates a weather forecasting service:

<cfcomponent>       <cffunction     name="getWeatherForecast"     returnType="string"     access="remote">          <cfargument name="zip" type="string">          <cfscript>       switch (RandRange(1,5)) {         case 1:            forecast="Sunny";           break;         case 2:           forecast="Partly cloudy";           break;         case 3:           forecast="Rain";           break;         case 4:           forecast="Hail";           break;         case 5:           forecast="Thunderstorm";           break;       }     </cfscript>          <cfreturn forecast>         </cffunction>     </cfcomponent>

Simply by adding access="remote" to your component function, you can expose it to the world as a Web service. This function can be called from ColdFusion applications on another server as well as Java, ASP, VB, Perl, and any other client that supports SOAP or WDDX.

Web service descriptions

There are many standards for creating Web services. The most popular standard is Simple Object Access Protocol (SOAP), and all major companies in the field have current or planned support for SOAP-based Web services. One of the major advantages that SOAP has over other standards is its related technologies. Web Services Description Languages (WSDL) is one of these technologies. WSDL provides a standardized way to describe the programmatic interface to a Web service.

ColdFusion MX automatically generates WSDL files for Web services by appending ?wsdl to the end of a URL. The following URL generates the WSDL for a weather-forecasting service: http://127.0.0.1/ StudioMXBible/WebServices/Listing01.cfc?wsdl.

When entered into a browser, this displays the WSDL file in the following figure.

click to expand
WSDL file for the weather forecast service

Luckily, the WSDL file is not meant to be read by a person. As you'll see later, the WSDL file allows for simplified consumption of Web services.

Complex Web services

The weather forecast service is a simple Web service. It accepts a single argument, the ZIP code, and returns a description of the weather in that area. Two options can expand the Web service to return more information.

The first option is to return a structure from the function. Review the following code, which returns a more detailed weather forecast in a structure:

<cffunction   name="getWeatherForecastDetailed"   returnType="struct"   access="remote">      <cfargument name="zip" type="string">      <cfscript>     detail=StructNew();     detail.temperature=86;     detail.sunrise=CreateTime(6,48,0);     detail.sunset=CreateTime(7,46,0);     detail.description=This.getWeatherForecast(Arguments.zip);   </cfscript>      <cfreturn detail> </cffunction>

If you look at the WSDL file for this service, you'll see the following description for the returned value shown in the following code:

<complexType name="Map">   <sequence>     <element name="item" minOccurs="0" maxOccurs="unbounded">       <complexType>         <all>           <element name="key" type="xsd:anyType" />            <element name="value" type="xsd:anyType" />          </all>       </complexType>     </element>   </sequence> </complexType>

The preceding fragment of the WSDL describes the data returned from our Web service. This tells the calling application that our service is providing a set of key/value pairs. Both the keys and the values can be of any type. The problem with this is twofold. First, the calling application has no way to know what the expected keys are and the types of values being returned. Second, this key/value pair definition is specific to ColdFusion MX and is not usable from all Web service clients, including .NET and MSSOAP.

To provide greater information to the calling application, ColdFusion MX allows a function to return another component. By using the cfproperty tag within the component being returned (as seen in the following code), you are telling ColdFusion what type of data to expect and this information can then be relayed to clients through the WSDL.

<cfcomponent>   <cfproperty name="temperature" type="numeric" />   <cfproperty name="sunrise" type="date" />   <cfproperty name="sunset" type="date" />   <cfproperty name="description" type="string" /> </cfcomponent>

The component is simple. It's sole purpose is to define the structure for your new detailed weather forecasting service, shown in the following code:

<cffunction   name="getWeatherForecastDetailed"   returnType="WeatherForecastDetail"   access="remote">      <cfargument name="zip" type="string">      <cfscript>     detail=CreateObject("component","WeatherForecastDetail");     detail.temperature=86;     detail.sunrise=CreateTime(6,48,0);     detail.sunset=CreateTime(7,46,0);     detail.description=This.getWeatherForecast(Arguments.zip);   </cfscript>      <cfreturn detail> </cffunction>

Only two lines have been changed. Instead of returning a structure, you have specified returnType="WeatherForecastDetail", which indicates you're returning an instance of the component WeatherForecastDetail. Next, you have changed the StructNew() function call with a call to CreateObject(), which instantiates your return component. Otherwise, the functions are identical.

The WSDL for your new function closely matches the return component, as shown in the following code. This detailed WSDL file enables non-ColdFusion clients to consume your Web service and also gives all consumers greater detail about the information you're returning:

<complexType name="weatherforecastdetail">   <sequence>     <element name="Description" nillable="true" type="xsd:string" />      <element name="Sunrise" nillable="true" type="xsd:dateTime" />      <element name="Sunset" nillable="true" type="xsd:dateTime" />      <element name="Temperature" nillable="true" type="SOAP-ENC:double" />    </sequence> </complexType>

Now that you know how to create both simple and complex Web services and the corresponding WSDL descriptors, the next section discusses how to consume Web services within ColdFusion.

Using Dreamweaver MX to consume Web services

Dreamweaver MX greatly simplifies the process of consuming Web services within ColdFusion MX. To list the functions provided by a Web service within Dreamweaver MX, follow these steps, as demonstrated in the following figure.

  1. Expand the Application panel.

  2. Click the Components tab.

  3. Choose Web Services from the drop-down list.

  4. Click the plus (+) sign.

  5. Enter the URL to the WSDL file.

  6. Choose ColdFusion MX as the proxy generator.

  7. Click OK.

click to expand
Using Dreamweaver MX to parse a WSDL file for a Web service

After you specify a Web service, Dreamweaver MX provides a list of the functions provided including the return type and arguments, as shown in the following figure. The Web service shown here is a currency converter provided by xmethods.net:

http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl


Dreamweaver MX displays a list of functions provided by the Web service.

To generate the ColdFusion MX code to consume the Web service, drag the function name to the code window. Doing this generates the following code:

<cfinvoke   webservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"  method="getRate"  returnvariable="aRate">   <cfinvokeargument name="country1" value="enter_value_here"/>   <cfinvokeargument name="country2" value="enter_value_here"/> </cfinvoke> 

As you can see, Dreamweaver MX leaves identifiers for the argument values. Change the values to usa and uk to return the exchange rate between the U.S. dollar and British pound.

Consuming a Web service

As you can see from the earlier example, ColdFusion MX uses two new tags to consume Web services. Use cfinvoke identifies the Web service being consumed, the method to call, and the variable name for the returned value. Use cfinvokeargument to specify the value for all arguments to the Web service method. An alternative to cfinvokeargument is to use the argumentcollection attribute of cfinvoke to specify a structure that has key/value pairs corresponding to the method's arguments.

ColdFusion MX also provides a second method for consuming a Web service. You can create a Web service proxy object through the CreateObject() function by specifying the object type as webservice and the URL to the Web service. Then, calls to the Web service are made by calling the methods as functions of the proxy object, as shown in the following code.

<cfscript>     currencyExchange=CreateObject("webservice","http://www.xmethods.net/sd/ 2001/CurrencyExchangeService.wsdl");   aRate=currencyExchange.getRate("usa","uk"); </cfscript>

This code has the same effect as that in the preceding code listing. The advantage of using CreateObject() over cfinvoke becomes obvious when calling multiple methods of the same Web service. Although performance is the same for both methods, programming multiple calls to a Web service is simpler with CreateObject().

Both publishing and consuming Web Services is very simple in ColdFusion MX. Including Web Services in your application allows you to expand the possible uses beyond the HTML forms you create to any client application with which you want to integrate. By consuming Web Services within your application, you can provide your users with greater functionality that otherwise would not be possible.

end sidebar



 < Day Day Up > 



Macromedia Studio MX Bible
Macromedia Studio MX Bible
ISBN: 0764525239
EAN: 2147483647
Year: 2003
Pages: 491

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