Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve matched resources of a request in a ContainerRequestFilter

I am working on a WebService using JAX-RS/Jersey.

I've set up a ContainerRequestFilter whose purpose is to authenticate the user. I only need to protect some of the paths with authentication, the rest can be available to everyone.

I want to retrieve matchedResources / matchedResults via ExtendedUriInfo in my ContainerRequestFilter so that I can check if the path should be protected or not. Is there a way to create a filter which is invoked after ExtendedUriInfo is populated, but before the matched resource class and method is invoked?

like image 407
John B Avatar asked Aug 04 '12 20:08

John B


2 Answers

Here is a more general answer (for instance if you're using another jax-rs implementation like CXF):

Just add the following in your filter class as an instance variable:

@Context

ResourceInfo info;

"javax.ws.rs.container.ResourceInfo is a new JAX-RS context which can be injected into filters and interceptors and checked which resource class and method are about to be invoked."

(source : https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Basics#JAX-RSBasics-ResourceInfo)

Original answer here

like image 50
Kevin L. Avatar answered Sep 18 '22 13:09

Kevin L.


Found a way to do it with ContainerRequestFilter:

public void filter(ContainerRequestContext requestContext) {
    UriRoutingContext routingContext = (UriRoutingContext) requestContext.getUriInfo();
    ResourceMethodInvoker invoker = (ResourceMethodInvoker) routingContext.getInflector();
    Class<?> className = invoker.getResourceClass();
    Method methodName = invoker.getResourceMethod();
}
like image 42
Arthur Avatar answered Sep 16 '22 13:09

Arthur