XWeb: ColdFusion

ColdFusion is an application server and software development framework used for the development of computer software in general, and dynamic web sites in particular. In this regard, ColdFusion is a similar product to Microsoft ASP.NET, JavaServer Pages or PHP. ColdFusion was the first amongst these technologies to provide the developer the capability of creating dynamic websites that were attached to a backend database.

Sample Code (cfc)

Only a few baseline xWeb methods are incorporated into the CFC code. An additional function (xmlToQuery) has been incorporated, so any SOAP response can be converted and returned as a ColdFusion Query - a huge time savings. Appropriate dependencies are commented in the code throughout.

<!--- 
//////////////////////////////////////////////////////////////
// //
// filename = netforum_xweb.cfc //
// //
// author = Derek T. Versteegen //
// email = dversteegen@aap.org //
// organization = American Academy of Pediatrics //
// date added = 10/18/2007 //
// date last modified = 10/10/2008 - DTV //
// //
// notes = netFORUM xWeb integration with web services //
// At the time of last edit, there are 20 methods //
// available in baseline xWeb, functions will be //
// added as needed. //
// //
//////////////////////////////////////////////////////////////
--->
 
<cfcomponent output="no">
 
<cfset nfws_address="http://localhost/netforum/xweb/secure/netFORUMXML.asmx?wsdl">
<cfset nfws_namespace="http://www.avectra.com/2005/">
 
<!---
//////////////////////////////////////////////////////////////
// //
// xmlToQuery //
// //
//////////////////////////////////////////////////////////////
--->
<cffunction name="xmlToQuery" output="false" access="public" returntype="query">
<cfargument name="xmlPacket" type="string" required="yes">
<cfargument name="parentTree" type="string" required="yes">
<cfargument name="childElement" type="string" required="yes">
 
<!--- ensure XML was passed in --->
<cfset xmlColumnList = "">
 
<!--- define number of rows and columns --->
<cfset xmlSize = ArrayLen(Evaluate("arguments.xmlPacket.#arguments.parentTree#.xmlChildren"))>
<cfset noColumns = ArrayLen(Evaluate("arguments.xmlPacket.#arguments.parentTree#.#childElement#[1].xmlChildren"))>
 
<!--- create columns list --->
<cfloop from="1" to="#noColumns#" step="1" index="i">
<cfset column = Evaluate("arguments.xmlPacket.#arguments.parentTree#.#childElement#[1].xmlChildren[#i#].xmlName")>
<cfset xmlColumnList = Listappend(xmlColumnList,column)>
</cfloop>
 
<!--- create query with correct number of rows --->
<cfset qryXML = QueryNew(xmlColumnList)>
<cfset tmp = QueryAddRow(qryXML,xmlsize)>
 
<!--- build query --->
<cfloop from="1" to="#xmlSize#" step="1" index="a">
<cfloop list="#xmlColumnList#" index="b">
<cftry>
<cfset cElement = "arguments.xmlPacket.#parentTree#.#childElement#[a].#b#.xmlText">
<cfset tmp = QuerySetCell(qryXML,"#b#",Evaluate(cElement),a)>
<cfcatch type="expression">
<!--- NULL DATA COLUMN SO DO NOT INSERT INTO QUERY & CONTINUE--->
</cfcatch>
</cftry>
</cfloop>
</cfloop>
 
<cfreturn qryXML>
</cffunction>
 
<!---
//////////////////////////////////////////////////////////////
// //
// xWeb:Authenticate //
// //
//////////////////////////////////////////////////////////////
--->
<cffunction name="xweb_Authenticate" access="public" returntype="string">
<cfargument name="nfws_uname" default="uname" type="string" required="yes">
<cfargument name="nfws_pword" default="pword" type="string" required="yes">
 
<!--- consume web service --->
<cfset nfws_method="AuthorizationToken">
<cfset nfws_token="">
<cftry>
<cfscript>
ws = CreateObject("webservice",application.nfws_address);
ws.Authenticate(userName = arguments.nfws_uname, password = arguments.nfws_pword);
nfws_token = getSOAPResponseHeader(ws,nfws_namespace,nfws_method, true);
</cfscript>
 
<!--- catch errors or unauthorized credentials --->
<cfcatch type="any">
<cfsavecontent variable="nfws_token">
<AuthorizationToken>
<Token>
00000000-0000-0000-0000-000000000000
</Token>
</AuthorizationToken>
</cfsavecontent>
</cfcatch>
</cftry>
 
<!--- parse and clean response --->
<cfset nfws_token = xmlparse(nfws_token)>
<cfset nfws_token = nfws_token.AuthorizationToken[1].Token.xmltext>
 
<!--- set token in session scope --->
<cfset session.nfws_token = nfws_token>
 
<cfreturn nfws_token>
</cffunction>
 
<!---
//////////////////////////////////////////////////////////////
// //
// xWeb:GetIndividualInformation //
// //
//////////////////////////////////////////////////////////////
--->
<cffunction name="xweb_GetIndividualInformation" access="public" returntype="query">
<cfargument name="nfws_namespace" default="" type="string" required="yes">
<cfargument name="nfws_address" default="" type="string" required="yes">
<cfargument name="nfws_token" default="" type="string" required="yes">
<cfargument name="cst_key" default="" type="string" required="yes">
 
<!--- create the raw SOAP XML --->
<cfsavecontent variable="nfws_soappacket">
<cfoutput>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="#arguments.nfws_namespace#">
<soapenv:Header>
<ns:AuthorizationToken>
<ns:Token>#arguments.nfws_token#</ns:Token>
</ns:AuthorizationToken>
</soapenv:Header>
<soapenv:Body>
<ns:GetIndividualInformation>
<ns:IndividualKey>#arguments.cst_key#</ns:IndividualKey>
</ns:GetIndividualInformation>
</soapenv:Body>
</soapenv:Envelope>
</cfoutput>
</cfsavecontent>
 
<!--- consume the web service with raw SOAP XML --->
<cfhttp url="#application.nfws_address#" method="POST">
<cfhttpparam type="HEADER" name="Content-Type" value="text/xml; charset=utf-8">
<cfhttpparam type="HEADER" name="Accept" value="application/soap+xml, application/dime, multipart/related, text/*">
<cfhttpparam type="HEADER" name="User-Agent" value="xWeb:ColdFusion">
<cfhttpparam type="HEADER" name="Host" value="localhost">
<cfhttpparam type="HEADER" name="Cache-Control" value="no-cache">
<cfhttpparam type="HEADER" name="Pragma" value="no-cache">
<cfhttpparam type="HEADER" name="SOAPAction" value="#arguments.nfws_namespace#GetIndividualInformation">
<cfhttpparam type="HEADER" name="Content-Length" value="#len(trim(nfws_soappacket))#">
<cfhttpparam type="BODY" value="#trim(nfws_soappacket)#">
</cfhttp>
<cfset obj = xmlParse(cfhttp.filecontent)>
 
<!--- set token in session scope --->
<cfset nfws_token=obj.Envelope.Header.AuthorizationToken.Token.xmltext>
<cfset session.nfws_token = nfws_token>
 
<!--- xmlToQuery --->
<cftry>
<cfinvoke component="/gateway/netforum_xweb" method="xmlToQuery" returnvariable="qryXML">
<cfinvokeargument name="xmlPacket" value="#obj#" omit="no">
<cfinvokeargument name="parentTree" value="Envelope.Body.GetIndividualInformationResponse.GetIndividualInformationResult.IndividualObjects" omit="no">
<cfinvokeargument name="childElement" value="IndividualObject" omit="no">
</cfinvoke>
<cfcatch type="any">
<cfscript>
qryXML = QueryNew("");
</cfscript>
</cfcatch>
</cftry>
 
<cfreturn qryXML>
</cffunction>
 
<!---
//////////////////////////////////////////////////////////////
// //
// xWeb:GetFacadeObject //
// //
//////////////////////////////////////////////////////////////
--->
<cffunction name="xweb_GetFacadeObject" access="public" returntype="query">
<cfargument name="nfws_namespace" default="" type="string" required="yes">
<cfargument name="nfws_address" default="" type="string" required="yes">
<cfargument name="nfws_token" default="" type="string" required="yes">
<cfargument name="szObjectName" default="" type="string" required="yes">
<cfargument name="szObjectKey" default="" type="string" required="yes">
 
<!--- create the raw SOAP XML --->
<cfsavecontent variable="nfws_soappacket">
<cfoutput>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="#arguments.nfws_namespace#">
<soapenv:Header>
<ns:AuthorizationToken>
<ns:Token>#arguments.nfws_token#</ns:Token>
</ns:AuthorizationToken>
</soapenv:Header>
<soapenv:Body>
<ns:GetFacadeObject xmlns:ns="#application.nfws_namespace#">
<ns:szObjectName>#arguments.szObjectName#</ns:szObjectName>
<ns:szObjectKey>#arguments.szObjectKey#</ns:szObjectKey>
</ns:GetFacadeObject>
</soapenv:Body>
</soapenv:Envelope>
</cfoutput>
</cfsavecontent>
 
<!--- consume the web service with raw SOAP XML --->
<cfhttp url="#application.nfws_address#" method="POST">
<cfhttpparam type="HEADER" name="Content-Type" value="text/xml; charset=utf-8">
<cfhttpparam type="HEADER" name="Accept" value="application/soap+xml, application/dime, multipart/related, text/*">
<cfhttpparam type="HEADER" name="User-Agent" value="xWeb:ColdFusion">
<cfhttpparam type="HEADER" name="Host" value="localhost">
<cfhttpparam type="HEADER" name="Cache-Control" value="no-cache">
<cfhttpparam type="HEADER" name="Pragma" value="no-cache">
<cfhttpparam type="HEADER" name="SOAPAction" value="#application.nfws_namespace#GetFacadeObject">
<cfhttpparam type="HEADER" name="Content-Length" value="#len(trim(nfws_soappacket))#">
<cfhttpparam type="BODY" value="#trim(nfws_soappacket)#">
</cfhttp>
<cfset obj = xmlParse(cfhttp.filecontent)>
 
<!--- set token in session scope --->
<cfset nfws_token=obj.Envelope.Header.AuthorizationToken.Token.xmltext>
<cfset session.nfws_token = nfws_token>
 
<!--- xmlToQuery --->
<cftry>
<cfinvoke component="/gateway/netforum_xweb" method="xmlToQuery" returnvariable="qryXML">
<cfinvokeargument name="xmlPacket" value="#obj#" omit="no">
<cfinvokeargument name="parentTree" value="Envelope.Body.GetFacadeObjectResponse.GetFacadeObjectResult" omit="no">
<cfinvokeargument name="childElement" value="#szObjectName#Object" omit="no">
</cfinvoke>
<cfcatch type="any">
<cfscript>
qryXML = QueryNew("");
</cfscript>
</cfcatch>
</cftry>
 
<cfreturn qryXML>
</cffunction>
 
<!---
//////////////////////////////////////////////////////////////
// //
// xWeb:UpdateFacadeObject //
// //
//////////////////////////////////////////////////////////////
--->
<cffunction name="xweb_UpdateFacadeObject" access="public" returntype="query">
<cfargument name="nfws_token" default="" type="string" required="yes">
<cfargument name="szObjectName" default="" type="string" required="yes">
<cfargument name="szObjectKey" default="" type="string" required="yes">
<cfargument name="oNode" default="" type="string" required="yes">
 
<!--- create the raw SOAP XML --->
<cfsavecontent variable="nfws_soappacket">
<cfoutput>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="#application.nfws_namespace#">
<soapenv:Header>
<ns:AuthorizationToken>
<ns:Token>#arguments.nfws_token#</ns:Token>
</ns:AuthorizationToken>
</soapenv:Header>
<soapenv:Body>
<ns:UpdateFacadeObject xmlns:ns="#application.nfws_namespace#">
<ns:szObjectName>#arguments.szObjectName#</ns:szObjectName>
<ns:szObjectKey>#arguments.szObjectKey#</ns:szObjectKey>
<ns:oNode>#arguments.oNode#</ns:oNode>
</ns:UpdateFacadeObject>
</soapenv:Body>
</soapenv:Envelope>
</cfoutput>
</cfsavecontent>
 
 
<!--- consume the web service with raw SOAP XML --->
<cfhttp url="#application.nfws_address#" method="POST">
<cfhttpparam type="HEADER" name="Content-Type" value="text/xml; charset=utf-8">
<cfhttpparam type="HEADER" name="Accept" value="application/soap+xml, application/dime, multipart/related, text/*">
<cfhttpparam type="HEADER" name="User-Agent" value="xWeb:ColdFusion">
<cfhttpparam type="HEADER" name="Host" value="localhost">
<cfhttpparam type="HEADER" name="Cache-Control" value="no-cache">
<cfhttpparam type="HEADER" name="Pragma" value="no-cache">
<cfhttpparam type="HEADER" name="SOAPAction" value="#application.nfws_namespace#UpdateFacadeObject">
<cfhttpparam type="HEADER" name="Content-Length" value="#len(trim(nfws_soappacket))#">
<cfhttpparam type="BODY" value="#trim(nfws_soappacket)#">
</cfhttp>
<cfset obj = xmlParse(cfhttp.filecontent)>
 
