Wednesday, March 1, 2017

Add failed endpoint name and address through fault sequence in WSO2 ESB

When generating custom fault sequence messages, one common use case is when you need to send the endpoint name and endpoint address of the failed endpoint back to the client. This can be done by getting the value of two properties which are 'last_endpoint' and 'ENDPOINT_PREFIX'.

However you can't get 'last_endpoint' property value directly as it sends the endpoint object. Therefore you will have to write a class mediator like below and get the value of that endpoint object and set it to a custom property that you have in your fault sequence.

 public boolean mediate(MessageContext mc) {
          // Get 'last_endpoint' property from message context
         AbstractEndpoint failedEP =(AbstractEndpoint)mc.getProperty("last_endpoint");
         // Get name of the failed endpoint
         String failedEPName =  failedEP.getName();
         // Set value to the endpoint name holder in proxy
         mc.setProperty("default_ep", failedEPName);
            return true;
        }

Now you can create your fault sequence like below.

<faultSequence>
         <property name="default_ep" value="default"/>
         <class name="org.test.EPMediator"/>
         <payloadFactory media-type="xml">
            <format>
               <tp:fault xmlns:tp="http://test.com">
                  <tp:message>Error connecting to the backend</tp:message>
                  <tp:description>Endpoint $1 with address $2 failed!</tp:description>
               </tp:fault>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('default_ep')"/>
               <arg evaluator="xml" expression="get-property('ENDPOINT_PREFIX')"/>
            </args>
         </payloadFactory>
        </send>
</faultSequence>

No comments:

Post a Comment