Validating XML message in Mule 3
Join the DZone community and get the full member experience.
Join For Free
a common use case
mule esb
flows is validating if an xml document is valid against a corresponding
xsd
. it can be quite annoying to find out you have spent a lot of time fixing an issue when it was actually caused by another system that was supplying invalid xml, especially if a project is in the development/test stage.
the ‘standard’ way to do this in mule is by using the
schema-validation-filter
, usually in combination with the
message-filter
. for example, the following configuration will send all non-valid messages to the ‘process-invalid-xml’ flow:
<message-filter onunaccepted="process-invalid-xml" throwonunaccepted="true"> <xml:schema-validation-filter .../> </message-filter>
in the ‘process-invalid-xml’ flow you can then create a response message (if applicable) to tell the sender of the original message that it contains invalid xml. this was the case in my situation and i also wanted to show the user
what
was wrong in the xml. to do this, i created my own sax errorhandler and supplied that to the schema-validation-filter like this:
<spring:bean id="xmlerrorhandler" class="net.pascalalma.xml.handlers.mysaxerrorhandler" /> <message-filter onunaccepted="invalidrelationxmlflow" throwonunaccepted="false"> <mulexml:schema-validation-filter errorhandler-ref="xmlerrorhandler" schemalocations="xsd/my-schema.xsd"/> </message-filter>
however, no matter what i did my error handler wasn’t being called by the filter. after some debugging i only got it working by extending the original schema-validation-filter and set the error-handler on the created validator like this:
public class myschemavalidationfilter extends schemavalidationfilter { static logger logger = logger.getlogger(myschemavalidationfilter.class); public validator createvalidator() throws saxexception { validator validator = super.createvalidator(); validator.seterrorhandler(geterrorhandler()); return validator; } }
i am not sure if this is a bug in the original schema-validation-filter or if there is another way to make use of the errorhandler, but this was the only way for me to get it working.
i use my version of the filter with the following configuration:
<custom-filter class="net.pascalalma.xml.filters.myschemavalidationfilter" > <spring:property name="schemalocations" value="xsd/my-schema.xsd"/> <spring:property name="returnresult" value="false"/> <spring:property name="errorhandler" ref="xmlerrorhandler"/> </custom-filter>
this way i can collect all errors in my error handler and put them with a outbound property on the message and return them to the calling client.
here is the source code of my error handler:
public class myerrorhandler implements errorhandler { static logger logger = logger.getlogger(myerrorhandler.class); public list<string> geterrors() { return errors; } public void seterrors(list<string> errors) { this.errors = errors; } private list<string> errors = new arraylist<string>(); @override public void warning(saxparseexception e) throws saxexception { logger.warn(e.getlinenumber() + "/" + e.getcolumnnumber() + ": " + e.getmessage()); } @override public void fatalerror(saxparseexception e) throws saxexception { logger.debug("fatalerror occurred: " + e.tostring()); errors.add(e.getlinenumber() +"/" + e.getcolumnnumber() + ": " + e.getmessage()); } @override public void error(saxparseexception e) throws saxexception { logger.debug("error occurred: " + e.tostring()); errors.add(e.getlinenumber() +"/" + e.getcolumnnumber() + ": " + e.getmessage()); } }
Published at DZone with permission of $$anonymous$$. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments