I'm trying to send SOAP request with soap header looks like this:
<SOAP-ENV:Header>
<Security xmlns="http://www.xxx.org/xxx/2003/05">
<UsernameToken><Username>yyyy</Username><Password>xxx</Password>
</UsernameToken></Security></SOAP-ENV:Header>
In order to do it I'm adding header element using SoapActionCallback
SoapActionCallback actionCallBack = new SoapActionCallback("https://aaa.com/bbb.asmx") {
public void doWithMessage(WebServiceMessage msg) {
SoapMessage smsg = (SoapMessage) msg;
smsg.setSoapAction("http://www.xxx.org/yyy/2003/05/SessionCreate");
SoapHeaderElement security = smsg.getSoapHeader().addHeaderElement(new QName("http://www.xxx.org/yyy", "Security"));
security.setText("<UsernameToken><Username>yyyy</Username><Password>xxx</Password></UsernameToken>");
}
};
My problem is that soap header looks like this
<SOAP-ENV:Header><Security xmlns="http://www.xxx.org/yyy/2003/05"><UsernameToken><Username>yyyyy</Username><Password>xxxx</Password></UsernameToken></Security></SOAP-ENV:Header>
And as result my request fails:
How can I add this message correct?
I want the same thing as well. However, as it appears in my own searching, Spring doesn't have full functions like the SOAPMessage in java. What you probably needed was an addChildElement() to the header.
I ended up ditching the SoapMessage building and went with this:
SOAPMessage message = MessageFactory.newInstance();
private void buildHeader(String userName, String password) throws SOAPException{
SOAPHeader header = message.getSOAPHeader();
QName authHeader = new QName(SCHEMA, "AuthenticationHeader", SCHEMA_PREFIX);
SOAPHeaderElement authElement = header.addHeaderElement(authHeader);
QName userNameHeader = new QName(SCHEMA, "UserName", SCHEMA_PREFIX);
SOAPElement userElement = authElement.addChildElement((userNameHeader));
userElement.setTextContent(userName);
QName passwordHeader = new QName(SCHEMA, "Password", SCHEMA_PREFIX);
SOAPElement passwdElement = authElement.addChildElement(passwordHeader);
passwdElement.setTextContent(password);
}
Do note that with the approach above passing a SOAPMessage using sendSourceAndReceiveToResult wraps the SOAPMessage in another envelope and this is not what we want. :(
Update (10/25/2013): I found this solution in another discussion. It's a little vague but worth looking into. I'll try to test this out on my end. One of my colleagues tried out spring-integration-ws and looking in google for the topic and soap headers yielded some promising resources.
Update (10/25/2013): After fooling around with the library I discovered there is a quicker way to wrap the SOAPMessage into Spring's SoapMessage using this:
// message is a SOAPMessage object with custom headers
SoapMessage soapMessage = new SaajSoapMessage(message);
soapMessage.writeTo(System.out);
Perhaps after you build your soapmessage you can route it to spring-ws and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With