无法在SOAPHandler中记录完整的SOAP消息

时间:2014-05-11 05:08:07

标签: java web-services soap jax-ws

我在客户端创建了一个SOAPHandler,用于将传出请求记录到服务器&在SOAP消息中添加了一个SOAP标头。它正在记录没有标题部分的输出soap xml。但是在服务器端我可以用soap标题记录整个消息。

服务器端日志:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
    <Credential xmlns="http://soap.header.test.com" password="123" username="ashok"/>
</SOAP-ENV:Header>
<S:Body>
    <ns2:addNumber xmlns:ns2="http://service.ashok.com/">
        <arg0>10</arg0>
        <arg1>200</arg1>
    </ns2:addNumber>
</S:Body>

客户端日志

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/> **NO HEADER**
<S:Body>
    <ns2:addNumber xmlns:ns2="http://service.ashok.com/">
        <arg0>10</arg0>
        <arg1>200</arg1>
    </ns2:addNumber>
</S:Body>

我在客户端的部分SOAPHandler

if (outgoingRequest) {
    SOAPMessage message = context.getMessage();
    SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
    SOAPHeader header = envelope.getHeader();
    if (null == header) {
      header = envelope.addHeader();
    }
    QName credential = new QName("http://soap.header.test.com","Credential");
    SOAPHeaderElement headerElement = header.addHeaderElement(credential);
    QName username = new QName("username");
    headerElement.addAttribute(username, "ashok");
    QName password = new QName("password");
    headerElement.addAttribute(password, "123");

    message.writeTo(System.out);
  }

请建议我如何在客户端使用标题部分记录SOAP消息

2 个答案:

答案 0 :(得分:5)

问题是您在修改邮件后调用 writeTo 但仍然打印旧版本(没有更改任何标头)。我建议你使用SOAPMessage的saveChanges方法

  

abstract void saveChanges()

     

使用对其进行的所有更改来更新此SOAPMessage对象。

然后尝试调用方法 writeTo 。如果有效,请告诉我。

我尝试了您的代码:

EchoServiceImplService echoService = new EchoServiceImplService();
        HandlerResolver resolver = new HandlerResolver(){

            @Override
            public List<Handler> getHandlerChain(PortInfo portInfo){
                List<Handler> handlers = new ArrayList<Handler>();
                handlers.add(new LogMessageHandler());
                return handlers;
            }
        };
        echoService.setHandlerResolver(resolver);
        EchoService port = echoService.getEchoServiceImplPort();

public class LogMessageHandler implements SOAPHandler<SOAPMessageContext> {

    @Override
    public boolean handleMessage(SOAPMessageContext context){
        try {
            if ((Boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
                SOAPMessage message = context.getMessage();
                SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
                SOAPHeader header = envelope.getHeader();
                if (null == header)
                    header = envelope.addHeader();

                QName credential = new QName("http://soap.header.test.com", "Credential");
                SOAPHeaderElement headerElement = header.addHeaderElement(credential);
                QName username = new QName("username");
                headerElement.addAttribute(username, "ashok");
                QName password = new QName("password");
                headerElement.addAttribute(password, "123");

                message.writeTo(System.out);
                return true;
            }

        } catch (Exception e) {
            System.err.println("An error in handler occurs.");
        }
        return false;
    }

我能够在客户端看到完整的SOAPMesssage,只需在将请求发送到服务器之前调用处理程序。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
   <Credential xmlns="http://soap.header.test.com" password="123" username="ashok"/></S:Header>
<S:Body>
   <ns2:echo xmlns:ns2="http://echo.soap.ws.jax.koitoer.com/">
       <arg0>Koitoer echo </arg0>
   </ns2:echo>
</S:Body>
</S:Envelope>

答案 1 :(得分:0)

解决方案是在方法writeTo之后调用方法saveChanges。

public boolean handleMessage(SOAPMessageContext context) {
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outboundProperty.booleanValue()) { // outbound: request to send
        try {

            SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
            SOAPHeader header = envelope.getHeader();
            if (header == null)
                header = envelope.addHeader();

            SOAPElement soapSecurity = header.addChildElement("Security", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            soapSecurity.setAttribute(header.getPrefix() + ":mustUnderstand", "true");

            ...

            context.getMessage().saveChanges();
            context.getMessage().writeTo(System.out);

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        // inbound: response received
    }
    return true;
}

对不起,我的英语。

问候。