<!--- set token in session scope --->
<cfset nfws_token=obj.Envelope.Header.AuthorizationToken.Token.xmltext>
<cfset session.nfws_token = nfws_token>
 
<!--- xmlToQuery --->
<cftry>
<cfinvoke component="/gateway/netforum_xweb" method="xmlToQuery" returnvariable="qryXML">
<cfinvokeargument name="xmlPacket" value="#obj#" omit="no">
<cfinvokeargument name="parentTree" value="Envelope.Body.UpdateFacadeObjectResponse" omit="no">
<cfinvokeargument name="childElement" value="UpdateFacadeObjectResult" omit="no">
</cfinvoke>
<cfcatch type="any">
<cfscript>
qryXML = QueryNew("");
</cfscript>
</cfcatch>
</cftry>
 
<cfreturn qryXML>
</cffunction>
 
<!---
//////////////////////////////////////////////////////////////
// //
// xWeb:InsertFacadeObject //
// //
//////////////////////////////////////////////////////////////
--->
<cffunction name="xweb_InsertFacadeObject" access="public" returntype="query">
<cfargument name="nfws_token" default="" type="string" required="yes">
<cfargument name="szObjectName" default="" type="string" required="yes">
<cfargument name="oNode" default="" type="string" required="yes">
 
<!--- create the raw SOAP XML --->
<cfsavecontent variable="nfws_soappacket">
<cfoutput>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="#application.nfws_namespace#">
<soapenv:Header>
<ns:AuthorizationToken>
<ns:Token>#arguments.nfws_token#</ns:Token>
</ns:AuthorizationToken>
</soapenv:Header>
<soapenv:Body>
<ns:InsertFacadeObject xmlns:ns="#application.nfws_namespace#">
<ns:szObjectName>#arguments.szObjectName#</ns:szObjectName>
<ns:oNode>#arguments.oNode#</ns:oNode>
</ns:InsertFacadeObject>
</soapenv:Body>
</soapenv:Envelope>
</cfoutput>
</cfsavecontent>
 
<!--- consume the web service with raw SOAP XML --->
<cfhttp url="#application.nfws_address#" method="POST">
<cfhttpparam type="HEADER" name="Content-Type" value="text/xml; charset=utf-8">
<cfhttpparam type="HEADER" name="Accept" value="application/soap+xml, application/dime, multipart/related, text/*">
<cfhttpparam type="HEADER" name="User-Agent" value="xWeb:ColdFusion">
<cfhttpparam type="HEADER" name="Host" value="localhost">
<cfhttpparam type="HEADER" name="Cache-Control" value="no-cache">
<cfhttpparam type="HEADER" name="Pragma" value="no-cache">
<cfhttpparam type="HEADER" name="SOAPAction" value="#application.nfws_namespace#InsertFacadeObject">
<cfhttpparam type="HEADER" name="Content-Length" value="#len(trim(nfws_soappacket))#">
<cfhttpparam type="BODY" value="#trim(nfws_soappacket)#">
</cfhttp>
<cfset obj = xmlParse(cfhttp.filecontent)>
 
<!--- set token in session scope --->
<cfset nfws_token=obj.Envelope.Header.AuthorizationToken.Token.xmltext>
<cfset session.nfws_token = nfws_token>
 
<!--- xmlToQuery --->
<cftry>
<cfinvoke component="/gateway/netforum_xweb" method="xmlToQuery" returnvariable="qryXML">
<cfinvokeargument name="xmlPacket" value="#obj#" omit="no">
<cfinvokeargument name="parentTree" value="Envelope.Body.InsertFacadeObjectResponse.InsertFacadeObjectResult.IndividualObjects" omit="no">
<cfinvokeargument name="childElement" value="IndividualObject" omit="no">
</cfinvoke>
<cfcatch type="any">
<cfscript>
qryXML = QueryNew("");
</cfscript>
</cfcatch>
</cftry>
 
