Forum Discussion

Scarabanza's avatar
Scarabanza
New Contributor
10 years ago

How do I validate a REST XML request using Groovy

I have a REST mock service with an OnRequest script that needs to apply XML validation to incoming requests. I found a post that explains how to do this for SOAP (viewtopic.php?t=2049), but I can't find anything that indicates how to do this for REST.

I have the XSD held in the WADL for the service, but I'm equally happy to store it in a file. I assume I need to achieve the following things, and any hints, tips, or complete solutions would be most welcome;
  • Load the XSD - I can't find out the right combination of methods to get me this information (presumably starting from the mockRequest)

  • Retrieve the request Xml - I can do this now I've downloaded the maintenance version of SoapUI 5

  • Create some kind of Xml Validator and check the request against the XSD - Can I use the WSDL validator that is mentioned in the post above, or is there a REST based version?


I'm quite surprised at how hard I'm finding this as I expected it to be trivial.

2 Replies

  • bzzup's avatar
    bzzup
    Occasional Contributor
    I'm struggling with the same problem currently. What i've found:


    import javax.xml.XMLConstants
    import javax.xml.transform.Source
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.Schema
    import javax.xml.validation.SchemaFactory
    import javax.xml.validation.Validator
    import org.xml.sax.SAXException
    import javax.xml.parsers.DocumentBuilderFactory
    import javax.xml.parsers.DocumentBuilder;
    import org.w3c.dom.Document;
    import javax.xml.transform.dom.DOMSource;

    String xsd = 'http://<server>/rest/application.wadl/xsd0.xsd' //path to xsd
    String xml = 'C:\\test.xml' //path to xml to validate

    //Create xsd validator
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaFile= new URL(xsd)
    Schema schema = schemaFactory.newSchema(schemaFile)
    Validator validator = schema.newValidator()

    //Create xml source to validate
    DocumentBuilderFactory bfactory = DocumentBuilderFactory.newInstance()
    DocumentBuilder dbuilder = bfactory.newDocumentBuilder()
    Document xsdDoc = dbuilder.parse(new File(xml))
    DOMSource source = new DOMSource(xsdDoc)

    validator.validate(source)


    However this doesn't work in my case - looks like i have some problems with my xml structure. At least point to start with
  • Scarabanza's avatar
    Scarabanza
    New Contributor
    Shortly after posting my question, I found an answer. Isn't that always the way.

    My solution is similar to what you have posted, but includes an error handler class to ensure all errors are reported on. My Groovy isn't very groovy, so this might not be the best way of coding it. I have also edited the code below to remove code that was specific to what I'm trying to do.

    import javax.xml.XMLConstants
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.SchemaFactory
    import org.xml.sax.ErrorHandler
    import org.xml.sax.SAXException
    import org.xml.sax.SAXParseException

    // Define a class to handle XML validation errors
    class LoggingErrorHandler implements ErrorHandler {
    def xmlErrorMessages
    public void warning(SAXParseException ex) {
    xmlErrorMessages += ex.message
    xmlErrorMessages += "\n"
    }
    public void error(SAXParseException ex) {
    xmlErrorMessages += ex.message
    xmlErrorMessages += "\n"
    }
    public void fatalError(SAXParseException ex) {
    xmlErrorMessages += ex.message
    xmlErrorMessages += "\n"
    }
    }

    // Validate the incoming message against the schema
    def incomingXml = mockRequest.requestContent
    def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    def schema = factory.newSchema(new StreamSource(new FileReader("Schema.xsd")))

    def validator = schema.newValidator()
    def errorHandler = new LoggingErrorHandler()
    validator.errorHandler = errorHandler
    validator.validate(new StreamSource(new StringReader(incomingXml)))

    if (errorHandler.xmlErrorMessages != "") {
    // Do things here to deal with/report the errors
    }

    // Remainder of code to do things with a valid XML message