You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Unknown macro: {span}

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

The following properties can be set from Spring:

  • "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. Examples:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransfromFeature">
  <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> 
  • "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.
  • "outAppendElements" map property: can be used to append new simple or qualified elements to the output; keys are the new elements, values are the elements the new ones will be appended before. Examples:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransfromFeature">
  <property name="outAppendElements">
    <map>
      <!-- append "book" before "thebook" -->
      <entry key="book" value="thebook"/>
      
      <!-- qualify "book" with "http://books" -->
      <entry key="{http://books}book" value="book"/> 
      
      <!-- drop the namespace from the "book" -->
      <entry key="book" value="{http://books}thebook"/> 
      
      <!--  change namespace to "http://book" for all the elements with the "http://books" namespace -->
      <entry key="{http://book}*" value="{http://books}*"/> 
    </map>
  </property>
</bean> 
  • "inAppendElements" map property : can be used to append new simple or qualified elements to the input; see the "outAppendElements" property description for an example.
  • "outDropElements" list property : can be used to drop elements2. during the serialization; note that children elements if any of a given dropped element are not affected. Examples:
<bean id="transformFeature" class="org.apache.cxf.feature.StaxTransfromFeature">
  <property name="outDropElements">
    <list>
      <!-- ignore drop and {http://numbers}number elements -->
      <value>{http://numbers}number</value>
      <value>index</value>
    </list>
  </property>
</bean> 
  • "inDropElements" list property : can be used to drop elements during the deserialization; note that children elements if any of a given dropped element are not affected. Please see the "outDropElements" property description for an example.
  • "attributesAsElements" boolean property : can be used to have attributes serialized as elements.

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

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);

// drop namespace from all elements qualified by 'http://customers'
Map<String, String> outTransformMap = Collections.singletonMap("{http://customers}*", "*");
org.apache.cxf.interceptor.transform.TransformOutInterceptor transformOutInterceptor =
    org.apache.cxf.interceptor.transform.TransformOutInterceptor();
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 =
    org.apache.cxf.interceptor.transform.TransformInInterceptor();
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);

// 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 =
    org.apache.cxf.interceptor.transform.TransformOutInterceptor();
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 =
    org.apache.cxf.interceptor.transform.TransformInInterceptor();
config.getInInterceptors().add(transformInInterceptor);
  • No labels