Building RESTful Service Layers Presentation with Expanded Answers to Questions

I'd like to publicly thank Doug Smith at DaveRamsey.com for making such a great presentation on RESTful service layers at the CFMeetup.  If you haven't heard Doug talk, you should put this presentation on your list to watch in the next couple of week especially if you might build REST APIs (over SOAP) in the future.

Description

REST-based service oriented architectures have become a popular pattern for offering services that can be consumed by diverse web clients. Using the architecture of the web itself, RESTful services provide a high performance, robust solution without the unnecessary complexities of SOAP and its related technologies.

In this session, Doug will provide a brief introduction to REST as it relates to web-enabled services, demonstrate a service currently used at daveramsey.com to support their 10M+ pageview per month site, and walk through the code required to implement a RESTful service using ColdFusion 9, Mach-II v1.9, and the new REST Endpoints (created by Doug in partnership with Team Mach-II).


You can watch the recording presentation here (special thanks to Charlie Arehart and the CFMeetup for hosting this event):

http://experts.na3.acrobat.com/p99836802/

Below are some questions that came up during the presentation that I thought would be a great to take as material for a blog post and expand on Doug's original answers with more information.  If you aren't familiar with REST, these questions will make more sense after you watch the presentation.

Expanded Answers to Questions from the Presentation

Where do you do your authentication/authorization? In the endpoint, or in another layer?

I would typically perform authentication in your REST endpoint.  This is because your authentication process is going to be specific to your REST endpoint implementation and not to your service layer directly.  It's not really the concern of your service layer to authenticate calls to it but really the responsibility of your REST endpoint to make sure the client is authenticated correctly before calling your service layer.  This implementation strategy makes your service layer more flexible as it's not tied to a specific authentication schema and allows you to swap in a new authentication layer in your REST endpoint in the future.

So, for authentication would you call authenticate method from all the secure methods?

Doug touched on the request lifecycle methods available in his presentation a bit, but you could easily do this in the "preProcess" method if you wanted to apply this at a global level.  By default, the base REST endpoint takes care of certain things in the "preProcess" like parsing the path info, deciding on the method to call, the type of response format, etc.

Because it's relatively new feature in the BER, Doug didn't mention that Mach-II 1.9 is shipping with a basic HTTP access authentication module.  By default, it reads an Apache authentication file for all credentials but this module is easily extensible and you can swap in other credential checking schemas like a database by extending this CFC.

Here is an example of adding basic HTTP access authorization to a REST endpoint.  In the "configure" method, you can see we are instantiating a basic auth module and passing in the "realm" (part of the basic authentication standard) and the path to the Apache-style credential file (the documentation explain more about this).  Later on in the code, we override the "preProcess" point in the endpoint request lifecycle.  We need the code in the base endpoint to run first so we call super.preProcess(arguments.event) before we try to authenticate the request.  In this case, if the authentication fails we include an exception template and abort the request.  The basic HTTP access authentication module takes care of checking the headers, decoding the auth, checking the credential file and ultimately setting other header if the authentication request fails.  This module returns a boolean so you can customize the exception output if the authentication fails.



<!---
 INITIALIZATION / CONFIGURATION
 --->
 <cffunction name="configure" access="public" returntype="void" output="false"
     hint="Configures the API endpoint.">
     
     <cfset variables.authentication = CreateObject("component", "MachII.security.http.basic.Authentication").init("Dashboard API", getParameter("apiCredentialFilePath")) />
             
     <cfset super.configure() />
 </cffunction>
 
 <!---
 PUBLIC METHODS - REQUEST
 --->
 <cffunction name="preProcess" access="public" returntype="void" output="true"
     hint="Runs when an endpoint request begins.">
     <cfargument name="event" type="MachII.framework.Event" required="true" />
 
     <cfset super.preProcess(arguments.event) />
     
     <!--- Authenticate the request via HTTP basic authentication --->
     <cfif NOT variables.authentication.authenticate(getHTTPRequestData().headers)>
         <cfoutput><cfinclude template="/MachII/dashboard/endpoints/Unauthorized.cfm" /></cfoutput>
         <!--- This is the one time we don't want the endpoint exception handling to process --->
         <cfabort>
     </cfif>
 </cffunction>


