记录jax-ws http请求和响应 [英] Log jax-ws http request and response

查看:151
本文介绍了记录jax-ws http请求和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



经过一番研究,我发现我可以获得这些信息与财产:

  -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump = true 

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



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

 公共类LoggingHandler实现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:在这种情况下我该怎么办?
}
} 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:在这种情况下我该怎么办?
}
}
返回true;
}

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

public String getFullHttpRequest(HttpServletRequest request)抛出IOException {
InputStream in = request.getInputStream();
String encoding = request.getCharacterEncoding();
encoding = encoding == null? UTF-8:编码;
String body = IOUtils.toString(in,encoding);
返回机构;
}

private String getHeaders(Map< String,List< String>> headers)抛出IOException {
StringBuffer result = new StringBuffer();
if(headers!= null){
for(Entry< String,List< String>> header:headers.entrySet()){
if(header.getValue()。isEmpty ()){
//我不认为这是合法的,但我们只是转储它,
//因为转储的目的是发现问题。
result.append(header.getValue());
} else {
for(String value:header.getValue()){
result.append(header.getKey()+:+ value);
}
}
result.append(\ n);
}
}
返回result.toString();
}

}

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



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



谢谢!!

解决方案

您也可以尝试

  -Dcom.sun.xml.ws.transport。 http.HttpAdapter.dump = true 

我假设你正在提供你的网络服务某种Java EE应用程序服务器(而不是来自独立客户端)。您无法在Web / Java EE容器的上下文之外访问Java EE基础结构,如 HttpServletRequest HttpServletResponse



您可以尝试使用



<$ p来获取实际的servlet响应对象(在Web环境中) $ p> HttpServletResponse response =(HttpServletResponse)messageContext.get(SOAPMessageContext.SERVLET_RESPONSE); // messageContext是SOAPMessageContext
List< String> responseHeaderNames =(List< String>)response.getHeaderNames();
for(String headerName:responseHeaderNames){
//用它做你想做的任何事。
}

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


I need to log the full http request and response in a JAX-WS WebService call. For the request I need the request headers and the body and for the response, response headers and body.

After some researching, I've found that I can get this information with the property:

-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true

and show the information that I need but it dumps it to the console and I need to store it in the database with an internal request id.

I've tried to implement a handler:

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();
    }

}

but in this case, I can get the http request headers and body but in the response, I only get the body, http response headers are always empty.

Any idea on how to archieve this? The objective is to be able to store the full http request and response in a database.

Thanks!!

解决方案

You could also try

    -Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true

I'm assuming you're providing your web service from within a Java EE application server of some sort (and not from a standalone client). You cannot have access to Java EE infrastructure like HttpServletRequest and HttpServletResponse outside of the context of a web/Java EE container.

You could try to get your hands on the actual servlet response object (within a web context) with

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

I seriously doubt that you'll be able to get your hands on the full response headers within a handler though. Your question really intrigued me and I've spent quite some time researching that part. In all the code samples I've seen, Not even the example on the metro site attempt to implement this functionality and I think the reason is simple. As at the point where a handler is invoked, the container may not have enough definitive information to stamp an http header on the outbound message. You might be able to add stuff but that's doubtful as well.

这篇关于记录jax-ws http请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