|
Listing 1. This simple consumer—written using SAAJ—accesses a synchronous WebLogic integration process to get the temperature at a given city. private static void invokeService(
String serviceUrl, String inputXml)
{
SOAPMessage reply = null;
try
{
//Create Soap Connection
SOAPConnectionFactory scf =
SOAPConnectionFactory.newInstance();
SOAPConnection con = scf.createConnection();
//Create Message
MessageFactory mf =
MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
// Get the SOAPPart from the message
SOAPPart soapPartInput = msg.getSOAPPart();
// Creating an attachment Part
AttachmentPart attachPart =
msg.createAttachmentPart();
attachPart.setContent(
"This is a text attachment", "text/plain");
// Create and set the content from the
// input XML document
StringReader stringReaderInput =
new StringReader(inputXml);
StreamSource ssInput =
new StreamSource(stringReaderInput);
soapPartInput.setContent(ssInput);
// Save the changes to the message
msg.saveChanges();
// Invoke the service synchronously and
// get the reply
reply = con.call(msg, serviceUrl);
// Get the SOAP body from the response
SOAPBody outSoapBody =
reply.getSOAPPart().getEnvelope().getBody();
// Check to see if there is any SOAP Fault
if (outSoapBody.hasFault())
{
// Take any actions for the exception scenario
SOAPFault fault = outSoapBody.getFault();
// Log the fault details
System.out.println("SOAP Fault occurred");
System.out.println("Fault Code is " +
fault.getFaultCode());
System.out.println("Fault String is " +
fault.getFaultString());
}
else
{
// Print the output SOAP envelope
System.out.println(
reply.getSOAPPart().getEnvelope());
}
// Close the connection
con.close();
}
catch (SOAPException e)
{
// Print the stack trace if there are any
// SOAPExceptions
e.printStackTrace();
}
}
|