Results 1 to 5 of 5

Thread: Soap envelope header and WebServiceMessageCallback

  1. #1
    Join Date
    Aug 2007
    Posts
    12

    Question Soap envelope header and WebServiceMessageCallback

    I am trying to add a customised header to my soap message and I use a WebServiceMessageCallback.soWithMessage to add it.

    I've tried several things, influenced by examples on this forum but no luck yet. They do something to the message but not actually what I was hoping to see.
    Code:
    ...
        SoapMessage soapMessage = (SoapMessage) message;
        SoapHeader header = soapMessage.getSoapHeader();
        
        StringBuffer sbHeader = new StringBuffer();
        sbHeader.append("<hdrns:MyHeader xmlns:hdrns=\"http://www.company.com/foo/bar/MyHeader.xsd\" ID=\"myHdr\">")
            .append("<hdrns:MessageID>")
            .append("1234")
            .append("</hdrns:MessageID>")
            .append("</hdrns:MyHeader>");
        
        StringSource mefHeaderSource = new StringSource(sbHeader.toString());
        SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(mefHeaderSource, soapHeader.getResult());
    ...
    The above is one of the example I found here and the result I got was:
    Code:
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <soapenv:Header>
          <hdrns:MyHeader soapenv:mustUnderstand="0" xmlns:hdrns="http://www.company.com/foo/bar/MyHeader.xsd"/>
       </soapenv:Header>
       <soapenv:Body>
    Huh? It obviously added something to the header because the top element is there but not the child element. I must have missed a step.

    I haven't specified the message handler and it defaulted to Saaj.

    Spring-ws 1.0.2
    saaj-1.4

    Thanks

  2. #2
    Join Date
    Jul 2005
    Location
    Rotterdam, the Netherlands
    Posts
    1,562

    Default

    I can't find any error in the code, it looks good.

    As an alternative, you could try:
    Code:
    SoapHeaderElement headerElement = soapHeader.addHeaderElement(new QName("http://www.company.com/foo/bar/MyHeader.xsd", "MyHeader");
    headerElement.addAttribute(new QName("ID"), "myHdr");
    StringBuffer sbHeader = new StringBuffer();
    sbHeader.append("<hdrns:MessageID xmlns:hdrns=\"http://www.company.com/foo/bar/MyHeader.xsd\")
            .append("1234")
            .append("</hdrns:MessageID>");
        
        StringSource mefHeaderSource = new StringSource(sbHeader.toString());
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(mefHeaderSource, headerElement.getResult());
    Arjen Poutsma

    Spring Web Services Dev Lead
    Please read the FAQ

  3. #3
    Join Date
    Aug 2007
    Posts
    12

    Default

    That does work just fine. It allows me to put one tag only under the header so I get this:
    Code:
    <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    	<eai:applicationContext 
    		soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" 
    		xmlns:eai="http://www.xxx.co.nz/EAI/UMTS_ICMSAdapter">
    		<hdrns:MessageID xmlns:hdrns="http://www.company.com/foo/bar/MyHeader.xsd">1234</hdrns:MessageID>
    	</eai:applicationContext>
    </soapenv:Header>
    But I actually have three tags to add at the level of hdrns:MessageID.
    So this is what my code look like now (to produce the above header):
    Code:
    SoapMessage soapMessage = (SoapMessage) message;
    SoapHeader soapHeader = soapMessage.getSoapHeader();
    SoapHeaderElement headerElement = soapHeader.addHeaderElement(new QName(WebServiceHelper.EAI_NAMESPACE,"applicationContext","eai"));
    headerElement.setMustUnderstand(false);
    StringBuffer sbHeader = new StringBuffer();
    sbHeader.append("<hdrns:MessageID xmlns:hdrns=\"http://www.company.com/foo/bar/MyHeader.xsd\">")
                                .append("1234")
                                .append("</hdrns:MessageID>");
                            
    StringSource mefHeaderSource = new StringSource(sbHeader.toString());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(mefHeaderSource, headerElement.getResult());
    If I add another tag to the list in the StringBuffereg:
    Code:
           <hdrns:MessageID xmlns:hdrns="http://www.xxx.com/x.xsd">1234</hdrns:MessageID>
           <hdrns:TransactionID xmlns:hdrns="http://www.xxx.com/x.xsd">1234</hdrns:TransactionID>
    Then I get problems with the XML not being well formed so (understandably) the transform doesn't work (I tried it anyway, just in case).

    Is there any way to make a multi-tag element and put it into the header?

  4. #4
    Join Date
    Aug 2007
    Posts
    12

    Unhappy Tried latest version

    I also just tried it with spring-ws-1.5.0 and got the same problem.
    It seems to want saxon 9 although xalan 2.7.0 is available so I gave it that.
    Also using saaj 1.4 and xerces 2.8.1

  5. #5
    Join Date
    Aug 2007
    Posts
    12

    Smile

    I gave up on doing it with a transformer and found this works:
    Code:
                        SoapMessage soapMessage = (SoapMessage) message;
                        SoapHeader soapHeader = soapMessage.getSoapHeader();
                        DOMResult result = (DOMResult) soapHeader.getResult();
                        Node n = result.getNode();
                        Element applicationContext = n.getOwnerDocument()
                                .createElementNS(WebServiceHelper.EAI_NAMESPACE,
                                        "applicationContext");
    
                        Element application = n.getOwnerDocument().createElementNS(
                                WebServiceHelper.EAI_NAMESPACE, "application");
                        applicationContext.appendChild(application);
                        application.appendChild(n.getOwnerDocument()
                                .createTextNode(
                                        getWebServiceHeader().getApplication()));
    
                        Element user = n.getOwnerDocument().createElementNS(
                                WebServiceHelper.EAI_NAMESPACE, "user");
                        user.appendChild(n.getOwnerDocument().createTextNode(
                                getWebServiceHeader().getUser()));
                        applicationContext.appendChild(user);
                        n.appendChild(applicationContext);
    Once I noticed that the soapHeader.getResult() returned a DOMResult I realised I had that option (I ought to have noticed earlier, of course).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •