Sunday, October 7, 2012

SOAP With Attachments

SOAP message can handle attachments. there is another api for that SAAJ. SAAJ stands for the "SOAP with Attachments API for Java  -- an offshoot of the Java API for XML Messaging (JAXM) -- automates many of the required steps, such as creating connections or creating and sending the actual messages. This tip chronicles the creation and sending of a synchronous SOAP message.

The process involves five steps:
  1. Creating a SOAP connection
  2. Creating a SOAP message
  3. Populating the message
  4. Sending the message
  5. Retrieving the reply
The figure shows a high-level structure of a SOAP message that has two attachments.

Base on above figure we have following structure of java objects. defined by SAAJ Apis

I. SOAP message

     A. SOAP part

         1. SOAP envelope

              a. SOAP header (optional)

              b. SOAP body

Note: Many SAAJ API interfaces extend DOM interfaces. 

When you create a new SOAPMessage object, it will automatically have the parts that are required to be in a SOAP message. In other words, a newSOAPMessage object has a SOAPPart object that contains a SOAPEnvelope object. The SOAPEnvelope object in turn automatically contains an empty SOAPHeader object followed by an empty SOAPBody object. If you do not need the SOAPHeader object, which is optional, you can delete it. The rationale for having it automatically included is that more often than not you will need it, so it is more convenient to have it provided.

The SAAJ API provides the AttachmentPart class to represent an attachment part of a SOAP message. A SOAPMessage object automatically has a SOAPPart object and its required subelements, but because AttachmentPart objects are optional, you must create and add them yourself. 

If a SOAPMessage object has one or more attachments, each AttachmentPart object must have a MIME header to indicate the type of data it contains. It may also have additional MIME headers to identify it or to give its location. These headers are optional but can be useful when there are multiple attachments.


The SAAJ APIs extend their counterparts in the org.w3c.dom package:

  1. The Node interface extends the org.w3c.dom.Node interface.
  2. The SOAPElement interface extends both the Node interface and the org.w3c.dom.Element interface.
  3. The SOAPPart class implements the org.w3c.dom.Document interface.
  4. The Text interface extends the org.w3c.dom.Text interface.

public class SOAPTip {
   public static void main(String args[]) {
      try {
         // First create the connection
         SOAPConnectionFactory soapConnFactory =  SOAPConnectionFactory.newInstance();
         SOAPConnection connection =  soapConnFactory.createConnection();
         //Next, create the actual message
         MessageFactory messageFactory = MessageFactory.newInstance();
         SOAPMessage message = messageFactory.createMessage();
         //Create objects for the message parts            
         SOAPPart soapPart =     message.getSOAPPart();
         SOAPEnvelope envelope = soapPart.getEnvelope();
         SOAPBody body =         envelope.getBody();

//Populate the body
        //Create the main element and namespace
        SOAPElement bodyElement =   body.addChildElement(envelope.createName("getPrice" , 
                                                                "ns1", "urn:xmethods-BNPriceCheck"));
        //Add content

//Populate the Message
        StreamSource preppedMsgSrc = new StreamSource( new FileInputStream("prepped.msg"));

        //Save the message

        //Check the input
//Send the message and get a reply   
        //Set the destination
        String destination =  "";
        //Send the message
        SOAPMessage reply =, destination);
        //Check the output
        //Create the transformer
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer =  transformerFactory.newTransformer();
        //Extract the content of the reply
        Source sourceContent = reply.getSOAPPart().getContent();
        //Set the output for the transformation
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
         //Close the connection            
        } catch(Exception e) {

No comments: