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:
- Creating a SOAP connection
- Creating a SOAP message
- Populating the message
- Sending the message
- 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.
SAAJ and DOM
The SAAJ APIs extend their counterparts in the org.w3c.dom package:
- The Node interface extends the org.w3c.dom.Node interface.
- The SOAPElement interface extends both the Node interface and the org.w3c.dom.Element interface.
- The SOAPPart class implements the org.w3c.dom.Document interface.
- 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
bodyElement.addChildElement("isbn").addTextNode("0672324229");
//Populate the Message
StreamSource preppedMsgSrc = new StreamSource( new FileInputStream("prepped.msg"));
soapPart.setContent(preppedMsgSrc);
//Save the message
message.saveChanges();
//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
String destination = "http://services.xmethods.net:80/soap/servlet/rpcrouter";
//Send the message
SOAPMessage reply = connection.call(message, destination);
//Check the output
System.out.println("\nRESPONSE:\n");
//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);
System.out.println();
//Close the connection
connection.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment