Reputation: 5253
I have a mule-config file where i have defined a "http inbound" to accept request on the corresponding URL.
Now what i have to to is to accept only a valid http inbound address and reject others.
So i have applied a "choice" filter to filter out valid URL's. (like the following) :
<flow name="abc">
<http:inbound-endpoint address="http://localhost:1212/jcore/abc"
transformer-refs="HttpParams" responseTransformer-refs="JavaObjectToJson"
contentType="application/json" encoding="UTF-8">
</http:inbound-endpoint>
<component class="main.java.com.jcore.abc"/>
<choice>
<when evaluator="header"
expression="INBOUND:http.request.path=/jcore/abc/a">
<vm:outbound-endpoint path="ToSomething"/>
</when>
<when evaluator="header"
expression="INBOUND:http.request.path=/jcore/abc/b">
<vm:outbound-endpoint path="ToSomething"/>
</when>
<otherwise>
<message-properties-transformer>
<add-message-property key="http.status" value="404"/>
</message-properties-transformer>
<expression-transformer>
<return-argument evaluator="string"
expression="{"Exception": "Could not Render the Request.
URL may be wrong"}"/>
</expression-transformer>
</otherwise>
</choice>
</flow>
It is Working ..!!
But i have around 30 "Flows" like this one. And i want to apply "choice" filter like this on every flow.
Note : the matching URL will get changed in each case. Like in this case it is "/abc/a". In others , it is different
So, i wanted to know, if there is a way to avoid writing much of this redundant code and make a Spring bean with parameters OR sumthing else, that i can apply on each flow..??
Upvotes: 2
Views: 632
Reputation: 33413
I'd separate the path validation logic from the actual request handling logic, which I will make generic, configurable, and shared across flows via a flow-ref construct.
Something like this:
<flow name="abc">
<http:inbound-endpoint address="http://localhost:1212/jcore/abc"
contentType="application/json" encoding="UTF-8" />
<message-properties-transformer scope="invocation">
<add-message-property key="supported.request.paths"
value="/jcore/abc/a,/jcore/abc/b"/>
</message-properties-transformer>
<flow-ref name="request-handler" />
</flow>
<flow name="request-handler">
<script:component>
<script:script engine="groovy">
def requestPath = message.getInboundProperty('http.request.path')
def supportedPaths = message.getInvocationProperty('supported.request.paths')
def requestPathOk = supportedPaths.split(',').toList().contains(requestPath)
message.setInvocationProperty('request.path.ok', requestPathOk)
return message
</script:script>
</script:component>
<choice>
<when evaluator="header" expression="INVOCATION:request.path.ok=true">
<vm:outbound-endpoint path="ToSomething" exchange-pattern="request-response" />
</when>
<otherwise>
<message-properties-transformer>
<add-message-property key="http.status" value="404" />
</message-properties-transformer>
<expression-transformer>
<return-argument evaluator="string"
expression="{"Exception": "Could not Render the Request. URL may be wrong"}" />
</expression-transformer>
</otherwise>
</choice>
</flow>
Upvotes: 2