2

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>
Community
  • 1
  • 1

1 Answers1

2
  • 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/>
Jean-Michel
  • 5,926
  • 1
  • 13
  • 19
  • Thank you for your response. I made change as above and now I am getting 200 http response. But not getting any message on browser. Verified thru log. But no luck. – Guravaiah P Dec 02 '14 at 11:11