简体   繁体   English

春季:记录传出的HTTP请求

[英]Spring: Logging outgoing HTTP requests

I am trying to log all the outgoing Http requests in my spring based web application. 我试图在基于Spring的Web应用程序中记录所有传出的Http请求。 Is there is interceptor for this purpose? 是否有用于此目的的拦截器? I want to log all outgoing the contents and headers before it leaves the application. 我要记录所有传出的内容和标头,然后再离开应用程序。 I am using spring-ws to send SOAP requests. 我正在使用spring-ws发送SOAP请求。 So basically, I want to log not only the SOAP request xml (as mentioned here How can I make Spring WebServices log all SOAP requests? ) but the http request as a whole. 因此,基本上,我不仅要记录SOAP请求xml(如此处所述, 如何使Spring WebServices记录所有SOAP请求? ),还要记录整个HTTP请求。

Intercept the request/response using a ClientInterceptor on the WebServiceGatewaySupport : 使用WebServiceGatewaySupport上的ClientInterceptor拦截请求/响应:

// soapClient extends WebServiceGatewaySupport
soapClient.setInterceptors(new ClientInterceptor[]{new ClientInterceptor() {
        @Override
        public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            try {
                messageContext.getRequest().writeTo(os);
            } catch (IOException e) {
                throw new WebServiceIOException(e.getMessage(), e);
            }

            String request = new String(os.toByteArray());
            logger.trace("Request Envelope: " + request);
            return true;
        }

        @Override
        public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            try {
                messageContext.getResponse().writeTo(os);
            } catch (IOException e) {
                throw new WebServiceIOException(e.getMessage(), e);
            }

            String response = new String(os.toByteArray());
            logger.trace("Response Envelope: " + response);
            return true;
        }
        ...

To get the headers as well you need an instance of TransportOutputStream . 要获得标头,还需要一个TransportOutputStream实例。 Unfortunately the class is abstract, so you need to subclass is. 不幸的是,该类是抽象的,因此您需要将其子类化。 Here's how it might look: 这是它的外观:

class ByteArrayTransportOutputStream extends TransportOutputStream {

    private ByteArrayOutputStream outputStream;

    @Override
    public void addHeader(String name, String value) throws IOException {
        createOutputStream();
        String header = name + ": " + value + "\n";
        outputStream.write(header.getBytes());
    }

    public byte[] toByteArray() {
         return outputStream.toByteArray();
    }

    @Override
    protected OutputStream createOutputStream() throws IOException {
        if (outputStream == null) {
            outputStream = new ByteArrayOutputStream();
        }
        return outputStream;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM