Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:00:34+00:00 2026-05-27T08:00:34+00:00

Jersey offers two classes to interact with annotations on resources: ResourceFilterFactory , one class

  • 0

Jersey offers two classes to interact with annotations on resources:

  • ResourceFilterFactory, one class can inherit it to be triggered one time when the application starts
  • ContainerRequestFilter, ContainerResponseFilter, several classes can inherit them to be triggered on every request/response

ResourceFilterFactory defines a create method (to implement) that take an AbstractMethod which gives access to methods and classes annotations.

ContainerRequestFilter and ContainerResponseFilter defines a filter method (to implement) that take request/response but those give solely access to the called method annotation, not the class one.

I’m trying to implement a CacheControl annotation that defines HTTP cache headers the following way.

@Path("/path")
@CacheControl(maxAge = 172800)
public class Resource
{   
    @GET
    @Path("/{id}")
    @CacheControl(mustRevalidate = true)
    public Response get(@PathParam("id") Long id)
    {
        ...
    }
}

My problem is that I don’t want to create a new CacheControlFilter for every REST method defined in my application.

public class FilterFactory implements ResourceFilterFactory
{    
    @Override
    public List<ResourceFilter> create(AbstractMethod method)
    {
        List<ResourceFilter> filters = newArrayList();
        if (isAnnotationPresent(method, CacheControl.class))
            filters.add(new CacheControlFilter(method));
        return filters;
    }

    private boolean isAnnotationPresent(AbstractMethod method, Class<? extends Annotation> clazz)
    {
        return method.isAnnotationPresent(clazz) || method.getResource().isAnnotationPresent(clazz);
    }
}

Is there a way to access the AbstractMethod without instancing a CacheContronlFilter for every REST method?

public class CacheControlFilter implements ResourceFilter, ContainerResponseFilter
{
    private AbstractMethod method;

    public CacheControlFilter(AbstractMethod method)
    {
        this.method = method;
    }

    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response)
    {
        putCacheControlIfExists(response, method.getAnnotations());
        putCacheControlIfExists(response, method.getResource().getAnnotations());
        return response;
    }

    private void putCacheControlIfExists(ContainerResponse response, Annotation[] annotations)
    {
        CacheControl annotation = findCacheControl(annotations);
        if (annotation != null)
            response.getHttpHeaders().put(CACHE_CONTROL, createCacheControlHeader(annotation));
    }

    private CacheControl findCacheControl(Annotation[] annotations)
    {
        for (Annotation annotation : annotations)
            if (annotation instanceof CacheControl)
                return (CacheControl) annotation;
        return null;
    }

    private List<Object> createCacheControlHeader(CacheControl annotation)
    {
        javax.ws.rs.core.CacheControl header = new javax.ws.rs.core.CacheControl();
        header.setMaxAge(annotation.maxAge());
        header.setMustRevalidate(annotation.mustRevalidate());
        header.setNoCache(annotation.noCache());
        header.setNoStore(annotation.noStore());
        header.setNoTransform(annotation.noTransform());
        header.setProxyRevalidate(annotation.proxyRevalidate());
        return Lists.<Object> newArrayList(Splitter.on(',').split(header.toString()));
    }

    @Override
    public ContainerRequestFilter getRequestFilter()
    {
        return null;
    }

    @Override
    public ContainerResponseFilter getResponseFilter()
    {
        return this;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:00:35+00:00Added an answer on May 27, 2026 at 8:00 am

    Why is it important to not have a separate instance of the filter for every applicable method? There may be a lot of concurrent access, so if you don’t want these to be separate instances, they would have to be mutable and you would have to get into the threadlocals mess (to store the abstract method currently applicable for the given thread). Not sure if that’s what you really want. Having a separate object for each is not that expensive.

    UPDATE: Also note, you don’t want to create a new instance for every method. You just want to do it for methods with any @CacheControl annotation attached to them or to their resources, right? Also you can share filter instances for common @CacheControl values – i.e. if a method uses the same cache control setting as some other method, reuse the same filter for that, if not, create a separate instance of the filter for that method. In other words – you can have one filter per one distinct cache-control setting as opposed to one filter per method – as you don’t really care about the method – you care about the annotations attached to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a Jersey Resource class. Can someone please tell me how can
Jersey 1.6 can produce: @Path(/stock) public class StockResource { @GET @Produces(MediaType.APPLICATION_JSON) public List<Stock> get()
I have a Jersey REST service which has endpoints that can return either application/xml
Do Java REST frameworks like Restlet and Jersey allow one to send a file
I have a Maven 2 RESTful application using Jersey/JAXB. I generate the JAXB beans
I can bring jersey + grizzly server up. But some problem occur during SessionFactory
I'm using Jersey (jax-rs), to build a REST rich application. Everything is great, but
The JAX-RS implementation Jersey supports MVC style web applications through the Viewable class, which
I have got a Jersey-based application, which for HTML requests uses Freemarker to generate
I am looking to use Jersey without the need of installing an application server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.