Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating SOAP message against WSDL with multiple XSD's

I've been looking around the net for a good few hours now, trying to find a simple way to validate a full SOAP message against a WSDL. I am aware that there are ways to do this with the various Web Service frameworks out there, but I don't want to do this as the requirement is simply to validate a piece of XML. I could validate against the schema, although the problem I have is that there are a number of schemas imported into the WSDL and I don't know which one I should be validating against. I could write some utility to first process the WSDL and the response to determine which XSD to validate against, but I presumed this could be done as a one-liner using an established library!

Does anyone know of a relatively straightforward way to validate an XML document given a WSDL and multiple XSD's?

like image 326
Ellis Avatar asked Jan 23 '12 21:01

Ellis


1 Answers

In a previous project I solved this problem by parsing the WSDL-file and extracting the schemas from it. The code was something like the following, it assumes that the WSDL has been read into the Source variable "wsdlSource" in some way and that the imported namespaces are declared in the "schema"-element. It would probably be a good idea to have this performed once on startup and then do the validation in a SOAPHandler.

    //First create a document from the WSDL-source
    DocumentBuilder db = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
    Document wsdlDoc = db.newDocument();

    TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(wsdlSource, new DOMResult(wsdlDoc));

    //Now get the schemas from the WSDL 
    NodeList schemaNodes = wsdlDoc.getElementsByTagNameNS(
            XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");

    int nrSchemas = schemaNodes.getLength();

    Source[] schemas = new Source[nrSchemas];

    for (int i = 0; i < nrSchemas; i++) {
        schemas[i] = new DOMSource(schemaNodes.item(i));
    }

    SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    //Now we have a schema that can validate the payload
    Schema schema = schemaFactory.newSchema(schemas);
    Validator validator = schema.newValidator();
like image 165
Olof Åkesson Avatar answered Dec 07 '22 01:12

Olof Åkesson