Validating XML messages against more than one XSD with WSO2 ESB Validate mediator

Request validation is one of the important feature of any ESB. If you do not validate the request, it will go through your system and make unnecessary traffic on your resources. If you could validate the requests at the beginning of your message flow, that would help you to respond quickly and avoid resource utilization for wrong requests.

WSO2 ESB is the world’s fastest one most comprehensive open source ESB available in the market. It is driven by the award winning WSO2 Carbon platform which you can use for any of your SOA implementations.

WSO2 ESB provides an OOTB (Out Of The Box) feature for request validation. This is called the Validate Mediator. This will provide you the capability to validate your request against any number of XSD schemas. If you are validating the request against a single XSD file, you can refer the below blog post written by Amani.

http://sparkletechthoughts.blogspot.com/2012/09/how-to-use-validate-mediator-to.html

In this blog post, I am going to discuss about a bit complex scenario where you have more than one XSD file to validate against. In this scenario, you have a XSD file A which has a reference to another XSD file B. In this kind of scenario, you need to take additional care when you implement your scenario.

1. Create your XSD file clear and make the references correct.

This is the main XSD(HelloSchema.xsd) file which we are validating the request against. This XSD has a reference to another XSD file(Hello.xsd)

HelloSchema.xsd

===============

<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema&#8221;

xmlns:q1=”http://www.wso2.org/hello&#8221;

xmlns:ns1=”http://org.apache.axis2/xsd&#8221; xmlns:ns=”http://www.wso2.org/types&#8221;

attributeFormDefault=”qualified” elementFormDefault=”qualified”

targetNamespace=”http://www.wso2.org/types”&gt;

<xs:import namespace=”http://www.wso2.org/hello&#8221;

schemaLocation=”hello.xsd” />

<xs:element name=”greet” type=”q1:hello”>

</xs:element>

</xs:schema>

In this schema definition, you can find there is a reference to the schema xmlns:q1=”http://www.wso2.org/hello&#8221; which is defined in a secondary schema file. type “hello” is defined in the secondary schema given below.

Hello.xsd

==========

<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema&#8221;

xmlns:ns1=”http://org.apache.axis2/xsd&#8221; xmlns:ns=”http://www.wso2.org/hello&#8221;

attributeFormDefault=”qualified” elementFormDefault=”qualified”

targetNamespace=”http://www.wso2.org/hello”&gt;

<xs:element name=”hello” type=”ns:hello”></xs:element>

<xs:complexType name=”hello”>

<xs:sequence>

<xs:element minOccurs=”1″ name=”name” >

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:minLength value=”1″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:schema>

Now you are going to validate the request against the HelloSchema.xsd file which has a reference to Hello.xsd file.

2. Once you create your XSD files, upload them to WSO2 ESB registry under the path \_system\conf\. Now your XSD files should be in the below registry paths.

/_system/config/Hello.xsd

/_system/config/HelloSchema.xsd

3. Create the proxy service to validate the incoming request.

<?xml version=”1.0″ encoding=”UTF-8″?>

<proxy xmlns=”http://ws.apache.org/ns/synapse&#8221;

name=”MyValidateProxy”

transports=”https,http”

statistics=”disable”

trace=”disable”

startOnLoad=”true”>

<target>

<inSequence>

<log level=”full”>

<property name=”Message” value=”Inside Insequance”/>

</log>

<validate>

<schema key=”conf:/HelloSchema.xsd”/>

<resource location=”hello.xsd” key=”conf:/Hello.xsd”/>

<on-fail>

<makefault version=”soap11″>

<code xmlns:tns=”http://www.w3.org/2003/05/soap-envelope&#8221; value=”tns:Receiver”/>

<reason value=”Invalid Request!!!”/>

<role/>

</makefault>

<log level=”full”/>

<property name=”RESPONSE” value=”true”/>

<header name=”To” action=”remove”/>

<send/>

<drop/>

</on-fail>

</validate>

<respond/>

</inSequence>

<outSequence>

<send/>

</outSequence>

</target>

<description/>

</proxy>

 

In this proxy configuration, you can find the validate mediator configuration like below.

<validate>

<schema key=”conf:/HelloSchema.xsd”/>

<resource location=”hello.xsd” key=”conf:/Hello.xsd”/>

<on-fail>

<makefault version=”soap11″>

<code xmlns:tns=”http://www.w3.org/2003/05/soap-envelope&#8221; value=”tns:Receiver”/>

<reason value=”Invalid Request!!!”/>

<role/>

</makefault>

<log level=”full”/>

<property name=”RESPONSE” value=”true”/>

<header name=”To” action=”remove”/>

<send/>

<drop/>

</on-fail>

</validate>

Here we are validating the request against the schema key conf:/HelloSchema.xsd it has a reference to another xsd which is stored with the location=”hello.xsd”. This value is the one we referenced inside the HelloSchema.xsd file like below.

 <xs:import namespace=”http://www.wso2.org/hello&#8221;

               schemaLocation=”hello.xsd” /> 

This is a very important reference and you should carefully understand this reference. This will make sure that the validate mediator can resolve the references correctly.

Now you have the proxy and the XSD files in place.

4. Send a request to the proxy service with the correct request like below.

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”&gt;

<soapenv:Header/>

<soapenv:Body>

<p:greet xmlns:p=”http://www.wso2.org/types&#8221; xmlns:q=”http://www.wso2.org/hello”&gt;

<q:name>chanaka</q:name>

</p:greet>

</soapenv:Body>

</soapenv:Envelope>

Here you can see we are using two namespaces

xmlns:p=”http://www.wso2.org/types&#8221; xmlns:q=”http://www.wso2.org/hello&#8221;

which we have defined in two different xsd files. Once you send this request, it will respond with the same request since it validated the XSD correctly.

If you send a different request like below it will respond with a failure response.

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”&gt;

<soapenv:Header/>

<soapenv:Body>

<p:greet xmlns:p=”http://www.wso2.org/types&#8221; xmlns:q=”http://www.wso2.org/hello”&gt;

<q:name></q:name>

</p:greet>

</soapenv:Body>

</soapenv:Envelope>

This will respond with the below response.

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”&gt;

<soapenv:Body>

<soapenv:Fault>

<faultcode xmlns:tns=”http://www.w3.org/2003/05/soap-envelope”>tns:Receiver</faultcode&gt;

<faultstring>Invalid Request!!!</faultstring>

</soapenv:Fault>

</soapenv:Body>

</soapenv:Envelope>

Here we go !!! The wrong request has been sent back with the fault message.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s