我如何将soaphandler与入站和出站联系起来

时间:2013-02-11 22:02:01

标签: java soap weblogic

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

如何将入站handleMessage(SOAPMessageContext)来电与出境handleMessage(SOAPMessageContext)相关联。

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

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

2 个答案:

答案 0 :(得分: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;
}

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

答案 1 :(得分: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 boilder板块:

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

loghandler.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>