backend-api HTTP interceptor logs [2]

This commit is contained in:
ialbert 2026-05-28 17:35:54 +03:00
parent 9b5f0657a8
commit 2b5a065466
2 changed files with 31 additions and 3 deletions

View file

@ -12,7 +12,7 @@ import org.springframework.util.StreamUtils;
public class CachedHttpServletRequest extends HttpServletRequestWrapper {
private byte[] cachedPayload;
private final byte[] cachedPayload;
public CachedHttpServletRequest(HttpServletRequest request) throws IOException {
super(request);
@ -25,6 +25,10 @@ public class CachedHttpServletRequest extends HttpServletRequestWrapper {
return new CachedServletInputStream(this.cachedPayload);
}
public byte[] getCachedPayload() {
return cachedPayload;
}
@Override
public BufferedReader getReader() {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedPayload);

View file

@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.filter.OncePerRequestFilter;
@ -32,9 +31,34 @@ public class RequestCachingFilter extends OncePerRequestFilter {
log.debug("received {} request, destination '{}', body: {}",
request.getMethod(),
request.getContextPath() + request.getServletPath(),
StreamUtils.copyToString(cachedHttpServletRequest.getInputStream(), StandardCharsets.UTF_8)
removeWhiteSpaces(cachedHttpServletRequest.getCachedPayload())
);
}
filterChain.doFilter(cachedHttpServletRequest, response);
}
private static String removeWhiteSpaces(byte[] cachedPayload) {
if (cachedPayload == null || cachedPayload.length == 0) {
return "[empty]";
}
String body = new String(cachedPayload, StandardCharsets.UTF_8);
StringBuilder result = new StringBuilder(body.length());
boolean previousWasWhitespace = false;
for (int i = 0; i < body.length(); i++) {
char ch = body.charAt(i);
if (Character.isWhitespace(ch)) {
if (!previousWasWhitespace) {
result.append(' ');
previousWasWhitespace = true;
}
} else {
result.append(ch);
previousWasWhitespace = false;
}
}
return result.toString().trim();
}
}