VishalDevgire
VishalDevgire

Reputation: 4268

how to get reference to the Method being invoked in jersey

I'm developing a REST based web service using jersey. I want to implement role-based authorization for this web service.

Now suppose i have one method getUsers() :

@RolesAllowed("ADMIN")
@Path("\Users")
@GET
public void getUsers() {
  // some code.
}

getUsers() will be invoked for a GET \Users request.

After getting the request i will authenticate the client using it's credentials and will determine his role then i have to decide to let the request pass or reject.

Getting credentials and authenticating user is not a problem, i need a way to get reference to the method which will be executed for a request.

So that i can check its @RolesAllowed annotation to grant or reject access.

In RestEasy we have PreProcessInterceptor interface which has following method :

 preProcess(HttpRequest request, ResourceMethod methodInvoked)

ResourceMethod's getMethod() gives us reference to the method being invoked in our case getUsers(). Using this Method reference i can check the annotations an do stuff.

Now my question is how can i do it in Jersey?

What is the Jersey alternative of RestEasy PreProcessInterceptor?

Upvotes: 0

Views: 383

Answers (1)

Pavel Bucek
Pavel Bucek

Reputation: 5324

You can inject ExtendedUriInfo and call getMatchedResourceMethod. This should work also in (post-matching) filter, so you should be able to use JAX-RS ContainerRequestFilter.

Then call ResourceMethod.getInvocable().getHandlingMethod() and you can check whatever you need :)

Upvotes: 2

Related Questions