繁体   English   中英

WSO2 EI 的过滤器中介中的动态正则表达式

[英]Dynamic REGEX in filter mediator of WSO2 EI

我已经让过滤器调解器检查 email 主题是否具有特定关键字或不使用 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>

在我的案例中,需要大量关键字(超过 60 个)。 我在代码中有硬编码的关键字,而不是这个,我试图将这些关键字存储在某个地方(例如 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>

从 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"/>

我无法使用上述属性 (SR) 来匹配 Filter Mediator 中的主题。

有什么办法可以实现我的用例吗?

PS:将来可能会添加新关键字,为了避免在需要更改关键字时更改代码级别,我只是在 localentry 中添加新关键字而不是代码,因为关键字更改是通用的,所以可以正常工作,这就是我尝试这个的原因。

您的localEntry为 XML 将不起作用,因为它以. (点)和 wstx 解析器将抛出错误。 改用LocalEntry作为Text

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

要将其用作正则表达式,您需要使用ScriptMediator ,如下所示:

<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>
      

您可以在FilterMediator中使用该testResult

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

根据过滤器中介的文档 [1],正则表达式只接受一个字符串。 因此,根据这个,您将无法在过滤器调解器中动态设置正则表达式的值。

作为替代方法,您可以为此使用 class 调解器。 您可以提供本地条目的内容和 email 主题,然后评估 class 中介中的正则表达式。

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

解决方案:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM