Announcement Announcement Module
Collapse
No announcement yet.
Any way to add a custom header to SOAP message? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Any way to add a custom header to SOAP message?

    Hi, I've searched through previous posts, but have not found anything that works. I need to add a custom header from a Spring-WS client. Something like WebServiceMessageCallback seems perfect, except that the only header you can set is SOAPAction. Is there anything similar that would let me add an arbitrary header?

  • #2
    There are a number of reasons you may want to add custom headers to a SOAP message:

    Passing security information outside the body, so the code handling the security is uncoupled from the business logic;
    Adding timestamps;
    Everything else which is not actually part of the message, but is required for/used by the infrastrcture.

    Comment


    • #3
      Thanks for reading, but I guess you're just agreeing with me that it should be possible to do this?

      Comment


      • #4
        So I ended up extending a messagefactory and a soapMessage (axiom in my case) so that I could intercept the setting of the SoapAction header and insert mine (a cookie).

        Arjen and others, please consider adding an addHeader(String name, String value) method to SoapMessage. Only being able to set the SoapAction header is very limiting.

        Here's what I did with the Axiom classes:
        Code:
        public class CustomAxiomSoapMessage extends AxiomSoapMessage {
        
            private String cookie;
        
            public CustomAxiomSoapMessage(SOAPFactory soapFactory) {
                super(soapFactory);
            }
        
            public CustomAxiomSoapMessage(SOAPFactory soapFactory, boolean payloadCaching) {
                super(soapFactory, payloadCaching);
            }
        
            public CustomAxiomSoapMessage(SOAPMessage soapMessage, String soapAction, boolean payloadCaching) {
                super(soapMessage, soapAction, payloadCaching);
            }
        
            public CustomAxiomSoapMessage(SOAPMessage soapMessage, Attachments attachments, String soapAction, boolean payloadCaching) {
                super(soapMessage, attachments, soapAction, payloadCaching);
            }
        
            public void writeTo(OutputStream outputStream) throws IOException {
                // set cookie before writing out
                if (outputStream instanceof TransportOutputStream) {
                    TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
                    if (cookie != null) {
                        transportOutputStream.addHeader("Cookie", cookie);
                    }
                }
                super.writeTo(outputStream);
            }
        
            public void setCookie(String cookie) {
                this.cookie = cookie;
            }
        }
        
        public class CustomAxiomSoapMessageFactory extends AxiomSoapMessageFactory {
        
            private boolean payloadCaching = true;
            // use SOAP 1.1 by default
            private SOAPFactory soapFactory = new SOAP11Factory();
        
            public WebServiceMessage createWebServiceMessage() {
                return new CustomAxiomSoapMessage(soapFactory, payloadCaching);
            }
        
            public void setSoapVersion(SoapVersion version) {
                super.setSoapVersion(version);
                if (SoapVersion.SOAP_11 == version) {
                    soapFactory = new SOAP11Factory();
                }
                else if (SoapVersion.SOAP_12 == version) {
                    soapFactory = new SOAP12Factory();
                }
                else {
                    throw new IllegalArgumentException(
                            "Invalid version [" + version + "]. " + "Expected the SOAP_11 or SOAP_12 constant");
                }
            }
        
            public void setPayloadCaching(boolean payloadCaching) {
                super.setPayloadCaching(payloadCaching);
                this.payloadCaching = payloadCaching;
            }
        
        }

        Comment


        • #5
          Arjen and others, please consider adding an addHeader(String name, String value) method to SoapMessage. Only being able to set the SoapAction header is very limiting.
          I suggest you create a Jira issue for that. Please specify that by header you mean transport-specific header (http in this case).
          Thanks!

          Comment


          • #6
            Done! Please vote for it if you need something similar.
            http://jira.springframework.org/browse/SWS-479

            Comment

            Working...
            X