记录jax-ws http请求和响应

dru*_*lik 6 java logging jax-ws

我需要在JAX-WS WebService调用中记录完整的http请求和响应.对于请求,我需要请求标头和正文以及响应,响应标头和正文.

经过一番研究,我发现我可以通过该属性获取此信息:

-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true
Run Code Online (Sandbox Code Playgroud)

并显示我需要的信息,但它将其转储到控制台,我需要将其存储在具有内部请求ID的数据库中.

我试图实现一个处理程序:

public class LoggingHandler implements SOAPHandler<SOAPMessageContext> {

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outbound) {
            System.out.println("SOAP outbound!!!!!");
            Map<String, List<String>> responseHeaders = (Map<String, List<String>>) context
                    .get(SOAPMessageContext.HTTP_RESPONSE_HEADERS);
            try {
                String headers = getHeaders(responseHeaders);
                System.out.println(headers);
                String body = getBody(context.getMessage());
                System.out.println(body);
            } catch (Exception ex) {
                // TODO: What do I have to do in this case?
            }
        } else {
            System.out.println("SOAP inbound!!!!!");
            Map<String, List<String>> requestHeaders = (Map<String, List<String>>) context
                    .get(SOAPMessageContext.HTTP_REQUEST_HEADERS);
            try {
                String headers = getHeaders(requestHeaders);
                System.out.println(headers);
                String body = getBody(context.getMessage());
                System.out.println(body);
            } catch (Exception ex) {
                // TODO: What do I have to do in this case?
            }
        }
        return true;
    }

    private String getBody(SOAPMessage message) throws SOAPException, IOException {
        OutputStream stream = new ByteArrayOutputStream();
        message.writeTo(stream);
        return stream.toString();
    }

    public String getFullHttpRequest(HttpServletRequest request) throws IOException {
        InputStream in = request.getInputStream();
        String encoding = request.getCharacterEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        String body = IOUtils.toString(in, encoding);
        return body;
    }

    private String getHeaders(Map<String, List<String>> headers) throws IOException {
        StringBuffer result = new StringBuffer();
        if (headers != null) {
            for (Entry<String, List<String>> header : headers.entrySet()) {
                if (header.getValue().isEmpty()) {
                    // I don't think this is legal, but let's just dump it,
                    // as the point of the dump is to uncover problems.
                    result.append(header.getValue());
                } else {
                    for (String value : header.getValue()) {
                        result.append(header.getKey() + ": " + value);
                    }
                }
                result.append("\n");
            }
        }
        return result.toString();
    }

}
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,我可以得到http请求标题和正文,但在响应中,我只获取正文,http响应标题始终为空.

关于如何实现这一点的任何想法?目标是能够在数据库中存储完整的http请求和响应.

谢谢!!

kol*_*sus 3

你也可以尝试

    -Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true
Run Code Online (Sandbox Code Playgroud)

我假设您是从某种 Java EE 应用程序服务器(而不是从独立客户端)提供 Web 服务。您无法访问 Java EE 基础设施,HttpServletRequest例如HttpServletResponseWeb/Java EE 容器上下文

您可以尝试使用实际的 servlet 响应对象(在 Web 上下文中)

    HttpServletResponse response = (HttpServletResponse) messageContext.get(SOAPMessageContext.SERVLET_RESPONSE); //messageContext is the SOAPMessageContext
    List<String> responseHeaderNames = (List<String>)response.getHeaderNames();
        for(String headerName : responseHeaderNames){
             //Do whatever you want with it.
              }
Run Code Online (Sandbox Code Playgroud)

我严重怀疑您是否能够在处理程序中获得完整的响应标头。你的问题确实引起了我的兴趣,我花了相当多的时间研究这部分。在我见过的所有代码示例中,甚至地铁站点上的示例都没有尝试实现此功能,我认为原因很简单。在调用处理程序时,容器可能没有足够的明确信息来在出站消息上标记 http 标头。你也许可以添加一些东西,但这也是值得怀疑的。