Upload
anirban-sen-chowdhary
View
200
Download
6
Embed Size (px)
Citation preview
Parameters as a part of Message body
By Anirban Sen Chowdhary
Today we will be looking at how to map an inbound properties of a Mule message with the Message payload
But before we go forward let’s quickly into what is a Mule Messages and the structure of a Mule message
If we look at the Mule message format, we find that Mule message is generally divided into 3 parts as follows:-
You can see there are basically 3 parts :- 1) Properties :- Outbound or Inbound properties like our query param2) Payload :- Contains the actual message content3) Attachment :- It’s optional field may or may not contain
attachment
Image source :- Mule Soft
Mule Message
Now, in our application what ever we send as a query parameter in the browser comes into our application as inbound properties of Message.
For example if we use the following url in our browser :-http://localhost:8888/test?id=231&name=Anirban
Here we are sending 2 query parameters id=231&name=Anirban in our application which will come as an inbound properties in our application
But how can we extract the value of inbounds properties in our application??
.
We can use following MEL expression in our logger to extract the values of query parameter in our Mule application :-
#[message.inboundProperties['id']] #[message.inboundProperties['name']]
If we put the above expression in a logger :-<logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO" doc:name="Logger"/>
we will get the following values in our console :-
Now what if we want to put the query parameter as a part of Message payload and not as an Inbound properties of Message ???
Yes, you can easily map an Inbound properties of a Mule Message with Mule payload by using :-<http:body-to-parameter-map-transformer/>
This component will transform all the Inbound properties ( query parameter in our case ) as a part of actual Message payload
Now, we will see a simple example to know how the component http:body-to-parameter-map-transformer transform all the Inbound message properties into actual message payload
You can see a simple flow, where we have put http:body-to-parameter-map-transformer after Http inbound endpoint. The main task of this component will be transforming all the Inbound properties of messages into the message payload as a hash map of name-value pairs
The flow in the Mule config will be :-
<flow name="Body-To-Parameter-MapFlow1" doc:name="Body-To-Parameter-MapFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" path="test" doc:name="HTTP" /> <logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO" doc:name="Logger"/> <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/> <logger level="INFO" category="Log1" doc:name="Logger" message="#[message.payload['id']] #[message.payload['name']]"/> <logger level="INFO" category="Full Message" doc:name="Logger" message="#[message.payload]"/> </flow>
The flow in the Mule config will be :-
<flow name="Body-To-Parameter-MapFlow1" doc:name="Body-To-Parameter-MapFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" path="test" doc:name="HTTP" /> <logger message="inbound #[message.inboundProperties['id']] #[message.inboundProperties['name']]" level="INFO" doc:name="Logger"/> <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/> <logger level="INFO" category="Log1" doc:name="Logger" message="#[message.payload['id']] #[message.payload['name']]"/> <logger level="INFO" category="Full Message" doc:name="Logger" message="#[message.payload]"/> </flow>
As you can see here we have put 2 loggers to check the message payload content after http:body-to-parameter-map-transformer with the MEL expression :- #[message.payload['id']] and #[message.payload['name']]And to check full message payload :- #[message.payload]
If we check behind the scene what the http:body-to-parameter-map-transformer is actually doing :-
You can see it’s transforming message properties into payload content
Now, if we test our application and hit the following url :-http://localhost:8888/test?id=231&name=Anirban We will get the following in our console :-
You can see in the log that the query parameters are now part of actual message payload
For more information on this http:body-to-parameter-map-transformer please visit MuleSoft documentation :- http://www.mulesoft.org/documentation/display/current/HTTP+Transport+Reference
PS: As of Mule 3.6 and later, the HTTP endpoint-based connector and transport has been replaced by an operation-based HTTP connector.
Hope you enjoyed the simple yet an amazing trick in Mule.
And I wish these simple trick would be beneficial for a beginner in Mule and help him/her in learning and implementing these concepts in Mule
Thank You