简体   繁体   中英

Spring ClientInterceptor shows empty SOAP header in response

I have a SOAP service which I need to talk to. The first SOAP request receives a response with a sessionID element in the SOAP header which I need to send in the final release command.

To grab this sessionID value I plan on using a ClientInterceptor . In my implementation of WebServiceGatewaySupport I register my interceptor:

this.setInterceptors(new ClientInterceptor[] { new MyWebServiceClientInterceptor() });

My Interceptor:

public class MyWebServiceClientInterceptor implements ClientInterceptor {

    public final QName SessionID_QNAME = new QName("http://xml.example.com/ws/session", "sessionID");

    public boolean handleFault(MessageContext context) throws WebServiceClientException {
        logger.info("Handle Fault");
        return true;
    }       

    public boolean handleResponse(MessageContext context) throws WebServiceClientException {
        logger.info("Handle Response");
        SoapMessage soapMessage = (SoapMessage) context.getRequest();
        SoapHeader soapHeader = soapMessage.getSoapHeader();

        logger.info("Response Header: " + soapHeader.getName());
        Iterator<SoapHeaderElement> qn = soapHeader.examineAllHeaderElements();
        while (qn.hasNext()) {
            SoapElement elem = qn.next();
            logger.info(elem.toString());
        }
        Iterator<QName> an = soapHeader.getAllAttributes();
        while (an.hasNext()) {
            QName elem = an.next();
            logger.info(elem.toString());
        }
        return true;
    }

    public boolean handleRequest(MessageContext context) throws WebServiceClientException {
        logger.info("Handle Request");
        return true;
    }
}

However I keep getting back an empty SOAP header. I'm running Wireshark and can clearly see the sessionID token in the returned SOAP packet so at a lost as to what's going on. Any ideas?

I'm an idiot. 'Twas a typo. I needed to call:

SoapMessage soapMessage = (SoapMessage) context.getResponse();

Not:

SoapMessage soapMessage = (SoapMessage) context.getRequest();

Full code for those wanting to set headers on requests and receive headers on responses:

ClientInterceptor to receive custom SOAP header values:

public class MyWebServiceClientInterceptor implements ClientInterceptor {

    public boolean handleFault(MessageContext context) throws WebServiceClientException {
        return true;
    }

    public boolean handleResponse(MessageContext context) throws WebServiceClientException {
        SoapMessage soapMessage = (SoapMessage) context.getResponse();
        SoapHeader soapHeader = soapMessage.getSoapHeader();

        Iterator<SoapHeaderElement> qn = soapHeader.examineHeaderElements(MY_SESSIONID_QNAME);

        while (qn.hasNext()) {
            SoapElement elem = qn.next();
            SoapHeaderElement headerElem = (SoapHeaderElement) elem;
            if (StringUtil.validString(headerElem.getText())) {
                if (!headerElem.getText().equals(sessionId)) {
                    sessionId = headerElem.getText();
                    logger.info("Bound to Session ID: " + sessionId);
                }
            }
        }
        return true;
    }

    public boolean handleRequest(MessageContext context) throws WebServiceClientException {
        return true;
    }
}

Where I use the interceptor:

public class MySoapClient extends WebServiceGatewaySupport {

public MySoapClient() {
    this.setInterceptors(new ClientInterceptor[] { new MyWebServiceClientInterceptor() });
    ...
}

...
}

Callback to set custom SOAP Header: class MySoapActionCallback implements WebServiceMessageCallback {

    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
        SaajSoapMessage soapMessage = (SaajSoapMessage) message;
        SoapHeaderElement messageId = soapMessage.getSoapHeader().addHeaderElement(SessionID_QNAME);
        if (StringUtil.validString(sessionId)) {
            messageId.setText(sessionId);
        }
    }
}

Where I use the callback:

JAXBElement<ReturnType> result = (JAXBElement<ReturnType>) getWebServiceTemplate().marshalSendAndReceive(of.createRelease(null), new MySoapActionCallback());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM