Jannar Molden said:
Hello.
I'm learning the XML SOAP with Java SAAJ library. This is what I need
to create:
<SOAP-ENV:Body>
<axl:getPhone xmlns:axl="
http://www.cisco.com/AXL/1.0
xsi:schemaLocation="
http://www.cisco.com/AXL/1.0
http://gkar.cisco.com/schema/axlsoap.xsd sequence="1234">
<phoneName>SEP003094C40973</phoneName>
</axl:getPhone>
</SOAP-ENV:Body>
I have managed to create all previous elements, but this Body element
is confusing. I only get the
"<axl:getPhone xmlns:axl="
http://www.cisco.com/AXL/1.0">
When using
Name bodyName = envelope.createName("getPhone", "axl",
"
http://www.cisco.com/AXL/API/1.0");
SOAPBodyElement getPhone = body.addBodyElement(bodyName);
but how do I add those two other parameters (xsi:schemaLocation and
sequence)?
Regards,
Jannar
I have to do manipulation of the body for security purposes. The way I
do it is with Axis Handlers. Here's a sample chapter that shows how to
use a handler with SAAJ.
http://www.j2ee-security.net/book/sample-chap/
Particulaly useful in your case would be the SOAPUtility class, which
gets gets the SOAPMessage to Document. Basically it goes like:
public static Document toDocument(SOAPMessage soapMsg) throws
TransformerConfigurationException, TransformerException,
SOAPException, IOException {
Source src = soapMsg.getSOAPPart().getContent();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMResult result = new DOMResult();
transformer.transform(src, result);
return (Document)result.getNode();
}
Once you have that, you can do something like the code below. Its for
the header, but something similair should work fot the body:
//Add this header to the SOAP message if it does not exist
String soap_header =
"
http://schemas.xmlsoap.org/soap/envelope/";
/******************* XML SIGNATURE INIT ***********************
Append the signature element to proper location before signing
***************************************************************/
// Look for the SOAP header
Element headerElement = null;
NodeList nodes = doc.getElementsByTagNameNS (soap_header,
"Header");
//No nodes are expected to be found (length of zero) - add
//header here.
if(nodes.getLength() == 0)
{
headerElement = doc.createElementNS (soap_header, "Header");
nodes = doc.getElementsByTagNameNS (soap_header, "Envelope");
if(nodes != null)
{
Element envelopeElement = (Element)nodes.item(0);
headerElement.setPrefix(envelopeElement.getPrefix());
envelopeElement.appendChild(headerElement);
}
}
else
{
//This shouldn't happen unless explicity done elsewhere
System.err.println("Unexpectedly Found " + nodes.getLength() +
" SOAP Header elements... probably ok but not tested");
headerElement = (Element)nodes.item(0);
}
This next part is perhaps a breed apart as its creating a digital
signature, but it might give some insight on what you´re trying to do:
//
http://xml-security is the base-uri, which needs to be
//unique within document
XMLSignature sig = new XMLSignature(doc, "
http://xml-security",
XMLSignature.ALGO_ID_SIGNATURE_DSA);
// Add SOAP Body to XML Signature
headerElement.appendChild(sig.getElement());
// Due to the bug in the Apache security lib, it does not allow
// us to sign whole message and make "enveloped-signature"
// transform - strictly part of the specification.
// Only sign the body - IT IS NOT CONFORMED TO THE SPEC!!!!!!
//
// Neat trick: since the XMLSignature is actually a part of the
// SOAP XML document, the SOAP body is referenced as a URI
// fragment
sig.addDocument("#Body");
HTH,
iksrazal
http://www.braziloutsource.com/