This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Routing Requests Based on Message Content

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.

  1. 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.
  2. 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.
  3. 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.

    Add Case Branches dialog with Number of branches set to 1

  4. With the Switch mediator selected, go to the Properties tab.

  5. 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')
    Namespaced property editor showing the get-property expression
    For more information on get-property(), see XPath Extension Functions.
  6. Click in the Value field of the Case Branches property, click the browse (...) button, and then change the RegExp value to fr in case 1. Click OK.
    Screen shot of dialog box showing case 1 with RegExp fr

  7. 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)
    to:
    fn:concat('Bonjour ', //m0:getQuote/m0:request/m1:symbol)
    Screen shot showing the configuration for the Log mediator
    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.

  8. 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 fr.
    The proxy service configuration should now look like this:
  9. 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:

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

  1. In the Project Explorer, right-click the C-App (SampleCApp) and click Export Composite Application Project.
    Screen shot of the pop-up menu showing the Export Composite Application Project command

  2. Provide the name as SampleCApp, specify a destination for the exported CAR file, and click Next.
    The WSO2 Platform Distribution dialog box with the values filled in

  3. 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.
    Dialog box showing the selected artifacts for the CAR file

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

  5. Go to Carbon Applications -> Add, select the CAR file you just created, and then click Upload.
    Add Carbon Applications dialog box
  6. After the CAR is successfully uploaded, the C-App will be available in the ESB management console.
    Carbon Applications List showing SampleCApp now listed

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.

  1. 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).
    Screen shot of Request 1 window with Headers tab circled below the request XML

  2. Add a new header by clicking the + icon just above the Header/Value section.
  3. Add the header name "Request-Language" and set the value to "fr" to indicate that the client's language is French.
  4. 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.
    Response showing stock quote data for WSO2 included low, high, volume, etc.
  5. If you click the Raw tab in the request on the left, you will see the new Request-Language header you added.
  6. 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.