How to send additional fields to soap handler alon

2019-06-06 04:53发布

问题:

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);
    ...
}