Skip to content
Mihail Kuznetsov edited this page Mar 16, 2015 · 5 revisions

Filters

Filters

EverRest supports request/response/methods filters.

  • Request filter able to modify original request (HTTP headers, URLs, HTTP method). To be able add own request filter developer need implement interface org.everrest.core.RequestFilter. All implementations of such interface must be annotated with @org.everrest.core.Filter annotation.

  • Response filter able to change able to modify original response created by RESTful service. To be able add own response filter developer need implement interface org.everrest.core.ResponseFilter. All implementations of such interface must be annotated with @org.everrest.core.Filter annotation. It can be usable for adding in response some information which is common for all RESTful services in JAX-RS Application, e. g. Cache-Control header, etc.

  • Method invocation filter can be used to determine is it possible to invoke method in current context (e.g. for current user, with current request attributes or environment settings, etc). To be able add own method invocation filter developer need implement interface org.everrest.core.method.MethodInvokerFilter. All implementations of such interface must be annotated with @org.everrest.core.Filter annotation.

Optional all filters can have @Path annotation. In this case filter will be applied to request, response or resource method only if request URL is matched to value specified in @Path annotation.

Filters must be packed in .war file with other JAX-RS components. Then there are two ways for deploy filters:

  1. Filters can be deployed via subclass of javax.ws.rs.core.Application in the same way as resources and providers.
  2. If Application subclass is not specified as context-param and scanning of components is turned on they will deployed automatically. You can read more about deploing components here.

Example:

  1. Create war maven project by using everrest-project-template-war as template, see ServletsIntegration
  2. Add simple hello world service:
@Path("a")
public class MyResource
{
   @GET
   public String hello()
   {
      return "hello world\n";
   }
}
  1. Build you project: mvn package and deploy it servlet container, e.g. under tomcat.
  2. Try to access you service by using curl:
curl -v -X GET \
http://localhost:8080/{CONTEXT_NAME}/a
You should see something like that as response (As you can see content type is not specified `**/**`):
< HTTP/1.1 200 OK
< Content-Type: **/**
< Transfer-Encoding: chunked
< Server: Jetty(6.1.10)
< 
** Connection #0 to host localhost left intact
** Closing connection #0
hello world
  1. Add Filter:
@Filter
@Path("a")
public class MyFilter implements ResponseFilter
{
   public void doFilter(GenericContainerResponse response)
   {
      response.getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
   }
}
Such filter added content type header to response. Buid you web application and deploy again. The try to access to service via `curl` as you did before. You should see following response (Content-type header present `text/plain`):
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Server: Jetty(6.1.10)
< 
** Connection #0 to host localhost left intact
** Closing connection #0
hello world
So response filter works as well.