Java Utililty Methods SOAP Message

List of utility methods to do SOAP Message

Description

The list of methods to do SOAP Message are organized into topic(s).

Method

DocumentcreateDoc(SOAPMessage soapMsg)
create Doc
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();
SOAPFaultExceptioncreateException(String code, String message)
create Exception
SOAPFault fault = soapFactory.createFault();
fault.setFaultCode(new QName(code));
fault.setFaultString(message);
return new SOAPFaultException(fault);
SOAPMessagecreateFault(String message)
Given a string message, create a SOAPFault
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage msg = messageFactory.createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
SOAPFault fault = body.addFault();
fault.setFaultCode("Server");
fault.setFaultActor("urn:picketlink");
fault.setFaultString(message);
...
SOAPFaultExceptioncreateSOAPFault(SOAPFault fault, Throwable cause)
create SOAP Fault
SOAPFaultException sfe = new SOAPFaultException(fault);
if (isEnableFaultDetail()) {
    sfe.initCause(cause);
} else {
    sfe.initCause(new Exception());
return sfe;
SOAPMessagecreateSOAPRequestForPartnerCategories(String userId)
create SOAP Request For Partner Categories
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://service.entitlement.siemens.com";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("ser", serverURI);
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
SOAPBody soapBody = envelope.getBody();
...
voidensureNamespaceDeclared(SOAPElement elem, String namespaceURI, String prefix)
ensure Namespace Declared
if (prefix == null || prefix.length() == 0) {
    if (namespaceURI == null || namespaceURI.length() == 0)
        return; 
    if (!namespaceURI.equals(elem.getNamespaceURI(""))) {
        elem.addNamespaceDeclaration("", namespaceURI);
} else {
    if (namespaceURI == null || namespaceURI.length() == 0)
...
StringensureNamespaceDeclared(SOAPElement element, String prefix, String nsURI)
Ensures the given namespace is declared in the scope of the given element.
if (prefix.length() == 0) {
    prefix = getNamespacePrefix(element, nsURI);
    if (prefix == null) {
        prefix = "valueNS";
        element.addNamespaceDeclaration(prefix, nsURI);
else if (!nsURI.equals(element.getNamespaceURI(prefix))) {
...
DocumentextractXMLPayloadFromSOAPMessage(SOAPMessage messageSOAP)
Extracts the payload from a SOAP message.
for (Iterator iter = messageSOAP.getSOAPBody().getChildElements(); iter.hasNext();) {
    Object child = iter.next();
    if (child instanceof SOAPBodyElement) {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Node node = doc.importNode((SOAPBodyElement) child, true);
        doc.appendChild(node);
        return doc;
return null;
StringgetAttachmentContentType(SOAPMessage message)
Returns the content type of the first SOAP attachment or null if there's no attachments.
if (message.countAttachments() == 0) {
    return null;
AttachmentPart att = (AttachmentPart) message.getAttachments().next();
return att.getContentType();
SOAPBodygetBody(SOAPMessage m)
get Body
try {
    return m.getSOAPBody();
} catch (UnsupportedOperationException ex) {
    return m.getSOAPPart().getEnvelope().getBody();