Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add child elements to custom SOAP header in Spring-WS

I am calling a SOAP webservice with Spring-WS. The webservice in question requires me to pass some information in the SOAP header as shown here:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <CustomHeaderElement>
         <clientID>xyz</clientID>
         <wsdlVersion>1.0</wsdlVersion>
         <serviceType>ExampleService_v1</serviceType>
      </CustomHeaderElement>
   </soapenv:Header>
   <soapenv:Body>
   ...
   </soapenv:Body>
</soapenv:Envelope>

I've figured out how to had the top level CustomHeaderElement, but I don't see anything in the Spring-WS API that allows me to add a child element. Here is what I have so far:

WebServiceTemplate template = ...;

template.marshalSendAndReceive(request, new WebServiceMessageCallback(){
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException{
        SoapMessage soapMessage = (SoapMessage)message;
        SoapHeader soapHeader = soapMessage.getSoapHeader();
        QName qName = new QName("CustomHeaderElement");
        SOAPHeaderElement headerElement = soapHeader.addHeaderElement(qName);
        //would like to do something like headerElement.addChild(clientIdNode);
    }
});

The problem is headerElement doesn't seem to expose any means of actually adding a child. I know I can add an attribute, but that's not what I need for this service call. Does anyone know how I could add the necessary child elements to my custom header?

like image 370
Mike Deck Avatar asked Oct 27 '10 16:10

Mike Deck


People also ask

How do you add elements to a SOAP header in Java?

SOAPHeader header = message. getSOAPHeader(); QName headerName = new QName("http://ws-i.org/schemas/conformanceClaim/", "Claim", "wsi"); SOAPHeaderElement headerElement = header. addHeaderElement(headerName); headerElement. addAttribute(new QName("conformsTo"), "http://ws-i.org/profiles/basic/1.1/");

What is mustUnderstand attribute in SOAP header?

"mustUnderstand" means that the header must be parsed and consumed before the normal SOAP body process. It will be marked for WS-Security, Session or other protocol related Headers. This one is a custom defined header.

How do I add a header in WSDL?

You can add soap header information to method calls by decorating the methods in the proxy class generated from the wsdl with the SoapHeader attribute. For example wsdl.exe will generate client proxy class Reference. cs for the web service reference when you "Add Web Reference".


1 Answers

I came across the same issue, here's my solution but it will work only simple elements not for complex:

template.marshalSendAndReceive(request, new WebServiceMessageCallback(){
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException{
        SaajSoapMessage soapMessage = (SaajSoapMessage) message;
        SoapHeaderElement messageId =  soapMessage.getSoapHeader().addHeaderElement(new QName("http://www.w3.org/2005/08/addressing", "MessageID", "wsa"));
        messageId.setText("urn:abcdef1234");
    }
});

it produces following XML:

<SOAP-ENV:Header>
  <wsa:MessageID>urn:abcdef1234</wsa:MessageID>
</SOAP-ENV:Header>

BTW javax.xml.soap.SOAPMessage can work too, see here: http://docs.oracle.com/javaee/5/tutorial/doc/bnbhr.html#bnbia

like image 162
hit3k Avatar answered Sep 19 '22 15:09

hit3k