6

I have a Java Web application which uses Jersey to expose a REST API.

@Path("/myRootResource")
public class resource
{
    @GET
    @Path("subResource_1")
    public Response subResource_1() {return null;}

    @GET
    @Path("subResource_2")
    public Response subResource_2() {return null;}
}

I want to run some code for every subresource under a specific resource. This code handles authentication tasks, performance metrics (e.g start time to calculate the request's time) initialization and debugging variables.

Until now I have all my code in a function (e.g. preTasks()), which I run in the beginning of each sub-resource method. I need to some how automate this and make that code-block to run for every sub resource of myRootResource, without having to copy-paste the aforementioned function to every sub-resource method.

Is this something that can be done with Jersey's Filters or Interceptors?

While I am not really familiar with Spring, I think that what I am trying to achieve is somehow similar to this: Spring web application: executing common code before entering RequestMapping in controller

Update 12/06/2015
As pointed in the comments, 'Interceptors' is a good way to go. But 'Interceptors' are supported only from Jersey 2.X and above. In my code I am using Jersey 1.18. Upgrading to Jersey 2.X breaks some part of my code, so I am trying to avoid this at the moment.

Is there any equivalent to 'Interceptors' in Jersey 1.18, or my only option is the upgrade. I think 'Dispatchers' may to the job, is this correct?

Athafoud
  • 2,898
  • 3
  • 40
  • 58
  • did you find a solution to this? – tryingToLearn Sep 21 '18 at 03:10
  • @tryingToLearn No, I did not find what I was looking for. As I say above, I have a function that has all my logic inside and I call / reuse this every time. The only 'automation' I managed to do, was to create something lake a template class that has all the common logic there. So any new class is based on this one, however refactoring can be messy. – Athafoud Sep 23 '18 at 17:22
  • @Athafoud filters also didn't work with jersey 1.18? – tryingToLearn Sep 24 '18 at 00:50
  • @tryingToLearn I did not use filters, but I can not remember why. So I am not sure if Filters work or not. – Athafoud Sep 25 '18 at 06:30
  • It's worth mentioning that you don't actually need to inject anything when you use the `@Context` annotation; simply tagging your "@Before" method with `@Context` does the trick, so long as you're working in a class that Jersey is managing. – RJStanford May 10 '19 at 15:53

2 Answers2

5

I use this:

/**
 * This method is called by JAX-RS for each request before the identified resource method is
 * invoked, since it is annotated with the Context annotation.
 */
@Context
public void setServletContext( ServletContext servletContext ) {

}

I put this in the Resource classes.

gsl
  • 676
  • 5
  • 16
2

In addition to the previous answer, to get the request, response and the session:

@Context
public void setServletContext(
                ServletContext servletContext,
                @Context final HttpServletRequest request,
                @Context final HttpServletResponse response
) {
    // you can also get to the session
    // it is recommended to uncomment the "false" argument below
    // to avoid creating sessions if they don't already exist
    HttpSession session = request.getSession(/*false*/);
}

You can also put this in a class which your resources extend to get it executed for every request in all resources.

jakubiszon
  • 3,229
  • 1
  • 27
  • 41
  • 1
    `request.getSession()` does not only _get_ a session but it also will **create** a session, if none exists yet! This is particularly important since we generally work _without_ sessions in REST, right? The variant `request.getSession(false)` will _not_ create a session if none exists, but return null. But again, try to avoid sessions since this is REST. – gsl Dec 11 '18 at 16:17
  • 1
    You are correct @gsl - sessions should be avoided. It's just that sometimes we need to use them because of existing infrastructure. – jakubiszon Dec 11 '18 at 18:38