Better Software - January 2008 - (Page 24) cases. Because of these robust properties, we will use XPath in our implementation. One of the disadvantages of using XPath is that it is relatively slow, but we are willing to trade performance for robustness. Since XPath engines need a DOM tree on which to operate, we will need to create such a tree from the incoming HTTP request. We do this using an abstract base class, the AbstractDomServlet, shown in listing 9. The AbstractDomServlet parses the incoming XML document into a DOM tree and passes that on to an abstract template method, onRequest(). This method takes the root elpackage com.bigcorporation.ws; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.*; ement of the XML message as a parameter and returns the response document, if any. If there is a response, it is written to the HTTP output stream; if there is no response, it simply sets the status accordingly. Having this base class allows implementers to focus on the specifics of the service, rather than on solving the generic problem of parsing the XML into a DOM tree. Another key point is that, focusing on the contents (payload) of the message, we can quite easily migrate from a POX service to SOAP. One can imagine an AbstractSoapServlet, which reads a SOAP envelope rather than a self-contained XML document and passes on the single public abstract class AbstractDomServlet extends HttpServlet { protected DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); protected TransformerFactory transformerFactory = TransformerFactory.newInstance(); public void init(ServletConfig servletConfig) throws ServletException { documentBuilderFactory.setNamespaceAware(true); } protected void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException { try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document request = documentBuilder.parse(httpRequest.getInputStream()); Element response = onRequest(request.getDocumentElement()); if (response != null) { httpResponse.setContentType("text/xml"); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(new DOMSource(response), new StreamResult(httpResponse.getOutputStream())); } else { httpResponse.setStatus(HttpServletResponse.SC_ACCEPTED); } } catch (Exception ex) { throw new ServletException(ex); } } protected abstract Element onRequest(Element request) throws Exception; } Listing 9 24 BETTER SOFTWARE JANUARY/FEBRUARY 2008 www.StickyMinds.com 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.