A way to transform message content in between the client and the backend server - WSO2 Payload Factory Mediator


WSO2 ESB version - 4.0.3

WSO2 PayLoad Factory Mediator is for transforming or replacing message content in between the client and the backend server. Incomming message from client can be transformed by the Payload Factory mediator into the format expected by the service. Then the response message can be formatted into which is expected by the client.

Each argument in the mediator configuration could be a static value or an XPath expression. When an expression is used, argument value is fetched at runtime by evaluating the provided XPath expression against the existing SOAP message. Also the format of the request or response message can be configured which is mapped with the arguments provided in order.

For instance in the following the values for format parameters Code & Price will be assigned with values evaluated from args given in the specified order.

<payloadFactory>
      <format>
               <m:checkpriceresponse xmlns:m="http://services.samples/xsd">
                  <m:code>$1</m:code>
                  <m:price>$2</m:price>
               </m:checkpriceresponse>
            </format>
            <args>
               <arg expression="//m0:symbol" xmlns:m0="http://services.samples/xsd">
               <arg expression="//m0:last" xmlns:m0="http://services.samples/xsd">
            </arg></arg></args>
</payloadFactory>

A sample service of WSO2 Payload Factory Mediator can be found from here. The sample mediates the incomming message, send to the server and response is sent back to the client.  Inside 'In' mediator, PayloadFactory mediator transforms the message. The response message is transformed inside 'Out' mediator.

We can use PayloadFactory mediator in WSO2 ESB proxy services as well. In the inSequence we can transform the request message, and in outSequence we can transform the response message. Here the publishWSDL must be specified with the WSDL which is visible to the user.

Imagine that there is a web service endpoint which has the getQuote operation with 'symbol' parameter. But client who invokes the service see that there is a getQuote operation with 'Code' parameter. So request message will be sent as,

<p:getQuote xmlns:p="http://services.samples">
      <p:request>
       <p:code>IBM</p:code>
      </p:request>
 </p:getQuote>
But server expects a message as,
<p:getQuote xmlns:p="http://services.samples">
     <p:request>
       <p:symbol>IBM</p:symbol>
      </p:request>
   </p:getQuote>
The PayloadFactory mediator does this transformation as follows. 
<proxy name="test" startonload="true" statistics="disable" trace="disable" transports="https,http" xmlns="http://ws.apache.org/ns/synapse">
   <target>
      <inSequence>
         <payloadFactory>
            <format>
               <m:getquote xmlns:m="http://services.samples">
                  <m:request>
                     <m:symbol>$1</m:symbol>
                  </m:request>
               </m:getquote>
            </format>
            <args>
               <arg expression="//m0:Code" xmlns:m0="http://services.samples"></arg>
            </args>
         </payloadFactory>
      </inSequence>
      <outSequence>
         <payloadFactory>
            <format>
               <m:checkpriceresponse xmlns:m="http://services.samples/xsd">
                  <m:code>$1</m:code>
                  <m:price>$2</m:price>
               </m:checkpriceresponse>
            </format>
            <args>
               <arg expression="//m0:symbol" xmlns:m0="http://services.samples/xsd"></arg>
               <arg expression="//m0:last" xmlns:m0="http://services.samples/xsd"></arg>
            </args>
         </payloadFactory>
         <log level="full"></log>
         <send></send>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:9000/services/SimpleStockQuoteService">
</address>
</endpoint>
   </target>
   <publishwsdl uri="ClientVisibleWSDL.wsdl"></publishwsdl>
</proxy>
Similarly the web service will send the response in the format of,
<m:checkpriceresponse xmlns:m="http://services.samples/xsd">
   <m:symbol>IBM</m:symbol>
   <m:last>84.76940826343248</m:last>
</m:checkpriceresponse>  
But the client expects the response message as,
<m:checkpriceresponse xmlns:m="http://services.samples/xsd">
   <m:code>IBM</m:code>
   <m:price>84.76940826343248</m:price>
</m:checkpriceresponse>
So this response message transformation happend on the outSequennce.

Comments

Popular posts from this blog

PHP-SOAP web service with out a WSDL

Boomi Mapping - Removing special chars from an input

How to add Maven dependency from a relative path referencing a local jar