Injecting a Provider<UriInfo> into a ContainerRequestFilter has worked
for me:
@Singleton
public class AuthContainerRequestFilter implements ContainerRequestFilter {
...
@Inject
AuthContainerRequestFilter(Provider<UriInfo> uriInfoProvider,
<other stuff>) {
...
}
...
@Override
public ContainerRequest filter(ContainerRequest request) {
boolean secure =
uriInfoProvider.get().getRequestUri().getScheme().equals("https");
...
}
Note that my ContainerRequestFilter impl is being registered with
ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, not constructed
manually. You may be able to inject Provider<UriInfo> into your
ResourceFilterFactory impl, though.
On 12/09/2013 06:10 PM, Martin Bedoian wrote:
I have been pulling my hair out trying to find the proper solution to
grabbing the UriInfo object when using Guice and Jersey and come to all
of you for help.
I am trying to figure out the proper way of injecting @UriInfo into a
non-resource class.
Let me explain:
I have implemented a custom RolesAllowedResourceFilterFactory:
|public class RsResourceFilterFactory extends RolesAllowedResourceFilterFactory
{
@Override
public List<ResourceFilter> create(AbstractMethod am) {
List<ResourceFilter> rolesFilters = super.create(am);
if (null == rolesFilters) {
rolesFilters = new ArrayList<ResourceFilter>();
}
List<ResourceFilter> filters = new ArrayList<ResourceFilter>(rolesFilters);
RsSecurityContextFilter securityContextFilter = new
RsSecurityContextFilter();
// do some custom stuff to set on the SecurityContextFilter
filters.add(0, securityContextFilter);
return filters;
}
}
|
RsSecurityContextFilter simply creates a new SecurityContext and sets
the requests security context:
|public class RsSecurityContextFilter implements ResourceFilter,
ContainerRequestFilter {
// ...
public ContainerRequest filter(ContainerRequest request) {
request.setSecurityContext(new RsSecurityContext(groupConstraint,
userConstraint));
return request;
}
// ...
};
|
In the SecurityContext I need to access the UriInfo.
I can't seem to find the magic combination of getting the proper values
injected into RsSecurityContext (I have not included it here, as I have
stripped all attempts out of the code).
In an effort to determine if it was a problem with my bindings I
resorted to holding the injector in a static instance and grabbing it
and then getting the WebApplication from the injector, and then
attempting the following:
|HtppContext httpContext = webApp.getThreadLocalHttpContext();
UriInfo uriInfo = httpContext.getUriInfo();
|
In this case getUrinfo() always fails as the thread local instance of
the httpContext is always null.
Similarly, injecting the webapplication directly in to a jersey Resource
(as provided directly by Guice) grabs the WebApplication, but it cannot
be used to resolve HttpContext or UriInfo.
All other approaches to get the UriInfo (Including using assisted
injection with the SecurityContextFilter and SecurityContext) either
fail to retrieve the UriInfo via its provider, or fail as above when
injecting WebApplication or HttpContext and attempting to get the UriInfo.
Any pointers to the proper approach would be GREATLY appreciated.
TIA
Martin
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.