在mule3中,如何为路由器选择设置自定义表达式?

时间:2013-08-09 04:18:48

标签: java mule

我是Mule的新手,我想验证来自URI的timestamp和nonce参数,以防止重放攻击。我使用choice-when路由器,当timestamp和nonce有效时(验证过程很复杂),将请求转发给后端rest服务,否则返回错误代码和消息。我发现很多MEL用法可供选择 - 何时,但是我可以使用java定义自定义表达式吗?还有一个例子吗? URI示例:

http://muledemo.org/ci2/ni/del?id=0xe413&timestamp=1376022133&nonce=a03ed9c

代码段:

<choice doc:name="Choice">
  <when expression="???how to call a java validator???">
  </when>
  <otherwise>
    <processor-chain doc:name="Processor Chain">
      <echo-component doc:name="Echo" />
      <http:outbound-endpoint exchange-pattern="request-response" method="POST"
        address="http://localhost:8081#[message.inboundProperties['http.request']]" doc:name="HTTP" />
    </processor-chain>
  </otherwise>
</choice>

编辑:

XML配置:

<spring:beans>
  <spring:bean id="replayAttackCheck" class="org.jamee.demo.mule.ReplayAttackCheck"/>
</spring:beans>

... ...

<when expression="#[replayAttackCheck.validate(payload)]">
  <expression-component doc:name="Check Required Params"><![CDATA[payload="{\"code\":\"PA002\", \"message\":\"timestamp or nonce is illegal\"}"]]>
  </expression-component>
</when>

错误日志:

ERROR 2013-08-09 14:32:39,090 [[demo.router].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Execution of the expression "replayAttackCheck.validate(payload)" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: HashMap
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. [Error: unresolvable property or identifier: replayAttackCheck]
[Near : {... replayAttackCheck.validate(pay ....}]

2 个答案:

答案 0 :(得分:4)

正确的MEL语法:

<when expression="#[app.registry.replayAttackCheck.validate(payload)]">

答案 1 :(得分:0)

您只需使用完全限定的路径

#[com.mulesoft.foo(payload, someFooVar)]

或者,您可以在配置中为您的应用程序全局定义它,并在MEL表达式中使用“foo”函数

<configuration doc:name="Config">
    <expression-language autoResolveVariables="false">
        <global-functions>
            def foo(fooVar){
            return com.mulesoft.foo(payload, fooVar);
            }                
        </global-functions>
    </expression-language>
</configuration>