Transformation Feature

The CXF Transformation feature provides for a flexible and fast way to do dynamic transformation of inbound and/or outbound XML messages.

This feature can be used in a number of cases: dropping the namespace of the outbound messages, qualifying the incoming message, changing namespaces, appending or dropping elements and converting attributes to elements.

The "outTransformElements", "inTransformElements", "outDropElements", "inDropElements", "outAppendElements", "inAppendElements" and "attributesAsElements" properties can be used.

Spring configuration

Changing input and output element names and namespaces

"outTransformElements" map property can be used to change the output element names and change or drop namespaces. Keys are the elements to be changed, values are the new element names. Example:

"inTransformElements" map property can be used to change the input element names and change or drop namespaces. See the "outTransfromElements" property description for an example.

Appending new input and output elements

"outAppendElements" and "inAppendElements" map properties can be used to append new simple or qualified elements to the output/input in a number of ways. Keys are the elements the new elements will be appended before, values are the new elements. Examples:

Append-Pre-Wrap

Using inAppendsElements:

Using outAppendsElements:

Append-Post-Wrap

Append-Pre-Include

Append-Post-Include

Comparing four append modes

 
input

append-pre-wrap

 

append-post-wrapappend-pre-includeappend-post-include
 key="book" value="thebook"key="book/" value="thebook"key="book" value="thebook=2"key="book/" value="thebook=2"
<sales>
<book>
<title>CXF ...</title>
<price>38.68</price>
</book>
</sales>
<sales>
<thebook>
<book>
<title>CXF ...</title>
<price>38.68</price>
</book>
</thebook>
</sales>
<sales>
<book>
<thebook>
<title>CXF ...</title>
<price>38.68</price>
</thebook>
</book>
</sales>
<sales>
<thebook>2</thebook>
<book>
<title>CXF ...</title>
<price>38.68</price>
</book>
</sales>
<sales>
<book>
<title>CXF ...</title>
<price>38.68</price>
<thebook>2</thebook>
</book>
</sales>

Replacing text content

It's possible to replace the text content of a given simple element only on the input and output, for example:

Dropping output and input elements

"outDropElements" and "inDropElements" list properties can be used to drop output and input elements. Note that children elements if any of a given dropped element are not affected. Please see the "outDropElements" property description for an example. It's a so-called "shallow" drop.

Additionally, outTransformElements and inTransformElements property can be used to deep-drop an element and all of its children if any, for example:

Converting attributes to elements

"attributesAsElements" boolean property can be used to have attributes serialized as elements on the output only.

The combination of "attributesAsElements" and "outDropElements" properties can be used to have certain attributes ignored in the output by turning them into elements and then blocking them.

Input Transformation and Redirection

Consider the case where a new endpoint has been introduced but some of the existing clients have not been updated yet to work with the new endpoint, they are still unaware of it.

In this case you may want to keep the CXFServlet serving the old clients but make it redirect them to a new CXFServlet serving a new endpoint only.
Now, in order to serve the old clients one needs to apply a transform feature, however the new clients should not be affected. Thus the feature can be configured such that it's only triggered if a certain contextual property has been set on a current Message. In this case the feature should only apply to the old redirected clients:

Configuring the feature from the code

The feature can be configured from the code for JAX-WS or JAX-RS clients and endpoints.

JAX-WS

Here is how a JAX-WS client can be configured:

JAX-RS

Here is how a JAX-RS client can be configured:

Transform interceptors and phases

TransformInInterceptor and TransformOutInterceptor interceptors run in POST_STREAM and PRE_STREAM phases.
In some cases it may be needed to change the phase, for example, the in transformation has to be applied after the encrypted payload has been decrypted and its signature checked. For such transformations to succeed TransformInInterceptor will need to be created with a constructor accepting a 'phase' String parameter, for the interceptor to run after the decrypption and signature validation actions have been performed.

Default namespace on the output

The 'outDefaultNamespace' feature property can be used to enforce the default namespace declaration. The value of this property has to match one of the out namespaces.