[logback-dev] svn commit: r1247 - logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Jan 18 10:57:07 CET 2007


Author: ceki
Date: Thu Jan 18 10:57:07 2007
New Revision: 1247

Added:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletResponse.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletOutputStream.java
Modified:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java

Log:
Added support for capturing the response as well

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java	Thu Jan 18 10:57:07 2007
@@ -9,6 +9,7 @@
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 public class TeeFilter implements Filter {
 
@@ -23,6 +24,10 @@
       HttpServletRequest httpRequest = (HttpServletRequest) request;
       request =  new TeeHttpServletRequest(httpRequest);
     }
+    if(response instanceof HttpServletResponse) {
+      HttpServletResponse httpResponse = (HttpServletResponse) response;
+      response =  new TeeHttpServletResponse(httpResponse);
+    }
     
     filterChain.doFilter(request, response);
 

Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletResponse.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletResponse.java	Thu Jan 18 10:57:07 2007
@@ -0,0 +1,35 @@
+package ch.qos.logback.access.servlet;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+public class TeeHttpServletResponse extends HttpServletResponseWrapper {
+
+  final TeeServletOutputStream teeServletOutputStream;
+  PrintWriter writer;
+
+  public TeeHttpServletResponse(HttpServletResponse httpServletResponse)
+      throws IOException {
+    super(httpServletResponse);
+    ServletOutputStream underlyingStream = httpServletResponse
+        .getOutputStream();
+    teeServletOutputStream = new TeeServletOutputStream(underlyingStream);
+  }
+
+  @Override
+  public ServletOutputStream getOutputStream() throws IOException {
+    return teeServletOutputStream;
+  }
+
+  @Override
+  public PrintWriter getWriter() throws IOException {
+    if (writer == null)
+      writer = new PrintWriter(getOutputStream());
+    return writer;
+  }
+
+}

Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletOutputStream.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletOutputStream.java	Thu Jan 18 10:57:07 2007
@@ -0,0 +1,47 @@
+package ch.qos.logback.access.servlet;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletOutputStream;
+
+public class TeeServletOutputStream extends ServletOutputStream {
+
+  final ServletOutputStream underlyingStream;
+  final ByteArrayOutputStream baos;
+
+  TeeServletOutputStream(ServletOutputStream underlyingStream) {
+    this.underlyingStream = underlyingStream;
+    baos = new ByteArrayOutputStream();
+  }
+
+  @Override
+  public void write(int val) throws IOException {
+    underlyingStream.write(val);
+    baos.write(val);
+  }
+
+  @Override
+  public void write(byte[] byteArray) throws IOException {
+    underlyingStream.write(byteArray);
+    baos.write(byteArray);
+  }
+
+  @Override
+  public void write(byte byteArray[], int offset, int length)
+      throws IOException {
+    underlyingStream.write(byteArray, offset, length);
+    baos.write(byteArray, offset, length);
+  }
+
+  public void close() throws IOException {
+    underlyingStream.close();
+    baos.close();
+  }
+
+  public void flush() throws IOException {
+    underlyingStream.flush();
+    baos.flush();
+  }
+
+}



More information about the logback-dev mailing list