/* * $Id: SendingServlet.java */ package soapfault.sender; import java.net.*; import java.io.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; public class SendingServlet extends HttpServlet { String to = null; String data = null; ServletContext servletContext; // Connection to send messages. private SOAPConnection con; public void init(ServletConfig servletConfig) throws ServletException { super.init( servletConfig ); servletContext = servletConfig.getServletContext(); try { SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance(); con = scf.createConnection(); } catch(Exception e) { System.out.println("ERROR: "+ e.getMessage()); } } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { String clientMessage =" "; try { // Create a message factory. MessageFactory mf = MessageFactory.newInstance(); // Create a message from the message factory. SOAPMessage msg = mf.createMessage(); SOAPPart sp = msg.getSOAPPart(); // Retrieve the envelope from the soap part to start building // the soap message. SOAPEnvelope envelope = sp.getEnvelope(); // Create a soap header from the envelope. SOAPHeader hdr = envelope.getHeader(); // Create a soap body from the envelope. SOAPBody bdy = envelope.getBody(); // Add a soap body element to the soap body SOAPBodyElement customerDetails = bdy.addBodyElement(envelope.createName("Customer")); customerDetails.addChildElement(envelope.createName("Name")) .addTextNode("Joe Bloggs"); customerDetails.addChildElement(envelope.createName("Customer_No")) .addTextNode("12383478"); SOAPElement creditCardDetails = customerDetails.addChildElement (envelope.createName("CreditCardDetails")); creditCardDetails.addChildElement(envelope.createName("Type")) .addTextNode("MasterCard"); creditCardDetails.addChildElement(envelope.createName("Number")) .addTextNode("4325-5325-5325"); creditCardDetails.addChildElement(envelope.createName("Expiry")) .addTextNode("01/2005"); SOAPBodyElement keyboardDetails =bdy.addBodyElement(envelope.createName("ComputerPart")); keyboardDetails.addChildElement(envelope.createName ("Category_Number")).addTextNode("124-342-343"); keyboardDetails.addChildElement(envelope.createName("Description")) .addTextNode("Keyboard"); keyboardDetails.addChildElement(envelope.createName("Maker")) .addTextNode("mycomputerCo"); keyboardDetails.addChildElement(envelope.createName("Price")) .addTextNode("50.32"); //Construct the URL of the SOAP server servlet. StringBuffer urlSB=new StringBuffer(); urlSB.append(req.getScheme()).append("://") .append(req.getServerName()); urlSB.append(":").append(req.getServerPort()) .append(req.getContextPath() ); String reqBase=urlSB.toString(); if(to==null) { to= reqBase+"/receiver"; } URLEndpoint urlEndpoint = new URLEndpoint(to); clientMessage += " Sent purchase request succesfully. \n "; FileOutputStream sentFile = new FileOutputStream("PurchaseRequest.msg"); msg.writeTo(sentFile); SOAPMessage reply = con.call(msg, urlEndpoint); if (reply != null) { String replyFileName = "PurchaseReply.msg"; FileOutputStream replyFile = new FileOutputStream(replyFileName); reply.writeTo(replyFile); replyFile.close(); //Check if reply includes soap fault SOAPPart replySP = reply.getSOAPPart(); SOAPEnvelope replySE = replySP.getEnvelope(); SOAPBody replySB = replySE.getBody(); if(replySB.hasFault()){ System.out.println("ReplySB has fault"); SOAPFault sf = replySB.getFault(); clientMessage += "\n Received Reply with SOAPFAULT \n "+ "ERROR: "+ sf.getFaultString() + "\nCode: "+ sf.getFaultCode() + ". See file "+ replyFileName +" for more details."; }else clientMessage += " and an acknowledgement received."; } else { System.err.println("No reply"); clientMessage += " no reply was received. "; } } catch(Throwable e) { System.out.println("ERROR "+ e.getMessage()); e.printStackTrace(); clientMessage += " There was an error " + "in constructing or sending message. "; } //Send a message to the client's display try { OutputStream os = resp.getOutputStream(); os.write(clientMessage.getBytes()); os.flush(); os.close(); } catch (IOException e) { System.out.println("ERROR IN SENDING MESSAGE TO CLIENT"+ e.getMessage()); e.printStackTrace(); } } }