users@jersey.java.net

thoughts on _at_Link and Marc's blog post

From: James Strachan <james.strachan_at_gmail.com>
Date: Thu, 11 Mar 2010 10:22:53 +0000

I saw Marc's blog post & really liked what I saw.
http://weblogs.java.net/blog/mhadley/archive/2010/03/10/declarative-hyperlinking-jersey

I started to reply in the comments but then did a preview and saw it
mangled the code so thought I'd post here instead :). (Its probably a
better place for discussions anyway I guess).

Basically - great work Marc, I'm really liking the simplicity and DRY
approach here!

This seems great for 'root resources' or top level resources; though
it gets harder when you build deeply nested resources, where (say) a
Shipment resource might be a child of a Widget resource - with the
Shipment resource only knowing how its linked within a Widget. e.g.

class Widget {
  @Path("shipment/{shipmentId}")
  Shipment getShipment(@PathParam("shipmentId") String shipmentId) {
     return new Shipment(this, shipmentId)
   }
}

I wonder if the @Link annotation could also be configured with the
'owner'? e.g.

class Shipment {
  @Link("shipment/{shipmentId}", ownedBy="widget")
   URI link;

   String shipmentId;
   Widget widget;
}

Though this is not very DRY as there's a need to replicate the child
URI template in both @Link and @Path.

I wonder if there's a way to just specify the URI template once on
Widget which can then be used both when navigating from Widget ->
Shipment and when trying to reverse engineer the URI from a Shipment?

e.g. if the @Link could just refer to the owner, then you could find
the getShipment() method on Widget and find its URI template from the
@Path annotation? e.g.

class Shipment {
  @Link(ownedBy="widget")
   URI link;

   String shipmentId;
   Widget widget;
}

Then the URI template is only specified once - in the usual place -
and it can be deduced by looking for the method in the owner with a
@Path annotation which returns an object of the type Shipment? (For
the @Path URI template on Widget.getShipment() to be usable with @Link
we'd need to use consistent naming of the 'shipmentId' parameter).

You could argue that the owner could be deduced too (by analysing all
of the available resources and figuring out which resource is capable
of making a Shipment) - but you could possibly have a resource which
is available via multiple URIs maybe - so being explicit in this case
is not such a bad thing IMHO.

Thoughts?

-- 
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/