Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add SoapHeader to org.springframework.ws.WebServiceMessage

How can I add an object into the soap header of a org.springframework.ws.WebServiceMessage

This is the structure I'm looking to end up with:

 <soap:Header>
    <credentials xmlns="http://example.com/auth">
      <username>username</username>
      <password>password</password>
    </credentials>
  </soap:Header>
like image 533
Mark Pope Avatar asked Feb 16 '10 16:02

Mark Pope


People also ask

How do you add elements to a SOAP header in Java?

To add content to the header, you create a SOAPHeaderElement object. As with all new elements, it must have an associated QName object. For example, suppose you want to add a conformance claim header to the message to state that your message conforms to the WS-I Basic Profile.

What is WebServiceMessageCallback?

WebServiceMessageCallback. To accommodate the setting of SOAP headers and other settings on the message, the WebServiceMessageCallback interface gives you access to the message after it has been created, but before it is sent.

What is marshalSendAndReceive?

marshalSendAndReceive(String uri, Object requestPayload) Sends a web service message that contains the given payload, marshalled by the configured Marshaller . Object.


2 Answers

You can do as below:

public class SoapRequestHeaderModifier implements WebServiceMessageCallback {
 private final String userName = "user";
 private final String passWd = "passwd";

 @Override
 public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
  if (message instanceof SaajSoapMessage) {
   SaajSoapMessage soapMessage = (SaajSoapMessage) message;
   MimeHeaders mimeHeader = soapMessage.getSaajMessage().getMimeHeaders();
   mimeHeader.setHeader("Authorization", getB64Auth(userName, passWd));
  }
 }

 private String getB64Auth(String login, String pass) {
  String source = login + ":" + pass;
  String retunVal = "Basic " + Base64.getUrlEncoder().encodeToString(source.getBytes());
  return retunVal;
 }
}

Then

Object response = getWebServiceTemplate().marshalSendAndReceive(request, new SoapRequestHeaderModifier());
like image 37
pranav kumar Avatar answered Sep 21 '22 18:09

pranav kumar


Basically, you need to use a WebServiceMessageCallback in your client to modify the message after its creation but before it is sent. To rest of the code has been described pretty accurately by @skaffman so the whole stuff might look like this:

public void marshalWithSoapActionHeader(MyObject o) {

    webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {

        public void doWithMessage(WebServiceMessage message) {
            try {
                SoapMessage soapMessage = (SoapMessage)message;
                SoapHeader header = soapMessage.getSoapHeader();
                StringSource headerSource = new StringSource("<credentials xmlns=\"http://example.com/auth\">\n +
                        <username>"+username+"</username>\n +
                        <password>"+password"+</password>\n +
                        </credentials>");
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.transform(headerSource, header.getResult());
            } catch (Exception e) {
                // exception handling
            }
        }
    });
}

Personally, I find that Spring-WS sucks hard for such a basic need, they should fix SWS-479.

like image 196
Pascal Thivent Avatar answered Sep 23 '22 18:09

Pascal Thivent