Better Software - January 2008 - (Page 26) child element of the SOAP body to the template method. By changing the superclass to this SOAP servlet, one can change the underlying message format quite easily. Obviously, using a composition would be preferable to inheritance in this case, but for simplicity’s sake we will stick to inheritance. Now that the base class is ready, we can implement a VacationServlet. This servlet extends from the AbstractDomServlet and uses the JAXP XPath libraries to extract the particular pieces of XML that are of interest. Using this information, it in- part of Dan’s article. In this part, Susie, an employee of the personnel department, decides to create a new version of the vacation request form, adding an optional field for including an employee extension number. Because the field is optional, the old form is not invalidated, and Susie can still expect old forms to come in. Handling a new version of the contract is quite easy when using XPath. We can simply query for the extension number element, and we’ll get an empty string when it is not there. For vokes handleVacationRequest, which in this example returns a boolean value indicating whether the request has been successful or not. In this case, it simply returns a random value—clearly not an honest way of granting vacations, but perhaps more honest than many corporations. A “state” attribute on the request gives the response shown in listing 10. This servlet uses a PersonnelNamespaceContext, which simply resolves the prefix “p” to the personnel namespace, so that anytime we use p, we are referring to an element from our vacation request schema. Note that the expressions used in this servlet are quite explicit. Rather than using “/p:vacation Request/p:vacation/p:startDate,” for instance, we could have used “//p:startDate” to indicate that the start date string can be obtained from any location in the XML document. This would make the vacation service more robust, though issues arrive when we can expect multiple start dates in the request. We use the open source framework Joda Time’s LocalDate as a representation of dates, because Java’s built-in types (Date and Calendar) are inconvenient and, more importantly, represent points in time. As indicated earlier, a vacation is an interval between dates, not points in time. Also, Joda Time has built-in support for the ISO 8601 date format, which removes a lot of parsing hassle. more complicated situations, we can try to transform the “old” XML requests to “new” requests using an XSLT style sheet. Thus, the service implementation can focus on the new requests, so that the code stays focused and clear. Using a different style sheet, we can transform the new responses to the old contract. Sometimes, having multiple versions of the same service is impossible or not desirable. In this case, returning a fault document (or a SOAP Fault) will be necessary. Summary Dan North’s “A Classic Example: Take a Low-Tech Approach to Understanding SOA” concludes that by keeping the discussion focused on real business scenarios and away from technology the business is able to play a useful and important role in identifying the requirements of an SOA. I made particular technological choices in this article, however, I was careful to take a document-driven approach, focusing on the XML. I started by defining the XML messages that are accepted by the service and inferred a schema from it. The implementation of the vacation service was made in a Java Servlet, using standard libraries, and I’ve shown where standards like SOAP and WSDL fit in and how the service can evolve. {end} Arjen Poutsma has more than ten years of experience in commercial software environments. He currently works at SpringSource as the development lead of Spring Web Services. This Spring project aims at facilitating development of document-driven Web services in a very similar fashion as featured in this article. Validation and Versioning Now that the base functionality is complete, we can try to improve the vacation service. Since we have written our personnel schema, one of the most tempting improvements would be to validate requests with it. Doing so would make the service stricter, because we can expect all input to conform to our schema. However, it is debatable whether such strictness is a good asset. Is it of key importance that the entire XML request is completely valid, or does it suffice if we can simply extract the pieces that are of interest to us? Rather than validating the request, we can validate the response. This enforces service implementers to conform to their own schema. And if the implementers don’t do it, nobody will! Another possible improvement has to do with the second 26 Sticky Notes For more on the following topics go to www.StickyMinds.com/bettersoftware. I I I I REST XSLT style sheet Trang References BETTER SOFTWARE JANUARY/FEBRUARY 2008 www.StickyMinds.com http://www.StickyMinds.com/bettersoftware http://www.StickyMinds.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.