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:
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:
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).
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).
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?
ReplyDeleteOk, 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.
ReplyDeleteI 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?
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.
ReplyDeleteIt 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.
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