/* * $Id: SendingServlet.java */ package JAXMInvoicing.company; import java.net.*; import java.io.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; import javax.activation.*; import javax.naming.*; 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(); // Message creation takes care of creating the SOAPPart - a // required part of the message as per the SOAP 1.1 // specification. 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("CustomerDetails")); customerDetails.addChildElement(envelope.createName("Name")) .addTextNode("Joe Bloggs"); customerDetails.addChildElement(envelope.createName("Customer_No")) .addTextNode("1234"); customerDetails.addChildElement(envelope.createName("Telephone")). addTextNode("01234-43525"); SOAPElement purchase = bdy.addBodyElement(envelope.createName("Purchase")); SOAPElement attachment = purchase.addChildElement(envelope.createName("Attachment")); attachment.addAttribute(envelope.createName("href"), "http://www.mystore.com/products/cdplayer.xml"); //cdplayer.xml is assumed to be place under the /WEB-INF directory of the war file. 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(data==null) { data=reqBase + "/cdplayer.xml"; } // Want to set an attachment from the following url. URL url = new URL(data); AttachmentPart ap = msg.createAttachmentPart(new DataHandler(url)); ap.setContentType("text/xml"); ap.setContentLocation("http://www.mystore.com/products/cdplayer.xml"); // Add the attachment part to the message. msg.addAttachmentPart(ap); // Create an endpoint for the recipient of the message. if(to==null) { to=reqBase + "/receiver"; } URLEndpoint urlEndpoint = new URLEndpoint(to); clientMessage += " Sent invoice form succesfully and "; FileOutputStream sentFile = new FileOutputStream("InvoiceForm.msg"); msg.writeTo(sentFile); sentFile.close(); // Send the message to the provider using the connection. SOAPMessage reply = con.call(msg, urlEndpoint); if (reply != null) { FileOutputStream replyFile = new FileOutputStream("PurcahseAcknowledgement.msg"); reply.writeTo(replyFile); replyFile.close(); clientMessage += " received acknolwedgement "; } else { System.err.println("No reply"); clientMessage += " no acknowlegment 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(); } } }