<cfreturn qryXML>
</cffunction>
 
<!---
//////////////////////////////////////////////////////////////
// //
// xWeb:GetQuery //
// //
//////////////////////////////////////////////////////////////
--->
<cffunction name="xweb_GetQuery" access="public" returntype="query">
<cfargument name="nfws_token" default="" type="string" required="yes">
<cfargument name="szObjectName" default="" type="string" required="yes">
<cfargument name="szColumnList" default="" type="string" required="yes">
<cfargument name="szWhereClause" default="" type="string" required="yes">
<cfargument name="szOrderBy" default="" type="string" required="yes">
 
<!--- create the raw SOAP XML --->
<cfsavecontent variable="nfws_soappacket">
<cfoutput>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="#application.nfws_namespace#">
<soapenv:Header>
<ns:AuthorizationToken>
<!--Optional:-->
<ns:Token>#arguments.nfws_token#</ns:Token>
</ns:AuthorizationToken>
</soapenv:Header>
<soapenv:Body>
<ns:GetQuery xmlns:m="#application.nfws_namespace#">
<ns:szObjectName>#arguments.szObjectName#</ns:szObjectName>
<ns:szColumnList>#arguments.szColumnList#</ns:szColumnList>
<ns:szWhereClause>#arguments.szWhereClause#</ns:szWhereClause>
<ns:szOrderBy>#arguments.szOrderBy#</ns:szOrderBy>
</ns:GetQuery>
</soapenv:Body>
</soapenv:Envelope>
</cfoutput>
</cfsavecontent>
 
<!--- consume the web service with raw SOAP XML --->
<cfhttp url="#application.nfws_address#" method="POST">
<cfhttpparam type="HEADER" name="Content-Type" value="text/xml; charset=utf-8">
<cfhttpparam type="HEADER" name="Accept" value="application/soap+xml, application/dime, multipart/related, text/*">
<cfhttpparam type="HEADER" name="User-Agent" value="xWeb:ColdFusion">
<cfhttpparam type="HEADER" name="Host" value="localhost">
<cfhttpparam type="HEADER" name="Cache-Control" value="no-cache">
<cfhttpparam type="HEADER" name="Pragma" value="no-cache">
<cfhttpparam type="HEADER" name="SOAPAction" value="#application.nfws_namespace#GetQuery">
<cfhttpparam type="HEADER" name="Content-Length" value="#len(trim(nfws_soappacket))#">
<cfhttpparam type="BODY" value="#trim(nfws_soappacket)#">
</cfhttp>
<cfset obj = xmlParse(cfhttp.filecontent)>
 
<!--- set token in session scope --->
<cfset nfws_token=obj.Envelope.Header.AuthorizationToken.Token.xmltext>
<cfset session.nfws_token = nfws_token>
 
<!--- xmlToQuery --->
<cftry>
<cfinvoke component="/gateway/netforum_xweb" method="xmlToQuery" returnvariable="qryXML">
<cfinvokeargument name="xmlPacket" value="#obj#" omit="no">
<cfinvokeargument name="parentTree" value="Envelope.Body.GetQueryResponse.GetQueryResult.#szObjectName#Objects" omit="no">
<cfinvokeargument name="childElement" value="#szObjectName#Object" omit="no">
</cfinvoke>
<cfcatch type="any">
<cfscript>
qryXML = QueryNew("");
</cfscript>
</cfcatch>
</cftry>
 
<cfreturn qryXML>
</cffunction>
 
</cfcomponent>

Assumptions

When invoking the component the /xxxxxx/ value should be substituted with the cfmapping value that is defined to the location the CFC is located on your server.

Known Issues

None.

Best Practices

Many might not consider defining a variable to the session scope as being a good practice inside a CFC. However, with the complexity of data structure all the methods do use this statement (<cfset session.nfws_token = nfws_token>) to carry over the new nfws_token so the next request doesn't need to consume the xweb_Authenticate each time. Do note that no where inside the CFC is there a reference to any other variable than what is passed in or learned along the way. Only the invoke statements rely on the token to be defined in the session scope.

See Also