Tuesday, March 12, 2013

Accessing various information of a message in a mediation flow in WSO2ESB

Property mediator helps us to access various information of a message ,which passes through ESB. Within a sequence , we could use property mediator to get info about a message.
We can assign a value to a property as a string value or using xpath expression. Two actions are defined for property mediator.
  • Set
  • Remove
eg:
 <property name="MESSAGE" value="Executing in sequence"/>
 <property xmlns:m="http://sample.com"   name="test"   expression="//m:getQuote/m:symbol"
                   scope="default"  type="STRING"/>

We can use supported xpath functions,such as mathematical/logical operations and other predefined functions can be used to assign values in the property mediator.

eg: 

<property xmlns:ns="http://org.apache.synapse/xsd"   name="testProp"
                expression="fn:concat('hello','world')"   scope="default"/>

We can use  built in get-property() xpath function to read properties,which are available in the synapse messagecontext.
Eg: If a property "SYSTEM_TIME" available in the synapse message context; 

<property xmlns:ns="http://org.apache.synapse/xsd"   name="systemtime"
                expression="get-property('SYATEM_TIME')"   scope="default"/>


We can use property mediator to set values in different scopes, which can be accessed in those specific scope. There are 4 scopes,which are;
  • synapse/default : This property will be available in both insequence and outsequence.
  • axis2 : This property will be available in the particular sequence. can be retrieved from Axsi2Messagecontext.
  • transport:  This scope is used to set a property in the transport header.
  • axis2-client: Like "axis2" scope, but can be  retrieved form the Axis2Messagecontext.options.
eg:

<property name="myProp" value="abc" scope="default" type="STRING"/>
<property name="myProp" value="abc" scope="transport" type="STRING"/>
<property name="myProp" value="abc" scope="axis2" type="STRING"/> 
<property name="myProp" value="abc" scope="axis2-client" type="STRING"/>

When we retrieve properties from a specific scope;
eg:
 <property xmlns:ns="http://org.apache.synapse/xsd"    name="remotehost"
                    expression="get-property('axis2', 'REMOTE_HOST')"/>

There are some predefined xpath properties , which we can use directly to retrieve some common properties.
  • $axis2: Used to get the property at the axis2 scope.
 eg :
 <property name="MessageType is" expression="$axis2:messageType"/>
  • $trp: Used to get transport header properties.
eg:
 <property name="axis2Prop" expression="$trp:Content-Type"/>
  • $ctx : Used to get synapse scope properties;
eg:
 If we have a property named as "myProp" in synapse scope, which can be retrieved in the following manner;
 <property name="testProp" expression="$ctx:myProp"/>
  • $url : Used to retrieve parameters defined in the url
  eg:
For a request url; http:// localhost:8280/getSimpleQuote?symbol=IBM

<property name="symbol" expression="$url:symbol"/>
The above will retrieve the symbol value; ie:IBM

6 comments:

  1. Hi,

    How can i make property store multilple value i.e. array of values?
    My request has multiple topics i.e.

    a
    b
    to store this topic i am writing property mediator as

    But it's not working:(

    ReplyDelete
  2. You can not store arrays in property mediator..you can store them as string and when you try to retrieve the values, you have to do string manipulation..

    ReplyDelete
  3. Hello, Thanks for your helpful post. I am trying to parse property values from an inbound endpoint to the called sequence. Is this possible and how does that work? I tried all scope settings when using the property mediator in the inbound endbound, but I cannot retrieve them in the sequence. Can you explain if and then how this works?
    Thank you.
    Cheers, Ronald

    ReplyDelete
  4. Hello, Thanks for your helpful post. I am trying to parse property values from an inbound endpoint to the called sequence. Is this possible and how does that work? I tried all scope settings when using the property mediator in the inbound endbound, but I cannot retrieve them in the sequence. Can you explain if and then how this works?
    Thank you.
    Cheers, Ronald

    ReplyDelete
  5. Hello, Thanks for your helpful post. I am trying to parse property values from an inbound endpoint to the called sequence. Is this possible and how does that work? I tried all scope settings when using the property mediator in the inbound endbound, but I cannot retrieve them in the sequence. Can you explain if and then how this works?
    Thank you.
    Cheers, Ronald

    ReplyDelete
  6. Hi, I read an xml file and stored it in a sequence and exposed as an API, no I want to send the sequence as an input to other endpoint, can you please help me on how to do that.

    ReplyDelete