El API JAXM

El siguiente ejemplo, basado en Servelt, demuestra el uso de JAXM en un caso donde se consumen mensaje SOAP (sin attachments): SOAPListener.java

package com.sun.xml.messaging.examples;

import javax.xml.messaging.*; 
import javax.xml.soap.*; 
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.activation.DataHandler;
import java.io.*; 
import org.w3c.dom.*; 
import org.xml.sax.SAXException;

public class SOAPListener extends JAXMServlet { 

    public void onMessage(Message message)  { 
        System.out.println("onMessage of MyListener invoked"); 
        try { 
            //Get the first part which is the SOAPPart. 
            SOAPPart soapPart = message.getSOAPPart(); 
            SOAPEnvelope soapEnvelope = soapPart.getSOAPEnvelope();
            DOMSource domSrc = (DOMSource)  soapEnvelope.getContentAs(DOMSource.FEATURE);

            //Now use the dom tree to actually get traverse and 
            //get some of the fields from it.
            Document doc = (Document) domSrc.getNode();
            Element root = doc.getDocumentElement(); 
            NodeList list = root.getElementsByTagName("GetLastTradePriceDetailed"); 
            Element element; 
            Element childElement; 
            for(int i = ; i < list.getLength(); i++ ) { 
                if (!(list.item(i  instanceof Element))  { 
                    continue; 
                } 
                element = (Element) list.item(i); 
                NodeList list2 = element.getChildNodes(); 
                for(int j =  ; j < list2.getLength() ; j++)  { 
                    if(!(list2.item(j  instanceof Element))  { 
                        continue; 
                    } 
                    childElement = (Element) list2.item(j); 
                    System.out.println(childElement.getTagName()); 
                    System.out.println("\t"); 
                    System.out.println( ((Text) childElement.getFirstChild()) .getData());
                    System.out.println("\n"); 
                } 
            }
        } catch(Exception jxe)  { 
            jxe.printStackTrace(); 
        } 
    } 
} 

.�Productor de Mensajes (sin attachments)

El siguiente ejemplo, basado en Servelt, demuestra el uso de JAXM en un caso donde se producen mensajes SOAP (sin attachments): SOAPServlet.java

package com.sun.xml.messaging.examples;
import javax.xml.messaging.*; 
import javax.xml.soap.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.parsers.*; 
import javax.servlet.http.*; 
import java.io.IOException;
import org.w3c.dom.Document;

public class SOAPServlet 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 SOAPHeader from the SOAPEnvelope 
            SOAPHeader soapHeader = soapEnvelope.createSOAPHeader();

            //Create a SOAPHeaderElement that will be appended to the 
            //SOAPHeader. 
            SOAPHeaderElement she = soapHeader.createSOAPHeaderElement(); 
            she.setName("from", "http://foo.bar/", "m"); 
            she.addContent("[email protected]"); 
            soapHeader.addContent(she); 
            soapEnvelope.setSOAPHeader(soapHeader);

            //Similarly create SOAPBody with SOAPBodyElements and append 
            //that to the soapEnvelope..
            SOAPBody soapBody = soapEnvelope.createSOAPBody(); 
            SOAPBodyElement sbe = soapBody.createSOAPBodyElement(); 
            sbe.setName("purchaseOrder", "http://foo.bar.com", "po"); 
            sbe.addContent("purchase order for a chair"); 
            soapEnvelope.setSOAPBody(soapBody);
            Endpoint endPoint = new Endpoint("http://foo.bar/Service"); 
            connection.send(message, endPoint); 
            response.setStatus(HttpServletResponse.SC_OK); 
        } catch(Exception jxe  { 
            jxe.printStackTrace( ; 
        } 
    } 
} 

.�Productor de Mensajes (con attachments)

El siguiente ejemplo, basado en Servelt, demuestra el uso de JAXM en un caso donde se producen y se env�an mensajes SOAP (con attachments) a un compa�ero de negocios: SampleServlet.java

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(); 
        } 
    } 
} 

.�Productor de Mensajes (con attachments)

El siguiente ejemplo, basado en Servelt, demuestra el uso de JAXM en un caso donde se se reciben y procesan mensajes SOAP (con attachments) enviados por un compa�ero de negocios: MyListener.java

package com.sun.xml.messaging.examples;
import javax.xml.messaging.*; 
import javax.xml.soap.*; 
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.activation.DataHandler;
import java.io.InputStream; 
import java.io.IOException;
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.xml.sax.SAXException;

public class MyListener extends JAXMServlet { 
    Connection connection; 
    public void init(Connection connection) { 
        this.connection = connection; 
    } 

    public void destroy()  { 
    }

    public void onMessage(Message message) { 
        System.out.println(" onMessage of MyListener invoked"); 
        try { 
            //Get the first part which is the SOAPPart. 
            SOAPPart soapPart = message.getSOAPPart(); 
            SOAPEnvelope soapEnvelope = soapPart.getSOAPEnvelope();
            DOMSource domSrc = (DOMSource) soapEnvelope.getContentAs(DOMSource.FEATURE);
            
            //Now use the dom tree to actually get traverse 
            //and get some of the fields from it. 
            Node node = domSrc.getNode();

            //Now start manipulating the attachments 
            int count = message.countAttachments(); 
            System.out.println("No. of attachments " + count); 
            AttachmentPart ap = null; 
            for(int i =  ; i < count; i++)  { 
                ap = message.getAttachmentPart(i) ; 
                if(ap.getDataHandler().getContentType().equals("text/xml"))  { 
                    StreamSource streamSource = (StreamSource) ap.getContent(); 
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
                    dbf.setValidating(true); 
                    DocumentBuilder db = dbf.newDocumentBuilder(); 
                    Document doc = db.parse(streamSource.getInputStream()); 
                } 
            } 
        } catch(JAXMException jxe  { 
            jxe.printStackTrace(); 
        } catch(IOException ioe  { 
            ioe.printStackTrace(); 
        } catch(ParserConfigurationException pce  { 
            pce.printStackTrace(); 
        } catch(SAXException se  { 
            se.printStackTrace(); 
        } catch(SOAPException spe  { 
            spe.printStackTrace(); 
        } 
    }
} 

COMPARTE ESTE ARTÍCULO

COMPARTIR EN FACEBOOK
COMPARTIR EN TWITTER
COMPARTIR EN LINKEDIN
COMPARTIR EN WHATSAPP
ARTÍCULO ANTERIOR

SIGUIENTE ARTÍCULO