0

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..??

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ramandeep Singh
  • 5,063
  • 3
  • 28
  • 34

2 Answers2

1
  • 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.
David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • I think u posted this answer before also..but as i told u , it wasnt working.. :-O ..i dont know y, but it was not working in my case..!! ..Any other suggestions..?? – Ramandeep Singh Jan 16 '12 at 04:49
  • I've posted something _similar_ because it is a common pattern. So no other suggestion: I'm pretty sure this will work unless there are some important information missing in the question. – David Dossot Jan 16 '12 at 16:40
  • ok..i'll try it again..thanx..!!..will let u know if that works..!! – Ramandeep Singh Jan 18 '12 at 04:30
1

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?

hequ
  • 761
  • 6
  • 14
  • Even the first expression is working..!!..but i have to do this thing twice in one flow : One for checking valid URL's and then at the end of Flow to route it to appropriate VM...And if i have 20 flows, then my config file will be too long (with redundant code). So i am asking for a better solution..!! – Ramandeep Singh Jan 18 '12 at 21:31