This is the latest release in the 6.x.x family. For EI 7.0.0, click here.

All docs This doc

Versions Compared

Key

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

...

Example 3 - Adding a SOAPEnvelope type object as a property to a message

In this example, you add the SOAP envelope in a SOAP request as a property to a message. The Enrich mediator is useful in this scenario since adding the property directly using the Property mediator results in the SOAPEnvelope object being created as an OM type object. The OM type object created cannot be converted back to a SOAPEnvelope object.

Code Block
languagexml
<enrich> 
<source type="envelope" clone="true"/>
<target type="property" property="ExtractedEnvelope"/>
</enrich>
Excerpt
hiddentrue

Added this content for the FAQ When ExtractedEnvelope is a SOAPEnvelope with OM type, why do we get "EnrichMediator SOAPEnvelope is expected" for SOAP requests?

Excerpt
hiddentrue
Excerpt
hiddentrue

Description of the Enrich Mediator in WSO2 ESB.

Example 2 is written with reference to Mediators

Example 4 - Preserving the original payload

In this example, you copy the original payload to a property using the Enrich mediator.

Code Block
languagexml
<enrich>
      <source clone="false" type="body"/>
      <target action="replace" type="property" property="ORIGINAL_PAYLOAD"/>
   </enrich>

Then whenever you need the original payload, you replace the message body with this property value using the Enrich mediator as follows:

Code Block
languagexml
<enrich>
      <source clone="false" type="property" property="ORIGINAL_PAYLOAD"/>
      <target action="replace" type="body"/>
   </enrich>


Example 5 - Enriching in JSON format

For a message to be enriched as a JSON current message in the message flow or incoming message should be a JSON and one of the following conditions should be matched.

Both source and target do not have custom path expressions.
Source has a custom json-path and target does not have any expressions.
Target has a custom json-path and source does not have any expressions.
Both target and source have custom json-path expressions.

The following sample scenarioes can be used to test the differnt JSON enriching scenarioes.

Info

In JSON enriching scenarios if the enrich mediator source defined as a property it should contain a json object or json array.

Sample 1 - Setting the custom path expressions to message body

Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?><proxy xmlns="http://ws.apache.org/ns/synapse" name="TestEnrich" startOnLoad="true" statistics="disable" trace="enable" transports="http,https">
    <target>
        <inSequence>
            <enrich>
                <source clone="false" xpath="json-eval($.SamplePayload)"/>
                <target type="body"/>
            </enrich>
            <respond/>
            <log level="full"/>
        </inSequence>
        <outSequence/>
    </target>
    <description/>
</proxy> 

Sample request 

Code Block
languagexml
{
    "SamplePayload": {
        "SampleArray": [
            123.45
        ]
    }
}

Response

Code Block
languagexml
{
    "SampleArray": [
        123.45
    ]
}

Sample 2 - Setting the property to custom as a sibling

Code Block
languagexml
 <?xml version="1.0" encoding="UTF-8"?>
<inSequence xmlns="http://ws.apache.org/ns/synapse">
    <property name="Greeting" scope="default" type="STRING" value="{&quot;name&quot;:&quot;John&quot;}"/>
    <enrich>
        <source clone="true" property="Greeting" type="property"/>
        <target action="child" type="custom" xpath="json-eval($.SamplePayload.SampleArray)"/>
    </enrich>
    <respond/>
    <log level="full"/>
</inSequence>

Sample request 

Code Block
languagexml
{
    "SamplePayload": {
        "SampleArray": [
            123.45
        ]
    }
}

Response

Code Block
languagexml
{  
   "SamplePayload":{  
      "SampleArray":[  
         123.45,
         {  
            "name":"John"
         }
      ]
   }
}

Sample 3 - Setting the property to custom as a child

Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?><proxy xmlns="http://ws.apache.org/ns/synapse" name="TestEnrich1" startOnLoad="true" statistics="disable" trace="enable" transports="http,https">
    <target>
        <inSequence>
            <property name="Greeting" scope="default" type="STRING" value="{ &quot;name&quot;:&quot;John&quot;}"/>
            <enrich>
                <source clone="true" property="Greeting" type="property"/>
                <target action="child" xpath="json-eval(SamplePayload.SampleArray)"/>
            </enrich>
            <respond/>
            <log level="full"/>
        </inSequence>
        <outSequence/>
    </target>
    <description/>
