AndreaNobili
AndreaNobili

Reputation: 43047

What expression have I to use to perform a choice related to a property value in a WSO2 ESB filter mediator?

I am very new in WSO2 ESB and I have the following doubt about how to implement an "if(){...} else{...}" like structure in my ESB project.

So in the input flow of the application on which I am working I have this property mediator followed by a log mediator that simply print the value of this property, something like this:

<property expression="count(//ds:Sample)" name="total_samples" scope="default" type="STRING" xmlns:ds="http://ws.wso2.org/dataservice"/>

<log level="custom">
    <property expression="$ctx:total_samples" name="total samples: "/>
</log>

This works fine.

This total_samples property contains the number of record obtained from a previous call of a DSS service (I am not putting here in the code).

So the value of this total_samples property could be:

Now what I need to do at this time is only to chain a n "if(){...} else{...}" structure that print different log message if the total_samples property value is 0 or whatever number >0.

It should be a ver simple task but I have some doubts about how achieve it:

FIRST DOUBT: Looking on the online documentation it seems to me that exists 2 mediator that can be used to perform choice in the WSB flow: the switch mediator and the filter mediator. They seems to me very similar. What are the difference between these mediators? And what is better for my purpose?

SECOND DOUBT: It seems to me that these mediators works only on XPATH expression (something like count(//ds:Sample)), can they work directly on my property (something like "$ctx:total_samples") ?

THIRD DOUBT: At this stage I have implemented something like this in my flow:

<property expression="count(//ds:Sample)" name="total_samples" scope="default" type="STRING" xmlns:ds="http://ws.wso2.org/dataservice"/>

<log level="custom">
    <property expression="$ctx:total_samples" name="total samples: "/>
</log>

<filter xpath="EXPRESSION THAT DO SOMETHING LIKE: $ctx:total_samples == 0">
    <then>
        <log description="No Resource Log">
            <property name="message" value="&quot;EMPTY RESULTSET, NO RESOURCES TO PROCESS&quot;"/>
        </log>
    </then>
    <else>
        <log description="Found Resource Log">
            <property name="message" value="&quot;Resources have been found, will be processed&quot;"/>
        </log>
    </else>
</filter>

Ok so my problem is: What have I to use as expression to enter in the case if the $ctx:total_samples value is 0 in the following line?

<filter xpath="EXPRESSION THAT DO SOMETHING LIKE: $ctx:total_samples == 0">

Upvotes: 0

Views: 1339

Answers (3)

ophychius
ophychius

Reputation: 2653

you are really asking three questions here so I'll try to answer all of them:

  1. The Switch mediator allows for multiple cases, so for example you could have a case for count = 0, count = 1 and count > 1. The filter mediator on the other hand is like the classic if/else. If this than do x, else do .

  2. The filter mediator can either work comparing some value with a regular expression using the 'source' and 'regex' attributes in which case it checks if they match. Or it uses the xpath attribute in which case it evaluates the result of the xpath expression as a boolean. In the xpath expression as well as the source you can refer directly to your property using $ctx. For example:

  3. What you could do is

    <filter xpath="fn:number($ctx:total_samples) = fn:number(0)">

Upvotes: 0

Jorge Infante Osorio
Jorge Infante Osorio

Reputation: 2153

A more generic solution:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="testIfElse"
       transports="https http"
       startOnLoad="true">
   <target>
      <inSequence>
            <payloadFactory media-type="xml">
              <format>
                <ds:Sample xmlns:ds="http://ws.wso2.org/dataservice">
                  <ds:INT_ID>1</ds:INT_ID>
                  <ds:INT_ID>2</ds:INT_ID>
                  <ds:INT_ID>3</ds:INT_ID>
                </ds:Sample>
              </format>
              <args>
              </args>
            </payloadFactory>     
        <property expression="count(//ds:Sample/ds:INT_ID)" name="total_samples" scope="default" xmlns:ds="http://ws.wso2.org/dataservice" type="DOUBLE"/>
        <property value="0" name="initial_value" scope="default" type="DOUBLE"/>
        <property expression="fn:number($ctx:total_samples) &gt; fn:number($ctx:initial_value)" name="result" scope="default"/>

        <log level="custom">
            <property expression="$ctx:initial_value" name="initial value: "/>
            <property expression="fn:number($ctx:total_samples)" name="total samples: "/>
            <property expression="$ctx:result" name="if total samples greater than initial value:  "/>
        </log>

        <filter xpath="$ctx:result" regex="true">
            <then>
                <log description="Found Resource Log">
                    <property name="message" value="&quot;Resources have been found, will be processed&quot;"/>
                </log>

            </then>
            <else>
                <log description="No Resource Log">
                    <property name="message" value="&quot;EMPTY RESULTSET, NO RESOURCES TO PROCESS&quot;"/>
                </log>
            </else>
        </filter>
      </inSequence>
      <outSequence>
           <log level="full"/>  
        <drop/>
      </outSequence>
      <faultSequence/>
   </target>
</proxy>

Upvotes: 3

alber arce
alber arce

Reputation: 371

Use this expression

<filter xpath="fn:number(get-property('total_samples')) = fn:number(0)">

Upvotes: 2

Related Questions