Friday, December 2, 2016

How to Read file Stored in Registry - WSO2 ESB

Sometimes we need to read file content in the mediation flow of WSO2 ESB.

Let's say we have a file named EndPoints.xml with the below content in the registry path of /_system/config/repository/demo as follows.

File Content :



<EndPointsList xmlns:ns1="http://endpoints">
<EP>www.google.com</EP>
<EP>www.yahoo.com</EP>
</EndPointsList>


Registry Path:



Sample Proxy:

In this proxy service, the file named EndPoints.xml is read and content is printed using log mediator.


<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestFileReadProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="EndPointList"
                   expression="get-property('registry','conf:/repository/demo/EndPoints.xml')"
                   scope="default"
                   type="OM"/>
         <foreach xmlns:nm="http://endpoints"
                  id="foreach_1"
                  expression="$ctx:EndPointList//EP">
            <sequence>
               <log level="custom">
                  <property name="EP:" expression="//EP"/>
               </log>
            </sequence>
         </foreach>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>
                                


Log Output:



TID: [-1234] [] [2016-12-02 11:58:13,964]  INFO {org.apache.synapse.mediators.builtin.LogMediator} -  EP: = www.google.com {org.apache.synapse.mediators.builtin.LogMediator}
TID: [-1234] [] [2016-12-02 11:58:13,966]  INFO {org.apache.synapse.mediators.builtin.LogMediator} -  EP: = www.yahoo.com {org.apache.synapse.mediators.builtin.LogMediator}

Thursday, December 1, 2016

WSO2 ESB 5.0 DB Configuration with ESB Analytics

Following are the databases used with ESB 5.0 and ESB analytics 5.0


  • WSO2_CARBON_DB - Local registry space which is specific to each APIM instance.
  • WSO2UM_DB - User Manager Database which stores information related to users and user roles.
  • WSO2REG_DB - Registry database which is a content store and a metadata repository for SOA artifacts
  • WSO2_ANALYTICS_EVENT_STORE_DB - Analytics Record Store which stores event definitions 
  • WSO2_ANALYTICS_PROCESSED_DATA_STORE_DB - Analytics Record Store which stores processed data
  • WSO2_METRICS_DB used to store Carbon metrics



How to use WSO2 ESB Enrich Mediator To Remove Elements from Payload.

The Enrich Mediator can process a message based on a given source configuration and then perform the specified action on the message by using the target configuration. In this example it is used to remove some elements from the message payload. In this example we need to find jsonObject elements which has jsonArray elements within that and remove the jsonObject element for them.

Sample Request:


<root:rootelement xmlns:root="www.test.com">
   <jsonObject xmlns="http://ws.apache.org/ns/synapse">
      <jsonArray>
         <jsonElement>
            <account_name>XYZ</account_name>
            <account_id>20</account_id>
         </jsonElement>
      </jsonArray>
   </jsonObject>
   <jsonObject>
      <account_name>DEF</account_name>
      <account_id>22</account_id>
   </jsonObject>
   <jsonObject xmlns="http://ws.apache.org/ns/synapse">
      <jsonArray>
         <jsonElement>
            <account_name>PQR</account_name>
            <account_id>10</account_id>
         </jsonElement>
         <jsonElement>
            <account_name>JKL</account_name>
            <account_id>11</account_id>
         </jsonElement>
         <jsonElement>
            <account_name>QWE</account_name>
            <account_id>12</account_id>
         </jsonElement>
      </jsonArray>
   </jsonObject>
   <jsonObject>
      <account_name>ABC</account_name>
      <account_id>42</account_id>
   </jsonObject>
</root:rootelement>

Sample Response:


<root:rootelement xmlns:root="www.test.com">
   <jsonArray xmlns="http://ws.apache.org/ns/synapse">
      <jsonElement>
         <account_name>XYZ</account_name>
         <account_id>20</account_id>
      </jsonElement>
   </jsonArray>
   <jsonObject>
      <account_name>DEF</account_name>
      <account_id>22</account_id>
   </jsonObject>
   <jsonArray xmlns="http://ws.apache.org/ns/synapse">
      <jsonElement>
         <account_name>PQR</account_name>
         <account_id>10</account_id>
      </jsonElement>
      <jsonElement>
         <account_name>JKL</account_name>
         <account_id>11</account_id>
      </jsonElement>
      <jsonElement>
         <account_name>QWE</account_name>
         <account_id>12</account_id>
      </jsonElement>
   </jsonArray>
   <jsonObject>
      <account_name>ABC</account_name>
      <account_id>42</account_id>
   </jsonObject>
</root:rootelement>


Example Proxy Service:

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestXPath"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <foreach expression="//*[local-name()='jsonObject']">
            <sequence>
               <filter xpath="boolean(//*[local-name()='jsonObject']/*[name()='jsonArray'])">
                  <then>
                     <enrich>
                        <source clone="true" xpath="//*[local-name()='jsonArray']"/>
                        <target type="body"/>
                     </enrich>
                  </then>
               </filter>
            </sequence>
         </foreach>
         <respond/>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>