creating webservices with cfml - carehart.org webservices with cfml.pdfcaching web service object...

38
CFUNITED – The premier ColdFusion conference www.cfunited.com Creating and Consuming Creating and Consuming Web Services with CFML Web Services with CFML Charlie Arehart [email protected]

Upload: others

Post on 22-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

CFUNITED – The premier ColdFusion conference www.cfunited.com

Creating and Consuming Creating and Consuming Web Services with CFMLWeb Services with CFML

Charlie [email protected]

Page 2: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 2

TopicsTopicsWeb Service BasicsPublishing Your First Web Service

Building, testing your web serviceConsuming the Web Service in CFML

Several ways to invoke them, pass dataWeb Service Details and CaveatsWhere to Learn More

Page 3: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 3

About Your SpeakerAbout Your SpeakerCTO Garrison Enterprises since Apr 2006

Formerly CTO, New Atlanta (BlueDragon)9 yrs CF experience (24 yrs in Enterprise IT)Co-author, ColdFusion MX Bible (Wiley)

Frequent contrib. to ColdFusion Dev JournalPast accomplishments of note

Tech Editor, CFDJ Certified Adv CF Developer (4, 5, MX), Certified Instructor, Team Macromedia Member

Frequent speaker: UGs, conferences worldwide

Page 4: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 4

WhatWhat’’s a Web Service?s a Web Service?Simple terms: a web page designed to be consumed by software, not by humans

Who remembers WDDX?Could “serialize” data to Allaire-specified XMLCould output from page request, pass to caller

Problems: Non-standard (though see openwddx.org)Wasn’t a function call/response approach

As used in similar service approaches: RPC, CORBA

Page 5: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 5

WhatWhat’’s a Web Service?s a Web Service?One technical definition of web services:

Remotely consumable software component published on the web through a universal interface

IT world moving to service-oriented architecture

One application provides services to another

Page 6: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 6

Web Service CommunicationsWeb Service Communications

Page 7: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 7

Coalescing StandardsCoalescing StandardsStandards have come together for web services

SOAP –Simple Object Access ProtocolWSDL – Web Services Description LanguageUDDI – Universal Description, Discovery, and IntegrationXML – eXtensible Markup Language

Good news: CFML hides need for you to understand any of these, for the most part

Page 8: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 8

Some ExamplesSome ExamplesBottom line: web services enable an organization to expose their data to other applications

Perhaps running on other platforms within an org.Or applications running in other organizationsSometimes referred to as syndication of content

Possible private implementationsSupplier exposing inventory status to partnersVendor exposing products to affiliatesGovernment organization sharing data with other orgsObtain weather, stock quotes for your web app without screen scrapingTo name a few

Page 9: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 9

Some ExamplesSome ExamplesCommercial examples

Amazon, Google, USPS, UPS, FedEx

Available service listingshttp://Xmethods.nethttp://www.serviceobjects.com/products/default.asp

Page 10: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 10

CFML Makes It EasyCFML Makes It EasyMany languages support web services, including use of those protocols, creation of XML, etc.

Including Java, ASP.NET, PERL, etc.These often use APIs involving lots of code

Page 11: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 11

CFML Makes It EasyCFML Makes It EasyCFMX and BlueDragon both support very easy publicationand consumption of web services

Don’t need to know Java, nor SOAP, WSDL, UDDIDon’t even need to understand XMLWeb Service processing in CFML is very easy

Other end of conversation need not be CFMLWeb Services are designed to be agnostic as to language and platformA CF web service could be consumed by a Java app, or .NET, etc.

And vice versaNote: if you’re doing CFCs already, you don’t need to change them to using web services

Unless you’re interested in exposing data to non-CF apps

Page 12: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 12

Publish/ConsumePublish/ConsumePublication of web svc in CFML is simple

Simply expose a CFC method as “remote”Return data to caller using CFRETURN

Consumption is equally easyUse CFINVOKE (or CFOBJECT/createObject)Invoke methods of the exposed service

We’ll explore each of these in remainder of talk

Page 13: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 13

Publishing Your First Web ServicePublishing Your First Web ServiceAssume we have some data to publishCan publish any kind of data via web svc

Simple stringArrayStructureArray of structuresCFML query result set (with a caveat, discussed later)XML object (using CFMX and BlueDragon’s support of XML)To name a few

Page 14: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 14

Publishing Your First Web ServicePublishing Your First Web ServiceNeed merely create a CFC function that returns that data, expose function as “remote”

If you’re new to CFCs’, they have many other benefits and featuresTheir use for web services is actually one of their easier purposes to understand

Page 15: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 15

Building the Component FunctionsBuilding the Component FunctionsLet’s build Hello World web service

<!--- hello.cfc ---><CFCOMPONENT>

<CFFUNCTION NAME="GetHello" ACCESS="REMOTE" RETURNTYPE="String">

<CFRETURN "Hello World"></CFFUNCTION>

</CFCOMPONENT>

