gpt4 book ai didi

java - 如何读取和复制 HTTP servlet 响应输出流内容以进行日志记录

转载 作者:IT老高 更新时间:2023-10-28 20:40:47 25 4
gpt4 key购买 nike

我在我的 Java 网络服务器(实际上是 appengine)中创建了一个过滤器,用于记录传入请求的参数。我还想记录我的网络服务器写入的结果响应。虽然我可以访问响应对象,但我不确定如何从中获取实际的字符串/内容响应。

有什么想法吗?

最佳答案

您需要创建一个 Filter其中您使用自定义 HttpServletResponseWrapper 包装 ServletResponse 参数实现,其中您覆盖 getOutputStream()getWriter() 以返回自定义 ServletOutputStream实现,其中您复制基本摘要中的写入字节OutputStream#write(int b)方法。然后,您将包装的自定义 HttpServletResponseWrapper 传递给 FilterChain#doFilter() 调用,最后您应该能够获得复制的响应 after来电。

换句话说,过滤器:

@WebFilter("/*")
public class ResponseLogger implements Filter {

@Override
public void init(FilterConfig config) throws ServletException {
// NOOP.
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
if (response.getCharacterEncoding() == null) {
response.setCharacterEncoding("UTF-8"); // Or whatever default. UTF-8 is good for World Domination.
}

HttpServletResponseCopier responseCopier = new HttpServletResponseCopier((HttpServletResponse) response);

try {
chain.doFilter(request, responseCopier);
responseCopier.flushBuffer();
} finally {
byte[] copy = responseCopier.getCopy();
System.out.println(new String(copy, response.getCharacterEncoding())); // Do your logging job here. This is just a basic example.
}
}

@Override
public void destroy() {
// NOOP.
}

}

自定义HttpServletResponseWrapper:

public class HttpServletResponseCopier extends HttpServletResponseWrapper {

private ServletOutputStream outputStream;
private PrintWriter writer;
private ServletOutputStreamCopier copier;

public HttpServletResponseCopier(HttpServletResponse response) throws IOException {
super(response);
}

@Override
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IllegalStateException("getWriter() has already been called on this response.");
}

if (outputStream == null) {
outputStream = getResponse().getOutputStream();
copier = new ServletOutputStreamCopier(outputStream);
}

return copier;
}

@Override
public PrintWriter getWriter() throws IOException {
if (outputStream != null) {
throw new IllegalStateException("getOutputStream() has already been called on this response.");
}

if (writer == null) {
copier = new ServletOutputStreamCopier(getResponse().getOutputStream());
writer = new PrintWriter(new OutputStreamWriter(copier, getResponse().getCharacterEncoding()), true);
}

return writer;
}

@Override
public void flushBuffer() throws IOException {
if (writer != null) {
writer.flush();
} else if (outputStream != null) {
copier.flush();
}
}

public byte[] getCopy() {
if (copier != null) {
return copier.getCopy();
} else {
return new byte[0];
}
}

}

自定义ServletOutputStream:

public class ServletOutputStreamCopier extends ServletOutputStream {

private OutputStream outputStream;
private ByteArrayOutputStream copy;

public ServletOutputStreamCopier(OutputStream outputStream) {
this.outputStream = outputStream;
this.copy = new ByteArrayOutputStream(1024);
}

@Override
public void write(int b) throws IOException {
outputStream.write(b);
copy.write(b);
}

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

}

关于java - 如何读取和复制 HTTP servlet 响应输出流内容以进行日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8933054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com