Add SoapHeader to org.springframework.ws.WebServiceMessage

社会主义新天地 提交于 2019-11-27 12:29:09

问题


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>

回答1:


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.




回答2:


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());



回答3:


You need to cast the WebServiceMessage to SoapMessage, which has a getSoapHeader() method you can use to modify the header. In turn, SoapHeader has various methods for adding elements, including getResult() (which can be used as the output of a Transformer.transform() operation).




回答4:


Response response = (Response)getWebServiceTemplate() .marshalSendAndReceive(request, new HeaderModifier());

Create class HeaderModifier and override doWithMessage public class HeaderModifier implements WebServiceMessageCallback {

 private static PrintStream out = System.out;

@Override
public void doWithMessage(WebServiceMessage message) throws IOException {
      SaajSoapMessage soapMessage = (SaajSoapMessage) message;

            SoapEnvelope soapEnvelope = soapMessage.getEnvelope();
            SoapHeader soapHeader = soapEnvelope.getHeader();

            //Initialize QName for Action and To 
            QName action = new QName("{uri}","Action","{actionname}");
            QName to = new QName("{uri}","To","{actionname}");


            soapHeader.addNamespaceDeclaration("{actionname}", "{uri}");


            SoapHeaderElement soapHeaderElementAction = soapHeader.addHeaderElement(action);
            SoapHeaderElement soapHeaderElementTo =  soapHeader.addHeaderElement(to);



            soapHeaderElementAction.setText("{text inside the tags}");


            soapHeaderElementTo.setText("{text inside the tags}");


            soapMessage.setSoapAction("{add soap action uri}");


            soapMessage.writeTo(out);

}

}



来源:https://stackoverflow.com/questions/2274378/add-soapheader-to-org-springframework-ws-webservicemessage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!