1

http://docs.oracle.com/javaee/5/api/javax/xml/ws/handler/soap/SOAPHandler.html

如何将呼入handleMessage(SOAPMessageContext)呼叫与呼出关联handleMessage(SOAPMessageContext)

我尝试了一些事情,Weblogic 不重用上下文对象,因此引用检查不起作用。我找不到任何明确指示请求的属性,因此我似乎无法在请求和响应之间创建链接。

或者,是否有更好的方法可以有问题地获取 Web 逻辑上的请求和响应并将它们关联在一起,以便可以将它们转储到数据库中以供将来调试。

4

2 回答 2

0

好的,所以这可能不适用于 JAX-WS 的所有实现,但对于 weblogic 来说确实如此。

我做了什么

public final boolean handleMessage(SOAPMessageContext context) {
    // Using `Object` type as we don't have any need to import servlet libraries into a JAX-WS library
    Object o = context.get(MessageContext.SERVLET_REQUEST);
    // o will hold a reference to the request 
    // if inbound.o == outbound.o the request objects are identical and therefor we can associate them.
    return true;
}

我不明白为什么这在其他容器中不起作用,但请在使用此方法之前仔细检查。

于 2013-02-11T22:52:53.973 回答
0

这需要一些样板代码,但它不依赖于应用服务器。

AttachAttributesFilter 将 startTime 和 UUID 添加到属性。然后在 LogSoapHandler 中为入站和出站消息读取这些属性。

日志中的简单搜索将显示特定 UUID 的输入和输出。

@WebFilter("/yourwebservice")
public class AttachAttributesFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        request.setAttribute("STARTTIME", System.currentTimeMillis());
        request.setAttribute("UUID",java.util.UUID.randomUUID().toString());
        chain.doFilter(request, response);
    }

}

然后我使用 LogSoapHander 中的属性

public class LogSoapHandler implements
        javax.xml.ws.handler.soap.SOAPHandler<SOAPMessageContext> {

    public boolean handleMessage(SOAPMessageContext messagecontext) {
        Boolean outbound = (Boolean) messagecontext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        String messageID = (String) httpSR.getAttribute("UUID");
        Object startTime = httpSR.getAttribute("STARTTIME");
        try {
            final SOAPMessage message = messagecontext.getMessage();
            String encoding = getMessageEncoding(message);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            boolean fault = (message.getSOAPBody().hasFault());

            message.writeTo(baos);
            String body = (baos.toString(encoding));
            log.info(outbound+"|"+messageID+"|"+startTime+"|"+System.currentTimeMillis()+"|"+body+"|"+fault));

        } catch (SOAPException | IOException ex) {
            //handle your error
        }
        return true;
    }
    private String getMessageEncoding(SOAPMessage msg) throws SOAPException {
        String encoding = "utf-8";
        if (msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING) != null) {
            encoding = msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING)
                    .toString();
        }
        return encoding;
    }

并完成 HandlerChain 锅炉板:

@HandlerChain(file="loghandler.xml")
public class MyWSDL {
..
}

日志处理程序.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains 
    xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <javaee:handler-chain>
        <javaee:handler>
            <javaee:handler-class>xxx.LogSoapHandler</javaee:handler-class>
        </javaee:handler>
    </javaee:handler-chain>
</javaee:handler-chains>
于 2016-01-07T07:43:58.393 回答