Guravaiah P
Guravaiah P

Reputation: 31

wso2 esb - How to handle request in case of mediator returning false

I have custom mediator where we are validating internal authentication system. If authentication fails, mediator return false. Client receiving response as HTTP status code 202. This I want to override with some JSON response like { "authError" : "Authetication failed - TOKEN_INVALID" }. Please let me know how to handle such scenarios.

ESB Version - 4.81. Here is my proxy configuration -

    <?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="UGCGetJSONFileList"
       transports="https,http"
       statistics="enable"
       trace="disable"
       startOnLoad="true">
   <target outSequence="WEB_OUT_Sequence">
      <inSequence>
         <log level="full" separator=","/>
         <class name="com.hc.synapse.mediator.HCAuthenticationMediator"/>
         <property name="RESPONSE" value="true" scope="axis2"/>
         <property name="uri.var.querystrings"
                   expression="substring-after(get-property('To'), '?')"
                   scope="axis2"
                   type="STRING"/>
         <log level="full" separator=","/>
         <switch source="$axis2:HTTP_METHOD">
            <case regex="GET">
               <property name="uri.var.querystrings"
                         expression="substring-after(get-property('To'), '?')"
                         scope="default"
                         type="STRING"/>
               <log level="full" separator=","/>
               <send>
                  <endpoint>
                     <http method="get"
                           uri-template="http://dalx-entsvc-d1:9660/ugc/getJSONFileList?{uri.var.querystrings}"/>
                  </endpoint>
               </send>
            </case>
            <case regex="POST">
               <log level="full" separator=","/>
               <send>
                  <endpoint>
                     <address uri="http://dalx-entsvc-d1:9660/ugc/getJSONFileList?{uri.var.querystrings};content"
                              trace="enable"
                              statistics="enable">
                        <timeout>
                           <duration>30000</duration>
                           <responseAction>discard</responseAction>
                        </timeout>
                        <suspendOnFailure>
                           <initialDuration>0</initialDuration>
                           <progressionFactor>1.0</progressionFactor>
                           <maximumDuration>0</maximumDuration>
                        </suspendOnFailure>
                     </address>
                  </endpoint>
               </send>
            </case>
            <default/>
         </switch>
      </inSequence>
      <faultSequence>
         <log level="full" separator=",">
            <property name="trace" expression="get-property('ERROR_MESSAGE')"/>
         </log>
         <payloadFactory media-type="json">
            <format>{ "authError" : "Authetication failed - TOKEN_INVALID" }</format>
            <args/>
         </payloadFactory>
         <property name="RESPONSE" value="true"/>
         <header name="To" action="remove"/>
         <send/>
      </faultSequence>
   </target>
   <description/>
</proxy>

Upvotes: 2

Views: 1101

Answers (1)

Jean-Michel
Jean-Michel

Reputation: 5946

  • In your class mediator, you can throw a SynapseException
  • In this case, inSequence mediation stop and faultSequence will be executed
  • In the faultSequence, your can use mediator payloadFactory with media-type="json" to build your json message and send it to the client with <send/>

Upvotes: 2

Related Questions