Schema Validation Filter (XML Schema Validation)

0
50

The Schema Validation filter takes XML inputs and validates these against a referenced XSD schema. Typically, we’ll place it at the start of the Mule flow to determine whether the incoming message or event should be handled or not, allowing the message to only continue on along the flow when the filter validations are met and the included XML is considered valid. Schema Validator is provided out of the box with Mule.

Use Case

A common use case in Mule ESB flows is validating if an XML document is valid against a corresponding XSD, so we can make sure we are receiving the correct XML, and will forward it for the further processing.

Syntax:

<mulexml:schema-validation-filter schemaLocations="src/main/resources/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation" />

The “standard” way of Validating XML schema in Mule is by using the schema-validation-filter and especially in combination with the message-filter.
In a case when you want to get an exception and/or call another flow when the validation fails, you could wrap the schema validation filter inside a message filter and then use “onUnaccepted” and “throwOnUnaccepted” configuration attributes to determine the behavior.
Here we have wrapped the schema-validation-filter in a message-filter and set throwOnUnaccepted to “true.”

Syntax:

<message-filter throwOnUnaccepted="true" >
    <mulexml:schema-validation-filter schemaLocations="/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation" />
</message-filter>

Note: the schemaLocations attribute accepts a comma separated list of schemas to validate the payload against.

Passing the XML Forward

In order to allow a valid XML to pass the filter, you need to set the returnResult to false in your schema-validation-filter. This way, the payload of the message that is passed onwards to the next element in your flow will retain the XML content.
Let’s walk through how to use All Validator in a Mule application. In this example, we are receiving the XML file through file connector, which will be validated against an XSD schema file defined in the application. If validation fails, then we catch the exception and print the “Schema Not Validated” message along with the exception.

Flow:

Schema Validation Flow
Schema Validation Flow

Code:

<mule xmlns:encryption="http://www.mulesoft.org/schema/mule/encryption" xmlns:filters="http://www.mulesoft.org/schema/mule/filters" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/filters http://www.mulesoft.org/schema/mule/filters/current/mule-filters.xsd http://www.mulesoft.org/schema/mule/encryption http://www.mulesoft.org/schema/mule/encryption/current/mule-encryption.xsd">
    <flow name="SchemaValidationFLow" >
        <file:inbound-endpoint path="src/main/resources/File/Process/input" responseTimeout="10000" doc:name="File" moveToDirectory="src/main/resources/File/Process/output" >
          <file:filename-wildcard-filter pattern="*.xml" caseSensitive="false"/>
        </file:inbound-endpoint>
        <file:file-to-string-transformer doc:name="File to String" />
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />    
       <message-filter throwOnUnaccepted="true" doc:name="Message">
           <mulexml:schema-validation-filter schemaLocations="src/main/resources/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation" />
       </message-filter>
        <logger message="Validation successful: #[message.payload]" level="INFO" doc:name="Logger" />
       <catch-exception-strategy doc:name="Catch Exception Strategy">
       <logger level="INFO" message="Schema Not Validated" doc:name="Logger"/>
       </catch-exception-strategy>      
    </flow>
</mule>

XSD Schema src/main/resources/File/Process/Schema.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="orderperson" type="xs:string"/>
       <xs:element name="shipto">
         <xs:complexType>
           <xs:sequence>
             <xs:element name="name" type="xs:string"/>
             <xs:element name="address" type="xs:string"/>
             <xs:element name="city" type="xs:string"/>
             <xs:element name="country" type="xs:string"/>
           </xs:sequence>
         </xs:complexType>
       </xs:element>
       <xs:element name="item" maxOccurs="unbounded">
         <xs:complexType>
           <xs:sequence>
             <xs:element name="title" type="xs:string"/>
             <xs:element name="note" type="xs:string" minOccurs="0"/>
             <xs:element name="quantity" type="xs:positiveInteger"/>
             <xs:element name="price" type="xs:decimal"/>
           </xs:sequence>
         </xs:complexType>
       </xs:element>
     </xs:sequence>
     <xs:attribute name="orderid" type="xs:string" use="required"/>
   </xs:complexType>
</xs:element>
</xs:schema>

Now in the above XML if we change the any of the element aganist the schema defined ,we will be getting the below exception.

<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd">
   <orderperson>John Smith</orderperson>
   <shipto>
     <name>Ola Nordmann</name>
     
<address>Langgt 23</address>

     <city>4000 Stavanger</city>
     <country>Norway</country>
   </shipto>
   <item>
     <title>Empire Burlesque</title>
     <note>Special Edition</note>
     <quantity>1</quantity>
     <price>10.90</price>
   </item>
   <item>
     <title>Hide your heart</title>
     <quantity>1</quantity>
     <price>9.90</price>
   </item>
</shiporder>

Exception:

ERROR 2017-08-08 21:01:19,585 [[schemavalidation1].SchemaValidationFLow.stage1.02] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : Message has been rejected by filter.
Filter                : org.mule.module.xml.filters.SchemaValidationFilter@39c5e4bb (null)
Payload               : <?xml version="1.0" encoding="UTF-8"?>
Element XML           : <message-filter throwOnUnaccepted="true" doc:name="Message">
                        <mulexml:schema-validation-filter schemaLocations="src/main/resources/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation"></mulexml:schema-validation-filter>
                        </message-filter>
Payload Type          : org.apache.xerces.dom.DocumentImpl
Element               : /SchemaValidationFLow/processors/2 @ schemavalidation1:schemavalidation1.xml:23 (Message)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.routing.filter.FilterUnacceptedException: Message has been rejected by filter.

Previous articleActiveMQ Read and Write Flow in Mule
Ankit has 6+ years of experience in design and development of web based applications in Java/JEE. He lately develpoed his interest in ESB Mule. His great passion and interest in this field guided him to become certified Mule Developer. Ankit is passionate about stock market and technology. He hails from beautiful Rajasthan in India and currently lives in Gurgaon with his family.

NO COMMENTS

LEAVE A REPLY