How to send additional fields to soap handler along with soapMessage?

纵饮孤独 提交于 2020-01-03 00:39:10

问题


I am logging RequestXML for a webservice client using SoapHandler as follows

public boolean handleMessage(SOAPMessageContext smc) {
    logToSystemOut(smc);
    return true;
}


private void logToSystemOut(SOAPMessageContext smc) {
     Boolean outboundProperty = (Boolean)
     smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {
        out.println("\nOutbound message:");
    } else {
        out.println("\nInbound message:");
    }

    SOAPMessage message = smc.getMessage();
    try {
        message.writeTo(out);
        out.println("");   
        } catch (Exception e) {
        out.println("Exception in handler: " + e);
    }
} 

Got a new requirenment to add this xml to DB along with some extra values(which are not present in the xml). Is there any way I can pass few additional fields to above soap handler (in handleMessage method)?

Please note that changing the xml/WSDL or adding this to SOAP message header is not an option for me as it is owned by other interface. Any other solution?

Thanks!


回答1:


You can cast your service class to a class of type "BindingProvider". In this form you can use it to assign it objects which you can access later from your SOAPHandler. Another useful usage is that you also can change the endPoint URL this way.

Before calling the service you do:

    MySoapServicePortType service = new MySoapService().getMySoapServicePort();
    BindingProvider bp = (BindingProvider)service;
    MyTransferObject t = new MyTransferObject();
    bp.getRequestContext().put("myTransferObject", t);
    TypeResponse response = service.doRequest();
    SOAPMessage message = t.getRequestMessage(message);

From your logging function you do:

private void logToSystemOut(SOAPMessageContext smc) {
    ...
    MyTransferObject t = (MyTransferObject) messageContext.get("myTransferObject");
    if (outboundProperty.booleanValue())
        t.setRequestMessage(message);
    else
        t.setResponseMessage(message);
    ...
}


来源:https://stackoverflow.com/questions/9404894/how-to-send-additional-fields-to-soap-handler-along-with-soapmessage

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