A long time ago, I filed Issue 650, an enhancement request to be able
to inject a SecurityContext or UserPrincipal into a resource class.
In response, Marek Potociar created an example for me that showed me
how to do it with a static username - just as an example. That example
is here:
http://java.net/projects/jersey/sources/svn/show/trunk/jersey/samples/jersey-guice-filter
I have changed things a little so that my application now uses a
ContainerRequestFilter. This filter implements the
ContainerRequest.filter(ContainerRequest cr) method to attach a
SecurityContext to the request then return it.
So now I'm back to wondering how I can do something like below, to get
rid of @Context injections:
/* THIS FAILS because Guice/GuiceServletContainer doesn't know how to
inject SecurityContext */
@RequestScoped
public class XxxPrincipalProvider implements Provider<Principal>{
private final SecurityContext sc;
@Inject
public XxxPrincipalProvider(SecurityContext sc) {
this.sc = sc;
}
@Override
public Principal get() {
return sc.getUserPrincipal();
}
}
or even this:
/* THIS FAILS because there is no context injection at this level, so
sc is null, resulting in an NPE */
@RequestScoped
public class OcbPrincipalProvider implements Provider<Principal>{
@Context
SecurityContext sc;
@Inject
public OcbPrincipalProvider() {
}
@Override
public Principal get() {
return sc.getUserPrincipal();
}
}
It still seems like there must be a way to do this that I haven't put
my finger on yet.
--Chris