You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSO2 EI的Filter Mediator中如何使用LocalEntry存储的动态正则匹配邮件主题

问题原因

WSO2 EI/ESB 内置的 Filter Mediator 的 regex 属性仅支持静态硬编码值,无法直接读取上下文的动态属性作为正则规则,这是 Synapse 组件的原生限制。

可行解决方案

方案1:通过 Script Mediator 完成动态正则匹配(推荐)

该方案兼容性最好,逻辑灵活,你已经正确读取到 SR 属性的正则字符串,只需要新增一段脚本判断匹配结果,再用 Filter 判断结果标识即可:

<!-- 你之前读取正则的配置保持不变 -->
<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"/>

<!-- 新增脚本判断匹配结果 -->
<script language="js"><![CDATA[
    var emailSubject = mc.getProperty("emailSubject");
    var regexStr = mc.getProperty("SR");
    var matchResult = new RegExp(regexStr).test(emailSubject);
    mc.setProperty("isKeywordMatch", matchResult);
]]></script>

<!-- 用Filter判断匹配结果 -->
<filter xpath="$ctx:isKeywordMatch = 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>

方案2:使用 XPath matches 函数直接在 Filter 中判断

如果不想引入脚本,也可以直接用 XPath 原生的正则匹配函数,无需额外组件:

<!-- 读取正则的配置保持不变 -->
<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"/>

<!-- 直接在Filter中用XPath matches函数做匹配 -->
<filter xpath="matches(get-property('emailSubject'), $ctx:SR)">
    <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>

优化建议

你可以把 LocalEntry 改成纯文本格式,不需要封装 XML 节点,省去 XML 解析的步骤:

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

读取的时候直接一行配置就能拿到正则串:

<property expression="get-property('EmailTicketing_Keyword')" name="SR" scope="default" type="STRING"/>

内容的提问来源于stack exchange,提问作者Justin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 22:48:03