DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Validating XML message in Mule 3

Validating XML message in Mule 3

$$anonymous$$ user avatar by
$$anonymous$$
·
Nov. 22, 13 · Integration Zone · Interview
Like (0)
Save
Tweet
12.40K Views

Join the DZone community and get the full member experience.

Join For Free

mulesoft-logo
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());
    }
}



XML

Published at DZone with permission of $$anonymous$$. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Counting Faster With Postgres
  • How to Handle Early Startup Technical Debt (Or Just Avoid it Entirely)
  • Testing Schema Registry: Spring Boot and Apache Kafka With JSON Schema
  • Message Queuing and the Database: Solving the Dual Write Problem

Comments

Integration Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo