Justin
Justin

Reputation: 987

Dynamic REGEX in filter mediator of WSO2 EI

I have made Filter mediator to check email subject has specific keyword or not using REGEX.

<property value="Test SR AWS onboarding of AWS server" name="emailSubject" scope="default" type="STRING" />

<filter regex=".*SRAWS.*|.*SR AWS.*|.*SRSAP.*|.*SR SAP.*|.*SRFW.*|.*SR FW.*|.*SRSEC.*|.*SR SEC.*|.*INAWS.*|.*INSAP.*|.*INFW.*|.*INSEC.*"
    source="get-property('emailSubject')">

    <then>
        <log level="custom">
            <property name="==Test Case ===" value="pass" />
        </log>

    </then>
    <else>
        <log level="custom">
            <property name="==Test Case ===" value="Fail" />
        </log>
    </else>
</filter>

Plenty of keywords(more than 60) are required in my case. I have hard coded keyword in Code, Instead of this, i am trying to store these keyword in somewhere(eg. localentry) and try to match subject with this to make code as generic.

Localentry:

    <?xml version="1.0" encoding="UTF-8"?>
<localEntry key="EmailTicketing_Keyword" xmlns="http://ws.apache.org/ns/synapse">
    <SR>.*SRAWS.*|.*SR AWS.*|.*SRSAP.*|.*SR SAP.*|.*SRFW.*|.*SR FW.*|.*SRSEC.*|.*SR SEC.*</SR>
</localEntry>

Reading from Localentry:

<property expression="get-property('EmailTicketing_Keyword')" name="tokenconfig" scope="default" type="OM"/>
<property expression="$ctx:tokenconfig//*[local-name()='SR']" name="SR" scope="default" type="STRING"/>

I am unable to use above property( SR) to match with subject in Filter Mediator.

Is there any way to achieve my use case?

PS: new Keyword may be added in future, to avoid code level changes whenever key word change required i just add new keyword in localentry instead of code which will work fine since keyword change is generic,That's why i am trying this.

Upvotes: 0

Views: 1491

Answers (3)

Justin
Justin

Reputation: 987

Solution:

<script language="js"><![CDATA[var log = mc.getServiceLog();
                      log.info("===Inside keyword Match Script Mediator===" );  
                       var emailSubject = mc.getProperty('transemailSubject').toString();
                       log.info("EmailSub::" + emailSubject);
                                            
                      var keywordSR = mc.getProperty('SR').toString();
                      //log.info("SR Keywords::" + keywordSR);
                      var regExpSR = new RegExp(keywordSR); 
                      mc.setProperty('SR_Match',regExpSR.test(emailSubject).toString());
                     log.info("SR_Match::" + mc.getProperty('SR_Match'));]]></script>
                     
                    <filter xpath="$ctx:SR_Match='true'">
                        <then>
                            <log level="custom">
                                <property name="==Test Case ===" value="pass" />
                            </log>

                        </then>
                        <else>
                            <log level="custom">
                                <property name="==Test Case ===" value="Fail" />
                            </log>
                        </else>
                    </filter>

Upvotes: 0

tmoasz
tmoasz

Reputation: 1340

Your localEntry as XML wont work, because it starts as . (dot) and wstx parser will throw error. Use instead LocalEntry as Text:

<localEntry xmlns="http://ws.apache.org/ns/synapse" key="RegTicketing">
.*SRAWS.*|.*SR AWS.*|.*SRSAP.*|.*SR SAP.*|.*SRFW.*|.*SR FW.*|.*SRSEC.*|.*SR 
 SEC.*
</localEntry>

For using that as regexp You need use ScriptMediator as below:

<property name="tokenconfig" expression="get-property('RegTicketing')" scope="default" type="STRING"/>
<script language="js">
  var regStr = mc.getProperty('tokenconfig').toString();
  var testStr = mc.getProperty('emailSubject').toString();
  var regExp = new RegExp(regStr);                   
  mc.setProperty('testResult',regExp.test(testStr).toString());
</script>
      

And you can use that testResult in FilterMediator:

<filter xpath="$ctx:testResult='true'">

Upvotes: 1

Dilan Premarathna
Dilan Premarathna

Reputation: 1294

As per the documentation [1] of the filter mediator, the regex only accepts a string. Therefore according to this you will not be able to dynamically set the value of the regex expression in the filter mediator.

As an alternative approach you can use a class mediator for this. You can feed the content of the local entry and the email subject, then evaluate the regex expression within the class mediator.

[1]-https://docs.wso2.com/display/EI660/Filter+Mediator+

Upvotes: 0

Related Questions