Wednesday, August 26, 2015

Using Validate Mediator with source defined

In WSO2 ESB, you can use validate mediator to define your custom xsd, and validate the input you are getting. If it doesn't comply to the given xsd, you can execute a custom flow and send a fault message. Most examples available is where you don't define the source property so that the first child of the SOAP request will be evaluated. This is just a sample proxy that shows how you can add the xpath to source property.


<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="validateProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="full">
            <property name="Message" value="Inside Insequance"/>
         </log>
         <validate xmlns:nfs="http://www.example.org/testWSO2/"
                   source="//nfs:CustomerReq/Customer">
            <schema key="conf:/validateCustomer.xsd"/>
            <on-fail>
               <log level="custom">
                  <property name="MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
               </log>
               <makefault version="soap11">
                  <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
                  <reason value="Invalid request received"/>
               </makefault>
               <send/>
               <drop/>
               <property name="RESPONSE" value="true"/>
               <header name="To" expression="get-property('ReplyTo')"/>
            </on-fail>
         </validate>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>

                             
A sample request for above would be like,
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:nfs="http://www.example.org/testWSO2/">
<soapenv:Header/>
<soapenv:Body>
<nfs:CustomerReq  xmlns:nfs="http://www.example.org/testWSO2/">
  <Customer>
      <customerid>str1234</customerid>
      <amount>3.1415926535</amount>
      <officeid>str1234</officeid>
      <homeid>str1234</homeid>
    </Customer>
</nfs:CustomerReq>
</soapenv:Body>
</soapenv:Envelope> 


Finally you can add your xsd file to mentioned location in schema key. (Under browse->resources->config ->add resource)

No comments:

Post a Comment