So you mention it is scalable, but how do you make it scale?

Just like any other web application.  It is proper form that REST calls are stateless.  This means that all calls should sent authentication credentials with each request (if required) and that the server does not maintain a "state" (like a CFML session scope) for the client.  This allows your REST endpoint to reside in a cluster of servers without having to tie calls from clients to specific servers.

Doug mentioned that they have rolled out a REST API at DaveRamsey.com using the Mach-II 1.9 M2 release (BER).  I believe he said they are handling about 800k requests per day with it.

Do you know how the Mach-II Dashboard handles reloading of endpoints in a clustered environment?

Currently the Dashboard handles applications on a per server basis.  For those who don't use Mach-II, the dashboard is a development tool build as a Mach-II module that can help you develop and manage your application.  I'll leave the laundry list of features out of this blog post, but check out the Dashboard documentation with screen shots for more information. Getting back to the question.  We're build a REST API for the Dashboard so I expect in the future we'll see some awesome cluster support where you can send messages to all your clustered servers.

When structuring xml responses, any tips on avoiding 'bloat' in your service layer?

Try to think of formatting your response in a way that is useful to the client instead of scratching your own desires in how you feel data should be organized.  Don't be tied to your database model as they may not be useful to your client.  Doug explained they have had great luck using JSON instead of XML because it has less "bloat" to in the response (i.e. smaller). It's not uncommon to offer responses in multiple response formats (XML, JSON, plain text, etc.) as a client that consumes your REST service may be able to work with XML over JSON easier or vice-versa.

Was the response type jason/html handled by base RESTAPI component or you had that in your service?

Yes, the base endpoint looks for certain "file extensions" and automatically sets the correct mime type in the response to the client.  By default, can set the default response type if the "file extension" is left off the URI.  The type of the response is in the event.getArg('format') event arg.

Have you ever done REST as a wrapper for SOAP in cases where you want to offer the API in both flavors to the world?

Doug answered that they haven't done that, but they have thought about creating REST wrappers for common SOAP services they have to interact with on a daily basis to make their lives better.

There is no WSDL equivilant for REST is there?

Yes, there it's called WADL and it sounds like Jason York at DaveRamsey.com is already looking into creating a documentation engine.  Jason, if you're listening... Let's get a WADL documentation engine built into the Dashboard.

Mach-II Dashboards Released: 1.1.0 for Mach-II 1.8.x series and 1.0.1 for Mach-II 1.6.x series

Team Mach-II is proud to present the latest stable releases of the Mach-II Dashboard.  There are two versions that have been released.  One version for the 1.6.x series of the Mach-II framework and one version for the 1.8.x series of the Mach-II framework.  We are now using OhLoh to manage our releases so all downloads will be from our OhLoh project listing.

Security Notice

Due to a possible directory transversal security flaw, we strongly suggest upgrading to these versions as they contain the latest enhancements and security patches.  This flaw if exploited correctly could lead to access to PNG, GIF, JPG, CSS and JS files that may not necessarily be available from the website root.  This flaw does not affect any other file types.

We have received absolutely no reports of this exploit being used in the wild and it only affects users of the Dashboard module when deployed to production environments. This does NOT affect the core Mach-II framework in any way.

This is an same day discovery release fix.  We issued the 1.0.1 maintenance release and the 1.1.0 final / gold stable on the same day the this possible flaw was discovered. This possible flaw was discovered by a source code audit performed by a Team Mach-II member.

