因此,在尝试实现此内容时,需要考虑几件事
-
对于请求实体,您将希望序列化由框架处理,这意味着您不希望执行类似操作
@Override
public void filter(ClientRequestContext requestContext) {
Object entity = requestContext.getEntity();
String serialized = serializeEntity(entity);
log(serialized);
在这里,您自己序列化它,也许使用Jackson或其他东西。你可以这样做,但它可以处理的类型有点有限。如果让对象以框架已经处理的方式进行序列化,则框架将能够支持比 JSON 更多的类型。ObjectMapper
为了让框架处理序列化,并且仍然能够获取序列化的数据,我们需要使用WriterInterceptor
。我们可以做的是将实体输出流设置为 a ,然后让框架将请求对象序列化到我们的 ,然后 afterwords 记录这些字节。这就是泽西岛如何处理这个问题。ByteArrayOutputStream
ByteArrayOutputStream
LoggingFilter
对于响应,在我们的筛选器中,我们需要从响应流中提取数据,但我们还需要确保流仍然具有数据,因为它尚未针对客户端进行反序列化。若要执行此操作和流,请假定支持标记。如果没有,请将其包装在 .同样,这就是泽西岛如何处理这个问题。mark()
reset()
BufferedOutputStream
LoggingFilter
下面是一个完整的简单实现。其中大部分是直接从泽西岛中获取的,尽管它只是为了您的用例而被剥离。泽西岛记录了许多其他信息,而不仅仅是实体。我遗漏的一件事是检查字符集。我刚刚使用了硬编码的UTF-8,因为泽西岛使用的类是泽西岛特定的。如果要使筛选器对其他字符集更具通用性,则可能需要考虑修复此问题。LoggingFilter
LoggingFilter
MessageUtil
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import javax.annotation.Priority;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
@Priority(Integer.MIN_VALUE)
public class EntityLoggingFilter implements ClientRequestFilter, ClientResponseFilter, WriterInterceptor {
private static final Logger logger = Logger.getLogger(EntityLoggingFilter.class.getName());
private static final String ENTITY_STREAM_PROPERTY = "EntityLoggingFilter.entityStream";
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final int maxEntitySize = 1024 * 8;
private void log(StringBuilder sb) {
logger.info(sb.toString());
}
private InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) throws IOException {
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
stream.mark(maxEntitySize + 1);
final byte[] entity = new byte[maxEntitySize + 1];
final int entitySize = stream.read(entity);
b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset));
if (entitySize > maxEntitySize) {
b.append("...more...");
}
b.append('\n');
stream.reset();
return stream;
}
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
if (requestContext.hasEntity()) {
final OutputStream stream = new LoggingStream(requestContext.getEntityStream());
requestContext.setEntityStream(stream);
requestContext.setProperty(ENTITY_STREAM_PROPERTY, stream);
}
}
@Override
public void filter(ClientRequestContext requestContext,
ClientResponseContext responseContext) throws IOException {
final StringBuilder sb = new StringBuilder();
if (responseContext.hasEntity()) {
responseContext.setEntityStream(logInboundEntity(sb, responseContext.getEntityStream(),
DEFAULT_CHARSET));
log(sb);
}
}
@Override
public void aroundWriteTo(WriterInterceptorContext context)
throws IOException, WebApplicationException {
final LoggingStream stream = (LoggingStream) context.getProperty(ENTITY_STREAM_PROPERTY);
context.proceed();
if (stream != null) {
log(stream.getStringBuilder(DEFAULT_CHARSET));
}
}
private class LoggingStream extends FilterOutputStream {
private final StringBuilder sb = new StringBuilder();
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
LoggingStream(OutputStream out) {
super(out);
}
StringBuilder getStringBuilder(Charset charset) {
// write entity to the builder
final byte[] entity = baos.toByteArray();
sb.append(new String(entity, 0, entity.length, charset));
if (entity.length > maxEntitySize) {
sb.append("...more...");
}
sb.append('\n');
return sb;
}
@Override
public void write(final int i) throws IOException {
if (baos.size() <= maxEntitySize) {
baos.write(i);
}
out.write(i);
}
}
}
另请参阅: