Friday, November 30, 2007

HttpMethod Delegation in Jersey

I came across a new use case for the JSR 311 (Jersey) implementation. Here's the juicy details:
  • Have a collection resource and a specific resource that is delegated to (ie: UsersResource and UserResource where UsersResource delegates to UserResource if a username is specified)
  • Want to load up a User object for each UserResource so that the duplication is not done and if you request information for a User that doesn't exist, you get an error
  • However, I would like to create a User using the same style (obviously just a different http method)
I was trying to make this happen, but there appears to be something in Jersey that makes it automagically delegate to the specific resource rather than look for an exact match to the method I really want to invoke.

Here's a taste of the code:

UsersResource:
@UriTemplate("{username}")
public UserResource getUser(@UriParam("username") String username) {
User user = RealmManager.instance().getUser(username);
if (null == user) {
throw new WebApplicationException(new RuntimeException("user '" + username + "' does not exist."), 404);
}

return new UserResource(formatterFactory, uriInfo, user);
}


Tried this in UsersResource, but this was not executed:
@UriTemplate("{username}")
@HttpMethod("PUT")
public void addUser(@UriParam("username") String username) {
RealmManager.instance().createUser(username);
}


So there you have it...anyone have any ideas?

No comments: