Felix OSGi Servlet/Resource 过滤器不过滤

Felix OSGi Servlet/Resource Filter does not filter

我正在尝试过滤 OSGi Apache-Felix 中 Jetty HTTP 服务器的 Web 资源。

我已经在框架中注册了一个过滤器作为 OSGi 组件:

@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
    }
    ...
}

当我启动 Felix 框架并通过浏览器访问资源和 servlet 时,Filter#doFilter(...) 方法从未被调用。

已使用 org.osgi.service.http.HttpService#registerServlet(...)org.osgi.service.http.HttpService#registerResources(...) 注册资源和 servlet。

我确定,过滤器已初始化,因为 Filter#init(...) 被调用:

这里是组件的 Felix scr 信息:

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 = .*

在此期间,感谢您的关注和参与。

R6规范没有明确说明HttpService和Whiteboard Services之间的交互。事实上,RFC 0223 的更新说:

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.

Felix 实现不共享 HttpContext 和 ServletContextHelper:您必须向 Http Whiteboard 注册您的 servlet 才能使用您的过滤器。