Skip to main content

ColdFusion 10: Working with REST Sub-resources

In REST everything is treated as a resource and each resource is associated with a URI from which it can be accessed. As mentioned in my series of posts on REST; in ColdFusion methods defined in a CFC are made RESTful by adding the attributes httpmethod and restpath. The restpath value specified in the method can then be used to access the resource. Although this would serve the given purpose, it would be a good idea to have a root resource resolve a generic URL and then have different methods that resolve other path of the URL.

For example, consider a generic resource say ‘/orders’. A HTTP PUT request can be sent to update the order (restpath = ‘/orders/{id}’) and a GET request can be sent to retrieve the order details (again restpath=’/orders/{id}’). Now a root resource can resolve the root path ‘/orders’ and then forward the request to another resource to handle the request. The resource that would handle the request is termed as a sub-resource. In order to make a resource as a root resource it should specify the restpath attribute and shouldn’t specify the httpmethod attribute. Also, it should return a reference of a component that would handle the request. Consider this example:

ResourceOne.cfc:

<cfcomponent rest="true" restpath="component1"> <cffunction name="function1" access="remote" returntype="ResourceTwo" restpath="resource1"> <cfset obj = createObject("component", "ResourceTwo")> <cfreturn obj> </cffunction> </cfcomponent>


As observed in the above code snippet, method 'function1' doesn't define the httpmethod attribute but it does define the restpath. Also it returns a reference to a component 'ResourceTwo'. Now this component (ResourceTwo) can handle the request by defining the httpmethod attribute or forward the request to another component by defining the restpath attribute and returninig a reference to the component that would handle the request.

ResourceTwo.cfc:

<cfcomponent> <cffunction name="function1" access="remote" returntype="string" httpmethod="GET"> <cfreturn "From ResourceTwo, handling GET request"> </cffunction> <cffunction name="function2" access="remote" returntype="string" httpmethod="POST"> <cfreturn "From ResourceTwo, handling POST request"> </cffunction> </cfcomponent>


As you can see there are two methods defined that handle the request (httpmethod="GET\POST"). Also, the restpath attribute is not defined in these methods.

Now, when a GET request is issued on the restpath '/resource1'; the root resource (function1 in ResourceOne.cfc) would accept the request and then forward the request to a sub-resource (function1 in ResourceTwo.cfc) to handle the request. The sub-resource would handle the request and return the result to the client. If a POST request is issued then the root resource would forward the request to a sub-resource that would handle the POST request (function2 in ResourceTwo.cfc).

Comments

  1. Hmm. This is rather interesting. Is it really a subresource though? To the outside world, there is no sub resource. There is just the resource. From the code perspective, yeah, you have some abstraction going on, but "resource" in terms of REST, to me, implies the public facing portion. Would you agree that the terminology here is maybe a bit misleading? Or am I reading it wrong?

    ReplyDelete
  2. Ok, I did a bit of reading - and I can see how this is a bit more complex than I thought. In your example, you just create a static CFC. In the docs, they show creating a unique CFC (well, one with particular properties), and then that can handle the http method.

    I still feel weird calling this s subresource though. I mean, /rest/student/5 as a URI would be the same whether or not you use this technique. Right?

    ReplyDelete
  3. Think of it this way - there is one public facing REST service which lists the entire model - customer, orders etc,. There can be two methods that handle the path '/orders' and '/customers'. The sub-resources can take care of crud operations on these entities.It actually can be viewed as a controller that directs the request to a component that can service the request.

    It is not always necessary to organize the code with sub-resources i.e. there can be one method that can handle /rest/student/5 to handle the request.

    ReplyDelete
  4. I guess what's bugging me is that we define Resource as something public facing. Subresources though are entirely server side. The client never knows how we actually implemented it. There is a total disconnect between the two.

    ReplyDelete

Post a Comment

Popular posts from this blog

Server sent events with HTML5 and ColdFusion

There are several ways to interact with the server apart from the traditional request\response and refresh all protocol. They are polling, long polling, Ajax and Websockets ( pusherapp ). Of all these Ajax and Websockets have been very popular. There is another way to interact with the server such that the server can send notifications to the client using Server Sent Events (SSE) . SSE is a part of HTML5 spec:  http://dev.w3.org/html5/eventsource/

File upload and Progress events with HTML5 XmlHttpRequest Level 2

The XmlHttpRequest Level 2 specification adds several enhancements to the XmlHttpRequest object. Last week I had blogged about cross-origin-requests and how it is different from Flash\Silverlight's approach .  With Level 2 specification one can upload the file to the server by passing the file object to the send method. In this post I'll try to explore uploading file using XmlHttpRequest 2 in conjunction with the progress events. I'll also provide a description on the new HTML5 tag -  progress which can be updated while the file is being uploaded to the server. And of course, some ColdFusion code that will show how the file is accepted and stored on the server directory.

Adding beforeRender and afterRender functions to a Backbone View

I was working on a Backbone application that updated the DOM when a response was received from the server. In a Backbone View, the initialize method would perform some operations and then call the render method to update the view. This worked fine, however there was scenario where in I wanted to perform some tasks before and after rendering the view. This can be considered as firing an event before and after the function had completed its execution. I found a very simple way to do this with Underscore's wrap method.