Hello,
I have a custom JIRA plugin that includes some rest resources and that also acts as a client for those resources.
I would like to add some filters to add common processing for all requests/responses (e.g. logging). This is typical for a Jersey rest api, so normally I'd have to define my own classes that implement ContainerRequestFilter and/or ContainerResponseFilter and add a @Provider annotation.
The REST Plugin Module documentation mentions that the plugin will scan my classes for @Provider annotations, but for some reasons my filters do not execute.
Is there any configuration I need to add to my atlassian-plugin.xml to make filters work?
I develop for JIRA 6.1, which contains rest-api-module 2.8.0-m8 and uses Jersey 1.18.3.
Preferably I'd also add some filters on the client side. I use SAL's RequestFactory to send requests, but this does not seem to provide any functionality to add filters. Is there a way to do this?
Any help appreciated
Community moderators have prevented the ability to post new answers.
This is what I ended up with:
See the Jersey 1.18 javadoc of the com.sun.jersey.api.container.filter package.
There are 2 kinds of filters: Container filters and Resource filters.
I didn't find a way to get a working Container filter. These require you to set some init-params on your Jersey servlet or filter, but I assume that Atlassian hides that configuration part for plugin developers, so you cannot set them.
Resource filters however, work fine. For example, you can create your own filter by implementing the ResourceFilter interface like so:
public class MyFilter implements ResourceFilter, ContainerRequestFilter, ContainerResponseFilter { @Override public ContainerRequest filter(ContainerRequest containerRequest) { // Do something with the incoming request here return containerRequest; } @Override public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) { // Do something with the outgoing response here return containerResponse; } @Override public ContainerRequestFilter getRequestFilter() { return this; } @Override public ContainerResponseFilter getResponseFilter() { return this; } }
And now you can annotate your rest method with it:
@Path("/myresource") public class MyResource { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @ResourceFilters(MyFilter.class) public MyValue createSomething(MyValue myValue) { // do things here } }
Whenever a request is sent to /myresource, MyFilter will process the request and response.
The RequestFactory of the SAL api does not expose Jersey client, so there is no way to access Jersey's features. If you want filter functionality you have to implement your own mechanism to do so.
@Glenn Verrijkt, thanks for your answer.
Is there a way to register a ResourceFilterFactory in JIRA to register a filter for all methods?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
https://bitbucket.org/atlassian/atlassian-rest/src/baaf8d8d18af578c2fb6c9df9cfc18ec26c807e2/atlassian-rest-module/src/main/resources/META-INF/spring/atlassian-plugin-context.xml?at=master&fileviewer=file-view-default this worked for me, you can add a ResourceFilterFactory as a service
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am trying to inject my dao class in the filter, its getting injected nicely in the rest class but not in the filter class.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So we are basically "highjacking" a ResourceFilter with additional Request/Response filters. Well thought out!
Also, it works like a charm for Jira 7.13. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.