CXF Component
The cxf: component provides integration with Apache CXF for connecting to JAX-WS services hosted in CXF.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
URI format
cxf:bean:cxfEndpoint[?options]
Where cxfEndpoint represents a bean ID that references a bean in the Spring bean registry. With this URI format, most of the endpoint details are specified in the bean definition.
cxf://someAddress[?options]
Where someAddress specifies the CXF endpoint's address. With this URI format, most of the endpoint details are specified using options.
For either style above, you can append options to the URI as follows:
cxf:bean:cxfEndpoint?wsdlURL=wsdl/hello_world.wsdl&dataFormat=PAYLOAD
Options
Name |
Description |
Example |
Required? |
Default Value |
---|---|---|---|---|
|
The location of the WSDL. |
file://local/wsdl/hello.wsdl or |
No |
WSDL is obtained from endpoint address by default. |
|
The name of the SEI (Service Endpoint Interface) class. This class can have, but does not require, JSR181 annotations. |
|
Yes |
|
|
In 1.6 or later (will be deprecated in 2.0), |
|
No (use either |
|
|
The service name this service is implementing, it maps to the |
{http://org.apache.camel}ServiceName |
Only if more than one |
|
|
The port name this service is implementing, it maps to the |
{http://org.apache.camel}PortName |
Only if more than one |
|
|
Which data type messages the CXF endpoint supports |
|
No |
|
|
Available since 1.6.1. Please see the Description of |
|
No |
|
|
Which kind of operation that CXF endpoint producer will invoke |
|
No |
|
|
Will set the default bus when CXF endpoint create a bus by itself |
|
No |
|
|
New in 2.0, use |
|
No |
Default bus created by CXF Bus Factory |
|
New in 2.0, use |
|
No |
An instance of |
|
New in 2.0, use |
|
No |
An instance of |
The serviceName
and portName
are QNames, so if you provide them be sure to prefix them with their {namespace
} as shown in the examples above.
NOTE From CAMEL 1.5.1 , the serviceClass
for a CXF producer (that is, the to
endpoint) should be a Java interface.
The descriptions of the dataformats
DataFormat |
Description |
---|---|
|
POJOs (Plain old Java objects) are the Java parameters to the method being invoked on the target server. Both Protocol and Logical JAX-WS handlers are supported. |
|
|
|
|
You can determine the data format mode of an exchange by retrieving the exchange property, CamelCXFDataFormat
. The exchange key constant is defined in org.apache.camel.component.cxf.CxfConstants.DATA_FORMAT_PROPERTY
.
How to enable CXF's LoggingOutInterceptor in MESSAGE mode
CXF's LoggingOutInterceptor
outputs outbound message that goes on the wire to logging system (Java Util Logging). Since the LoggingOutInterceptor
is in PRE_STREAM
phase (but PRE_STREAM
phase is removed in MESSAGE
mode), you have to configure LoggingOutInterceptor
to be run during the WRITE
phase. The following is an example.
Description of relayHeaders option
There are in-band and out-of-band on-the-wire headers from the perspective of a JAXWS WSDL-first developer.
The in-band headers are headers that are explicitly defined as part of the WSDL binding contract for an endpoint such as SOAP headers.
The out-of-band headers are headers that are serialized over the wire, but are not explicitly part of the WSDL binding contract.
Headers relaying/filtering is bi-directional.
When a route has a CXF endpoint and the developer needs to have on-the-wire headers, such as SOAP headers, be relayed along the route to be consumed say by another JAXWS endpoint, then relayHeaders
should be set to true
, which is the default value.
Available in Release 1.6.1 and after (only in POJO mode)
The relayHeaders=true
express an intent to relay the headers. The actual decision on whether a given header is relayed is delegated to a pluggable instance that implements the MessageHeadersRelay
interface. A concrete implementation of MessageHeadersRelay
will be consulted to decide if a header needs to be relayed or not. There is already an implementation of SoapMessageHeadersRelay
which binds itself to well-known SOAP name spaces. Currently only out-of-band headers are filtered, and in-band headers will always be relayed when relayHeaders=true
. If there is a header on the wire, whose name space is unknown to the runtime, then a fall back DefaultMessageHeadersRelay
will be used, which simply allows all headers to be relayed.
The relayHeaders=false
setting asserts that all headers in-band and out-of-band will be dropped.
You can plugin your own MessageHeadersRelay
implementations overriding or adding additional ones to the list of relays. In order to override a preloaded relay instance just make sure that your MessageHeadersRelay
implementation services the same name spaces as the one you looking to override. Also note, that the overriding relay has to service all of the name spaces as the one you looking to override, or else a runtime exception on route start up will be thrown as this would introduce an ambiguity in name spaces to relay instance mappings.
<cxf:cxfEndpoint ...> <cxf:properties> <entry key="org.apache.camel.cxf.message.headers.relays"> <list> <ref bean="customHeadersRelay"/> </list> </entry> </cxf:properties> </cxf:cxfEndpoint> <bean id="customHeadersRelay" class="org.apache.camel.component.cxf.soap.headers.CustomHeadersRelay"/>
Take a look at the tests that show how you'd be able to relay/drop headers here:
Changes since Release 2.0
POJO
andPAYLOAD
modes are supported. InPOJO
mode, only out-of-band message headers are available for filtering as the in-band headers have been processed and removed from header list by CXF. The in-band headers are incorporated into theMessageContentList
in POJO mode. Thecamel-cxf
component does make any attempt to remove the in-band headers from theMessageContentList
as it does in 1.6.1. If filtering of in-band headers is required, please usePAYLOAD
mode or plug in a (pretty straightforward) CXF interceptor/JAXWS Handler to the CXF endpoint.- The Message Header Relay mechanism has been merged into
CxfHeaderFilterStrategy
. TherelayHeaders
option, its semantics, and default value remain the same, but it is a property ofCxfHeaderFilterStrategy
.
Here is an example of configuring it.Error formatting macro: snippet: java.lang.NullPointerExceptionThen, your endpoint can reference theCxfHeaderFilterStrategy
.Error formatting macro: snippet: java.lang.NullPointerException - The
MessageHeadersRelay
interface has changed slightly and has been renamed toMessageHeaderFilter
. It is a property ofCxfHeaderFilterStrategy
. Here is an example of configuring user defined Message Header Filters:Error formatting macro: snippet: java.lang.NullPointerException - Other than
relayHeaders
, there are new properties that can be configured inCxfHeaderFilterStrategy
.Name
Description
type
Required?
Default value
relayHeaders
All message headers will be processed by Message Header Filters
boolean
No
true
(1.6.1 behavior)relayAllMessageHeaders
All message headers will be propagated (without processing by Message Header Filters)
boolean
No
false
(1.6.1 behavior)allowFilterNamespaceClash
If two filters overlap in activation namespace, the property control how it should be handled. If the value is
true
, last one wins. If the value isfalse
, it will throw an exceptionboolean
No
false
(1.6.1 behavior)
Configure the CXF endpoints with Spring
You can configure the CXF endpoint with the Spring configuration file shown below, and you can also embed the endpoint into the camelContext
tags. When you are invoking the service endpoint, you can set the operationName
and operationNameSpace
headers to explicitly state which operation you are calling.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://activemq.apache.org/camel/schema/cxfEndpoint" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/cxfEndpoint http://activemq.apache.org/camel/schema/cxf/camel-cxf-1.6.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd "> <cxf:cxfEndpoint id="routerEndpoint" address="http://localhost:9003/CamelContext/RouterPort" serviceClass="org.apache.hello_world_soap_http.GreeterImpl"/> <cxf:cxfEndpoint id="serviceEndpoint" address="http://localhost:9000/SoapContext/SoapPort" wsdlURL="testutils/hello_world.wsdl" serviceClass="org.apache.hello_world_soap_http.Greeter" endpointName="s:SoapPort" serviceName="s:SOAPService" xmlns:s="http://apache.org/hello_world_soap_http" /> <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="cxf:bean:routerEndpoint" /> <to uri="cxf:bean:serviceEndpoint" /> </route> </camelContext> </beans>
NOTE In Camel 2.x we change to use {{http://camel.apache.org/schema/cxf}} as the CXF endpoint's target namespace.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf-2.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> ...
Be sure to include the JAX-WS schemaLocation
attribute specified on the root beans element. This allows CXF to validate the file and is required. Also note the namespace declarations at the end of the <cxf:cxfEndpoint/>
tag--these are required because the combined {namespace}localName
syntax is presently not supported for this tag's attribute values.
The cxf:cxfEndpoint
element supports many additional attributes:
Name |
Value |
---|---|
|
The endpoint name this service is implementing, it maps to the |
|
The service name this service is implementing, it maps to the |
|
The location of the WSDL. Can be on the classpath, file system, or be hosted remotely. |
|
The |
|
The service publish address. |
|
The bus name that will be used in the JAX-WS endpoint. |
|
The class name of the SEI (Service Endpoint Interface) class which could have JSR181 annotation or not. |
It also supports many child elements:
Name |
Value |
---|---|
|
The incoming interceptors for this endpoint. A list of |
|
The incoming fault interceptors for this endpoint. A list of |
|
The outgoing interceptors for this endpoint. A list of |
|
The outgoing fault interceptors for this endpoint. A list of |
|
A properties map which should be supplied to the JAX-WS endpoint. See below. |
|
A JAX-WS handler list which should be supplied to the JAX-WS endpoint. See below. |
|
You can specify the which |
|
You can specify the |
|
The features that hold the interceptors for this endpoint. A list of {{<bean>}}s or {{<ref>}}s |
|
The schema locations for endpoint to use. A list of {{<schemaLocation>}}s |
|
The service factory for this endpoint to use. This can be supplied using the Spring |
You can find more advanced examples which show how to provide interceptors , properties and handlers here:
http://cwiki.apache.org/CXF20DOC/jax-ws-configuration.html
NOTE
You can use cxf:properties to set the camel-cxf endpoint's dataFormat and setDefaultBus properties from spring configuration file.
<cxf:cxfEndpoint id="testEndpoint" address="http://localhost:9000/router" serviceClass="org.apache.camel.component.cxf.HelloService" endpointName="s:PortName" serviceName="s:ServiceName" xmlns:s="http://www.example.com/test"> <cxf:properties> <entry key="dataFormat" value="MESSAGE"/> <entry key="setDefaultBus" value="true"/> </cxf:properties> </cxf:cxfEndpoint>
How to make the camel-cxf component use log4j instead of java.util.logging
CXF's default logger is java.util.logging
. If you want to change it to log4j, proceed as follows. Create a file, in the classpath, named META-INF/cxf/org.apache.cxf.logger
. This file should contain the fully-qualified name of the class, org.apache.cxf.common.logging.Log4jLogger
, with no comments, on a single line.
How to consume a message from a camel-cxf endpoint in POJO data format
The camel-cxf
endpoint consumer POJO data format is based on the cxf invoker, so the message header has a property with the name of CxfConstants.OPERATION_NAME
and the message body is a list of the SEI method parameters.
How to prepare the message for the camel-cxf endpoint in POJO data format
The camel-cxf
endpoint producer is based on the cxf client API. First you need to specify the operation name in the message header, then add the method parameters to a list, and initialize the message with this parameter list. The response message's body is a messageContentsList, you can get the result from that list.
NOTE After Camel 1.5 , we change the message body from object array to message content list. If you still want to get the object array from the message body, you can get the body using message.getbody(Object[].class)
, as follows:
How to deal with the message for a camel-cxf endpoint in PAYLOAD data format
PAYLOAD
means that you process the payload message from the SOAP envelope. You can use the Header.HEADER_LIST
as the key to set or get the SOAP headers and use the List<Element>
to set or get SOAP body elements.
Camel 1.x branch, you can get the List<Element>
and header from the CXF Message, but if you want to set the response message, you need to create the CXF message using the CXF API.
Change in 2.0, CxfMessage.getBody()
will return an org.apache.camel.component.cxf.CxfPayload
object, which has getters for SOAP message headers and Body elements. This change enables decoupling the native CXF message from the Camel message.
How to get and set SOAP headers in POJO mode
POJO
means that the data format is a "list of Java objects" when the Camel-cxf endpoint produces or consumes Camel exchanges. Even though Camel expose message body as POJOs in this mode, Camel-cxf still provides access to read and write SOAP headers. However, since CXF interceptors remove in-band SOAP headers from Header list after they have been processed, only out-of-band SOAP headers are available to Camel-cxf in POJO mode.
The following example illustrate how to get/set SOAP headers. Suppose we have a route that forwards from one Camel-cxf endpoint to another. That is, SOAP Client -> Camel -> CXF service. We can attach two processors to obtain/insert SOAP headers at (1) before request goes out to the CXF service and (2) before response comes back to the SOAP Client. Processor (1) and (2) in this example are InsertRequestOutHeaderProcessor and InsertResponseOutHeaderProcessor. Our route looks like this:
In 2.x SOAP headers are propagated to and from Camel Message headers. The Camel message header name is "org.apache.cxf.headers.Header.list" which is constant defined in CXF (org.apache.cxf.headers.Header.HEADER_LIST). The header value is a List of CXF SoapHeader objects (org.apache.cxf.binding.soap.SoapHeader). The following snippet is the InsertResponseOutHeaderProcessor (that insert a new SOAP header in the response message). The way to access SOAP headers in both InsertResponseOutHeaderProcessor and InsertRequestOutHeaderProcessor are actually the same. The only difference between the two processors is setting the direction of the inserted SOAP header.
In 1.x SOAP headers are not propagated to and from Camel Message headers. Users have to go deeper into CXF APIs to access SOAP headers. Also, accessing the SOAP headers in a request message is slight different than in a response message. The InsertRequestOutHeaderProcessor and InsertResponseOutHeaderProcessor are as follow.
How to throw a SOAP Fault from Camel
If you are using a camel-cxf
endpoint to consume the SOAP request, you may need to throw the SOAP Fault from the camel context.
Basically, you can use the throwFault
DSL to do that; it works for POJO
, PAYLOAD
and MESSAGE
data format.
You can define the soap fault like this
Then throw it as you like
If your CXF endpoint is working in the MESSAGE
data format, you could set the the SOAP Fault message in the message body and set the response code in the message header.
NOTE the response code setting only works in Camel's version >= 1.5.1
How to propagate a camel-cxf endpoint's request and response context
cxf client API provides a way to invoke the operation with request and response context. If you are using a camel-cxf
endpoint producer to invoke the outside web service, you can set the request context and get response context with the following code:
CxfExchange exchange = (CxfExchange)template.send(getJaxwsEndpointUri(), new Processor() { public void process(final Exchange exchange) { final List<String> params = new ArrayList<String>(); params.add(TEST_MESSAGE); // Set the request context to the inMessage Map<String, Object> requestContext = new HashMap<String, Object>(); requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, JAXWS_SERVER_ADDRESS); exchange.getIn().setBody(params); exchange.getIn().setHeader(Client.REQUEST_CONTEXT , requestContext); exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION); } }); org.apache.camel.Message out = exchange.getOut(); // The output is an object array, the first element of the array is the return value Object\[\] output = out.getBody(Object\[\].class); LOG.info("Received output text: " + output\[0\]); // Get the response context form outMessage Map<String, Object> responseContext = CastUtils.cast((Map)out.getHeader(Client.RESPONSE_CONTEXT)); assertNotNull(responseContext); assertEquals("Get the wrong wsdl opertion name", "{http://apache.org/hello_world_soap_http}greetMe", responseContext.get("javax.xml.ws.wsdl.operation").toString());
CXF Bean Component (2.0 or later)
The cxfbean: component allows other Camel endpoints to send exchange and invoke Web service bean objects. (Currently, it only supports JAXRS annotated service bean.)
Note: CxfBeanEndpoint
is a ProcessorEndpoint
so it has no consumers. It works similarly to a Bean component.
URI format
cxfbean:serviceBeanRef
Where serviceBeanRef is a registry key to look up the service bean object. If serviceBeanRef
references a List
object, elements of the List
are the service bean objects accepted by the endpoint.
Options
Name |
Description |
Example |
Required? |
Default Value |
---|---|---|---|---|
|
CXF bean binding specified by the |
|
No |
An instance of |
|
CXF bus reference specified by the |
|
No |
Default bus created by CXF Bus Factory |
|
Header filter strategy specified by the |
|
No |
An instance of |
|
Will set the default bus when CXF endpoint create a bus by itself. |
|
No |
|
Headers
Name |
Description |
Type |
Required? |
Default Value |
In/Out |
Examples |
---|---|---|---|---|---|---|
|
Character encoding |
|
No |
None |
In |
ISO-8859-1 |
|
Content type |
|
No |
*/* |
In |
|
CamelHttpBaseUri |
The value of this header will be set in the CXF message as the |
|
Yes |
The Endpoint URI of the source endpoint in the Camel exchange |
In |
|
|
Request URI's path |
|
Yes |
None |
In |
|
|
RESTful request verb |
|
Yes |
None |
In |
|
|
HTTP response code |
|
No |
None |
Out |
200 |
Note: Currently, CXF Bean component has (only) been tested with Jetty HTTP component it can understand headers from Jetty HTTP component without requiring conversion.
A Working Sample
This sample shows how to create a route that starts a Jetty HTTP server. The route sends requests to a CXF Bean and invokes a JAXRS annotated service.
First, create a route as follows. The from
endpoint is a Jetty HTTP endpoint that is listening on port 9000. Notice that the matchOnUriPrefix
option must be set to true
because RESTful request URI will not match the endpoint's URI http://localhost:9000 exactly.
The to
endpoint is a CXF Bean with bean name customerServiceBean
. The name will be looked up from the registry. Next, we make sure our service bean is available in Spring registry. We create a bean definition in the Spring configuration. In this example, we create a List of service beans (of one element). We could have created just a single bean without a List.
That's it. Once the route is started, the web service is ready for business. A HTTP client can make a request and receive response.