How to intercept a request during the jersey life cycle?

I used Jersey for most of the year and have just stumbled upon a problem that I cannot find the answer to: how do you intercept (or capture) the life cycle of a request for Jersey?

Ideally, I could do some custom filtering / validation / rejection between the time during which the container receives the request from the network and the time when my handler methods are called. Bonus points if there is an easy way to filter interceptors by subpath (for example, have one interceptor for something under /, another for something under / user /, etc.).

Thank!

Change To be a little clearer, the general idea here is to write code that will run for many API calls without having to explicitly call this code from each handler method. This will reduce additional code and eliminate the need to pass request contexts.

+48
java api jersey jax-ws jax-rs
Dec 05 2018-10-12T00:
source share
4 answers

I have found the answer.

First create a class that implements ContainerRequestFilter. The interface defines the following method in which filtering occurs. The ContainerRequest object contains information about the current request.

public ContainerRequest filter(ContainerRequest req); 

After that, include the following XML in the servlet configuration in web.xml

 <init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>path.to.filtering.class</param-value> </init-param> 

Sources:

http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html http://markmail.org/message/p7yxygz4wpakqno5

+50
Dec 06 2018-10-12T00:
source share

This thread is a bit outdated, but I had time to intercept the request before and after . After a long search on the Internet, I finally realized this:

 <init-param> <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name> <param-value>blah.LoggingFilter</param-value> </init-param> <init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>blah.LoggingFilter</param-value> </init-param> 

and then this class:

 public class LoggingFilter extends LoggingFilter implements ContainerRequestFilter { private static final ThreadLocal<Long> startTime = new ThreadLocal<Long>(); public static boolean verboseLogging = false; @Override public ContainerRequest filter(ContainerRequest arg0) { startTime.set(System.currentTimeMillis()); return arg0; } @Override public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { System.out.println(System.currentTimeMillis() - startTime.get().longValue()); StringBuilder sb = new StringBuilder(); sb.append("User:").append((request.getUserPrincipal() == null ? "unknown" : request.getUserPrincipal().getName())); sb.append(" - Path:").append(request.getRequestUri().getPath()); //... } 

This intercepts the request at the beginning and at the end so that you can set a timer or something else.

This works for Jersey 1.17. Not sure about 2.x.

+8
Jul 26 '13 at 14:06
source share

For the back end, we use the Jersey Specific class to do something like this: ContainerResponseFilter

Signature:

 public ContainerResponse filter(ContainerRequest request, ContainerResponse response) 

then you can make calls such as:

 Object entity = response.getEntity(); ... your logic here ... return response; 

Could this help? ..

0
Dec 05 '10 at 23:44
source share

Have you looked at the ClientFilter Jersey ClientFilter ?

We are currently using this to intercept and execute APIs, etc. Registration filters are built in there, so you can look at the code so that they can understand what to write.

Signature:

 public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException... 

So you can start with things like:

 .... cr.getHeaders() .... return getNext().handle(cr); 
-one
Dec 05 '10 at 11:34
source share



All Articles