package argon; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.xml.messaging.*; import javax.xml.soap.*; import java.util.*; /** * Synchronous JAXM service that returns a list of invoices for a given client. */ public class InvoiceInfoServlet extends JAXMServlet implements ReqRespListener { MessageFactory factory = null; /** * Returns a list of invoices in the form of a SOAPMessage. * @param message Incoming SOAPMessage containing the client's name. * @return A SOAPMessage containing a list of invoices. */ public SOAPMessage onMessage(SOAPMessage message) { try { // Extract the name of the client. String client = extractClient(message); // Search the invoice processing system for a collection of invoices for a given client. Collection invoices = InvoiceProcessingSystem.getInstance().findInvoices(client); // Build and return a SOAPMessage from the collection of invoices. return buildMessage(invoices); } catch (SOAPException ex) { // Return a SOAPMessage with a SOAPFault. ex.printStackTrace(); return buildFault(ex); } } /** * Creates an instance of a JAXM MessageFactory. */ public void init(ServletConfig config) throws ServletException { super.init(config); try { factory = MessageFactory.newInstance(); } catch (SOAPException se) { throw new ServletException(se); } } /** * Extracts the client's name. */ private String extractClient(SOAPMessage message) throws SOAPException { SOAPPart part = message.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPBody body = envelope.getBody(); // Extract the client's name. java.util.Iterator itr = body.getChildElements(envelope.createName("findInvoices")); if (itr.hasNext()) { SOAPElement element = (SOAPElement)itr.next(); Iterator children = element.getChildElements(envelope.createName("client")); return extractData(children); } return null; } /** * Returns a text value of a certain element. */ private String extractData(Iterator itr) { if (itr.hasNext()) { SOAPElement element = (SOAPElement)itr.next(); return element.getValue(); } else { return ""; } } // Builds a SOAPMessage from a collection of invoices. private SOAPMessage buildMessage(Collection collection) throws SOAPException { SOAPMessage message = factory.createMessage(); SOAPPart sp = message.getSOAPPart(); SOAPEnvelope envelope = sp.getEnvelope(); SOAPBody bdy = envelope.getBody(); // Add the root element "invoices" to the SOAPBody. SOAPBodyElement billsElement = bdy.addBodyElement(envelope.createName("invoices")); Iterator itr = collection.iterator(); while (itr.hasNext()) { Invoice bill = (Invoice)itr.next(); // Add information about the bill to the root element "invoices". SOAPElement billElement = billsElement.addChildElement(envelope.createName("invoice")); billElement.addChildElement(envelope.createName("company")).addTextNode(bill.getCompany()); billElement.addChildElement(envelope.createName("client")).addTextNode(bill.getClient()); String hours = String.valueOf(bill.getHours()); billElement.addChildElement(envelope.createName("hours")).addTextNode(hours); String rate = String.valueOf(bill.getRate()); billElement.addChildElement(envelope.createName("rate")).addTextNode(rate); String total = String.valueOf(bill.getAmount()); billElement.addChildElement(envelope.createName("total")).addTextNode(total); } return message; } // Returns SOAPMessage containing a SOAPFault. private SOAPMessage buildFault(Exception ex) { try { SOAPMessage message = factory.createMessage(); SOAPPart sp = message.getSOAPPart(); SOAPEnvelope envelope = sp.getEnvelope(); SOAPBody bdy = envelope.getBody(); SOAPFault fault = bdy.addFault(); fault.setFaultString(ex.getMessage()); return message; } catch (SOAPException se) { se.printStackTrace(); return null; } } }