I'm writing a REST API, making use of RestEasy 2.3.4.Final.
I know that a Interceptor will intercept all of my requests, and that a PreProcessInterceptor will be the first (before everything) to be called. I would like to know how can I make this Interceptor to be called just when specific methods are called.
I tried to use both PreProcessInterceptor and AcceptedByMethod, but I was not able to read the parameters I need.
For example, I need to run my Interceptor only when this method is called:
@GET
@Produces("application/json;charset=UTF8")
@Interceptors(MyInterceptor.class)
public List<City> listByName(@QueryParam("name") String name) {...}
To be more specific, I need to run my Interceptor in all methods whose have a @QueryParam("name")
on its signature, so that I can grab the name and do something before everything.
Is it possible? I tried to catch the "name" parameter inside the Interceptor, but I was not able to do that.
Could someone help me, please?
You can use AcceptedByMethod
as explained in the RESTEasy documentation
Create a class that implement both PreProcessInterceptor
and AcceptedByMethod
. In the accept
-method, you can check if the method has a parameter with annotated with @QueryParam("name")
. If the method has that annotation, return true from the accept
-method.
In the preProcess
-method, you can get the query parameter from request.getUri().getQueryParameters().getFirst("name")
.
EDIT:
Here is an example:
public class InterceptorTest {
@Path("/")
public static class MyService {
@GET
public String listByName(@QueryParam("name") String name){
return "not-intercepted-" + name;
}
}
public static class MyInterceptor implements PreProcessInterceptor, AcceptedByMethod {
@Override
public boolean accept(Class declaring, Method method) {
for (Annotation[] annotations : method.getParameterAnnotations()) {
for (Annotation annotation : annotations) {
if(annotation.annotationType() == QueryParam.class){
QueryParam queryParam = (QueryParam) annotation;
return queryParam.value().equals("name");
}
}
}
return false;
}
@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod method)
throws Failure, WebApplicationException {
String responseText = "intercepted-" + request.getUri().getQueryParameters().getFirst("name");
return new ServerResponse(responseText, 200, new Headers<Object>());
}
}
@Test
public void test() throws Exception {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getProviderFactory().getServerPreProcessInterceptorRegistry().register(new MyInterceptor());
dispatcher.getRegistry().addSingletonResource(new MyService());
MockHttpRequest request = MockHttpRequest.get("/?name=xxx");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals("intercepted-xxx", response.getContentAsString());
}
}
if you return return new ServerResponse(responseText, 200, new Headers<Object>());
you will lose the end-point. you need to return null
if you still want the message to be delivered to the final point.