Tuesday, July 7, 2015

How to use Enrich Mediator in WSO2 ESB

Enrich mediator is used when a particular message needs to be send to customer with some modification as required. It basically process message from the source configuration to target configuration. It takes the OMElement from the source configuration and modifies the message by putting it on the main message by the target configuration.

Syntax
<enrich>
    <source [clone=true|false] [type=custom|envelope|body|property|inline] xpath="" property="" />
    <target [action=replace|child|sibiling] [type=custom|envelope|body|property|inline] xpath="" property="" />
</enrich>

Here whatever message or property needs to be updated that is put in source tag and where this message needs to be updated put the value in the target tag.

UI Configuration:




Source Configuration:
  • Clone: to make the message clone this property is used, default value is false.
  • Type: This is basically used that how and what message is being modified from the original message. It could be property, custom, body inline etc. in our example we are using property type.

Target Configuration:

It is the property where you need to modify the required message.
  • Action: Any action which we want to be executed for target message we can provide it can be replace, child or sibling. For example replace will replace the XML message based on the target type specified on the target configuration. .
  • Xpath: This is provided when a xml parameter needs to be replaced with this we need to pass the namespace as well to replace the source value in this target xml tag.

Example:

<proxy name="EnrichExampleProxy" transports="https http" startOnLoad="true" trace="disable" statistics="enable">
        <target inSequence="EnrichExampleProxy_IN" outSequence="EnrichExampleProxy_OUT" faultSequence="Enrich_Fault_Handler"/>
        <publishWSDL key="EnrichExampleProxy_wsdl"/>
    </proxy>
  <localEntry key="EnrichExampleProxy_wsdl" src="file:repository/conf/enrich/resources/proxy/EnrichExample.wsdl"/>
            <sequence name="EnrichExampleProxy_IN">
    <log level="full"/>
                                    <property name="blogName" action="set" value="Java is Easy"/>
            <enrich>
                        <source type="property" clone="true" property="blogName"/>
                        <target xpath="//blogName"  xmlns:jis="http://shriwithjava.blogspot.co.nz/enrich/" />
            </enrich>
            <log level="full"/>
        <send>
            <endpoint key="EnrichExampleProxy_EPR"/>
        </send>
    </sequence>
    <sequence name="EnrichExampleProxy_OUT">
        <log level="full"/>
        <send/>
    </sequence>
     <sequence name="Enrich_Fault_Handler">
        <log level="custom">
            <property name="MESSAGE" value="Executing default &quot;fault&quot; sequence"/>
            <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
            <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
        </log>
        <send/>
    </sequence>         




   In the above example we have created a proxy which as In Sequence and out sequence and trying ‘blogName’ property as a ‘Java Is Easy’. In enrich mediator we have used property as a source and trying to set it in ‘blogName’ parameter in service and for the same we have provided the namespace as ‘xmlns:jis="http://shriwithjava.blogspot.co.nz/enrich/”’, so before sending this request to the client it will send the Java is easy value in blogName tag.
This mediator can be used to set any value in request xml before sending it to client because in the some cases you don’t get the proper request but as per the client requirement we can update the request and its value before sending it.