Providing Schema Validation

When using web services that pass and return a W3C Document object you may want to use schema validation to verify the integrity of the document you are processing. Whether you choose to do this might depend on factors such as:

The steps for validating an XML Document in an inbound web service are as follows:

  1. Include the schema document in the application ear by storing it somewhere within directory SERVER_DIR/components/**/webservices/**/*.xsd.
  2. Provide code within the implementation code of the BPO method that loads the schema file, and passes it into the infrastructure validator class along with the org.w3c.Document class to be validated.

The code example below (Providing Schema Validation) illustrates how this can be implemented.

Figure 1. Sample Illustrating Schema Validation
import curam.util.exception.AppException;
    import curam.util.exception.InformationalException;
    import curam.util.webservices.DOWSValidator;
    import java.io.InputStream;
    import org.w3c.dom.Document;

     . . .

    /**
     * A sample XML document web service.
     */
    public org.w3c.dom.Document
      myWebServiceOperation(final org.w3c.dom.Document docIn)
      throws AppException, InformationalException {

      // DOWSValidator is the SDEJ infrastructure class for
      // validating org.w3c.Document classes in web services.
      final curam.util.webservices.DOWSValidator validator =
        new curam.util.webservices.DOWSValidator();

      try {
        // The following is used only for error reporting
        // purposes by DOWSValidator.  In your code you can
        // provide a relevant value to help identify the schema
        // in the event of an error.
        final String schemaURL = "n/a";

        // Load the schema file from the .ear file.
        // For example, the source location of
        // 'test1.xsd' was
        // SERVER_DIR/components/custom/webservices.

        final InputStream schemaStream =
          getClass().getClassLoader().
            getResourceAsStream("schemas/test1.xsd");

      // if schema file is in
      // SERVER_DIR/components/custom/webservices/test/test1.xsd
         schemaStream =
          getClass().getClassLoader().
           getResourceAsStream("schemas/test/test1.xsd");


        // Invoke the validator.
        validator.validateDocument(docIn, schemaStream,
          schemaURL);

      } catch (Exception e) {
        // Schema validation failed. Throw an exception.
        AppException ae = new
          AppException(SOME_MESSAGES.ERR_SCHEMA_VALIDATION_ERROR,
          e);
      }

      // normal BPO logic goes here.
      // ...

      return result;
    }