From 6737dde71f6cdec79904c219efdf4aa348e158e4 Mon Sep 17 00:00:00 2001 From: "Agarwal, Ruchira (ra1926)" Date: Tue, 9 Apr 2019 00:08:10 +0000 Subject: add support for SendError in LoggingFilter Fix for illegalStateException in loggingFilter Change-Id: I357266468a3a9ed11c1d1aeeeb31b52dc653c432 Issue-ID: CCSDK-1198 Signed-off-by: Agarwal, Ruchira (ra1926) --- .../core/filters/RequestResponseLoggingFilter.java | 85 +++++++++++++++++----- 1 file changed, 68 insertions(+), 17 deletions(-) diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java index c4972959..e24a7527 100644 --- a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java +++ b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java @@ -79,6 +79,9 @@ public class RequestResponseLoggingFilter implements Filter { private static class ByteArrayPrintWriter extends PrintWriter { private ByteArrayOutputStream baos; + private int errorCode = -1; + private String errorMsg = ""; + private boolean errored = false; public ByteArrayPrintWriter(ByteArrayOutputStream out) { super(out); @@ -88,6 +91,27 @@ public class RequestResponseLoggingFilter implements Filter { public ServletOutputStream getStream() { return new ByteArrayServletStream(baos); } + + public Boolean hasErrored() { + return errored; + } + public int getErrorCode() { + return errorCode; + } + public String getErrorMsg() { + return errorMsg; + } + + public void setError(int code) { + errorCode = code; + errored = true; + } + + public void setError(int code, String msg) { + errorMsg = msg; + errorCode = code; + errored = true; + } } @@ -217,6 +241,18 @@ public class RequestResponseLoggingFilter implements Filter { public ServletOutputStream getOutputStream() { return pw.getStream(); } + + @Override + public void sendError(int sc) throws IOException { + super.sendError(sc); + pw.setError(sc); + + } + @Override + public void sendError(int sc, String msg) throws IOException { + super.sendError(sc, msg); + pw.setError(sc, msg); + } }; try { @@ -225,29 +261,44 @@ public class RequestResponseLoggingFilter implements Filter { log.error("Chain Exception", e); throw e; } finally { - byte[] bytes = baos.toByteArray(); - response.getOutputStream().write(bytes); - response.getOutputStream().flush(); - - StringBuilder responseHeaders = new StringBuilder("RESPONSE HEADERS|"); - - for (String headerName : response.getHeaderNames()) { - responseHeaders.append(headerName); - responseHeaders.append(":"); - responseHeaders.append(response.getHeader(headerName)); - responseHeaders.append(";"); + try { + byte[] bytes = baos.toByteArray(); + StringBuilder responseHeaders = new StringBuilder("RESPONSE HEADERS|"); + + for (String headerName : response.getHeaderNames()) { + responseHeaders.append(headerName); + responseHeaders.append(":"); + responseHeaders.append(response.getHeader(headerName)); + responseHeaders.append(";"); + } + responseHeaders.append("Status:"); + responseHeaders.append(response.getStatus()); + responseHeaders.append(";IsCommited:" + wrappedResp.isCommitted()); + + log.info(responseHeaders.toString()); + + if ("gzip".equals(response.getHeader("Content-Encoding"))) { - } - log.info(responseHeaders.toString()); + log.info("UNGZIPED RESPONSE BODY|" + decompressGZIPByteArray(bytes)); - if ("gzip".equals(response.getHeader("Content-Encoding"))) { + } else { - log.info("UNGZIPED RESPONSE BODY|" + decompressGZIPByteArray(bytes)); + log.info("RESPONSE BODY|" + new String(bytes)); + } - } else { + if (pw.hasErrored()) { + log.info("ERROR RESPONSE|" + pw.getErrorCode() + ":" + pw.getErrorMsg()); + } else { + if (!wrappedResp.isCommitted()){ + response.getOutputStream().write(bytes); + response.getOutputStream().flush(); + } + } - log.info("RESPONSE BODY|" + new String(bytes)); + } catch (Exception e) { + log.error("Exception in response filter", e); } + } } -- cgit 1.2.3-korg