fb
WSO2 4 min

WSO-2-EASY: Working with properties

Rob Blaauboer
Rob Blaauboer
Integration Consultant & WSO2 Trainer
WSO2 logo
Scroll
WSO2_logo.png

As a developer you are familiar with the use of variables. You use them to store temporary information, count a number of occurrences and so on. Within the WSO2 Enterprise Server Bus (ESB) you can also use properties. Not only for the purpose as mentioned before but also to make changes to messages, e.g. switch from XML to JSON and vice versa. The property mediator that you can use does not have an impact on the message itself. 

There are a number of properties available that you can use and set. These are:

  • Generic Properties like OUT_ONLY (no response is expected)
  • HTTP Transport Properties like FORCE_SC_ACCEPTED (a 202 statuscode is generated)
  • SOAP Headers like To and From
  • Axis2 Properties like the CacheLevel for JMS objects
  • Synapse Message Context Properties like SYSTEM_DATE or MESSAGE_FORMAT

You can use these properties for instance in the MAIN sequence to log in a user friendly way when a non-existing proxy was called (the main sequence will gather all calls to the ESB where there is no proxy defined, e.g. wrong service name).  For a complete list of properties see the WSO2 documentation.

Banner-WSO2-Community-1-7

Property Mediator

The property mediator is used to set or remove properties.  Below you will find the syntax of this mediator. The type is not limited to string, but also integer, Boolean and so on. More information can be found online.

<property name="string" [action=set|remove] [type="string"] (value="literal" | expression="xpath") [scope=default|transport|axis2|axis2-client] [pattern="regex" [group="integer"]]>

    <xml-element/>?

</property>

In the example below the value of the TestValue Axis2 is retrieved and logged to the console.

<log level="custom">

<property expression="get-property('axis2','TestValue Axis2')"name="SETProperty Axis2"/>

</log>

31_dec_no_1.png

Get-property

The way to get the value of a property is with the get-property command. It takes two commands, the scope and the property name.

You define a property with a scope. The default scope is Synapse, that allows the value to be persisted as long as message handling is running within the particular message context. Such kind of properties are only visible to the current message context and do not affect other messages. A variable defined on the axis2 scope will only persist in the In, Out or Fault sequence depending on where it has been defined.

If no scope is defined, the default Synapse scope will be used.  If a scope is used where there is no property defined with that name, a null value is returned.

<?xml version="1.0" encoding="UTF-8"?>

<proxy name="TestProxy" startOnLoad="true" trace="disable"

  transports="http https" xmlns="http://ws.apache.org/ns/synapse">

  <target>

    <inSequence>

      <property description="" name="TestValue Axis2" scope="axis2"

        type="STRING" value="GOING IN"/>

      <property description="" name="TestValue Synapse"scope="default"

        type="STRING" value="GOING IN"/>

      <log level="custom">

        <property name="Value IN" value="Static value"/>

        <property expression="get-property('axis2','TestValue Axis2')"name="SETProperty Axis2"/>

        <property expression="get-property('TestValue Synapse')"name="SETProperty Synapse"/>

      </log>

      <send>

        <endpoint>

          <address trace="disable"uri="http://localhost:8280/services/echo"/>

        </endpoint>

      </send>

    </inSequence>

    <outSequence>

      <log level="custom">

        <property name="Value OUT" value="Static value"/>

        <property expression="get-property('axis2','TestValue Axis2')"name="SETProperty Axis2"/>

        <property expression="get-property('TestValue Synapse')"name="SETProperty Synapse"/>

      </log>

      <send/>

    </outSequence>

    <faultSequence/>

  </target>

</proxy>

An example

To make this clear we created a simple proxy that calls the Echo axis2 service of the ESB. We set two variables in this proxy, TestValue Axis2 and TestValue Synapse, each on the scope that is indicated by the name.

The two log statements will put the actual value of the variable to the console.

31_dec_no_2.png

As you can see, the value defined at the Synapse level is available in both the In- and Outsequence. The Axis2 defined variable is null in the Outsequence.

31_dec_no_3.png

Properties are handy to store variables as well as change the context of the message. If you want to change the content of a message, the PayloadFactory mediator needs to be used. We will cover that mediator in a future blog.

Working daily with WSO2 products we sometimes encounter things that even we did not know yet, but in fact are so handy we want to share it with the whole world. We have created a new group of articles called WSO-2-EASY that will show some of best tips and tricks that will help you to either save time or create tidier code   WSO2_logo_klein.png