I’ve got a fever, and the only prescription is more cowbell.

Getting RESTful with Mach-II

Since working with Ruby, I have been inspired to improve my coldfusion practices. While using Sinatra, I enjoyed being able to specify HTTP methods while mapping routes. In the same flavor, I wanted to write something for Mach-II that would allow me to do something similar. I wrote a Mach-II filter to accomplish this.

<cfcomponent display="RESTfulFilter" extends="MachII.framework.EventFilter">
  <cffunction name="filterEvent" returnType="boolean">
    <cfargument name="event" type="MachII.framework.Event" required="true" />
    <cfargument name="eventContext" type="MachII.framework.EventContext" required="true" />
    <cfargument name="paramArgs" type="struct" required="false" default="#StructNew()#" />

    <cfset var requestMethod = cgi.REQUEST_METHOD />
    <cfset var _method = arguments.event.getArg("_method", requestMethod) />
    <cfset var restMethod = "" />

    <cfif LCase(requestMethod) eq "get">
      <cfset restMethod = "get" />
    <cfelseif LCase(_method) eq "put">
      <cfset restMethod = "put" />
    <cfelseif LCase(_method) eq "delete">
      <cfset restMethod = "delete" />
    <cfelse>
      <cfset restMethod = "post" />
    </cfif>

    <cfif StructKeyExists(paramArgs, restMethod)>
      <cfset announceEvent(paramArgs[restMethod], arguments.event.getArgs()) />
    <cfelse>
      <cfthrow message="No event specified for REST method '#restMethod#'" />
    </cfif>

    <cfreturn false />
  </cffunction>
</cfcomponent>

A sample configuration is shown below. Each HTTP method specified in the parameters is essentially enabled and announces the specified event. If there is no event specified for the reqest HTTP method, then the request will error out.

<mach-ii version="1.5">
  <event-filters>
    <event-filter name="RESTfulFilter" type="model.RESTfulFilter" />
  </event-filters>

  <event-handlers>
    <event-handler event="contact">
      <filter name="RESTfulFilter">
        <parameter name="get" value="contact.form" />
        <parameter name="post" value="contact.action" />
      </filter>
    </event-handler>
    <event-handler event="contact.form" access="private">
      <event-arg name="contentPieceMappingName" value="contact" />
      <notify listener="ContentListener" method="getContentPiece" />
      <event-arg name="theme" value="white" />
      <notify listener="ReCaptchaListener" method="getReCaptcha" resultArg="reCaptcha" />
      <view-page name="contact" contentArg="content" />
      <execute subroutine="layout.default" />
    </event-handler>
    <event-handler event="contact.action" access="private">
      <notify listener="ReCaptchaListener" method="eventArgValidation" resultArg="reCaptchaValid" />
      <event-bean name="Contact" type="model.Contact" />
      <filter name="FormValidator">
        <parameter name="formObjectName" value="Contact" />
        <parameter name="invalidEvent" value="contact.form" />
      </filter>
      <notify listener="ContactListener" method="addContact" />
      <event-arg name="contactsent" value="1" />
      <redirect event="contact" args="contactsent" />
    </event-handler>
  </event-handlers>
</mach-ii>

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe

Search

You Can Find Me On ...

Categories

RSS Other Articles By Me

  • reCAPTCHA: Blocking Spam and More
    A few months ago, I developed a ColdFusion/Mach-II implementation of the reCAPTCHA API.  This type of CAPTCHA requires the person to enter two words rather than a single sequence of random letters.  In addition to protecting your website from spam, reCAPTCHA helps digitize text books.  They do this by using a mystery word as one of the two words, until there […]
  • Using Friendfeed to Automatically Post Your Blog Entries on Twitter
    Friendfeed is a feed aggregator that groups together updates from twitter, facebook, digg, youtube, your blog and much more.  It can also publish entries from your friendfeed to twitter.  This article shows you how to tap into that twitter publishing feature of friendfeed to automate the posting of your blog entries to twitter.  Watch my screencast to create […]
  • Intense Debate Now on the Sitepro Blog!
    Recently on the Sitepro Blog, we have switched to using an enhanced commenting system called Intense Debate.  Intense Debate provides many features that will supercharge the community, increase comments, and increase pageviews.  Some notable features include commenter profiles, reputation scores, email notifications, reply by email, and facebook integration. […]
  • Sitepro Developers Attend TCCFUG
    Last Thursday, a few Sitepro developers attended the Twin Cities Coldfusion User Group at University of St. Thomas.  We were introduced to the future of Coldfusion, specifically Coldfusion 9.  There was free admission, free food, free shirt, prizes, and an abundance of new Coldfusion features. […]