Ruby/Rails URI Patterns and JSR311

I had been working with a rails project mapping a uniform REST interace onto complex resource representations assembled at runtime, and as such no object/relational mapping was used to pull information from a local datasource.  Instead I use ‘virtual’ resource representations which are fulfilled on-demand, within synch or asynch interactions, from back-end complex back-end data sources and interfaces.

As such the primary Rails convention of mapping resource URI’s to controllers and actions, and through an object/relational layer to a datasource is effectively useless in my scenario. Instead of mapping resource to a datasource, I instead have to  map URI’s to a generic controller, still supporting the GET, POST, PUT, DELETE semantics, but essentially data-driving a dynamic integration tier with the resource context.

So if I were resource-enabling a database with  2 tables, customers and orders, then I’d use the following routes.rb entries (along with a rails resource scaffold) to build my URI to datasource relationships.

map.resources :customers
map.resources :orders

This would route requests for http://domain:port/customers to the Customers controller, and route the http://domain:port/orders to the Orders controller, where each would sit across O/R mappings to specific tables in my database. Nice and easy !

However in my world the Customers resource and the Orders resource are virtual within the front-end runtime, and must be instantiated by using a dynamic mapping algorithm to initiate back-end integration transactions over a range of mechanisms to instantiate the correct representations for each resource type (focusing on GETS’s for this sake of this example). As such it makes  sense for me to have a single VirtualResource Controller, which uses request context information to infer the correct representation algorithm. Rails has a neat way of enabling this in the routing table:

map.resources :operations, :controller=>"virtualresources"
map.resources :customers, :controller=>"virtualresources"

This enables me to pipe a range of URI patterns into my virtual handler and manage a lot of complexity through very few generalised routines.

At this point I’m considering the migration across to Jersey on Glassfish – using the JSR311 annotation scheme for this kind of URI pattern matching.

 @UriTemplate("widgets/{id}")
 public class Widget {
 ...
 }

Only problem is – at the moment I see no way of assigning multiple URI patterns to a single Virtual Resource class in the way I have with Rails. Only one URI pattern is enabled for each POJO class – which would mean I’d have to create many proxy classes into my virtual resource handler? I’m gonna have to dig a little deeper here but hope this kind of flexibility and learning from the Rails community will be factored into the JSR spec?

2 Responses to Ruby/Rails URI Patterns and JSR311

  1. Paul Sandoz says:

    Hi Stew,

    I am working on the 311 spec and the Jersey implementation.

    I am not sure i understand your requirement for “assigning multiple URI patterns to a single Virtual Resource class”, most likely because i don’t understand what a VirtualResource is in Rails…

    Would it be possible to present an example of the URIs and what you would like to do?

    Thanks,
    Paul.

  2. Stew Welbourne says:

    Ok first my appologies for using informal terminology – and I hope this is not simply a lack of research on my part in relation to JSR311. Virtual resources are not a rails thing…in fact the existence of this concept in my world has led me to move off rails and look for a more suitable platform.

    What I actually mean by ‘virtual’ is that the resource URI (~/resources/customers) hitting the framework cannot be resolved by a straight-forward O/R mapping to an atomic datasource. All RESTful service examples I see involve a relatively simple data-centric resource.

    However the reason I’m looking at RESTful exposure is to replace heavy WS/SOAP/B2B protocols at the Enterprise level, and as such a resource URI may (i.e. ~/faults) may resove to a federated query given there is no single repository within the enterprise. Cachine will undoubtedly come into play with such a scheme, but the single issue I face in creation of a RESTful facade for a complex, diverse environment is the virtual nature of the resource in implementation terms. The resource namespace becomes the user-contract – and my framework takes the resonsibility of resolving one or more user resource views into the underlying information architecture.

    So – when I deal with ‘virtual’ resource representations – I have a model for the representation in my RESTful service provider, so I know what I need to present to my client, and I then map the USER/VERB/NOUN context information to a back-end integration spec to go and get my representation elements. As such all of the URI->CONTROLLER->O/R->DATASOURCE convention is sort of useless to me as I need to selectively route some URI’s to a virtual resource CONTROLLER which bypasses the O/R layer in favour of other styles of integration.

    As such if I have 2 URI’s exposed say ‘~/usage’ and ‘~/problems’ and the representations for both are resolved at runtime (i.e. virtuals) then I want both URI’s to route into a generic handler who knows how to execute the data/context-driven back-end routines to form the representations….

    I see in JSR311 a means of assigning @UriTemplate(“/usage) to a class dedigated to resovling one specific resource representation, but I also want to assign other templates such as @UriTemplate(“/faults”) to a single public class VirtualResourceHandler{…} ?

    I hope this clarifies my confusion 🙂

Leave a comment