| |

Lesson 14: Validating data structures

Lesson objective

The objective of this lesson is to learn how to validate UDDI data structures.

Reference material

Source code of reference:

Related lessons

In most lessons covering publication in an UDDI registry, data structures are sent "as is", without any verification. The source code provided can be enhanced using the validation feature learnt in this lesson.

Review of concepts

UDDI data structures are rather complex. There are, for example, many restrictions on the length of UDDI data structure elements. UDDI registries have to validate the data they receive. If data is not valid, an error message is sent back to the client. Ruddi validators allow validating Ruddi data structures before they are sent to the UDDI registry.

Code snippets

We first instantiate a ValidationFactory for a given UDDI version:

ValidatorFactory factory = new ValidatorFactory(UDDIConstants.UDDI_V2);

We then use one of the various createXXXValidator() to create a validator for a given UDDI notion. For example, to create a TModel validator, we write:

TModel tModel = ... // the technical model to validate

TModelValidator validator = factory.createTModelValidator(tModel, true);

The boolean flag indicates whether the exception, if any, should be thrown on the first exception of after all validations have been performed.

To perform the validation, just invoke the validate() method that comes with each validator and throws a ValidationException whenever validation fails:

try {

validator.validate();

} catch(ValidationException validationException) {

validationException.printStackTrace();

}

To get more fine-grained information on what parts of the data structure have failed validation and why, access the list of sub-exceptions (themselves ValidationException objects) and handle them appropriately. For example:

ValidationException subexceptions[] = validationException.getValidationExceptions();

for(int i=0; i< subexceptions.length; i++) {

System.out.print("Exception #" + i + " detected on ");

switch(subexceptions[i].getType()) {

case TModelException.AUTHORIZEDNAME:

System.out.println("the AuthorizedName.");

break;

case TModelException.CATEGORYBAG:

System.out.println("the CategoryBag.");

break;

// etc ...

}

}

The non-valid objects can be obtained using the getFaulty() method. For example, a faulty category bag could be obtained the following way:

case TModelException.CATEGORYBAG:

CategoryBag categoryBag = (CategoryBag)subexceptions[i].getFaulty();

break;

In that context, the CategoryBagException that has triggered the higher-level TModelException could also be accessed via the getSubException() method:

CategoryBagException e = (CategoryBagException)subexceptions[i].getSubException();

A CategoryBagException being iteself a validation exception, the principles describes above can be applied on that exception as well, and like this recursively.

| |

(c) INSPIRE IT, 2003 | Send us your feedback: developers@ruddi.biz