Security Resolution Paths

  1. Upgrade the version of the Dashboard you are using to one of the versions below. Be sure to clear your CFML engine template cache and restart your application to clear any Dashboard components that had been loaded into the application memory.
  2. If you cannot upgrade at this time, removing the dashboard from production applications (i.e. commenting it out in your mach-ii.xml file) will fix this security concern until you can update your Dashboard source code

Downloads

For Mach-II 1.6.x Series:

Download Mach-II Dashboard 1.0.1 Stable (Maintenance Release for 1.0.0)

For Mach-II 1.8.x Series:

Download Mach-II Dashboard 1.1.0 Stable

For Mach-II 1.9.x Series using integrated Dashboard:

Use the latest BER zip or SVN version.  Do not use milestone 1 or milestone 2 on production

For the Future

We will blog more about this possible exploit -- how it was discovered, what the specific exploit is, how it works and to resolve it.  At the moment, we are refraining from discussing the specifics since this is an active (although medium level) security concern.  A full postmortem will be coming in the next several weeks as it can provide information on securing your own applications.

Mach-II Integrity (1.9) - Milestone 2 Released!

Team Mach-II is proud the annouce the availability of Mach-II Integrity Milestone 2!  This is the labor of the team over the past few months to get a major feature done - endpoints!  Special thanks to new team member Doug Smith for writing all the nifty REST endpoint code.  Now you can write REST APIs in CFML code -- no XML (except to register your endpoint with the framework which is one line of XML). Check out our REST endpoint documentation for examples.  Check out our complete list of change here: Features for Milestone 2

Overview of New Features:

Download Mach-II Integrity (1.9) Milestone 2 from Ohloh

Happy Mach-II-ing!

 

 

Ready for the World: 1.8.1 Simplicity - Maintenance Release

About six month after the release of Mach-II 1.8.0 Simplicity, Team Mach-II is proud to present that the 1.8.1 maintenance release is gold.

Download 1.8.1 Now

This is a maintenance release for the 1.8 series of Mach-II versions.  You can find out more about What's New in Mach-II 1.8.1 on our wiki.  Have a fun time Mach-ing out and be sure to look our for Mach-II Integrity (1.9.0) Milestone 2 release soon!

 

Free* One Day of Mach-II Training at BFusion - 9/11/20 in Bloomington, IN

Well, it's not so free - it's $30 for one day or $45 for two days.  But come on down on September 11 to Bloomington IN for a one day quick start on using Mach-II with Peter Farrell and Kurt Wiersma.  Come one, come all -- it's the one of the best CFML events in the nation. Details below.  But hurry, registration closes today - September 9th!

BFusion/BFlex 2010
Hands-on training for developers and designers from industry leaders coming to IU. You will not find a more effective and cost-effective professional development opportunity.
 
Want to learn more about ColdFusion and Adobe Flex? What about cross-platform AIR applications? Already handy with Illustrator and Photoshop and want to quickly turn your work into Rich Internet Applications? Whether you are a curious beginner or an advanced developer, BFusion (a full day of ColdFusion training) and BFlex (a full day of Flash Platform development) have something for you:

  • What: Hands-on training from the experts in ColdFusion, Flex, AIR, Catalyst, and other technologies
  • When: September 11 and 12 (Saturday and Sunday)
  • Where: IU Bloomington
  • Cost: $30 for one day, or $45 for both days (price includes lunch)


Designers: BFlex also includes an all-day session for you (no programming necessary), Flash Catalyst - From Design to Rich Internet Applications without Coding.

For more information about the sessions, speakers, and registration,  please see:  http://bflex.indiana.edu

See this post from one of our speakers Matt Woodward, Principle It Specialist of the US Senate, about his take on this year’s event: http://blog.mattwoodward.com/get-your-hands-dirty-at-bfusionbflex.

The event is also listed on Facebook and LinkedIn. Please share it with friends and colleagues.

On Facebook: http://www.facebook.com/home.php#!/event.php?eid=108657742524082
On LinkedIn: http://events.linkedin.com/BFlex-BFusion-2010/pub/4046