Welcome!

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

Related Topics: ColdFusion

ColdFusion: Article

How May I Be of Web Service?

How May I Be of Web Service?

There is a phenomenon that appears to be unique to our industry - something I call buzzword du jour. On a regular basis a new buzzword (usually accompanied by a series of acronyms) appears on the front pages of the trade rags proclaiming to be "the future" - the message being "jump on board or become obsolete." This happens so frequently that many of us are becoming immune to it all, and in doing so are running the risk of missing something important when (or if) it happens. Which brings us to Web services.

What's the Deal?
If you are to believe the hype, Web services are more exciting than car chases, are the answer to world peace, can cure male-pattern baldness, will patch the ozone layer, can resurrect failing economies, will make you rich and desirable, may bring an end to the glut of reality TV shows... well, you get the idea.

So, the big question has to be, are Web services for real? Microsoft thinks so (but not everything Microsoft believes in is truly worthy of consideration - anyone remember Microsoft Bob?). Sun (thanks to applets) thinks so too, as does IBM (those folks who brought us OS/2, which did so well). The point is, just because the big guys say it's hot does not necessarily make it so. And yet everyone is talking and writing about Web services (and I myself must plead guilty here - I have mentioned Web services in passing in several columns since ColdFusion MX shipped). So, again, what's the deal?

It's Distributed Applications - Again
For starters, here's an explanation. Web services are simply a set of technologies that facilitate distributed computing; it is that simple. The idea is that an application can invoke another application (located anywhere, locally or remotely) which does some processing, possibly returning results when complete. That's all there is to it.

It's not a new idea. Distributed computing has been around in some way, shape, or form for a while now (heck, if you use ColdFusion's <CFHTTP> tag to invoke a remote page, you are using distributed computing). So why the excitement this time around? There are a few reasons:

  • Vendor support: It's been a while since everyone from Microsoft to Sun to IBM to Apple all talked the same language (for the most part). Web services are truly the first really viable solution to interoperability.
  • Standards: Unlike other distributed computing technologies, Web services are built on existing standards and systems; no single vendor owns the space.
  • Cost: The two prior bullets make this technology highly usable and accessible, and not at all cost prohibitive. Most developers can continue to use the platforms and languages they already use and don't need to throw out everything and start over.

    Need a practical example? Try this one: you create an online store (I know that is so '90s, but bear with me). Customers visit your site and buy goods that you ship, using a carrier of your choice. Great. That is until a week goes by and no package shows up, so the customer revisits your site to track the order. You display the order information, complete with the shipper information and tracking number, allowing the customer to check on the delivery (and maybe, as you are so customer-service driven, you even provide a direct link to the shipper's site). Problem solved. Right?

    Wrong! If you have an online store, the last thing you want is customers wandering off to other sites; you want them to stay and part with their hard-earned money. You never want to send them off to fedex.com or ups.com or any other site. Amazon.com doesn't do this - they show shipping information provided by those companies right in their own pages, the customer goes nowhere.

    I have no idea what systems the shipping companies use, I don't know what software they use, and I have no access to it anyway (I am guessing the same is true for you). Amazon.com is a very significant player in the e-commerce space, the shipping companies are likely to work with them to provide them with access to this kind of data. But what about the rest of us? How could we do the same?

    The answer is Web services (predictable, I know, sorry). If the shipping companies created a Web service that exposed tracking information, any application could submit a real-time request for the data, and then process or display it as needed right within their own pages. In other words, the client browser talks to your application; your application talks to the shipper via their Web service, retrieving the necessary information; and then your application displays it as needed.

    Fantasy? Well, that depends on who you ship your products with. But some shipping companies have indeed made Web services available. It's not fantasy at all.

    Web Services Lingo
    Okay, so now that you know why you need Web services, let's review some of the lingo you'll need to use (drop these in conversations to sound like a seasoned pro):

  • Web service: This is the application itself.
  • XML: Web service requests are made in XML and returned data is also in XML.
  • SOAP (Simple Object Access Protocol): The XML-based protocol used to send and receive Web service requests.
  • WSDL (Web Services Description Language): An XML language used to document Web services; every Web ser-vice has an associated WSDL URL which lists the methods (functions) that may be invoked as well as what parameters they expect and what data is returned.
  • UDDI (Universal Description, Discovery and Integration [of Web services]): A directory technology used to locate Web services (UDDI is not supported by ColdFusion in CFMX).

    When you use a Web service you consume it, and when you create a Web service you publish it.

    Consuming Web Services
    Enough talk, let's try this thing. In ColdFusion, Web services are consumed using the <CFINVOKE> tag. <CFINVOKE> requires the URL to a Web service's WSDL, the name of the method to invoke (a single Web service may expose multiple methods), and any required parameters.

    The following is a simple example. It invokes a Web service on my own Web site - this one returns my ColdFusion Tip-of-the-Day:

    <!--- Get tip for today --->
    <CFINVOKE WEBSERVICE="http://www.forta.com/cf/tips/syndicate.cfc?wsdl"
    METHOD="Get"
    DATE="#Now()#"
    RETURNVARIABLE="DailyTip">

    As you can see, the Web service itself is specified using the URL to its WSDL file. ColdFusion retrieves and reads this file so as to know how to interact with the Web service, and how to call the method specified in the METHOD attribute. RETURNVARIABLE is the name of a ColdFusion variable to be populated with whatever results are returned. In this example my Web service returns a ColdFusion structure, and so DailyTip will be a structure containing the tip.

    If you want to try this example you can use <CFDUMP> to display the results, like this:

    <CFDUMP VAR="#DailyTip#">

    You'll notice that there is no XML here, no SOAP, nothing beyond a simple tag call. ColdFusion hides all the complexity from you - it does not matter what is on the other end, what language it was written in, what data type it returns, what platform it runs on - none of that matters. All that is important is that the returned data is ready to use just like any other ColdFusion variable.

    Here is another example, the same Web service call, but this time using a slightly different calling convention:

    <!--- Get tip for today --->
    <CFINVOKE WEBSERVICE="http://www.forta.com/cf/tips/syndicate.cfc?wsdl"
    METHOD="Get"
    RETURNVARIABLE="DailyTip">
    <CFINVOKEARGUMENT NAME="date"
    VALUE="#Now()#"/>
    </CFINVOKE>

    As you can see, arguments may be specified as name=value pairs in <CFINVOKE>, or passed to nested <CFINVOKEARGUMENT> tags - the choice is yours, the end result is the same.

    Let's look at a complete example. Alta Vista has a translation engine named Babel Fish - pass it text and a destination language and it returns the translated text to you. The Babel Fish service requires two pieces of information, the text to be translated, and the translation mode (from what to what, for example en_de to translate English to German). See Listing 1 for a complete working application. It prompts for text and a destination language and displays the translation.

    As you can see, using Web services in ColdFusion is easy. I have no idea what the Babel Fish Web service is written in or what platform it runs on, nor do I care - the important thing is that I can invoke it and it just works. And that is the beauty of Web services.

    Note: Looking for a Web service to experiment with? Try www.xmethods.net.

    If you are using Dreamweaver MX you'll find that Web services are even easier to use. In Dreamweaver, open the Application window, click on the Component tab, and then change the dropdown to Web services. Dreamweaver will list any known Web services (the list will be empty if this is the first time you are doing this). Click the + button and you'll be prompted for the WSDL URL of a Web service - simply enter it, and click OK. Dreamweaver MX will retrieve the WSDL, process and parse it, and will then display a tree of Web service methods and arguments. Then all you need to do is drag the desired method into your ColdFusion code, and Dreamweaver will create the <CFINVOKE> call for you automatically.

    Publishing Web Services
    Now that you know how to consume Web services, let's look at how to create and publish them. ColdFusion MX makes creating Web services incredibly easy - they are created as ColdFusion Comp-onents (explained in detail in CFDJ, Volume 4, issues 6, 7, and 10).

    Here is a simple ColdFusion Component. The file is named random.cfc, and it contains two methods: (1) Get returns a random number and (2) GetRange returns a random number within the specific range. (Okay, not the most glamorous examples, but they'll have to do). See Listing 2 for the code.

    The CFC code itself is rather simple. Get accepts no arguments and returns whatever is generated by Rand(); GetRange() accepts two required numeric arguments and passes them to RandRange() returning the results. This CFC can be used within your ColdFusion code using <CFINVOKE>, <CFOBJECT>, or CreateObject().

    So what makes this a Web service? The answer is the ACCESS level. Each of the <CFFUNCTION> tags has ACCESS="remote". Any ColdFusion Component methods with an ACCESS level of remote can be accessed remotely - as Web services; it's as simple as that.

    What about the WSDL file you need? ColdFusion does that too. The WSDL URL is the URL to the CFC with ?wsdl appended to it. I saved my random.cfc in my test directory, so the WSDL URL is http://localhost:8500/test/random.cfc?wsdl.

    Listing 3 is sample code showing this CFC being used as a Web service.

    Of course this particular CFC could be invoked as a CFC rather than as a Web service (and probably should be as it is ColdFusion and it is local). But with no extra work, the ColdFusion Component can also be used by:

    • Other ColdFusion servers
    • .NET
    • Java
    • PHP
    • Client-side Flash
    ColdFusion, and CFCs in particular, makes publishing Web services painless and even fun. You don't have to worry about XML, SOAP, data-type conversions - ColdFusion does all that for you. In fact, as I demonstrated at DevCon in Orlando, you'll find it easier to create Web services for Java and .NET in ColdFusion than in Java or any .NET language!

    Conclusion
    Web services are all the rage right now. While the hype will die down a bit, all indications are that this is an important technology and one that will play an important role in application development now and in the foreseeable future. And best of all, as a ColdFusion developer, you have nothing to lose and lots to gain - it does not get much better than that.

  • More Stories By Ben Forta

    Ben Forta is Adobe's Senior Technical Evangelist. In that capacity he spends a considerable amount of time talking and writing about Adobe products (with an emphasis on ColdFusion and Flex), and providing feedback to help shape the future direction of the products. By the way, if you are not yet a ColdFusion user, you should be. It is an incredible product, and is truly deserving of all the praise it has been receiving. In a prior life he was a ColdFusion customer (he wrote one of the first large high visibility web sites using the product) and was so impressed he ended up working for the company that created it (Allaire). Ben is also the author of books on ColdFusion, SQL, Windows 2000, JSP, WAP, Regular Expressions, and more. Before joining Adobe (well, Allaire actually, and then Macromedia and Allaire merged, and then Adobe bought Macromedia) he helped found a company called Car.com which provides automotive services (buy a car, sell a car, etc) over the Web. Car.com (including Stoneage) is one of the largest automotive web sites out there, was written entirely in ColdFusion, and is now owned by Auto-By-Tel.

    Comments (3) View Comments

    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.


    Most Recent Comments
    Jon 10/02/06 03:13:39 PM EDT

    Thank you for taking the time to report on this topic. I have a question though.

    When you get errors using CFINVOKE to utilize web services and you're on a shared environment, how do you go about determining where the problem is? The error information is cryptic. The web service provider points to the programming or hosting, the host points to the service or programming and the programmer points to the host or web service provider! Of course, being the programmer, and having done several other CFINVOKE tags successfully, I am looking at the other two!

    So where to next?

    Could not generate stub objects for web service invocation. Name: http://app.campaignmonitor.com/api/api.asmx?WSDL. WSDL: http://app.campaignmonitor.com/api/api.asmx?WSDL. org.apache.axis.wsdl.toJava.DuplicateFileException: Duplicate file name: C:\CFusionMX7\stubs\WS1938286156\com\campaignmonitor\app\api\SubscriberUnsubscribe.java. Hint: you may have mapped two namespaces with elements of the same name to the same package name.

    Jon 10/02/06 03:12:48 PM EDT

    Thank you for taking the time to report on this topic. I have a question though.

    When you get errors using CFINVOKE to utilize web services and you're on a shared environment, how do you go about determining where the problem is? The error information is cryptic. The web service provider points to the programming or hosting, the host points to the service or programming and the programmer points to the host or web service provider! Of course, being the programmer, and having done several other CFINVOKE tags successfully, I am looking at the other two!

    So where to next?

    Could not generate stub objects for web service invocation. Name: http://app.campaignmonitor.com/api/api.asmx?WSDL. WSDL: http://app.campaignmonitor.com/api/api.asmx?WSDL. org.apache.axis.wsdl.toJava.DuplicateFileException: Duplicate file name: C:\CFusionMX7\stubs\WS1938286156\com\campaignmonitor\app\api\SubscriberUnsubscribe.java. Hint: you may have mapped two namespaces with elements of the same name to the same package name.

    jim kellow 01/16/03 08:25:00 AM EST

    Thanks for the time you spent on this.
    Like others, I have seen web services
    in articles but passed over it as I
    didn't have a clue what it was about.
    A couple of years ago, my company
    jumped into the web world with both
    feet and I rushed to create the site,
    but since then the interest has fallen
    to zero. I haven't been involved in the
    CF world for over a year now(we still
    use cf 4.5).
    I tried pushing for us to convert
    some of our existing application
    jobs to CF but it turned out that the
    conversion is about 10+ CF jobs to replace 1 application(oracle forms).
    I am trying to get us back into the
    CF world, but its gotten a lot bigger
    since we were there. I have a new
    copy of CF 5.0 on my desk but it is
    way out of date, so I am going to have
    to figure out which version of CF to
    suggest we move up to. Again, thanks
    for the article.

    JimK

    @ThingsExpo Stories
    Connected devices and the Internet of Things are getting significant momentum in 2014. In his session at Internet of @ThingsExpo, Jim Hunter, Chief Scientist & Technology Evangelist at Greenwave Systems, will examine three key elements that together will drive mass adoption of the IoT before the end of 2015. The first element is the recent advent of robust open source protocols (like AllJoyn and WebRTC) that facilitate M2M communication. The second is broad availability of flexible, cost-effective storage designed to handle the massive surge in back-end data in a world where timely analytics...
    Internet of @ThingsExpo Silicon Valley announced on Thursday its first 12 all-star speakers and sessions for its upcoming event, which will take place November 4-6, 2014, at the Santa Clara Convention Center in California. @ThingsExpo, the first and largest IoT event in the world, debuted at the Javits Center in New York City in June 10-12, 2014 with over 6,000 delegates attending the conference. Among the first 12 announced world class speakers, IBM will present two highly popular IoT sessions, which will take place November 4-6, 2014 at the Santa Clara Convention Center in Santa Clara, Calif...
    The Internet of Things (IoT) promises to evolve the way the world does business; however, understanding how to apply it to your company can be a mystery. Most people struggle with understanding the potential business uses or tend to get caught up in the technology, resulting in solutions that fail to meet even minimum business goals. In his session at Internet of @ThingsExpo, Jesse Shiah, CEO / President / Co-Founder of AgilePoint Inc., will show what is needed to leverage the IoT to transform your business. He will discuss opportunities and challenges ahead for the IoT from a market and tec...
    SYS-CON Events announced today that TeleStax, the main sponsor of Mobicents, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. TeleStax provides Open Source Communications software and services that facilitate the shift from legacy SS7 based IN networks to IP based LTE and IMS networks hosted on private (on-premise), hybrid or public clouds. TeleStax products include Restcomm, JSLEE, SMSC Gateway, USSD Gateway, SS7 Resource Adaptors, SIP Servlets, Rich Multimedia Services, Presence Services/RCS, Diame...
    From a software development perspective IoT is about programming "things," about connecting them with each other or integrating them with existing applications. In his session at @ThingsExpo, Yakov Fain, co-founder of Farata Systems and SuranceBay, will show you how small IoT-enabled devices from multiple manufacturers can be integrated into the workflow of an enterprise application. This is a practical demo of building a framework and components in HTML/Java/Mobile technologies to serve as a platform that can integrate new devices as they become available on the market.
    SYS-CON Events announced today that O'Reilly Media has been named “Media Sponsor” of SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. O'Reilly Media spreads the knowledge of innovators through its books, online services, magazines, and conferences. Since 1978, O'Reilly Media has been a chronicler and catalyst of cutting-edge development, homing in on the technology trends that really matter and spurring their adoption by amplifying "faint signals" from the alpha geeks who are creating the future. An...
    SYS-CON Events announced today that Aria Systems, the recurring revenue expert, has been named "Bronze Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Aria Systems helps leading businesses connect their customers with the products and services they love. Industry leaders like Pitney Bowes, Experian, AAA NCNU, VMware, HootSuite and many others choose Aria to power their recurring revenue business and deliver exceptional experiences to their customers.
    The Transparent Cloud-computing Consortium (abbreviation: T-Cloud Consortium) will conduct research activities into changes in the computing model as a result of collaboration between "device" and "cloud" and the creation of new value and markets through organic data processing High speed and high quality networks, and dramatic improvements in computer processing capabilities, have greatly changed the nature of applications and made the storing and processing of data on the network commonplace.
    The Internet of Things (IoT) is going to require a new way of thinking and of developing software for speed, security and innovation. This requires IT leaders to balance business as usual while anticipating for the next market and technology trends. Cloud provides the right IT asset portfolio to help today’s IT leaders manage the old and prepare for the new. Today the cloud conversation is evolving from private and public to hybrid. This session will provide use cases and insights to reinforce the value of the network in helping organizations to maximize their company’s cloud experience.
    As a disruptive technology, Web Real-Time Communication (WebRTC), which is an emerging standard of web communications, is redefining how brands and consumers communicate in real time. The on-going narrative around WebRTC has largely been around incorporating video, audio and chat functions to apps. In his session at Internet of @ThingsExpo, Alex Gouaillard, Founder and CTO of Temasys Communications, will look at a fourth element – data channels – and talk about its potential to move WebRTC beyond browsers and into the Internet of Things.
    SYS-CON Events announced today that Gigaom Research has been named "Media Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Ashar Baig, Research Director, Cloud, at Gigaom Research, will also lead a Power Panel on the topic "Choosing the Right Cloud Option." Gigaom Research provides timely, in-depth analysis of emerging technologies for individual and corporate subscribers. Gigaom Research's network of 200+ independent analysts provides new content daily that bridges the gap between break...
    We certainly live in interesting technological times. And no more interesting than the current competing IoT standards for connectivity. Various standards bodies, approaches, and ecosystems are vying for mindshare and positioning for a competitive edge. It is clear that when the dust settles, we will have new protocols, evolved protocols, that will change the way we interact with devices and infrastructure. We will also have evolved web protocols, like HTTP/2, that will be changing the very core of our infrastructures. At the same time, we have old approaches made new again like micro-services...
    The Industrial Internet revolution is now underway, enabled by connected machines and billions of devices that communicate and collaborate. The massive amounts of Big Data requiring real-time analysis is flooding legacy IT systems and giving way to cloud environments that can handle the unpredictable workloads. Yet many barriers remain until we can fully realize the opportunities and benefits from the convergence of machines and devices with Big Data and the cloud, including interoperability, data security and privacy.
    Swiss innovators dizmo Inc. launches its ground-breaking software, which turns any digital surface into an immersive platform. The dizmo platform seamlessly connects digital and physical objects in the home and at the workplace. Dizmo breaks down traditional boundaries between device, operating systems, apps and software, transforming the way users work, play and live. It supports orchestration and collaboration in an unparalleled way enabling any data to instantaneously be accessed on any surface, anywhere and made interactive. Dizmo brings fantasies as seen in Sci-fi movies such as Iro...
    Software AG helps organizations transform into Digital Enterprises, so they can differentiate from competitors and better engage customers, partners and employees. Using the Software AG Suite, companies can close the gap between business and IT to create digital systems of differentiation that drive front-line agility. We offer four on-ramps to the Digital Enterprise: alignment through collaborative process analysis; transformation through portfolio management; agility through process automation and integration; and visibility through intelligent business operations and big data.
    One of the biggest challenges when developing connected devices is identifying user value and delivering it through successful user experiences. In his session at Internet of @ThingsExpo, Mike Kuniavsky, Principal Scientist, Innovation Services at PARC, will describe an IoT-specific approach to user experience design that combines approaches from interaction design, industrial design and service design to create experiences that go beyond simple connected gadgets to create lasting, multi-device experiences grounded in people’s real needs and desires.
    The Internet of Things will greatly expand the opportunities for data collection and new business models driven off of that data. In her session at Internet of @ThingsExpo, Esmeralda Swartz, CMO of MetraTech, will discuss how for this to be effective you not only need to have infrastructure and operational models capable of utilizing this new phenomenon, but increasingly service providers will need to convince a skeptical public to participate. Get ready to show them the money! Speaker Bio: Esmeralda Swartz, CMO of MetraTech, has spent 16 years as a marketing, product management, and busin...
    As the Internet of Things unfolds, mobile and wearable devices are blurring the line between physical and digital, integrating ever more closely with our interests, our routines, our daily lives. Contextual computing and smart, sensor-equipped spaces bring the potential to walk through a world that recognizes us and responds accordingly. We become continuous transmitters and receivers of data. In his session at Internet of @ThingsExpo, Andrew Bolwell, Director of Innovation for HP’s Printing and Personal Systems Group, will discuss how key attributes of mobile technology – touch input, senso...
    Connected devices are changing the way we go about our everyday life, from wearables to driverless cars, to smart grids and entire industries revolutionizing business opportunities through smart objects, capable of two-way communication. But what happens when objects are given an IP-address, and we rely on that connection, sometimes with our lives? How do we secure those vast data infrastructures and safe-keep the privacy of sensitive information? This session will outline how each and every connected device can uphold a core root of trust via a unique cryptographic signature – a “bir...
    Predicted by Gartner to add $1.9 trillion to the global economy by 2020, the Internet of Everything (IoE) is based on the idea that devices, systems and services will connect in simple, transparent ways, enabling seamless interactions among devices across brands and sectors. As this vision unfolds, it is clear that no single company can accomplish the level of interoperability required to support the horizontal aspects of the IoE. The AllSeen Alliance, announced in December 2013, was formed with the goal to advance IoE adoption and innovation in the connected home, healthcare, education, aut...