1

I am trying to modify a soap header, and i want header to be like this

<soapenv:Header xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ns:authnHeader soapenv:mustUnderstand="0" xmlns:ns="http://webservices.averittexpress.com/authn">
                <Username>xxxxxxxx</Username>
                <Password>xxxxxxxx</Password>
            </ns:authnHeader>

This is what i have done till now...

SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();

    header.addAttribute(new QName("xmlns:soapenc"), "http://schemas.xmlsoap.org/soap/encoding/");
    header.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
    header.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");

SOAPElement authnHeader = header.addChildElement("authnHeader", "ns" , "http://webservices.averittexpress.com/authn");

authnHeader.addAttribute(new QName("soapenv:mustUnderstand"), "0");

But I am getting

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

at first header.addAttribute.

Please help.

My Import Statements

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.PortInfo;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50

2 Answers2

2

You are getting this error because you are trying to define namespace attributes in the SOAP header. Namespace attributes xmlns must be defined in the SOAP envelope. So the XML SOAP envelope you really want would look something like this:

<soap:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Header>
        <ns:authnHeader soapenv:mustUnderstand="0" xmlns:ns="http://webservices.averittexpress.com/authn">
            <Username>xxxxxxxx</Username>
            <Password>xxxxxxxx</Password>
        </ns:authnHeader>
    </soap:Header>
    <soap:Body>
        <!-- your content goes here -->
    </soap:Body>
</soap:Envelope>

According to conventions if your XML does not give a SOAP namespace in the envelope applications may reject your SOAP message.

For reference, I spent about 3 hours trying to find one code example where someone calls header.addAttribute() on a SOAP header, and I could not find even one.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Finally had some luck.

This code works

Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

if(outboundProperty.booleanValue())
{

   try
    {
         SOAPHeader header = context.getMessage().getSOAPPart().getEnvelope().getHeader();

         SOAPFactory soapFactory = SOAPFactory.newInstance();
         SOAPElement authnHeader = soapFactory.createElement("authnHeader", "ns", "http://webservices.averittexpress.com/authn");

         SOAPElement username = authnHeader.addChildElement("Username");
         username.setTextContent("xxxxxxx");

         SOAPElement password = authnHeader.addChildElement("Password");
         password.setTextContent("xxxxxxx");

         header.addChildElement(authnHeader);
    }
    catch(Exception e)
    {
       e.printStackTrace();
    }
}

After adding header do not forget to save the message

context.getMessage().saveChanges();

or

context.getMessage().writeTo(System.out);

also saves message if any changes are done.

Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50