Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Transformation

...

Feature

...

Table of Contents

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

...

"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:

Code Block
xml
xml

<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
  <property name="outTransformElements">
    <map>
      <!-- change "book" to "thebook" -->
      <entry key="book" value="thebook"/>
      
      <!-- drop the namespace from "book" -->
      <entry key="{http://books}book" value="book"/> 
      
      <!-- qualify "book" with "http://books" -->
      <entry key="book" value="{http://books}thebook"/> 
      
      <!--  change namespace to "http://books" for all the elements with the "http://book" namespace -->
      <entry key="{http://book}*" value="{http://books}*"/> 
    </map>
  </property>
</bean> 

...

Using inAppendsElements:

Code Block
xml
xml

<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
  <property name="inAppendElements">
    <map>
      <!-- get "book" wrapped with the new "thebook" element-->
      <entry key="book" value="thebook"/>
    </map>
  </property>
</bean> 

Using outAppendsElements:

Code Block
xml
xml

<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
  <property name="outAppendElements">
    <map>
      <!-- get "book" wrapped with the new "thebook" element-->
      <entry key="book" value="thebook"/>
    </map>
  </property>
</bean> 

Append-Post-Wrap

Code Block
xml
xml

<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

Code Block
xml
xml

<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
  <property name="inAppendElements">
    <map>
      <!-- append new simple "thebook" element with a text value '2' before "the book" -->
      <entry key="book" value="thebook=2"/>
    </map>
  </property>
</bean> 

Append-Post-Include

Code Block
xml
xml

<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
  <property name="inAppendElements">
    <map>
      <!-- append new simple "thebook" element with a text value '2' after "the book", using a '/' convention -->
      <entry key="book/" value="thebook=2"/>
    </map>
  </property>
</bean> 

...

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

Code Block
xml
xml

<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
  <property name="inAppendElements">
    <map>
      <!-- replace the text content of {ns}a element with the 'new Text' value -->
      <entry key="{ns}a" value="{ns}a=new Text"/>
    </map
  </property>
</bean> 

...

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

Code Block
xml
xml

<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransformFeature">
  <property name="outTransformElements">
    <map>
      <!-- drop "book" and all of its children, using an empty value convention -->
      <entry key="book" value=""/>
       
    </map>
  </property>
</bean> 

...

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:

Code Block
xml
xml

<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"/>
      <!-- the transform configuration -->
</bean>

...

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

Code Block
java
java

CustomerServiceService service = new CustomerServiceService();
CustomerService customerService = service.getCustomerServicePort();
Client client = ClientProxy.getClient(customerService);

// drop namespace from all elements qualified by 'http://customers'
Map<String, String> outTransformMap = Collections.singletonMap("{http://customers}*", "*");
org.apache.cxf.interceptor.transform.TransformOutInterceptor transformOutInterceptor =
    new org.apache.cxf.interceptor.transform.TransformOutInterceptor();
transformOutInterceptor.setOutTransformElements(outTransformMap);
client.getOutInterceptors().add(transformOutInterceptor);

// qualify the incoming 'customer' element with 'http://customers' 
Map<String, String> inTransformMap =  Collections.singletonMap("customer", "{http://customers}customer");
org.apache.cxf.interceptor.transform.TransformInInterceptor transformInInterceptor =
    new org.apache.cxf.interceptor.transform.TransformInInterceptor();
transformInInterceptor.setInTransformElements(inTransformMap);
client.getInInterceptors().add(transformInInterceptor);

...

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

Code Block
java
java

CustomerService customerServiceProxy = JAXRSClientFactory.create(endpointAddress, CustomerService.class);

ClientConfiguration config = WebClient.getConfig(customerServiceProxy);

// or
//WebClient client = WebClient.create(endpointAddress);
//ClientConfiguration config = WebClient.getConfig(client);

// drop namespace from all elements qualified by 'http://customers'
Map<String, String> outTransformMap = Collections.singletonMap("{http://customers}*", "*");
org.apache.cxf.interceptor.transform.TransformOutInterceptor transformOutInterceptor =
    new org.apache.cxf.interceptor.transform.TransformOutInterceptor();
transformOutInterceptor.setOutTransformElements(outTransformMap);
config.getOutInterceptors().add(transformOutInterceptor);

// qualify the incoming 'customer' element with 'http://customers' 
Map<String, String> inTransformMap =  Collections.singletonMap("customer", "{http://customers}customer");
org.apache.cxf.interceptor.transform.TransformInInterceptor transformInInterceptor =
    new org.apache.cxf.interceptor.transform.TransformInInterceptor();
transformInInterceptor.setInTransformElements(inTransformMap);
config.getInInterceptors().add(transformInInterceptor);

...