Working with SOAP Requests


ColdFusion offers a variety of ways to work with the SOAP requests and responses involved in Web services. Let's start with a simple example. As discussed earlier, for a component to be used as a Web service, it must have at least one method with access="remote". This same method, however, can be called by other ColdFusion templates on the server. It can also be called via Flash Remoting. What if you want to ensure that the method is only called as a Web service? ColdFusion MX 7 has a new function, isSoapRequest(), that returns TRue if the current method was executed as a Web service. Listing 24.18 demonstrates a simple Web service with a method that can only be called as a Web service.

Listing 24.18. A Web-Service-Only Component (justawebservice.cfc)
 <cfcomponent>   <cffunction name="test" returntype="string" access="remote">          <cfif isSoapRequest()>            <cfreturn "Good call!">     <cfelse>       <cfthrow message="This method must be called as a web service.">     </cfif>        </cffunction> </cfcomponent> 

This component has only one method: test. The body of the method simply checks to see if it is being executed as a SOAP request. It does this using isSoapRequest(). If this function returns true, we return a simple string. If not, we use <cfthrow> to throw an error. Listing 24.19 demonstrates using the same component both as a Web service and as a local CFC.

Listing 24.19. Testing justawebservice.cfc
 <!--- Construct the URL dynamically ---> <cfif not findNoCase("https", cgi.server_protocol)>   <cfset theURL = "http://"> <cfelse>   <cfset theURL = "https://"> </cfif> <!--- Add the server and current path ---> <cfset theURL = theURL & cgi.server_name & ":" & cgi.server_port &        cgi.script_name> <!--- Now remove this file's name, which is at the end ---> <cfset theURL = listDeleteAt(theURL, listLen(theURL,"/"), "/")> <!--- Call as a web service ---> <cfinvoke webservice="#theURL#/justawebservice.cfc?wsdl" method="test"           returnVariable="result"> <cfoutput>#result#</cfoutput> <p> <!--- Call as a CFC ---> <cftry>   <cfinvoke component="justawebservice" method="test" returnVariable="result">   <cfoutput><p>#result#</cfoutput>   <cfcatch>     Sorry, but I couldn't call the method.   </cfcatch> </cftry> 

As in the other templates we have used, the script begins by determining the current URL. Once past that, we invoke the Web service method and then call the same method as a local CFC. Since we know this isn't going to work, we wrap it in a <cftry><cfcatch> block. When run, the Web service will return a good result, while the local CFC invocation will throw an error.

Calling Web Services with Nillable Arguments

When building a method, any combination of arguments can be used to define its behavior. Some Web services may define a method as having "nillable" arguments. This simply means the value can be null. However, ColdFusion doesn't allow you to create null values. Thus in earlier versions of ColdFusion it was impossible to call these Web services. A new argument, omit, can be used in <cfinvokeargument> to pass a null value to a Web service. Listing 24.20 demonstrates a simple Web service with an optional argument (this will act as a nillable argument).

Listing 24.20. NillableWS.cfc
 <cfcomponent>        <cffunction name="test" returntype="string" access="remote">          <cfargument name="alpha" type="string" required="true">          <cfargument name="beta" type="string" required="false">          <cfargument name="carny" type="string" required="true">          <cfreturn "Foo">        </cffunction> </cfcomponent> 

This is an extremely simple Web service. The test method has three arguments. The second argument, beta, is marked as optional. Listing 24.21 demonstrates how we can correctly call this Web service.

Listing 24.21. Testing NillableWS.cfc (TestNillable.cfm)
 <!--- Construct the URL dynamically ---> <cfif not findNoCase("https", cgi.server_protocol)>   <cfset theURL = "http://"> <cfelse>   <cfset theURL = "https://"> </cfif> <!--- Add the server and current path ---> <cfset theURL = theURL & cgi.server_name & ":" & cgi.server_port &        cgi.script_name> <!--- Now remove this file's name, which is at the end ---> <cfset theURL = listDeleteAt(theURL, listLen(theURL,"/"), "/")> <cfinvoke webservice="#theURL#/NillableWS.cfc?wsdl" method="test"           returnVariable="result">   <cfinvokeargument name="alpha" value="foo">   <cfinvokeargument name="beta" omit="yes">   <cfinvokeargument name="carny" value="moo"> </cfinvoke> <cfoutput>Result is #result#</cfoutput> 

As with our numerous other examples, we begin by simply grabbing the current URL dynamically. We then invoke the NillableWS Web service. This Web service takes three arguments, with the middle argument, beta, being optional. By default, you cannot omit this argument, even though the method was defined as optional. With ColdFusion MX 7, however, we can pass in the argument and use omit="yes" to pass a null value to the Web service. Think of this as a simple way of passing a null value, which ColdFusion doesn't natively support.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

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