我在使用 Pax Web Whiteboard 服务向javax.servlet.Filter
通过 CXF 注册的正在运行的 JaxRS 端点注册 a 时遇到问题。我尝试了几种不同的方法,即将过滤器注册为服务,并使用 org.ops4j.pax.web.service.WebContainer 直接注册过滤器。
对于我的测试,我启动了 karaf,并安装了 pax-web、webconsole、cxf 和 pax-web 白板服务。我注册了一个带有蓝图的捆绑包:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
xmlns:cxf="http://cxf.apache.org/blueprint/core"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
">
<cxf:bus>
<cxf:features>
<cxf:logging/>
</cxf:features>
</cxf:bus>
<jaxrs:server id="webfiltersample" address="/webfilter">
<jaxrs:serviceBeans>
<ref component-id="serviceBean" />
</jaxrs:serviceBeans>
</jaxrs:server>
<service id="servletFilterService" interface="javax.servlet.Filter">
<service-properties>
<entry key="filter-name" value="BasicWebFilter"/>
<entry key="urlPatterns" value="/*"/>
<entry key="initParams" value=""/>
</service-properties>
<bean class="test.webfilter.BasicWebFilter"/>
</service>
<bean id="serviceBean" class="test.webfilter.WebFilterSample" init-method="init" destroy-method="destroy">
<property name="bundleContext" ref="blueprintBundleContext"/>
</bean>
</blueprint>
但是,永远不会调用此过滤器。我已经尝试使用 servlet 名称和 urlpatterns,甚至尝试使用 urlpattern /* 然后我尝试了一种稍微不同的方法,从蓝图中删除服务声明,并通过蓝图的 init 方法直接添加过滤器:
public void init(){
logger.info("Starting Sample");
filter = new WebFilter();
ServiceReference<WebContainer> ref = bundleContext.getServiceReference(BasicWebFilter.class);
WebContainer container = bundleContext.getService(ref);
logger.info("Registering "+ filter + " with "+ container);
container.registerFilter(filter, new String[]{"/cxf/*"}, new String[]{""}, null, null);
bundleContext.ungetService(ref);
}
正如日志语句所反映的,该方法确实被调用了,但仍然没有执行过滤器。
那么我完全错了这是如何工作的?我的“应用程序”实际上只是注册到 CXF servlet 的一个端点。这部分正在工作,我可以调用其中定义的 REST 服务。但无论我做什么,过滤器都没有执行。我在这里使用一些我不太了解的库(Servlets/Filters、Pax-Web 和 Writeboard 扩展器)我不知道为什么这不起作用?我的猜测是每个包都有不同的httpcontexts,我不能简单地在我自己的测试包中为另一个包(CXF)注册一个过滤器。
如果这是真的,有人可以告诉我如何正确解决这个问题吗?我可以得到 CXF 捆绑包 bundlecontext 并将过滤器注册到那个,但这似乎是一个可怕的可怕黑客。如果不是这样,有人可以告诉我为什么这不起作用吗?