import java.io.IOException;
import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import junit.framework.*;
/**
* Example and Unit test that does a simple SOAP dialog to verify that SAAJ is
* set up properly. My goal was to get SAAJ to work with examples different and
* more complex than then ones I was finding in articles.
* <p>
* This code is based upon two examples:
* <dl>
* <dt>The SOAP example used in the docs for the Web Service Console Eclipse Plugin
* <dd>
http://wscep.sourceforge.net/
* <dt>and Nicholas Chase's article Send and receive SOAP messages with SAAJ
* <dd>
http://www-106.ibm.com/developerworks/xml/library/x-jaxmsoap/</dl>
*
* Uses SAAJ to send a message to
*
http://www.ebob42.com/cgi-bin/Romulan.exe/soap/IRoman
*
* @author Chris Baker <ignatz_at_sieve.net>
*/
public class RomanSoap extends TestCase {
public static String TARGET =
"
http://www.ebob42.com/cgi-bin/Romulan.exe/soap/IRoman";
/**
* Yes, this is JUnit 3.7 :-P
*/
public RomanSoap(String name) {
super(name);
}
public void testSAAJ() {
assertTrue(romanExample());
}
public static boolean romanExample() {
/* The original raw SOAP message:
*
* <soap:Envelope xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
* xmlns:xsd="
http://www.w3.org/2001/XMLSchema"
* xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/"
* xmlns:soapenc="
http://schemas.xmlsoap.org/soap/encoding/"
* xmlns:n="x">
* <soap:Body soap:encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
* xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/">
* <n:IntToRoman>
* <Int xsi:type="soapenc:long">1992</Int>
* </n:IntToRoman>
* </soap:Body>
* </soap:Envelope>
*/
boolean success = false;
try {
// First create the connection
SOAPConnection connection =
SOAPConnectionFactory.newInstance().createConnection();
// Next, create the actual message
SOAPMessage message = MessageFactory.newInstance().createMessage();
////////////////////////////////////////////////////////////////////
// ENVELOPE
// Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
// remove the blank header
// The soap server we're talking to chokes on it
SOAPHeader header = message.getSOAPHeader();
header.detachNode();
// Add all of the message's namespace delarations
envelope.addNamespaceDeclaration(
"soapenc",
"
http://schemas.xmlsoap.org/soap/encoding/");
envelope.addNamespaceDeclaration("n", "x");
////////////////////////////////////////////////////////////////////
// BODY
SOAPBody body = envelope.getBody();
// Populate the body
// Create the main element and namespace
SOAPElement bodyElement =
body.addChildElement(
envelope.createName("IntToRoman", "n", "x"));
////////////////////////////////////////////////////////////////////
// ADD CONTENT
// Create the element that will house the integer that we want to
// convert Roman numberals
SOAPElement intChild = bodyElement.addChildElement("Int");
// Add the attribute that will define the type being sent to the
// remote SOAP server.
intChild.addAttribute(
SOAPFactory.newInstance().createName(
"type",
"xsi",
"
http://www.w3.org/2001/XMLSchema-instance"),
"soapenc:long");
// Add the year that we want to translate
intChild.addTextNode("1992");
// Save the message
message.saveChanges();
// Display the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
////////////////////////////////////////////////////////////////////
// DIALOG
// Send the message
SOAPMessage reply = connection.call(message, TARGET);
// Check the output
System.out.println("\nRESPONSE:\n");
// Create the transformer
Transformer transformer =
TransformerFactory.newInstance().newTransformer();
// Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
// Set the output for the transformation
StreamResult result = new StreamResult(System.out);
// Display in to the console
transformer.transform(sourceContent, result);
System.out.println();
// Close the connection
connection.close();
success = true;
} catch (SOAPException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (TransformerConfigurationException e1) {
e1.printStackTrace();
} catch (TransformerException e2) {
e2.printStackTrace();
} catch (Exception e999) {
e999.printStackTrace();
}
return success;
}
public static void main(String[] args) {
romanExample();
}
}
--
Christopher R. Baker
Tune Smith / Programmer
http://cbaker.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_saaj.dev.java.net
For additional commands, e-mail: users-help_at_saaj.dev.java.net