WSO2 EI 过滤器中介中的动态正则表达式
Dynamic REGEX in filter mediator of WSO2 EI
我已经制作了过滤器调解器来检查电子邮件主题是否具有特定关键字或未使用 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 中添加新关键字而不是代码,因为关键字更改是通用的,所以可以正常工作我为什么要尝试这个。
根据过滤器中介的文档 [1],正则表达式只接受一个字符串。因此,根据这个,您将无法在过滤器调解器中动态设置正则表达式的值。
作为替代方法,您可以为此使用 class 调解器。您可以提供本地条目的内容和电子邮件主题,然后在 class 中介器中评估正则表达式。
[1]-https://docs.wso2.com/display/EI660/Filter+Mediator+
您的 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'">
解决方案:
<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>
我已经制作了过滤器调解器来检查电子邮件主题是否具有特定关键字或未使用 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 中添加新关键字而不是代码,因为关键字更改是通用的,所以可以正常工作我为什么要尝试这个。
根据过滤器中介的文档 [1],正则表达式只接受一个字符串。因此,根据这个,您将无法在过滤器调解器中动态设置正则表达式的值。
作为替代方法,您可以为此使用 class 调解器。您可以提供本地条目的内容和电子邮件主题,然后在 class 中介器中评估正则表达式。
[1]-https://docs.wso2.com/display/EI660/Filter+Mediator+
您的 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'">
解决方案:
<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>