How to check for valid URL's and route it to VM at the end of Flow in MULE..?

1.3k views Asked by At

I have a flow in Mule. It contains an HTTP Inbound listening to a Port number and an Address. Now according to the address of the HTTP Inbound I have to route it to another VM.

This part I have done like below :

    <flow name="MetaService">
        <http:inbound-endpoint address="http://localhost:8000/jcore/meta"  
    transformer-refs="HttpParams" responseTransformer-refs="JavaObjectToJson">
        </http:inbound-endpoint>

        <component>
               <spring-object bean="MetaServiceBean"/>
        </component>
        
        <choice>
            <when evaluator="header" expression="INBOUND:http.request.path=/jcore/meta">
                <vm:outbound-endpoint path="ToJSON" exchange-pattern="request-response"/>
            </when>
             <when evaluator="header" expression="INBOUND:http.request.path=/jcore/meta.json">
                <vm:outbound-endpoint path="ToJSON" exchange-pattern="request-response"/>
            </when>
            <when evaluator="header" expression="INBOUND:http.request.path=/jcore/meta.xml">
                <vm:outbound-endpoint path="ToXML" 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="{&quot;Exception&quot;: &quot;Could not Render the Request. URL may be wrong&quot;}"/>
                </expression-transformer>
            </otherwise>
        </choice>
    </flow>

What happens is, if there is ".json" OR ".xml" at the end of the Address, then I am routing it to a VM and in case of invalid URL's I am raising a HTTP 404 Error..


But the question is : I have to check the Valid / Invalid URL's at the start of the Flow , and not at the end.. And also I have to route them at the end (acc to URL's as shown)..!!

I can use the choice component at the starting also , but then it would be redundant..!!

Is there any good option..??

2

There are 2 answers

3
David Dossot On
  • Use a message-properties-transformer right after your inbound HTTP endpoint to add a new invocation-scoped property.
  • In this transformer, use a Groovy expression to give this property a value of "json", "xml" or "error" based on the inbound property "http.request.path".
  • Then in your choice router, just base your routing on this invocation property.
1
hequ On

I can't see why the solution presented in the starting post would not work?

If you just change your choices from:

<when evaluator="header" expression="INBOUND:http.request.path=/jcore/meta">

to

<when evaluator="header" expression="INBOUND:http.request.path == /jcore/meta">

then these should evaluate to true or false.

Or Am I missing something here?