Skip to main content

WSO2 ESB properties tutorial

WSO2 ESB provides properties as a way to control different aspects of the messages flowing through the mediation engine. They will not change the content (payload) of the message but they will be used to change the behavior of the message flowing through the ESB. Property mediator is used to access or modify the properties defined in the WSO2 ESB.

https://docs.wso2.org/display/ESB481/Property+Mediator

You can define a property mediator inside the ESB configuration language as below if you are setting a static value for the property.

<property name="TestProperty" value="Chanaka" scope="default" type="STRING">

If you are setting a dynamic value for the property, then you can use the following method.

<property name="TestProperty" expression="//m0:getQuote/m0:request/m0:symbol"

    xmlns:m0="http://services.samples/xsd"  scope="default" type="STRING">

In the above property declaration, you can find that we are defining a scope for the property. This scope definition will define the scope where this property can be visible inside the ESB.

1. default - or the Synapse
Once you set a property under this scope - the value of it will be available throughout both the in/out sequences.

2. axis2
Once you set a property under this scope - the value of it will be available only throughout the sequence it's been set. If you set the Property mediator to the in-sequence, you cannot access it in the out-sequence.

3. axis2-client
This is similar to Synapse scope. The difference is - you can access it in following two ways inside a custom mediator.

public boolean mediate(org.apache.synapse.MessageContext mc) {
org.apache.axis2.context.MessageContext axis2MsgContext;
axis2MsgContext = ((Axis2MessageContext) mc).getAxis2MessageContext();
String propValue = (String) axis2MsgContext.getProperty("PropName");
System.out.println("SCOPE_AXIS2_CLIENT - 1 : " + propValue);

propValue = (String) axis2MsgContext.getOptions().getProperty("PropName");
System.out.println("SCOPE_AXIS2_CLIENT - 2: " + propValue);
return true;
}

4. transport
Once you set a property under this scope - it will be added to the transport header of the out going message from the ESB.

Now we know how to define properties at different scopes. Let's see what are the properties available in the WSO2 ESB. You can find a good reference about properties from the link below.

https://docs.wso2.org/display/ESB481/Generic+Properties

In the above reference, you can find a descriptive information about most of the properties. Here is an example of using the "messageType" property.

messageType

Name
messageType
Possible Values
string
Default Behavior
Content type of incoming request.
Scope
axis2
Description
Message formatter is selected based on this property. This property should have the content type, such as text/xml, application/xml, or application/json.
Example
<property name="messageType" value="text/xml" scope="axis2"/>

Let's say you have need convert the Content-Type of your message from application/xml to application/json. Then you can use this property to achieve the same as below.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="ToJSON"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="messageType" value="application/json" scope="axis2"/>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>


To test this property, send a request to this proxy service like below.

curl -v -X POST -H "Content-Type:application/xml" -d@request1.xml "http://localhost:8280/services/tojson"

where your request1.xml is like below.

<coordinates>
   <location>
       <name>Bermuda Triangle</name>
       <n>25.0000</n>
       <w>71.0000</w>
   </location>
   <location>
       <name>Eiffel Tower</name>
       <n>48.8582</n>
       <e>2.2945</e>
   </location>
</coordinates>


You can get the response from the proxy service as below.


{"coordinates":{"location":[{"name":"Bermuda Triangle","n":25.0000,"w":71.0000},{"name":"Eiffel Tower","n":48.8582,"e":2.2945}]}}

Here you can see that the Content-Type of the message is converted to application/json.





Comments

Popular posts from this blog

How to protect your APIs with self contained access token (JWT) using WSO2 API Manager and WSO2 Identity Server

In a typical enterprise information system, there is a high chance that people will use different types of systems built by different vendors to implement certain types of functionalities. The APIs might be hosted in an API Manager developed by vendor A and the user management can be implemented using a different vendor (vendor B). In this type of a situation, one system will not be able to directly contact the other system but they want to use both systems in tandem. Self-contained access tokens are used in these types of situations where applications can get the token from one system and use that in another system to access protected resources. In this scenario, the second system does not need to make a contact to the first system over the network to validate the user information since the token is self-contained and it has relevant details about the user. This will improve the token processing time significantly since it completely removes the network interaction. The below fig...

WSO2 ESB creating a response for a "GET" request

When you are creating REST APIs with WSO2 ESB, you may need to send some response message back to the user when something goes wrong. In this kind of scenario, you can create a payload inside the ESB and send it back. For doing this, you can use the below configuration. <api xmlns=" http://ws.apache.org/ ns/synapse " name="LoopBackProxy" context="/loopback">    <resource methods="POST GET">       <inSequence>          <property name="NO_ENTITY_BODY" scope="axis2" action="remove"></property>          <log level="full"></log>          <payloadFactory media-type="xml">             <format>                <m:messageBeforeLoopBack xmlns:m=" http://services...

WSO2 ESB usage of get-property function

What are Properties? WSO2 has a huge set of mediators but property mediator is mostly used mediator for writing any proxy service or API. Property mediator is used to store any value or xml fragment temporarily during life cycle of a thread for any service or API. We can compare “Property” mediator with “Variable” in any other traditional programming languages (Like: C, C++, Java, .Net etc). There are few properties those are used/maintained by ESB itself and on the other hand few properties can be defined by users (programmers). In other words, we can say that properties can be define in below 2 categories: ESB Defined Properties User Defined Properties. These properties can be stored/defined in different scopes, like: Transport Synapse or Default Axis2 Registry System Operation Generally, these properties are read by get-properties() function. This function can be invoked with below 2 variations. get-property(String propertyName) get-property(String scop...