Transformation Feature
The CXF Transformation feature provides for a flexible and fast way to do the dynamic transformations of the 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:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="outTransformElements">
<map>
<entry key="book" value="thebook"/>
<entry key="{http://books}book" value="book"/>
<entry key="book" value="{http://books}thebook"/>
<entry key="{http://book}*" value="{http://books}*"/>
</map>
</property>
</bean>
"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 output elements
"outAppendElements" map property can be used to append new simple or qualified elements to the output. Keys are the elements the new elements will be appended before, values are the new elements. Examples:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="outAppendElements">
<map>
<entry key="book" value="thebook"/>
</map>
</property>
</bean>
Only this so-called Append-Wrap is currently supported for out elements.
Appending new input elements
"inAppendElements" map property can be used to append new simple or qualified elements to the input in a number of ways.
Append-Pre-Wrap
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="inAppendElements">
<map>
<entry key="book" value="thebook"/>
</map>
</property>
</bean>
Append-Post-Wrap
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="inAppendElements">
<map>
<!--
get all "book" children elements wrapped with the new "thebook" element
using the "/" convention.
-->
<entry key="book/" value="thebook"/>
</map>
</property>
</bean>
Append-Pre-Include
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="inAppendElements">
<map>
<entry key="book" value="thebook=2"/>
</map>
</property>
</bean>
Append-Post-Include
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="inAppendElements">
<map>
<entry key="book/" value="thebook=2"/>
</map>
</property>
</bean>
Replacing text content
It's possible to replace the text content of a given simple element only on the input, for example:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="inAppendElements">
<map>
<entry key="{ns}a" value="{ns}a=new Text"/>
</map
</property>
</bean>
Dropping output elements
"outDropElements" list property can be used to drop output elements. Note that children elements if any of a given dropped element are not affected. Examples:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="outDropElements">
<list>
<value>{http://numbers}number</value>
<value>index</value>
</list>
</property>
</bean>
Dropping input elements
"inDropElements" list property can be used to drop 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.
Additionally, inTransformElements property can be used to deep-drop an element and all of its children if any, for example:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
<property name="outTransformElements">
<map>
<entry key="book" value=""/>
</map>
</property>
</bean>
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:
<bean id="transformFeatureRest" class="org.apache.cxf.feature.StaxTransformFeature">
<!--
apply the transformation only if the boolean property with the given name
is set to true on the message
-->
<property name="contextPropertyName" value="http.service.redirection"/>
</bean>
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:
CustomerServiceService service = new CustomerServiceService();
CustomerService customerService = service.getCustomerServicePort();
Client client = ClientProxy.getClient(customerService);
Map<String, String> outTransformMap = Collections.singletonMap("{http:, "*");
org.apache.cxf.interceptor.transform.TransformOutInterceptor transformOutInterceptor =
new org.apache.cxf.interceptor.transform.TransformOutInterceptor();
transformOutInterceptor.setOutTransformElements(outTransformMap);
client.getOutInterceptors().add(transformOutInterceptor);
Map<String, String> inTransformMap = Collections.singletonMap("customer", "{http:);
org.apache.cxf.interceptor.transform.TransformInInterceptor transformInInterceptor =
new org.apache.cxf.interceptor.transform.TransformInInterceptor();
transformInInterceptor.setInTransformElements(inTransformMap);
client.getInInterceptors().add(transformInInterceptor);
JAX-RS
Here is how a JAX-RS client can be configured:
CustomerService customerServiceProxy = JAXRSClientFactory.create(endpointAddress, CustomerService.class);
ClientConfiguration config = WebClient.getConfig(customerServiceProxy);
Map<String, String> outTransformMap = Collections.singletonMap("{http:, "*");
org.apache.cxf.interceptor.transform.TransformOutInterceptor transformOutInterceptor =
new org.apache.cxf.interceptor.transform.TransformOutInterceptor();
transformOutInterceptor.setOutTransformElements(outTransformMap);
config.getOutInterceptors().add(transformOutInterceptor);
Map<String, String> inTransformMap = Collections.singletonMap("customer", "{http:);
org.apache.cxf.interceptor.transform.TransformInInterceptor transformInInterceptor =
new org.apache.cxf.interceptor.transform.TransformInInterceptor();
transformInInterceptor.setInTransformElements(inTransformMap);
config.getInInterceptors().add(transformInInterceptor);