</proxy>

Sample request

Code Block
languagexml
{
    "SamplePayload": {
        "SampleArray": [
            123.45
        ]
    }
}

Response

Code Block
languagexml
{  
   "SamplePayload":{  
      "SampleArray":[  
         123.45,
         {  
            "name":"John"
         }
      ]
   }
}



Sample 4 - Setting the inline to custom as a child

Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?><proxy xmlns="http://ws.apache.org/ns/synapse" name="TestEnrich1" startOnLoad="true" statistics="disable" trace="enable" transports="http,https">
    <target>
        <inSequence>
            <enrich>
                <source clone="true" type="inline"> { "isEnrichJsonSupported": true } </source>
                <target action="child" xpath="json-eval($.SamplePayload.SampleArray)"/>
            </enrich>
            <respond/>
            <log level="full"/>
        </inSequence>
        <outSequence/>
    </target>
    <description/>
</proxy>

Sample request

Code Block
languagexml
{
    "SamplePayload": {
        "SampleArray": [
            123.45
        ]
    }
}

Response

Code Block
languagexml
{
    "SamplePayload": {
        "SampleArray": [
            123.45,
            {
                "isEnrichJsonSupported": true
            }
        ]
    }
}


Sample 5 - Setting the inline to body

Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?><proxy xmlns="http://ws.apache.org/ns/synapse" name="TestEnrich1" startOnLoad="true" statistics="disable" trace="enable" transports="http,https">
    <target>
        <inSequence>
            <enrich>
                <source clone="true" type="inline"> { "isEnrichJsonSupported": true } </source>
                <target action="child" xpath="json-eval($.SamplePayload.SampleArray)"/>
            </enrich>
            <respond/>
            <log level="full"/>
        </inSequence>
        <outSequence/>
    </target>
    <description/>
</proxy>

Sample request
Code Block
languagexml
empty {} or any JSON payload

Response

Code Block
languagexml
{  
   "Speed of light":299792458
}


Sample 6 - Setting the custom path expressions to property

Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?><proxy xmlns="http://ws.apache.org/ns/synapse" name="TestEnrich1" startOnLoad="true" statistics="disable" trace="enable" transports="http,https">
    <target>
        <inSequence>
            <enrich>
                <source clone="true" xpath="json-eval(SamplePayload.SampleArray[1])"/>
                <target property="ValueOfPi" type="property"/>
            </enrich>
            <log level="custom">
                <property expression="get-property('ValueOfPi')" name="Value of Pi : "/>
            </log>
        </inSequence>
        <outSequence/>
    </target>
    <description/>
</proxy>                                

Sample request
Code Block
languagexml
{  
   "SamplePayload":{  
      "SampleArray":[  
         1.618,
         3.14
      ]
   }
}

Response

Code Block
languagexml
Following line will appear as a log
Value of Pi :  = 3.14

Sample 7 - Remove selected parts from the payload ( this feature is available from EI 6.6.0 WUM level 1595516738094 )

Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?><proxy xmlns="http://ws.apache.org/ns/synapse" name="TestEnrich1" startOnLoad="true" statistics="disable" trace="enable" transports="http,https">
    <target>
       <inSequence>
         <enrich>
            <source clone="true" xpath="json-eval($.store.book[*].author,$.store.book[0])"/>
            <target type="body" action="remove"/>
         </enrich>
         <respond/>
      </inSequence>
      <outSequence/>
    </target>
    <description/>
</proxy>                                

Sample request

Code Block
languagexml
{
    "store": {
        "book": [
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}
                        

Response

Code Block
languagexml
{
    "store": {
        "book": [
            {
                "category": "fiction",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}


For other example using the Enrich mediator, see  Sample 15: Using the Enrich Mediator for Message Copying and Content Enrichment and Sample 440: Converting JSON to XML Using XSLT


...