That’s really all there is to it!Now any web service client (caller) can invoke this web service (hello.cfc) and its method (GetHello)

First time it’s called, CFMX/BlueDragon will generate WSDL needed for callers

Page 16: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 16

Calling Your Web ServiceCalling Your Web ServiceCan call from any language/platform that supports web services

Simply need to refer to:1. Domain name (and port, if needed) that’s

used to access code on CFMX/BlueDragon2. Directory in which CFC is located3. CFC name4. ?WSDL indicator

http://localhost/demo/hello.cfc?wsdlSince I stored the CFC in my demo directory

Page 17: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 17

Viewing WSDLViewing WSDLFor testing, can enter the web service URL in your browser, to see resulting WSDL

Page 18: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 18

Consuming via CFINVOKEConsuming via CFINVOKETo use the web service within CFML, use either CFINVOKE, CFOBJECT, or createObject

CFINVOKE:Calls web service (using URL described earlier, with ?wsdl as querystring), names method to execute, and variable to hold returned result

<cfinvokewebservice="http://localhost/demo/hello.cfc?wsdl" returnvariable="fromhello" method="GetHello">

<cfoutput>#fromhello#</cfoutput>

Page 19: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 19

Can also use CFOBJECT/CreateObject insteadSlight difference from CFINVOKE

They return an object representing the web serviceThen can invoke method in CFML as with other objects

<cfobject webservice="http://localhost/demo/hello.cfc?wsdl“ name="fromhello"><cfoutput>#fromhello.GetHello()#</cfoutput>

Or<cfscript>fromhello=createobject("webservice","http://localhost/demo/hello.cfc?wsdl");writeoutput(fromhello.GetHello());</cfscript>

Consuming via CFOBJECT/Consuming via CFOBJECT/CreateObjectCreateObject

Page 20: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 20

Calling a Calling a ““RealReal”” Web ServiceWeb ServiceInvoking xmethods.net Temperature svc

Reports current temperature for given zipcodeAvailable methods/properties docs at site

<cfinvokewebservice="http://www.xmethods.net/sd/2001/DemoTemperatureService.wsdl" method="GetTemp“returnvariable="weather">

<cfinvokeargument name="zipcode" value="30005"/></cfinvoke><cfdump var="#weather#">

Page 21: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 21

Passing Input to Web ServicePassing Input to Web ServiceSimple examples so far took no inputLet’s change to say ‘hello’ to us personally

Can add a new CFFUNCTION to existing CFCmultiple functions with different names

<CFFUNCTION NAME="GetPersonalHello" ACCESS="REMOTE" RETURNTYPE="String"><CFARGUMENT NAME="fname" TYPE="string"><CFRETURN "Hello, #fname#">

</CFFUNCTION>

Page 22: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 22

Passing Input to Web ServicePassing Input to Web ServiceCan pass named argument on CFINVOKE:<cfinvoke webservice="http://localhost/demo/hello.cfc?wsdl"

returnvariable="fromhello" method="GetPersonalHello" fname="charlie">

Or, using CFINVOKEARGUMENT<cfinvoke webservice="http://localhost/demo/hello.cfc?wsdl"

returnvariable="fromhello" method="GetPersonalHello"><cfinvokeargument name="fname" value="charlie">

</cfinvoke>

Useful to build args dynamically such as CFIF, CFLOOP inside the CFINVOKE

Page 23: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 23

Testing Web ServicesTesting Web ServicesCan invoke methods on browser request:http://localhost/demo/hello.cfc?wsdl&method=G

etHelloCan even pass simple string as args on URL in a browser request:http://localhost/demo/hello.cfc?wsdl&method=G

etPersonalHello&fname=charlie

Mistaken URL in notes

Page 24: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 24

Web Service Details/Caveats Web Service Details/Caveats Exception Handling

Web service requests may failConsider cftry/cfcatch to detect/handle errors

TimeoutCFMX 6.1 added ability to timeout web service requests

how long you’re willing to wait for a reply

Page 25: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 25

Web Service Details/Caveats Web Service Details/Caveats Security

Can secure CFC using either web server authentication

just as you can limit access to any web pageCFINVOKE offers USERNAME/PASSWORD

Could even implement your own alternative attributes for authentication, and test for that in your CFC methodCan secure in CFML using ROLE attribute on CFFUNCTION

Tied to CFLOGIN/CFLOGINUSER tagsSee CFMX documentation for more details

Page 26: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 26

Web Service Details/Caveats Web Service Details/Caveats Caching Web Service object

As we might cache a query resultset if it doesn’t change often, can do with web svcNo current feature to cache web service results

Can do it yourself, storing result in shared scopes (session/application/server)Use some timing mechanism to determine when to refresh result, re-execute web service invocation

Page 27: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 27

Web Service Details/Caveats Web Service Details/Caveats Beware: non-CF consumers won’t understand if you return CF query result

Can instead convert into an array of structuresConsider following UDFs at the cflib.org site

