/* * Copyright 2 1 by Sun Microsystems, Inc., * 9 1 San Antonio Road, Palo Alto, California, 943 3, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information" . You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package com.sun.xml.messaging.examples; import javax.xml.messaging.*; import javax.xml.soap.*; import javax.xml.transform.dom.DOMSource; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.activation.DataHandler; import javax.servlet.http.*; import javax.servlet.*; import java.io.IOException; import java.net.URL; import org.w3c.dom.Document; public class SampleServlet extends HttpServlet { ConnectionFactory connectionFactory; public void init(ServletConfig servletConfig) throws ServletException { //Get the servlet context to lookup a connection factory for a //particular client id. When done in a standalone application //or even in servlets a more sophisticated mechanism like //JNDI could be used to lookup connection factories. ServletContext ctxt = servletConfig.getServletContext(); //Lookup connection factory for Client1 and store it to //create connection to the provider to send messages. connectionFactory = (ConnectionFactory) ctxt.getAttribute("Client1"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //A request comes in to send a message. So we create //a connection from the factory. Connection connection = connectionFactory.createConnection(); //Create a message factory from the Connection to //produce messages. MessageFactory mf = connection.createMessageFactory(); //Create messgages from the message factory. Message message = mf.createMessage(); //Get the SOAPPart from the message. //Note the infrastructure takes care of creating the //SOAPPart. The user needs to get the SOAPPart and //get the SOAPEnvelope and populate it appropriately. SOAPPart soapPart = message.getSOAPPart(); //Get the SOAPEnvelope from the header container //obtained above. SOAPEnvelope soapEnvelope = soapPart.getSOAPEnvelope(); //Create a DOMSource. DOMSource domsrc = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("file:///foo.bar/soap.xml"); domsrc = new DOMSource(doc); } catch(Exception e { System.err.println("Error in creating DOMSource" + e.getMessage()); } //Use the DOMSource created above to set the contents of //the SOAPEnvelope. soapEnvelope.setContent(domsrc); //Create an attachment and add it to the message. URL url = new URL("http://foo.bar/img.jpg"); DataHandler dh = new DataHandler(url); AttachmentPart ap = message.createAttachmentPart(dh); message.addAttachmentPart(ap); //Create an Endpoint passing it the URI of the client with //which this client wants to exchange messages. Endpoint endPoint = new Endpoint("http://foo.bar/Service"); connection.send(message, endPoint); response.setStatus(HttpServletResponse.SC_OK); } catch(Exception jxe { jxe.printStackTrace(); } } }