Decide which resource + URI method will match in Jersey

In our Jersey application, we would like to present / restrict various parts of our API based on roles. To be specific, we would like to only offer links to resources that the role can visit, i.e. Already in preparation for the answer. And we wouldn't even offer a link to resource X if the user was not allowed to use X anyway.

To be able to distinguish between these links, we would like to ask Jersey how it will resolve a specific URI - that is, what resource and what method on that resource. If we could do that, we would also be able to determine if it will be annotated in some way (@RolesAllowed) that would not allow access ... and therefore we could skip adding a link to the response on creation. Obviously Jersey has this knowledge / ability as it can handle incoming requests.

Question: Is there a way to ask Jersey what resolved the URI in terms of both class and method?

Edit: Yes ... we noticed there is a ResourceContext (which can be injected) that can provide us with the appropriate resource class ... but we still can't find which method resolved the URI.

+3


source to share


2 answers


Tobias,



ResourceContext.matchUriInfo (URI) . getDeclaringResource () and ResourceContext.matchUriInfo (URI) . getMatchedMethod () getMethod () will do what you ask for.

+3


source


Interesting. I don't think this is the default service that is exposed, but assuming you are configuring the jersey in web.xml, your config is stored in the WebServletConfig and ServletContainer. I would look at the source for these two classes and see what's available. At the very least, you should be able to subclass something on the stack that does the initialization and stores the information you need. It would be much more practical to get the mapping during class loading and annotation processing rather than searching for it for every request.



0


source







All Articles