Hi!
Is there any good practice on how to generate URIs for Restlet
resources? As we all should know, one of the mantra's of Roy Fielding
was "The hypermedia as the engine of the application state" which in
short terms means that the resource representations should contain
links to other resource representations, so that the clients don't
need to have any hard-coded URI templates (just a single starter URI
for the application). For instance, a listing in the collection
resource /users should show links to /users/john and /users/jane, not
just their names.
The question is what would be the best way to generate such links.
Requests in Restlet are normally routed to the right Resource using a
Template, say "/users/{john}" to a UserResource - but how should that
resource's getRepresentation() method best find the links to it's
other resources, in the simplest case say a user includes:
<friend xlink:href="/users/jane">Jane</friend>
If you consider the getRepresentation() producing this XML using any
of the fancy XML tools and JPA beans, say it does something like:
// Fetch from persistency layer - ideally this should also be done by
the URI-thing
User u = userDAO.read(request.getAttributes().get("user")));
Doc doc = XMLMagic.newUserDoc();
// add user info
doc.setFullname(u.getFullName())
// Add friend
User friend = u.getFriend();
doc.newFriend(friend.getUsername())
doc.getFriend.setHref(uriFactory.getURI(friend));
Now the two main mappings is URI (template) to bean, the first line,
and the mapping of bean to URI, the last line. Currently, as far as I
understand, all Restlet users have to do this themselves, and it's up
to them to try to keep them in sync.
In my application I ended up building what I called an URIFactory
which takes a JPA bean (as all my resources are stored as) and gives
a generated URI, absolute just to be sure. (I don't want to worry
about how to do relative links yet).
However, this is done quite separate from the Templates in the
Restlet router, although they re-use a few constants. Also, each
Resource need to know which template variable to pick up so that they
can fetch the right JPA bean.
Some kind of integrated functionality here would be great, although
I'm not sure what would be the look of that API. I realise one of the
problems for this would be to support any kind of 'beans'.
(Would using Spring help? I have not looked into using Spring with
Restlet yet. Any quick arguments on why I should? :-) )
--
Stian Soiland, myGrid team
School of Computer Science
The University of Manchester
http://www.cs.man.ac.uk/~ssoiland/