From a9f822d903faf80df94c08bd881788956f7f2640 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Fri, 7 Feb 2020 15:57:29 +0000 Subject: PayloadLoggingServletFilter update limit PayloadLoggingServletFilter log record size Issue-ID: LOG-1197 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I8e13d2be26e61ad0ab18f097c30ef0b166e229db --- .../filter/base/PayloadLoggingServletFilter.java | 53 ++++++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java index 0c0f5c4..e8d2813 100644 --- a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/PayloadLoggingServletFilter.java @@ -22,18 +22,51 @@ package org.onap.logging.filter.base; -import javax.servlet.*; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.zip.GZIPInputStream; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ReadListener; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletOutputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.WriteListener; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; -import java.io.*; -import java.util.zip.GZIPInputStream; public class PayloadLoggingServletFilter extends AbstractServletFilter implements Filter { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PayloadLoggingServletFilter.class); + private static final int defaultMaxSize = 100000; + private static Integer maxResponseSize; + private static Integer maxRequestSize; + + public PayloadLoggingServletFilter() { + String maxRequestSizeOverride = System.getProperty("FILTER_MAX_REQUEST_SIZE"); + if (maxRequestSizeOverride != null) { + maxRequestSize = Integer.valueOf(maxRequestSizeOverride); + } else { + maxRequestSize = defaultMaxSize; + } + String maxResponseSizeOverride = System.getProperty("FILTER_MAX_RESPONSE_SIZE"); + if (maxResponseSizeOverride != null) { + maxResponseSize = Integer.valueOf(maxResponseSizeOverride); + } else { + maxResponseSize = defaultMaxSize; + } + } private static class ByteArrayServletStream extends ServletOutputStream { ByteArrayOutputStream baos; @@ -194,7 +227,13 @@ public class PayloadLoggingServletFilter extends AbstractServletFilter implement requestHeaders.append(getSecureRequestHeaders(httpRequest)); log.info(requestHeaders.toString()); - log.info("REQUEST BODY|{}", new String(bufferedRequest.getBuffer())); + + byte[] buffer = bufferedRequest.getBuffer(); + if (buffer.length < maxRequestSize) { + log.info("REQUEST BODY|{}", new String(buffer)); + } else { + log.info("REQUEST BODY|{}", new String(buffer, 0, maxRequestSize)); + } final HttpServletResponse response = (HttpServletResponse) servletResponse; final ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -242,7 +281,11 @@ public class PayloadLoggingServletFilter extends AbstractServletFilter implement if ("gzip".equals(response.getHeader("Content-Encoding"))) { log.info("UNGZIPED RESPONSE BODY|{}", decompressGZIPByteArray(bytes)); } else { - log.info("RESPONSE BODY|{}", new String(bytes)); + if (bytes.length < maxResponseSize) { + log.info("RESPONSE BODY|{}", new String(bytes)); + } else { + log.info("RESPONSE BODY|{}", new String(bytes, 0, maxResponseSize)); + } } if (pw.hasErrored()) { -- cgit 1.2.3-korg