Sample 7: Introduction to Local Registry Entries and Using Schema Validation

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main"> <in> <validate> <schema key="validate_schema"/> <on-fail> <!-- if the request does not validate againt schema throw a fault --> <makefault response="true"> <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/> <reason value="Invalid custom quote request"/> </makefault> </on-fail> </validate> </in> <send/> </sequence> <localEntry key="validate_schema"> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://services.samples" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://services.samples"> <xs:element name="getQuote"> <xs:complexType> <xs:sequence> <xs:element name="request"> <xs:complexType> <xs:sequence> <xs:element name="stocksymbol" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </localEntry> </definitions>

Objective

Demonstrating the usage of the validate mediator for XML schema validation and using local registry (local entries) for storing configuration metadata.

Pre-requisites

  • Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
  • Start Synapse using the configuration numbered 7 (repository/conf/sample/synapse_sample_7.xml)
    Unix/Linux: sh synapse.sh -sample 7
    Windows: synapse.bat -sample 7

Executing the Client

This example shows how a static XML fragment could be stored in the the Synapse local registry. Resources defined in the local registry are static (i.e. never changes over the lifetime of the configuration) and may be specified as a source URL, in-line text or in-line xml. In this example the schema is made available under the key 'validate_schema'.

The validate mediator by default operates on the first child element of the SOAP body. You may specify an XPath expression using the 'source' attribute to override this behaviour. The validate mediator in this sample uses the 'validate_schema' resource to validate the incoming message, and if the message validation fails it invokes the 'on-fail' sequence of mediators.

If you send a stockquote request using the 'ant stockquote ...' command as follows you will get a fault back with the message 'Invalid custom quote request' as the schema validation fails. This is because the schema used in the example expects a slightly different message than what is created by the stock quote client. (i.e. expects a 'stocksymbol' element instead of 'symbol' to specify the stock symbol)

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/

Back to Catalog