In this scenario, we will look at how messages are mediated within WSO2 ESB based on the message content. The messages that are sent by the client to WSO2 ESB will contain a
Request_Language property in the message header indicating whether the message is in French or English. If the request message language of the customer is French, the Log mediator prints “Bonjour WSO2”, and if the request message language is English, it will print “Hello WSO2” as the message.
For more details on how routing of messages within WSO2 ESB is done based on the message content, refer to Content-Based Router Enterprise Integration Pattern.
This scenario contains the following sections:
Updating the proxy service
To implement the routing scenario, we will update the proxy service we created in the previous section and use a Switch mediator to route the message to the relevant Log mediator based on the value received in the header of the request message. The Switch mediator uses a filter that returns a String. The String is matched against a regular expression that is specified in the Case statement, and if it matches, the mediator within Case will be executed. If there are no matches, the mediators within Default will be executed.
Let’s update the proxy service we created in the last lesson in WSO2 Developer Studio.
- In the proxy service configuration, right-click the Request Log mediator and choose Delete from Model. We will be adding two new Log mediators after we add the Switch mediator.
- In the Mediators palette, scroll down to the Switch mediator and drag it to the start of the In sequence to enable routing of the request message based on the header contents.
Right-click the Switch mediator you just added and select Add/Remove Case to add the number of cases you want to specify. In this scenario, we are adding one case, so enter 1 and click OK.
With the Switch mediator selected, go to the Properties tab.
The Source Xpath field is where we will specify the XPath expression that obtains the value of Request-Language in the header of the request message. To specify the expression, click in the Value field of the Source Xpath property, click the browse (...) button, and then overwrite the default expression with the following and click OK:
get-property('transport' , 'Request-Language')
For more information on
get-property(), see XPath Extension Functions.
Click in the Value field of the Case Branches property, click the browse (...) button, and then change the RegExp value to
frin case 1. Click OK.
Drag a Log mediator to the Case (the top box) of the Switch mediator, name it "French Message", right-click it and choose Configure, and then enter the same properties you did for the Log mediator in the last lesson as described in Configuring the In sequence, with the exception of changing its Value/Expression from:
fn:concat('Welcome ', //m0:getQuote/m0:request/m1:symbol)
fn:concat('Bonjour ', //m0:getQuote/m0:request/m1:symbol)You have now configured the Switch mediator so that when a request is sent to this proxy service, if its Request_Language header property is set to
fr, the message "Bonjour, <Stock Company Name>" will be logged.
- Add a Log mediator to the Default (the bottom box) of the Switch mediator and configure it the same way you did the French Message Log mediator, this time naming it "Welcome Message" and changing its Value/Expression to say "Hello" instead of "Welcome". This causes the message "Hello <Stock Company Name>" to be logged for requests whose Request_Language header property is not set to
The proxy service configuration should now look like this:
- Click the Save icon (or choose File -> Save) to save the updated proxy service configuration.
Now that we have modified and saved the proxy service configuration, we must redeploy the C-App to WSO2 ESB so that the changes will be updated on the ESB server.
Redeploying the C-App to WSO2 ESB
Developer Studio provides a simple method for redeploying C-Apps to the ESB:
- On the Servers tab, expand the WSO2 Carbon server, right-click SampleCApp, and choose Redeploy. The Console window will indicate that the C-App has been deployed successfully.
In some versions of Developer Studio, the Redeploy command did not work on all operating systems. If you encounter problems redeploying, redeploy the artifacts using a CAR file instead, as described in the following section.
Deploying artifacts using a CAR file
A Composite Application Archive (CAR) file is a deployable C-App file. To create a CAR file, take the following steps.
In the Project Explorer, right-click the C-App (SampleCApp) and click Export Composite Application Project.
Provide the name as SampleCApp, specify a destination for the exported CAR file, and click Next.
The following window displays the projects that are included in the CAR. Ensure that the SampleServices ESB project is selected and click Finish. The CAR will now be created in the specified location.
In your Web browser, navigate to the WSO2 ESB management console at https://localhost:9443/carbon/ and log into the management console using the admin/admin credentials.
- Go to Carbon Applications -> Add, select the CAR file you just created, and then click Upload.
After the CAR is successfully uploaded, the C-App will be available in the ESB management console.
Updating and sending requests to SimpleStockQuoteProxy
Now that we have updated and redeployed SimpleStockQuoteProxy, we must add a header to our request to provide the Request-Language for the request message.
In SoapUI, click the Headers tab at the bottom of the Request 1 window (make sure you are clicking the Headers tab in the request, not the response).
- Add a new header by clicking the + icon just above the Header/Value section.
- Add the header name "Request-Language" and set the value to "fr" to indicate that the client's language is French.
- Click the green arrow at the top of the Request 1 window. The request will be sent and the response will be received as follows.
- If you click the Raw tab in the request on the left, you will see the new Request-Language header you added.
Check the Console in Eclipse to see the log message “Bonjour WSO2”. If you change the Request-Language to any other value and send a new request, “Hello WSO2” will be logged instead.