Search code examples
javaservletsjettyosgiapache-felix

Felix OSGi Servlet/Resource Filter does not filter


I am trying to filter the web resources of the Jetty HTTP server inside OSGi Apache-Felix.

I've registered a filter as a OSGi component in the Framework:

@Component(property = { "osgi.http.whiteboard.filter.name=MyFilter",
                        "osgi.http.whiteboard.filter.regex=.*" },
           scope = ServiceScope.PROTOTYPE)
public class MyFilter implements Filter {
    ...
    @Override
    public void doFilter(ServletRequest request, 
                         ServletResponse response,
                         FilterChain chain) {
       ...
       // some logging
    }
    ...
}

When I start the Felix framework, and access resources and servlets via my browser, the Filter#doFilter(...) method never gets called.

The resources and servlets have been registered using org.osgi.service.http.HttpService#registerServlet(...) and org.osgi.service.http.HttpService#registerResources(...).

I'm sure, the filter gets initialized, since Filter#init(...) gets called:

Here the Felix scr info for the component:

Component Description:
  Name: org.myCompany.MyFilter
  Implementation Class: org.myCompany.MyFilter
  Default State: enabled
  Activation: delayed
  Configuration Policy: optional
  Activate Method: activate
  Deactivate Method: deactivate
  Modified Method: -
  Configuration Pid: [org.myCompany.MyFilter]
  Services: 
    javax.servlet.Filter
  Service Scope: prototype
  Component Description Properties:
      osgi.http.whiteboard.filter.name = MyFilter
      osgi.http.whiteboard.filter.regex = .*
  Component Configuration:
    ComponentId: 7
    State: active      
    Component Configuration Properties:
        component.id = 7
        component.name = org.myCompany.MyFilter
        osgi.http.whiteboard.filter.name = MyFilter
        osgi.http.whiteboard.filter.regex = .*
  • Does resource/servlet filtering work within Felix, Jetty and OSGi?
  • Did I register the filter properly?

In the meantime, thank you for your attention and participation.


Solution

  • The R6 specification is not clear on the interaction between the HttpService et Whiteboard Services. In fact, the update on RFC 0223 says :

    3.1 Whiteboard Services and Http Service (Bug 2872)

    If a Http Whiteboard implementation is also implementing the Http Service, the whiteboard specification does not specify whether the Http contexts for the Http Service are represented as ServletContextHelper services. There is no way for a whiteboard service to be registered in a Http Context of the Http Service. For example adding a servlet filter for all servlets managed by the Http Service is not possible.

    The Felix implementation doesn't share HttpContext and ServletContextHelper: You have to register your servlet with the Http Whiteboard in order to use your Filter.