users@jersey.java.net

Re: [Jersey] Superclass subclass howto

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 19 May 2010 17:19:00 +0200

On May 13, 2010, at 10:02 AM, Ross Mcdonald wrote:

> Hi Guys.
>
> I would like to get a pointer for how to get some inheritance
> behaviour for a particular use case. We have many resource methods
> for CRUD operations, and for all of the CUD ops we need to grab
> '_at_Context HttpServletRequest' as a parameter to the method, grab
> the user principal, and store it somewhere. We don't want to do
> this in all our methods as it would be cool to do it in one place
> instead. Sorry for the newb question but how do we do this in Jersey?
>

If you have root resource classes you can do:

public MyBaseResource {
   private @Context HttpServletRequest hsr;

   public String getUserPrinciple() {
    return hsr...
   }
}


@Path("root")
public class MyRootResource exrends MyBaseResource {
}


You can also inject SecurityContext:

   https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/SecurityContext.html

instead of SecurityContext from which the user principle can be
obtained.

If you would like to inject the user principle directly please log an
issue (it is possible to add that support yourself as well but that is
more for advanced users and i can give details if you require).

Paul.