backend-api HTTP interceptor logs [2]
This commit is contained in:
parent
9b5f0657a8
commit
2b5a065466
2 changed files with 31 additions and 3 deletions
|
|
@ -12,7 +12,7 @@ import org.springframework.util.StreamUtils;
|
||||||
|
|
||||||
public class CachedHttpServletRequest extends HttpServletRequestWrapper {
|
public class CachedHttpServletRequest extends HttpServletRequestWrapper {
|
||||||
|
|
||||||
private byte[] cachedPayload;
|
private final byte[] cachedPayload;
|
||||||
|
|
||||||
public CachedHttpServletRequest(HttpServletRequest request) throws IOException {
|
public CachedHttpServletRequest(HttpServletRequest request) throws IOException {
|
||||||
super(request);
|
super(request);
|
||||||
|
|
@ -25,6 +25,10 @@ public class CachedHttpServletRequest extends HttpServletRequestWrapper {
|
||||||
return new CachedServletInputStream(this.cachedPayload);
|
return new CachedServletInputStream(this.cachedPayload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] getCachedPayload() {
|
||||||
|
return cachedPayload;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BufferedReader getReader() {
|
public BufferedReader getReader() {
|
||||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedPayload);
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedPayload);
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StreamUtils;
|
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -32,9 +31,34 @@ public class RequestCachingFilter extends OncePerRequestFilter {
|
||||||
log.debug("received {} request, destination '{}', body: {}",
|
log.debug("received {} request, destination '{}', body: {}",
|
||||||
request.getMethod(),
|
request.getMethod(),
|
||||||
request.getContextPath() + request.getServletPath(),
|
request.getContextPath() + request.getServletPath(),
|
||||||
StreamUtils.copyToString(cachedHttpServletRequest.getInputStream(), StandardCharsets.UTF_8)
|
removeWhiteSpaces(cachedHttpServletRequest.getCachedPayload())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
filterChain.doFilter(cachedHttpServletRequest, response);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue