Friday, August 9, 2013

Defining dynamic endpoint in wso2esb

In synapse(/wso2esb), when we define endpoints, we have to provide endpoint URL, which is  most of the time  hard coded. Those endpoint urls can not be changed dynamically at runtime.
But if user likes to send requests to an endpoint which address has to be constructed from incoming request/any other way, we can use Header mediator with Property mediator to achieve this.
For instance, if the End service name has to be picked from the request, we can use property mediator to pick and store the service name. Later use the Header mediator to set "To" header using the property mediator and forward the request to default endpoint.

Eg:
The sample request is as follows( it contains the service name , which is used to find the endpoint address)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getQuote>    
         <ser:request>    
            <xsd:symbol>MSFT </xsd:symbol>  
        <xsd:service>SimpleStockQuoteService</xsd:service>
         </ser:request>
     </ser:getQuote>
   </soapenv:Body>
</soapenv:Envelope>

If we send above request to the "testProxy", which will forward the request to http://localhost:9000/services/SimpleStockQuoteService endpoint, which can be constructed as mentioned above.

testProxy conf:

  <proxy name="testProxy"    transports="https http"
          startOnLoad="true"   trace="disable">
      <description/>
      <target>
         <endpoint>
            <default/>
         </endpoint>
         <inSequence>
            <property name="server"  value="http://localhost:9000/services/"
                      scope="default"  type="STRING"/>
            <property xmlns:m1="http://services.samples/xsd"
                      xmlns:m0="http://services.samples"
                      name="service"   expression="//m0:getQuote/m0:request/m1:service"
                      scope="default"   type="STRING"/>
            <header xmlns:m1="http://services.samples/xsd"
                    xmlns:m0="http://services.samples"   name="To"
                    expression="fn:concat(get-property('server'),get-property('service'))"/>
         </inSequence>
         <outSequence>
            <send/>
         </outSequence>
      </target>
   </proxy>


You can use xpath functions in the Header/Property mediator expressions.

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Getting following WARNING while using Header Mediator:

    targetHandler Connection closed by target host before receiving the request

    Can you please suggest something else that I can use here to make SOAP request URL dynamic?

    ReplyDelete
  5. You get above issue because backend server has closed the connection after reading the request without sending a response back. Check with increased backend server connection timeout.

    In the above sample, the Header mediator sets the "To" header, where the request needs to be sent.(That is your actual backend service)

    Try invoke the backend service directly and see you can get response. Then try to troubleshoot the actual issue.

    Other way is, if you use ESB > V4.8.0, try to use HTTP endpoint.[1]

    [1]https://docs.wso2.com/display/ESB481/HTTP+Endpoint

    ReplyDelete