Search code examples
wso2wso2-api-manager

How to define regex condition in switch case in WSO2 sequence?


I defined a sequence which have Switch...Case mediator and check the condition based on RegEx in WSO2. what is wrong in my below definition ? It supposed to execute first case condition. But, log prints always default section of the switch. Below code sample, when i print PATH_PROP, returns "/rest/api/v1.0/fee" which is my resource path in the URL. I am trying to match, is there word 'fee' in PATH_PROP variable. In my case, it is.. so, i expected go Case 1. i tried RegEx of "fee", ".fee.", "\bfee\b" and etc. None of them work. What is wrong in my definition ?
Your help is appreciated !!

<property expression="get-property('axis2','REST_URL_POSTFIX')"
name="PATH_PROP" scope="default" type="STRING"/>
<switch source="$ctx:PATH_PROP">
<case regex="fee">
  <log level="custom">
    <property name="CUSTOM_MESSAGE" value="CASE 1 *************"/>
  </log>
  <header name="To" scope="default" value="https://localhost:9448/am/sample/pizzashack/v1/api/menu"/>
</case>
<case regex=".*application/xml.*">
  <property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
</case>
<default>
  <property name="messageType" scope="axis2" type="STRING" value="text/xml"/>
  <log level="custom">
    <property name="CUSTOM_MESSAGE" value="CASE_DEFAULT *************"/>
  </log>
  <header name="To" scope="default" value="http://was.apv.local:9080/rpe/rest/api/v1.0/fee?appID=1"/>
</default>


Solution

  • user regex .*fee.* to match the word "fee" in the url. if you wan to match "/fee" then use .*/fee.*

    e.g to match "fee" in $ctx:PATH_PROP

    <switch source="$ctx:PATH_PROP">
    <case regex=".*fee.*">
    ------------------
    </case>
    <default>
    ------
    </default>