Announcement Announcement Module
Collapse
No announcement yet.
Spring WS: Add custom SOAP header Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Last edited by tellen; Feb 25th, 2011, 06:26 AM.

  • #2
    SoapHeader soapHeader = soapMsg.getSoapHeader();


    His is where you add headers natively. im doint it whithout any OXM tool even if im using Jaxb2 in my Endpoint.
    Return true at the end.

    Comment


    • #3
      Hey Mada,

      Thanks for the reply.

      I've managed to create an interceptor class that creates a nice SOAP response message with custom headers.

      But it all seems to be for nothing: I deploy the WAR on JBoss and the only thing JBoss spits out is an exception ("SOAP Element expected"). Thought it had to do something with the headers I created, but the error occurs when the Spring framework wants to relay/send the response message. The interceptor's "handleResponse()" method is therefore not even being called or reached.

      I know that the interceptor works, though: when testing the webservice locally on my Jetty server I receive a proper SOAP reply message with all custom headers. Not one error at all. So, for now I'm gonna try to find out why this whole does not work on JBoss (4).

      For those who would like to know how I added the custom headers, here's my interceptor implementation:

      Code:
      public class HeaderInterceptor extends EndpointInterceptorAdapter {
      
      	@Override
      	public boolean handleResponse(MessageContext msgContext, Object endpoint) {
      		/* Create headers and add to msg */
      		WebServiceMessage responseMsg = msgContext.getResponse();
      		SoapMessage soapMsg = (SoapMessage) responseMsg;
      		SoapHeader header = soapMsg.getSoapHeader();
      
      		Result result = header.getResult();
      		HeaderDocument hdrDoc = HeaderDocument.Factory.newInstance();
      		Header hdr = hdrDoc.addNewHeader();
      		CustomHeader ch = hdr.addNewCustomHeader();
      		CustomElement ce = ch.addNewCustomElement();
      		ce.setSomeSubElement("value of this sub element");
      
      		XmlBeansMarshaller marshaller = new XmlBeansMarshaller();
      		try {
      			marshaller.marshal(hdrDoc, result);
      		} catch (XmlMappingException e) {
      			e.printStackTrace();
      		} catch (IOException e) {
      			e.printStackTrace();
      		}
      
      		/* Return false? Then response will not be sent back to client */
      		return true;
      	}
      }

      Comment

      Working...
      X