Spring WS: Add custom SOAP header
Hey there,
What's my goal?
I'm rather new to Spring WS, although I have created a few webservices with it. Only this time it is more different: I got a WSDL (and along some XSDs, ofcourse) and those contracts want me to add some custom header elements to the SOAP response. I've been searching the web, tried various code pieces, but it's all without any luck... nothing seems to work properly :(.
What's the problem?
The response SOAP message is quite intact: it has a body (what spring calls a Payload?) and my SOAP client (SOAPUI) receives the response rather well. I've implemented an interceptor with which I can retrieve the response soap message. But here it comes: how should I add new (custom) SOAP headers to the response message?
What's the request?
So, anyhow has an example or a clue of how my Interceptor class should look like?
Any side info?
I'm using XMLBeans. It generates a load of classes based upon the XSDs and among those classes are SOAP header interfaces. I don't know how to add those interfaces/classes to the response message, but as that may be a specific XMLBeans topic I will not elaborate on this now. It's just for your info.
Furthermore: I'm using Maven. Spring version = 1.5.9, XMLBeans version 2.3.0.
My current Intercepter implementation in an empty base form:
Code:
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.dom.DOMResult;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
import org.springframework.ws.soap.AbstractSoapMessage;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class HeaderInterceptor extends EndpointInterceptorAdapter {
@Override
public boolean handleRequest(MessageContext msgContext, Object endpoint)
throws Exception {
}
@Override
public boolean handleResponse(MessageContext msgContext, Object endpoint) {
/* Create headers and add to msg */
WebServiceMessage responseMsg = msgContext.getResponse();
SoapMessage soapMsg = (SoapMessage) responseMsg;
SoapHeader soapHeader = soapMsg.getSoapHeader();
/* Return false? Then response will not be sent back to client */
return true;
}
@Override
public boolean handleFault(MessageContext msgContext, Object endpoint) {
}
}
Thanks for the effort, folks :)