More fine grained control for @HttpWhiteboardResource (Cache Control HTTP Header)

Not sure if this is a bnd question, but maybe somebody has pointers.

We have registered a resource for a webapplication (org.eclipse.jetty bundles). The folder contains static assets like images, .css or .js files. Works great.

@HttpWhiteboardResource(pattern = "/templates/*", prefix = "/src/main/resource/apps/webapp/templates")

accessed via URL e.g.: https://myapp.com/templates/css/styles.css

Under the hood there it seems that some ResourceServlet is handling the delivery of the file to the browser.

Question:
We would like to customize the behavior of this ResourceServlet and add an additional HTTP “Cache-Control” header for some very specific files (by filename).

I have not found a way to override this ResourceServlet or do what I want.
Has anybody done something similar or a hint?

Note: this is in no way related to bnd :slight_smile:

However, since I have experience in this area I can offer two avenues for you to pursue:

  1. Look at the Http whiteboard implementation you are using to see if it has support for this type of special attribution (like maybe via service properties).
  2. More generically, register a servlet filter matching the resource (servlet) to add response headers.
1 Like

Thanks a lot. But at least here are all the OSGi gurus. If i manage to solve it i consider reposting on Stackoverflow and answer my own question😃

Thanks @rotty3000, ServletFilter was the hint I needed.
I found a more complete example acs-aem-samples/SampleServletFilter.java at master · Adobe-Consulting-Services/acs-aem-samples · GitHub which I just modified to my needs (e.g. changing the Component property to `HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN + “=/templates/*”)

For other people searching for a solution, I did:

 @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        if (isStaticFileLikeJavascriptOrCSS(httpServletRequest, httpServletResponse)) {

        	// static resource. should instruct the browser to cache it for some time
        	httpServletResponse.setHeader("Cache-Control", "max-age=3600");
                
                 chain.doFilter(httpServletRequest, httpServletResponse);
        }
}