diff options
author | 2019-04-09 00:08:10 +0000 | |
---|---|---|
committer | 2019-04-09 13:22:51 +0000 | |
commit | 6737dde71f6cdec79904c219efdf4aa348e158e4 (patch) | |
tree | 3b7e6a86f8bcebf4487a590e92d9fa440a89bdfb /filters/provider/src/main/java | |
parent | 59779f3301d3a8fcb2b44b90c962ed55f9cea16e (diff) |
add support for SendError in LoggingFilter
Fix for illegalStateException in loggingFilter
Change-Id: I357266468a3a9ed11c1d1aeeeb31b52dc653c432
Issue-ID: CCSDK-1198
Signed-off-by: Agarwal, Ruchira (ra1926) <ra1926@att.com>
Diffstat (limited to 'filters/provider/src/main/java')
-rw-r--r-- | filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java | 85 |
1 files 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 c49729595..e24a75274 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); } + } } |