Announcement Announcement Module
Collapse
No announcement yet.
How to add XML elements to the SOAP Header? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to add XML elements to the SOAP Header?

    Hello,

    I would like to use Spring WS to write a Webservice client.

    The special webservice which I want to get data from has the requirement, that I transfer an access-token contained in the SOAP header. So the request must look like this:

    Code:
    <Envelope xmlns="http://www.w3.org/2003/05/soap-envelope">
      <Header>
        <Authorization xmlns="xxx">
    		<Token>[Access Token]</Token>
        </Authorization>
      </Header>
      <Body>
    	<!-- SOAP Payload here... -->
      </Body>
    </Envelope>

    I think I have to use a WebServiceMessageCallback to manipulate the SOAP Header, adding the "<Authorization>..." element and sub elements.

    What I have so far is:

    Code:
    WebServiceMessageCallback webServiceMessageCallback = new WebServiceMessageCallback()
    {
    	@Override
    	public void doWithMessage( WebServiceMessage webServiceMessage ) throws IOException, TransformerException
    	{
    		SaajSoapMessage soapMessage = (SaajSoapMessage)webServiceMessage;
    		soapMessage.setSoapAction( "..." );
    		
    		soapMessage.getSoapHeader().... // ??
    	}
    };
    I'm stuck here - I hope this is the right way to add an element to the SOAP header? But what are the next steps, how may the code look to add the desired "<Authorization>..." block (see above) to the SOAP header?

    I really appreciate any help you can offer

    Thanks a lot!

  • #2
    Hi patb!
    I managed to do what you are asking about using an interceptor.
    The structure of my interceptor is described in another thread:
    http://forum.springsource.org/showth...-SOAP-envelope

    What you need is not described there.
    Here is the missing part, from the handleResponse method.

    Code:
    		SoapMessage response2 = (SoapMessage) messageContext.getResponse();
    		SoapHeader responseSoapHeader2 = response2.getSoapHeader();
    
    		String htngHeaderString = "<HTNGHeader xmlns=\"http://htng.org/1.1/Header/\">"
    				+ "<From>"
    				+ "<systemId>PMS</systemId>"
    				+ "<Credential>"
    				+ "<userName>xxx</userName>"
    				+ "<password>yyy</password>"
    				+ "</Credential>"
    				+ "</From>"
    				+ "<To>"
    				+ "<systemId>zzz</systemId>"
    				+ "<address>https://myurl</address>"
    				+ "</To>"
    				+ "<transactionId>"
    				+ transactionId
    				+ "</transactionId>"
    				+ "<action>response</action>" + "</HTNGHeader>";
    
    		TransformerFactory transformerFactory =
    				TransformerFactory.newInstance();
    		Transformer transformer = transformerFactory.newTransformer();
    		StringSource htngHeaderStringSource = new StringSource(htngHeaderString);
    
    		try {
    			transformer.transform(htngHeaderStringSource,
    					responseSoapHeader2.getResult());
    		} catch (SOAPProcessingException e) {
    			e.printStackTrace();
    		}
    I think you do not need to switch from SAAJ to Axiom as I did, but I'm not sure now.

    Hope this helps.
    Cheers,
    Vito Meuli

    Comment


    • #3
      Hello Vito,

      thanks a lot for your answer - and I'm very sorry for my late one.

      Your tip was great, I managed to add my XML code to the SOAP header that way.

      So the solution is to have a prepared XML-string. What if I want to build up the DOM tree programmatically and manipulate the soap header this way?

      I could to something like
      Code:
      Node node = soapMessage.getDocument().getChildNodes().item( 0 ).getChildNodes().item( 0 );
      and start manipulating it - but it seems I have no idea how to add a new XML element that way. I'm fairly new to XML in Java, you must know.

      Do you know how to do that programmatically instead of having a prepared XML-string?

      And as a side-question: how do I add more than one XML element? In your example, you are using the transformer with the "responseSoapHeader2.getResult()" as the destination, so your XML gets written as the whole new content for the SOAP header. But what, when I need to add two new elements, one after another, to the SOAP header?


      Thanks a lot for your answer
      Last edited by patb; Jul 31st, 2011, 04:31 PM.

      Comment


      • #4
        Hi patb,
        unfortunately I'm not so skilled in XML and Java to help you further.
        Your question are interesting ones, and I'd like to know the answers as well.

        About adding more than one XML element, I'd try it out writing a longer string and seeing what happens :-)
        Or you can read the API documentation around the classes and methods already tested.

        I don't know how to add elements programmatically.
        If you find a way, please share your solution with us.

        Cheers,
        Vito

        Comment


        • #5
          I recently tried to do that via JDOM - build up a DOM tree using JDOM methods and classes (very easy to use) and then use the JDOM's DOMBuilder to build a w3c Document. I then tried to set that as the header... but well, it didn't work and I can't say why, unfortunately. Maybe I can try that with JDOM again and then use a TransformerFactory/Transformer to write the XML into the header's result-object.

          Comment


          • #6
            Hi Patb ,

            Following may be helpful to achieve your target :
            <code>

            SOAPEnvelope soapEnvelope = ((SaajSoapMessage) webServiceMessage)
            .getSaajMessage().getSOAPPart()
            .getEnvelope();

            SaajSoapMessage soapMessage = (SaajSoapMessage)webServiceMessage;
            SOAPHeaderElement soapHeaderElement = soapMessage.getSaajMessage().getSOAPHeader().addHe aderElement(soapEnvelope.createName("Authorization "));
            soapHeaderElement.addAttribute(soapEnvelope.create Name("xmlns"), "");
            soapHeaderElement.addChildElement(soapEnvelope.cre ateName("Token"));


            </code>

            Cheers
            Mohan

            Comment


            • #7
              Hello Mohan,

              thanks a lot! That was really helpful

              Comment


              • #8
                Following resource could serve as a starting point
                http://www.w3schools.com/soap/soap_header.asp .

                Happy Learning

                Cheers
                Mohan

                Comment


                • #9
                  Originally posted by mohareddy View Post
                  Hi Patb ,

                  Following may be helpful to achieve your target :
                  <code>

                  SOAPEnvelope soapEnvelope = ((SaajSoapMessage) webServiceMessage)
                  .getSaajMessage().getSOAPPart()
                  .getEnvelope();

                  SaajSoapMessage soapMessage = (SaajSoapMessage)webServiceMessage;
                  SOAPHeaderElement soapHeaderElement = soapMessage.getSaajMessage().getSOAPHeader().addHe aderElement(soapEnvelope.createName("Authorization "));
                  soapHeaderElement.addAttribute(soapEnvelope.create Name("xmlns"), "");
                  soapHeaderElement.addChildElement(soapEnvelope.cre ateName("Token"));


                  </code>

                  Cheers
                  Mohan
                  Hello Mohan bro..,

                  thanks a lot! That was really helpful Keep it Up!!

                  Comment


                  • #10
                    Originally posted by mohareddy View Post
                    Following resource could serve as a starting point
                    http://ww.w3schools.com/soap/soap_header.asp



                    Happy Learning

                    Cheers
                    Mohan
                    Thanks for the link!

                    Comment


                    • #11
                      Soap header in Webservicetemplate

                      Hi can you help in adding the custom elements to soap request.

                      Thanks in advance.


                      Originally posted by mohareddy View Post
                      Following resource could serve as a starting point
                      http://www.w3schools.com/soap/soap_header.asp .

                      Happy Learning

                      Cheers
                      Mohan

                      Comment

                      Working...
                      X