Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
39.3. Implementing a Custom Filter
Overview 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
You can implement your own customer message header filters by implementing the
MessageHeaderFilter
Java interface. You must associate a filter with one or more XML schema namespaces (representing the header's namespace) and it is possible to differentiate between request message headers and response message headers.
MessageHeaderFilter interface 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
MessageHeaderFilter
interface is defined in the org.apache.camel.component.cxf.common.header
package, as follows:
Implementing the filter() method 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
MessageHeaderFilter.filter()
method is reponsible for applying header filtering. Filtering is applied both before and after an operation is invoked on an endpoint. Hence, there are two directions to which filtering is applied, as follows:
Direction.OUT
- When the
direction
parameter equalsDirection.OUT
, the filter is being applied to a request either leaving a consumer endpoint or entering a producer endpoint (that is, it applies to a WS request message propagating through a route). Direction.IN
- When the
direction
parameter equalsDirection.IN
, the filter is being applied to a response either leaving a producer endpoint or entering a consumer endpoint (that is, it applies to a WS response message being sent back).
Filtering can be applied by removing elements from the list of headers,
headers
. Any headers left in the list are propagated.
Binding filters to XML namespaces 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
It is possible to register multiple header filters against a given CXF endpoint. The CXF endpoint selects the appropriate filter to use based on the XML namespace of the WSDL binding protocol (for example, the namespace for the SOAP 1.1 binding or for the SOAP 1.2 binding). If a header's namespace is unknown, the header is propagated by default.
To bind a filter to one or more namespaces, implement the
getActivationNamespaces()
method, which returns the list of bound XML namespaces.
Identifying the namespace to bind to 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 39.1, “Sample Binding Namespaces” illustrates how to identify the namespaces to which you can bind a filter. This example shows the WSDL file for a Bank server that exposes SOAP endpoints.
Example 39.1. Sample Binding Namespaces
From the
soap:binding
tag, you can infer that namespace associated with the SOAP binding is http://schemas.xmlsoap.org/wsdl/soap/
.
Implementing a custom filter 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
If you want to implement your own custom filter, define a class that inherits from the
MessageHeaderFilter
interface and implement its methods as described in this section. For example, Example 39.2, “Sample Header Filter Implementation” shows an example of a custom filter, CustomHeaderFilter
, that binds to the namespace, http://cxf.apache.org/bindings/custom
, and relays all of the headers that pass through it.
Example 39.2. Sample Header Filter Implementation