Web Services Integration Patterns, Part 2
by Massimiliano Bigatti
|
Pages: 1, 2, 3, 4, 5
Input/output Validator
It validates input and output from the service to reduce garbage data

One of the key elements of web services and SOA Architectures is the loose coupling between the services versus the application and between different services. This can be true at the wire level, but the coupling has to be strengthened at the application level, to assure the correct operation of the logic implemented in the program.
For example, consider a simple application that would exchange the value of a sum of money expressed in a specific currency to another currency, using the web service shown before. Imagine that the country codes are manually introduced by the user using a web page and the output rate is used to calculate the converted value.
To work properly, this simple application has to perform a series of checks: first, it should verify that the country codes provided by the user are supported by the service; secondly, it should check that the resulting rate hasn't invalid values, like zero, or NaN.
Embedding those checks in the Service Proxy allows to maintain proximity between the service and related controls. When a problem is found, the Service Proxy should throw an exception, providing the caller with field that caused the error failing the check (data missing, bad length, wrong format, and so on).
The check implemented in the Service Proxy usually has an incrementally approach: each check raise a single exception, describing that single problem; an alternative is to perform all the checks and then throwing a single exception that describes all the problems encountered.
The Abstract Services defines the abstract methods that will check the input and output of the service. The Service Proxy implements such methods, providing specialized checks to input and output data. The Service Exception represents the problem encountered during verification.
Sample code
The following class represents the usualExchangeService, but comprises the
checkParameters() and checkResponse
methods. The first one ensures that the country provided by the
caller is valid, checking against null and against the
list of possible countries. The second method verifies that the
result value is a valid number, not infinite and more than zero.
public class ExchangeService extends AbstractService {
String country1;
String country2;
float rate;
//valid country list
List countryList;
...
public void checkParameters() throws ServiceException {
if( country1 == null || countryList.get( country1 ) == null ) {
throw new ServiceException("Country1 " + country 1 + " not valid");
}
if( country2 == null || countryList.get( country2 ) == null ) {
throw new ServiceException("Country2 " + country 2 + " not valid");
}
}
public void checkResponse() throws ServiceException {
if( rate == null || rate == Float.NaN ||
rate == Float.NEGATIVE_INFINITY ||
rate == Float.POSITIVE_INFINITY ||
rate < 0 ) {
throw new ServiceException("The service returned an invalid value");
}
}
public void setCountry1( String country ) {
country1 = country;
}
public void setCountry2( String country ) {
country2 = country;
}
public float getRate() {
return rate;
}
}
Have you had experience with similar patterns? Share your comments and questions in our forum.
(* You must be a member of XML.com to use this feature.)
Comment on this Article
