package argon; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.messaging.Endpoint; import javax.xml.messaging.ProviderConnection; import javax.xml.messaging.ProviderConnectionFactory; import javax.xml.messaging.ProviderMetaData; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import com.sun.xml.messaging.jaxm.soaprp.SOAPRPMessageImpl; /** * Manages interactions between the user and the JAXM system. * */ public class Dispatcher extends HttpServlet { private ProviderConnectionFactory connectionFactory; private ProviderConnection connection; private MessageFactory mf = null; // This URL is configured using the JAXM administration tool. private String submitBillURL = "http://invoice.argon.com/process"; /** * Creates a connection to the JAXM provider. */ public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); // Create a connection to the Message Provider. try { connectionFactory = ProviderConnectionFactory.newInstance(); connection = connectionFactory.createConnection(); } catch (SOAPException e) { throw new ServletException(e); } } protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { doPost(arg0, arg1); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Extract the action. String action = request.getParameter("action"); if (action == null) { sendResponse("/home.jsp", request, response); } // Process the invoice. else if (action.equals("process")) { submitBill(request); sendResponse("/home.jsp", request, response); } else if (action.equals("find")) { findInvoices(request, response); } else { sendResponse("/home.jsp", request, response); } } private void sendResponse( String name, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher rd = getServletContext().getRequestDispatcher(name); rd.forward(request, response); } private void findInvoices(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String client = request.getParameter("client"); if (client == null || client.trim().equals("")) { request.setAttribute("error", "Client Name cannot be null or empty"); sendResponse("/findInvoice.jsp", request, response); } else { try { // Create a SOAPMessage SOAPMessage message = MessageFactory.newInstance().createMessage(); SOAPPart part = message.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPBody body = envelope.getBody(); SOAPBodyElement bodyElement = body.addBodyElement(envelope.createName("findInvoices")); bodyElement.addChildElement(envelope.createName("client")).addTextNode(client); // Create a SOAP Connection SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection(); java.net.URL endpoint = new java.net.URL("http://localhost:8080/argon/findInvoices"); // Send the message message.writeTo(System.out); SOAPMessage responseMessage = connection.call(message, endpoint); responseMessage.writeTo(response.getOutputStream()); } catch (Exception se) { throw new ServletException(se); } } } private void submitBill(HttpServletRequest request) throws ServletException { try { // Create a message factory. if (mf == null) { ProviderMetaData metaData = null; try { metaData = connection.getMetaData(); } catch (SOAPException e) { throw new ServletException(e); } // Find the SOAP-RP profie and create a MessageFactory that supports // SOAP-RP messages. String[] supportedProfiles = metaData.getSupportedProfiles(); String profile = null; for (int i = 0; i < supportedProfiles.length; i++) { if (supportedProfiles[i].equals("soaprp")) { profile = supportedProfiles[i]; break; } } mf = connection.createMessageFactory(profile); } // Create a SOAP-RP message from the message factory. SOAPRPMessageImpl msg = (SOAPRPMessageImpl) mf.createMessage(); // Set the end point. msg.setTo(new Endpoint(submitBillURL)); SOAPPart sp = msg.getSOAPPart(); SOAPEnvelope envelope = sp.getEnvelope(); SOAPBody bdy = envelope.getBody(); // Add a root element "invoice" to the soap message. SOAPBodyElement billElement = bdy.addBodyElement(envelope.createName("invoice")); // Add the name of the company to the message String company = request.getParameter("company"); billElement.addChildElement(envelope.createName("company")).addTextNode(company); // Add the name of the client to the message String customer = request.getParameter("client"); billElement.addChildElement(envelope.createName("client")).addTextNode(customer); // Add the hours to the message String hours = request.getParameter("hours"); billElement.addChildElement(envelope.createName("hours")).addTextNode(hours); // Send the message msg.writeTo(System.out); connection.send(msg); } catch (Exception se) { se.printStackTrace(); } } }