summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseLoggingFilter.java471
1 files changed, 231 insertions, 240 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 7b48a2361..c49729595 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
@@ -3,7 +3,7 @@
* ONAP : CCSDK
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights
- * reserved.
+ * reserved.
* ================================================================================
* Modifications Copyright (C) 2018 IBM.
* ================================================================================
@@ -32,7 +32,6 @@ import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
-
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -50,272 +49,264 @@ import javax.servlet.http.HttpServletResponseWrapper;
public class RequestResponseLoggingFilter implements Filter {
- private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("org.onap.ccsdk.sli.core.filters.request.response");
-
- private static class ByteArrayServletStream extends ServletOutputStream {
-
- ByteArrayOutputStream baos;
-
- ByteArrayServletStream(ByteArrayOutputStream baos) {
- this.baos = baos;
- }
-
- @Override
- public void write(int param) throws IOException {
- baos.write(param);
- }
-
- @Override
- public boolean isReady() {
- return true;
- }
-
- @Override
- public void setWriteListener(WriteListener arg0) {
- //this method does nothing
-
- }
- }
-
- private static class ByteArrayPrintWriter {
-
- private ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- private PrintWriter pw = new PrintWriter(baos);
+ private static org.slf4j.Logger log =
+ org.slf4j.LoggerFactory.getLogger("org.onap.ccsdk.sli.core.filters.request.response");
- private ServletOutputStream sos = new ByteArrayServletStream(baos);
+ private static class ByteArrayServletStream extends ServletOutputStream {
- public PrintWriter getWriter() {
- return pw;
- }
+ ByteArrayOutputStream baos;
- public ServletOutputStream getStream() {
- return sos;
- }
+ ByteArrayServletStream(ByteArrayOutputStream baos) {
+ this.baos = baos;
+ }
- byte[] toByteArray() {
- return baos.toByteArray();
- }
- }
+ @Override
+ public void write(int param) throws IOException {
+ baos.write(param);
+ }
- private class BufferedServletInputStream extends ServletInputStream {
+ @Override
+ public boolean isReady() {
+ return true;
+ }
- ByteArrayInputStream bais;
+ @Override
+ public void setWriteListener(WriteListener arg0) {
+ // this method does nothing
- public BufferedServletInputStream(ByteArrayInputStream bais) {
- this.bais = bais;
- }
+ }
+ }
- @Override
- public int available() {
- return bais.available();
- }
+ private static class ByteArrayPrintWriter extends PrintWriter {
+ private ByteArrayOutputStream baos;
- @Override
- public int read() {
- return bais.read();
- }
+ public ByteArrayPrintWriter(ByteArrayOutputStream out) {
+ super(out);
+ this.baos = out;
+ }
- @Override
- public int read(byte[] buf, int off, int len) {
- return bais.read(buf, off, len);
- }
+ public ServletOutputStream getStream() {
+ return new ByteArrayServletStream(baos);
+ }
- @Override
- public boolean isFinished() {
- return available() < 1;
- }
+ }
- @Override
- public boolean isReady() {
- return true;
- }
+ private class BufferedServletInputStream extends ServletInputStream {
- @Override
- public void setReadListener(ReadListener arg0) {
- // this method does nothing
- }
+ ByteArrayInputStream bais;
- }
+ public BufferedServletInputStream(ByteArrayInputStream bais) {
+ this.bais = bais;
+ }
- private class BufferedRequestWrapper extends HttpServletRequestWrapper {
+ @Override
+ public int available() {
+ return bais.available();
+ }
- ByteArrayInputStream bais;
+ @Override
+ public int read() {
+ return bais.read();
+ }
- ByteArrayOutputStream baos;
+ @Override
+ public int read(byte[] buf, int off, int len) {
+ return bais.read(buf, off, len);
+ }
- BufferedServletInputStream bsis;
+ @Override
+ public boolean isFinished() {
+ return available() < 1;
+ }
- byte[] buffer;
+ @Override
+ public boolean isReady() {
+ return true;
+ }
- public BufferedRequestWrapper(HttpServletRequest req) throws IOException {
- super(req);
+ @Override
+ public void setReadListener(ReadListener arg0) {
+ // this method does nothing
+ }
- InputStream is = req.getInputStream();
- baos = new ByteArrayOutputStream();
- byte[] buf = new byte[1024];
- int letti;
- while ((letti = is.read(buf)) > 0) {
- baos.write(buf, 0, letti);
- }
- buffer = baos.toByteArray();
+ }
- }
+ private class BufferedRequestWrapper extends HttpServletRequestWrapper {
- @Override
- public ServletInputStream getInputStream() {
- try {
- bais = new ByteArrayInputStream(buffer);
- bsis = new BufferedServletInputStream(bais);
- } catch (Exception ex) {
- log.error("Exception in getInputStream",ex);
- }
+ ByteArrayInputStream bais;
- return bsis;
- }
+ ByteArrayOutputStream baos;
- public byte[] getBuffer() {
- return buffer;
- }
+ BufferedServletInputStream bsis;
- }
+ byte[] buffer;
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- //this method does nothing
- }
+ public BufferedRequestWrapper(HttpServletRequest req) throws IOException {
+ super(req);
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
- throws IOException, ServletException {
+ InputStream is = req.getInputStream();
+ baos = new ByteArrayOutputStream();
+ byte[] buf = new byte[1024];
+ int letti;
+ while ((letti = is.read(buf)) > 0) {
+ baos.write(buf, 0, letti);
+ }
+ buffer = baos.toByteArray();
- final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
- BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpRequest);
+ }
+
+ @Override
+ public ServletInputStream getInputStream() {
+ try {
+ bais = new ByteArrayInputStream(buffer);
+ bsis = new BufferedServletInputStream(bais);
+ } catch (Exception ex) {
+ log.error("Exception in getInputStream", ex);
+ }
+
+ return bsis;
+ }
+
+ public byte[] getBuffer() {
+ return buffer;
+ }
+
+ }
- StringBuilder requestHeaders = new StringBuilder("REQUEST|");
- requestHeaders.append(httpRequest.getMethod());
- requestHeaders.append(":");
- requestHeaders.append(httpRequest.getRequestURL().toString());
- requestHeaders.append("|");
- String header;
- for (Enumeration<String> e = httpRequest.getHeaderNames(); e.hasMoreElements();) {
- header = e.nextElement();
- requestHeaders.append(header);
- requestHeaders.append(":");
- requestHeaders.append(httpRequest.getHeader(header));
- requestHeaders.append(";");
-
- }
- log.info(requestHeaders.toString());
-
- log.info("REQUEST BODY|" + new String(bufferedRequest.getBuffer()));
-
- final HttpServletResponse response = (HttpServletResponse) servletResponse;
-
- final ByteArrayPrintWriter pw = new ByteArrayPrintWriter();
- HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) {
- @Override
- public PrintWriter getWriter() {
- return pw.getWriter();
- }
-
- @Override
- public ServletOutputStream getOutputStream() {
- return pw.getStream();
- }
-
- };
-
- try {
-
- filterChain.doFilter(bufferedRequest, wrappedResp);
-
- }catch (Exception e){
- log.error("Chain Exception",e);
- throw e;
- } finally {
- byte[] bytes = pw.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(";");
-
- }
- log.info(responseHeaders.toString());
-
- if ("gzip".equals(response.getHeader("Content-Encoding"))) {
-
- log.info("UNGZIPED RESPONSE BODY|" + decompressGZIPByteArray(bytes));
-
- } else {
-
- log.info("RESPONSE BODY|" + new String(bytes));
- }
- }
- }
-
- @Override
- public void destroy() {
- //this method does nothing
- }
-
- private String decompressGZIPByteArray(byte[] bytes) {
-
- BufferedReader in = null;
- InputStreamReader inR = null;
- ByteArrayInputStream byteS = null;
- GZIPInputStream gzS = null;
- StringBuilder str = new StringBuilder();
- try {
- byteS = new ByteArrayInputStream(bytes);
- gzS = new GZIPInputStream(byteS);
- inR = new InputStreamReader(gzS);
- in = new BufferedReader(inR);
-
- if (in != null) {
-
- String content;
-
- while ((content = in.readLine()) != null) {
- str.append(content);
- }
- }
-
- } catch (Exception e) {
- log.error("Failed get read GZIPInputStream", e);
- } finally {
-
- if (byteS != null)
- try {
- byteS.close();
- } catch (IOException e1) {
- log.error("Failed to close ByteStream", e1);
- }
- if (gzS != null)
- try {
- gzS.close();
- } catch (IOException e2) {
- log.error("Failed to close GZStream", e2);
- }
- if (inR != null)
- try {
- inR.close();
- } catch (IOException e3) {
- log.error("Failed to close InputReader", e3);
- }
- if (in != null)
- try {
- in.close();
- } catch (IOException e) {
- log.error("Failed to close BufferedReader", e);
- }
- }
- return str.toString();
- }
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ // this method does nothing
+ }
+
+ @Override
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
+ throws IOException, ServletException {
+
+ final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
+ BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpRequest);
+
+ StringBuilder requestHeaders = new StringBuilder("REQUEST|");
+ requestHeaders.append(httpRequest.getMethod());
+ requestHeaders.append(":");
+ requestHeaders.append(httpRequest.getRequestURL().toString());
+ requestHeaders.append("|");
+ String header;
+ for (Enumeration<String> e = httpRequest.getHeaderNames(); e.hasMoreElements();) {
+ header = e.nextElement();
+ requestHeaders.append(header);
+ requestHeaders.append(":");
+ requestHeaders.append(httpRequest.getHeader(header));
+ requestHeaders.append(";");
+
+ }
+ log.info(requestHeaders.toString());
+
+ log.info("REQUEST BODY|" + new String(bufferedRequest.getBuffer()));
+
+ final HttpServletResponse response = (HttpServletResponse) servletResponse;
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final ByteArrayPrintWriter pw = new ByteArrayPrintWriter(baos);
+
+ HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) {
+ @Override
+ public PrintWriter getWriter() {
+ return pw;
+ }
+
+ @Override
+ public ServletOutputStream getOutputStream() {
+ return pw.getStream();
+ }
+ };
+
+ try {
+ filterChain.doFilter(bufferedRequest, wrappedResp);
+ } catch (Exception e) {
+ 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(";");
+
+ }
+ log.info(responseHeaders.toString());
+
+ if ("gzip".equals(response.getHeader("Content-Encoding"))) {
+
+ log.info("UNGZIPED RESPONSE BODY|" + decompressGZIPByteArray(bytes));
+
+ } else {
+
+ log.info("RESPONSE BODY|" + new String(bytes));
+ }
+ }
+ }
+
+ @Override
+ public void destroy() {
+ // this method does nothing
+ }
+
+ private String decompressGZIPByteArray(byte[] bytes) {
+
+ BufferedReader in = null;
+ InputStreamReader inR = null;
+ ByteArrayInputStream byteS = null;
+ GZIPInputStream gzS = null;
+ StringBuilder str = new StringBuilder();
+ try {
+ byteS = new ByteArrayInputStream(bytes);
+ gzS = new GZIPInputStream(byteS);
+ inR = new InputStreamReader(gzS);
+ in = new BufferedReader(inR);
+
+ if (in != null) {
+
+ String content;
+
+ while ((content = in.readLine()) != null) {
+ str.append(content);
+ }
+ }
+
+ } catch (Exception e) {
+ log.error("Failed get read GZIPInputStream", e);
+ } finally {
+
+ if (byteS != null)
+ try {
+ byteS.close();
+ } catch (IOException e1) {
+ log.error("Failed to close ByteStream", e1);
+ }
+ if (gzS != null)
+ try {
+ gzS.close();
+ } catch (IOException e2) {
+ log.error("Failed to close GZStream", e2);
+ }
+ if (inR != null)
+ try {
+ inR.close();
+ } catch (IOException e3) {
+ log.error("Failed to close InputReader", e3);
+ }
+ if (in != null)
+ try {
+ in.close();
+ } catch (IOException e) {
+ log.error("Failed to close BufferedReader", e);
+ }
+ }
+ return str.toString();
+ }
}