Setting custom response headers for static resources served in OSGi environment

886 views Asked by At

The task that we are trying to solve is to set custom response headers for static resources (e.g. Cache-Control) that are exposed in OSGi environment using HttpService.registerResources() method.

What is the best away to achieve this goal given the following details:

  • There could be numerous resource registrations based on the set of 'resource' services. These OSGi services provide information about static resource paths and bundles they reside in. The amount of resource path registrations we anticipate is in between 50 and 200.
  • Implementation should be OSGi-friendly. Usage of Pax Web extensions is acceptable.
  • Implementation should be web server agnostic (e.g. no direct knowledge about Jetty).

Solutions that we have in mind are:

  1. Set response headers in HttpContext.handleSecurity

    • The downside of this approach is that the method for handling security will be also responsible for the logic that is not related to security at all.
  2. Register filter for each registerResource() call using the same URI as the resources URI. Filter will be response for setting response headers.

    • The downside of this approach is that there may be too many filters serving the same purpose. But so far this seems to the easiest solution.
  3. Register one filter on the root path and configure it to set response headers if request URI correspond to a known resource URI path.

    • The downside of this approach is that filter has to be educated to know about resource paths, which means a bit more complex implementation.

We are eager to hear opinions on the suggested solutions and know other alternatives.

1

There are 1 answers

4
Gunnar On

The Equinox Jetty based HttpService implementation already supports If-None-Match and If-Modified-Since request headers for all resource registrations out of the box.

However, if it really needs to be framework agnostic and compliant with the HttpService standard and you are registering your resources programmatically anyway then I suggest writing your own servlet (eg. like the one from Equinox) which handles resource requests. Instead of using registerResources directly wrap any resource registration using this servlet and registerServlet. You can implement any caching strategy there.