QueryToArrayOfStructures: http://cflib.org/udf.cfm?ID=10 ArrayOfStructuresToQuery: http://cflib.org/udf.cfm?ID=287

QueryToStructOfArrays: http://cflib.org/udf.cfm?ID=470QueryToStructOfStructures: http://cflib.org/udf.cfm?ID=523

Page 28: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 28

Web Service Details/Caveats Web Service Details/Caveats How can you know the type of data returned from web svc/passed into CFC?

It it a query? an array? a structure? something else? You may need to know its type to determine how to process it. See “typeof”, at:

http://cflib.org/udf.cfm?ID=689

Reports if something is a array, struct, query, string, date, numeric, boolean, binary, wddx, xml object, or even a custom function (udf)

Page 29: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 29

Why Web Services Will SucceedWhy Web Services Will SucceedWhy will web services succeed?

They’re relatively simplePeople generally agree on their useMajor IT vendors and svc supporting themPeople are excited about them, using them

For CFers, best thing is they’re so easyYou can easily introduce them into your org

Page 30: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 30

More You Can LearnMore You Can LearnCFMX docs elaborate on many additional topics

Working with WSDL filesConsuming web svcs not generated by CFMXCalling web services from a Flash clientCatching errors when consuming web servicesConfiguring web svcs in CFMX AdministratorConversions between CF/WSDL datatypesDefining data types for web servicesHandling complex data typesIntegrating with Dreamweaver MX

Page 31: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 31

More You Can LearnMore You Can LearnAvailable “tips & tricks” presentation to show some of these and more

Offered Friday here at CFUnitedArticles that follow offer pointers to finding publicly available web services that you can explore

Page 32: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 32

Learning MoreLearning MoreMacromedia Documentation

6.1: Developing ColdFusion MX Applications, Chapter 32 7: ColdFusion MX Developer's Guide, Chapter 36Available at livedocs.macromedia.com

BooksCFMX Bible, Wiley (Churvis, Helms, Arehart), Chapter 25Programming ColdFusion MX (Brooks-Bilson), Chapter 25

http://www.webreference.com/programming/coldfusion/1/index.html

And others

Page 33: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 33

Learning MoreLearning MoreCFDJ Web Services Articles

Jul 02, “CFMX & Web Services”, Ron WestJul 02, “A Quick and Easy Web Service with ColdFusion MX”, Kevin SchmidtJul 02, “Using ColdFusion Components--Part 2”, Ben FortaFeb 03, “Consuming Amazon Web Services”, Charlie ArehartAnd others

Page 34: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 34

Other CFML/Web Svc ResourcesOther CFML/Web Svc Resources“A Beginner's Guide to Creating and Consuming Web Services with ColdFusion and Flash”

http://assets.macromedia.com/devnet/coldfusion/articles/beginner_ws_print.html

“How May I Be of Web Service?”http://cfdj.sys-con.com/read/41554.htm

“CFMX & Web Services”http://cfdj.sys-con.com/read/41624.htm

“Creating a Web Service in ColdFusion MX”http://www.macromedia.com/desdev/mx/coldfusion/articles/webservices.pdf

Page 35: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 35

Other CFML/Web Svc ResourcesOther CFML/Web Svc Resources“Macromedia MX: Components and Web Services” , Jeremy Allaire

http://www.macromedia.com/desdev/mx/coldfusion/whitepapers/components_ws.pdf

“Creating ColdFusion web service pages with Macromedia Dreamweaver MX”

http://www.macromedia.com/support/dreamweaver/content/websrv_cf/

“Creating a Web Service Application Using ColdFusion MX and Dreamweaver MX”

http://www.macromedia.com/desdev/mx/dreamweaver/articles/webservice.html

“Crossing the .NET Divide: CFMX, Web Services, and .NET”http://cfdj.sys-con.com/read/47199.htm

Page 36: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 36

Other CFML/Web Svc ResourcesOther CFML/Web Svc ResourcesService-specific, but perhaps dated“Consuming Amazon.com web services with CFMX ”

http://www.macromedia.com/desdev/mx/coldfusion/articles/wsamazon.html

“Techniques for Invoking the Google Web APIs service ”, Kevin Hoyt

http://www.macromedia.com/desdev/mx/coldfusion/articles/googlews.html

Page 37: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 37

SummarySummaryWeb Services open new possibilities for CFML developersVery easy to both create and consume in CFML

CFC method w/ “Access=Remote” publishesCFINVOKE/CFOBJECT/CreateObject consume

Don’t need to understand SOAP, WSDL, nor XML (usually)

Page 38: Creating WebServices with CFML - CArehart.org WebServices with CFML.pdfCaching Web Service object 9As we might cache a query resultset if it doesn’t change often, can do with web

June 28th – July 1st 2006 38

SummarySummaryAllows you to expose your CFML based processing to non-CFML clientsCFMX and BlueDragon support web svcsCFML developers can lead the charge to using web services in their orgs!

Questions: [email protected]