Use the ESBREQUEST parameter to enrich and filter Routing Services in Oracle ESB

1

Every developer must have those moments when you feel you are completely stuck with some problem. You feel like you are never going to solve the issue at hand. The attempt is doomed, you have failed. Well, let’s get a good night’s sleep, who knows what will happen. And the next day, that glorious moment when all of a sudden you see the light, you get going again and you resolve the issue. It may very well be that those moment define what really is so great about programming. Getting unstuck.

The other night, I was working with the Oracle Enterprise Service Bus, building a Domain Service that is called with the name of a specific domain of allowable values, reads data synchronously from a file that contains a great number of domains, filters the results from the file read and returns the filtered results, enriched with the name of the domain as originally requested. But I could not figure out how to filter the results from the File Adapter based on information from the Service Request. I really got stuck.

Sometimes a single remark in some article or an off-hand comment in a tutorial or a bit of information Googled up from the internet can force that breakthrough. The exact right piece of information at the exact right time. My precious piece of knowledge to get unstuck came from a Discussion Thread on the OTN Forum on Oracle Enterprise Service Bus:



Data enrichment in Oracle ESB started by Allan from Glasgow. I have to thank Allan for asking my questions for me and Alistair from Vancouver and Dave Berry from Oracle for providing the bits and pieces I was in need of. Thanks guys!

So what that incredible clue I got from this OTN thread? It is called ESBREQUEST. It is a parameter you can add (or have added) to the XSLT document used to map the reply from the service invoked by one of the Routing Rules in the Routing Service. The parameter makes the content of the Request that went into the Routing Service available during the transformation of the results. This data from the original request can be added to the results (enrichment) or used to filter the results. I will show a simple example of using the ESBREQUEST parameter.....

I have created the DomainValuesService, a very simple service that is called with a request defined as follows:

  &lt;complexType name=&quot;DomainServiceCallType&quot;&gt;<br />    &lt;sequence&gt;<br />      &lt;element name=&quot;name&quot; type=&quot;string&quot; /&gt;<br />      &lt;element name=&quot;language&quot; type=&quot;string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;<br />      &lt;element name=&quot;matchRegExpPattern&quot; type=&quot;string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;<br />      &lt;element name=&quot;likePattern&quot; type=&quot;string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; /&gt;<br />      &lt;element name=&quot;maximumNumberOfElements&quot; type=&quot;int&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;<br />    &lt;/sequence&gt;<br />  &lt;/complexType&gt; <br />

It requires the name of a domain and can also handle a language, a search/match pattern and a maximum number of elements to return. The reply returned by this Synchronous service is defined through the next XSD complexType:

&lt;complexType name=&quot;DomainServiceResponseType&quot;&gt;<br />    &lt;sequence&gt;<br />      &lt;element name=&quot;name&quot; type=&quot;string&quot; /&gt;<br />      &lt;element name=&quot;language&quot; type=&quot;string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;<br />      &lt;element name=&quot;numberOfElements&quot; type=&quot;int&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;<br />      &lt;element name=&quot;DomainValue&quot; type=&quot;ds:DomainValueType&quot; minOccurs=&quot;0&quot; /&gt;<br />    &lt;/sequence&gt;<br />  &lt;/complexType&gt;<br />  &lt;complexType name=&quot;DomainValueType&quot;&gt;<br />  &lt;sequence&gt;<br />    &lt;element name=&quot;value&quot; type=&quot;string&quot; /&gt;<br />    &lt;element name=&quot;label&quot; type=&quot;string&quot; /&gt;<br />    &lt;element name=&quot;description&quot; type=&quot;string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; /&gt;<br />    &lt;/sequence&gt;<br />  &lt;/complexType&gt;<br />&nbsp;

This specifies a reply that can hold the allowable values for a single domain – a child element DomainValue within the root element.

The domain values are being maintained in an XML file that lives on the file system. The ESB Service needs to receive the request for the values in a specific domain, read the file with all the domain, filter the results read from the file to only contain values from the domain requested, return the results in the format specified in the DomainServiceResponseType.

First of all, I create a File Adapter Service that synchronously reads the file that holds the domains. This file is specified through and XSD:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;windows-1252&quot; ?&gt;<br />&lt;schema elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;qualified&quot;<br />        targetNamespace=&quot;http://technology.amis.nl/blog/DomainService/FileBased&quot;<br />        xmlns:ds=&quot;http://technology.amis.nl/blog/DomainService/FileBased&quot;<br />        xmlns=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;<br />  &lt;element name=&quot;domains&quot; type=&quot;ds:domainsType&quot;&gt;&lt;/element&gt;<br />  &lt;complexType name=&quot;domainsType&quot;&gt;<br />    &lt;sequence&gt;<br />      &lt;element name=&quot;domain&quot; type=&quot;ds:domainType&quot; minOccurs=&quot;0&quot;<br />               maxOccurs=&quot;unbounded&quot;/&gt;<br />    &lt;/sequence&gt;<br />  &lt;/complexType&gt;<br />  &lt;complexType name=&quot;domainType&quot;&gt;<br />    &lt;sequence&gt;<br />      &lt;element name=&quot;Name&quot; type=&quot;string&quot; minOccurs=&quot;1&quot;/&gt;<br />      &lt;element name=&quot;domainValue&quot; type=&quot;ds:ValueType&quot; minOccurs=&quot;0&quot;<br />               maxOccurs=&quot;unbounded&quot;/&gt;<br />    &lt;/sequence&gt;<br />  &lt;/complexType&gt;<br />  &lt;complexType name=&quot;ValueType&quot;&gt;<br />    &lt;sequence&gt;<br />      &lt;element name=&quot;value&quot; type=&quot;string&quot;/&gt;<br />      &lt;element name=&quot;label&quot; type=&quot;string&quot;/&gt;<br />      &lt;element name=&quot;description&quot; type=&quot;string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;<br />    &lt;/sequence&gt;<br />  &lt;/complexType&gt;<br />&lt;/schema&gt; <br />

The file looks like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;<br />&lt;domains &gt;<br /> &lt;domain&gt;<br />  &lt;Name&gt;color&lt;/Name&gt;<br />  &lt;domainValue&gt;<br />   &lt;value&gt;r&lt;/value&gt;<br />   &lt;label&gt;Red&lt;/label&gt;<br />   &lt;description&gt;The beautiful red color&lt;/description&gt;<br />  &lt;/domainValue&gt;<br />  &lt;domainValue&gt;<br />   &lt;value&gt;g&lt;/value&gt;<br />   &lt;label&gt;Green&lt;/label&gt;<br />...<br /> &lt;/domain&gt;<br /> &lt;domain&gt;<br />  &lt;Name&gt;gender&lt;/Name&gt;<br />  &lt;domainValue&gt;<br />   &lt;value&gt;m&lt;/value&gt;<br />   &lt;label&gt;Male&lt;/label&gt;<br />   &lt;description&gt;Man the hunter, Marsian and all&lt;/description&gt;<br />  &lt;/domainValue&gt;<br />  &lt;domainValue&gt;<br />...<br />   &lt;/domain&gt;<br />&lt;/domains&gt;<br />&nbsp;

This is a special File Adapter Service: it reads a file yet it is considered outbound. It does not poll for files, it does not write a file. It reads one particular file – as if it were a database table!

After having set up the File Adapter Service, I create a Routing Service: it receives the Service invocation, calls the File Adapter Service to read the file and then has to transform and filter the results from the file read into the reply to be returned to the caller.

Press OK and go define the Service(s) to be invoked:


I have indicated that I want to invoke the SynchRead operation on the ReadFileDomains File Adapter Service. This service call will return the XML document with all the domains. Now I want to filter that document in order to only reply from this Routing Service with the requested domain. I need to set up the proper Transformation Map to that end:

Press the transformation icon after the Reply Transformation Map field. The following, crucial popup opens:

="0" hspace=
"0" border="0" align="bottom" src="http://technology.amis.nl/wp-content/uploads/images/esbrequest_replytransformationMap.jpg" /> 

If you check the check box Include Request in the Reply Payload you will have an extra parameter ESBREQUEST to retrieve data from during the transformation! When you look at the source of the XSLT Mapper document, it is specified as follows:

  &lt;xsl:param name=&quot;ESBREQUEST&quot;/&gt; <br />

Note that the $ESBREQUEST parameter starts at the root-element of the Request element that was sent into the Routing Service. in this example, that means that the $ESBREQUEST parameter is an XML fragment starting with a domainServiceCall element.

The esbsvc file created for the Routing Service is where the  check Include Request in the Reply Payload check box reemerges:

...<br />&lt;routingRules&gt;                                                                                                             <br />   &lt;routingRule guid=&quot;8CF28D209AF611DBBFE9995917951383&quot; subscriptionType=&quot;ANY&quot; executionType=&quot;Immediate&quot; status=&quot;ENABLED&quot;&gt; <br />      &lt;targetOperation guid=&quot;68E67E509AF611DBBFE9995917951383&quot; qname=&quot;DomainValuesService.ReadFileDomains.SynchRead&quot; servic....<br />      &lt;replyHandler&gt;                                                                                                       <br />         &lt;transformation attachRequestPayload=&quot;true&quot;&gt;                                                                      <br />            &lt;xslFileURL&gt;domains_To_domainServiceResponse.xsl&lt;/xslFileURL&gt;                                                  <br />         &lt;/transformation&gt;                                                                                                 <br />      &lt;/replyHandler&gt;                                                                                                      <br />   &lt;/routingRule&gt;                                                                                                          <br />&lt;/routingRules&gt;                                                                                                            <br />....<br /><br />

If the attachRequestPayload attribute of the transformation element is not set to true (and by default it is set to false), there will be no ESBREQUEST parameter available during the transformation. There does not seem to be a way to include the request into the payload later on: if you miss the checkbox during initial creation of the mapper file, you cannot get it back through the GUI. It seems that the best you can do is close all files in JDeveloper (even better, close the project) edit the esbsvc file for the Router Service through a text editor on the file system, refresh the files in JDeveloper (or reopen the project) and edit the mapper XSL file.

Making use of the ESBREQUEST parameter in the transformation

The importance of the ESBREQUEST parameter of course lies in what we can do with it. As it contains all information that we received in the request into the Routing Service, we can use that information for filtering or other transformation purposes. In my domains example, I want to create a Reply that only contains the requested domain. Furthermore, I want the name of the domain as requested to also appear in the reply (basically copying that value from the ESBREQUEST parameter into the reply).

The essential part of the XSL document is shown here:

  &lt;xsl:param name=&quot;ESBREQUEST&quot;/&gt;                                                                                      <br />  &lt;xsl:template match=&quot;/&quot;&gt;                                                                                            <br />    &lt;out1:domainServiceResponse&gt;                                                                                      <br />      &lt;out1:name&gt;                                                                                                     <br />        &lt;xsl:value-of select=&quot;$ESBREQUEST//out1:name&quot;/&gt;                                                               <br />      &lt;/out1:name&gt;                                                                                                    <br />    &lt;xsl:for-each select=&quot;/domains/domain[Name=$ESBREQUEST//out1:name]/domainValue&quot;&gt;                                  <br />        &lt;out1:DomainValue&gt;                                                                                            <br />          &lt;out1:value&gt;                                                                                                <br />            &lt;xsl:value-of select=&quot;value&quot;/&gt;                                                                            <br />          &lt;/out1:value&gt;                                                                                               <br />          &lt;out1:label&gt;                                                                                                <br />            &lt;xsl:value-of select=&quot;label&quot;/&gt;                                                                            <br />          &lt;/out1:label&gt;                                                                                               <br />          &lt;out1:description&gt;                                                                                          <br />            &lt;xsl:value-of select=&quot;description&quot;/&gt;                                                                      <br />          &lt;/out1:description&gt;                                                                                         <br />        &lt;/out1:DomainValue&gt;                                                                                           <br />      &lt;/xsl:for-each&gt;                                                                                                 <br />    &lt;/out1:domainServiceResponse&gt;                                                                                     <br />  &lt;/xsl:template&gt;                                                                                                     <br />&lt;/xsl:stylesheet&gt;                                                                                                     <br />&nbsp;

Note that I have left out the namespace definitions.

Using this transformation map with the filter on domains using the XPath expression [Name=$ESBREQUEST//out1:name] fueled by data from the request, I can quickly extract the domain that is requested.

In general the ESBREQUEST parameter allows me to manipulate the reply from any synchronous RouterService using the content from the request into the service. I can add information from the request to the reply as well as use the data from the request to perform filtering and other manipulation. That is really useful!

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

1 Comment

  1. Hi Lucas,

    is it also possible to write into the ESBREQUEST parameter. E.g. I send data into the routing service, lets imagine this data are email and attachment data. Inside the routing service I call a webservice to archive the attachment(s) into an archive system. This webservice sends me the unique document id back in the reply message, where I also include the ESB Request Information. Now I would like, to store this received document id into the originanl routing service request –> ESBRequest paramenter.

    After this I would hope that the routing service request is also able to use in a further routing rule, where I call another webservice to store the enriched payload into a message queue for further processing.

    Please tell me if this is possible as also how I could call the same webservice several times (in case of more than one attachment).

    Best regards,
    Peter