March 1, 2009, 9:09 p.m.
posted by maxidax
Sending SOAP Messages with SAAJBuilding SOAP messages with SAAJ wouldn't be very useful if you didn't send them anywhere. Usually SAAJ is used in combination with JAX-RPC, but it's not dependent on JAX-RPC. SAAJ comes with its own, fairly simple and limited, message-delivery system, which is a part of the basic API. Using SAAJ you can exchange Request/Response-style SOAP messages with a Web service over HTTP. You can also use SAAJ's native message delivery system with any other kind of URL-based protocol, depending on what your vendor supports—but remember that HTTP is the only protocol sanctioned by the Basic Profile. SAAJ's native message-delivery system is so simple that it takes only a few lines of code to send and receive SOAP messages. In fact it's so simple that there is not much to explain. You simply create a SOAPConnection and send the message. SaajExample_8 in Listing 13-28 demonstrates this procedure. -28 Using SAAJ to Send and Receive SOAP Messages
package com.jwsbook.saaj;
import javax.xml.soap.*;
import java.net.URL;
import java.io.FileInputStream;
public class SaajExample_8 {
public static void main(String [] args) throws SOAPException,
java.io.IOException{
// Build a SOAPMessage from a file
MessageFactory msgFactory = MessageFactory.newInstance();
MimeHeaders mimeHeaders = new MimeHeaders();
mimeHeaders.addHeader("Content-Type","text/xml; charset=UTF-8");
FileInputStream file = new FileInputStream("soap.xml");
SOAPMessage requestMsg = msgFactory.createMessage(mimeHeaders, file);
file.close();
// Send the SOAP message to the BookQuote Web service
SOAPConnectionFactory conFactry = SOAPConnectionFactory.newInstance();
SOAPConnection connection = conFactry.createConnection();
URL url = new URL(args[0]);
SOAPMessage replyMsg =connection.call(requestMsg, url);
// Print out the reply message
SaajOutputter.writeToScreen(replyMsg);
}
}
SaajExample_8 builds a SOAP message from a file (soap.xml), then sends the message to the URL you provide as an argument. The SOAPConnection object creates an HTTP connection to the specified URL and sends the SOAP message to the Web service as an HTTP POST message. The HTTP reply that's sent from the Web service back to the SOAPConnection object will contain a SOAP message, a SOAP fault, or an HTTP error code; in the last case a SOAPException is thrown. The SOAP reply message is returned by the SOAPConnection.call() method as a SOAPMessage object, which can be accessed using the SAAJ API. SaajExample_8 writes the reply SOAP message to the screen. It will look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns0="http://www.Monson-Haefel.com/jwsbook/BookQuote" >
<soap:Body>
<ns0:getBookPriceResponse>
<result>24.99</result>
</ns0:getBookPriceResponse>
</soap:Body>
</soap:Envelope>
SAAJ's native messaging service is optional, so your vendor may not support it; if so, the SOAPConnection.call() method may not work. Check your vendor's documentation to determine whether it's supported. |
- Comment