[logback-dev] svn commit: r1240 - logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Wed Jan 17 14:15:13 CET 2007
Author: ceki
Date: Wed Jan 17 14:15:13 2007
New Revision: 1240
Added:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/InputStreamDuplicatingFilter.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletRequest.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletInputStream.java
Log:
adding support for HttpServletRequest.inputStream duplication
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/InputStreamDuplicatingFilter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/InputStreamDuplicatingFilter.java Wed Jan 17 14:15:13 2007
@@ -0,0 +1,33 @@
+package ch.qos.logback.access.servlet;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+public class InputStreamDuplicatingFilter implements Filter {
+
+ public void destroy() {
+ // NOP
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain filterChain) throws IOException, ServletException {
+
+ if(request instanceof HttpServletRequest) {
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+ request = new TeeHttpServletRequest(httpRequest);
+ }
+ filterChain.doFilter(request, response);
+ }
+
+ public void init(FilterConfig arg0) throws ServletException {
+ // NOP
+ }
+
+}
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletRequest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletRequest.java Wed Jan 17 14:15:13 2007
@@ -0,0 +1,37 @@
+package ch.qos.logback.access.servlet;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+/**
+ * As the "tee" program on Unix, duplicate the request's input stream.
+ *
+ * @author Ceki Gülcü
+ */
+public class TeeHttpServletRequest extends HttpServletRequestWrapper {
+
+ static final int BUF_SIZE = 512;
+
+ private ServletInputStream inStream;
+ private BufferedReader reader;
+
+ public TeeHttpServletRequest(HttpServletRequest request) {
+ super(request);
+ inStream = new TeeServletInputStream(request);
+ reader = new BufferedReader(new InputStreamReader(inStream));
+ }
+
+ public ServletInputStream getInputStream() throws IOException {
+ return inStream;
+ }
+
+ public BufferedReader getReader() throws IOException {
+ return reader;
+ }
+
+}
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletInputStream.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletInputStream.java Wed Jan 17 14:15:13 2007
@@ -0,0 +1,45 @@
+package ch.qos.logback.access.servlet;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+
+public class TeeServletInputStream extends ServletInputStream {
+
+ InputStream in;
+ byte[] inputBuffer;
+
+ TeeServletInputStream(HttpServletRequest request) {
+ duplicateInputStream(request);
+ }
+
+ @Override
+ public int read() throws IOException {
+ return in.read();
+ }
+
+ private void duplicateInputStream(HttpServletRequest request) {
+ try {
+ int len = request.getContentLength();
+ ServletInputStream originalSIS = request.getInputStream();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ inputBuffer = new byte[len];
+ int n = 0;
+ while ((n = originalSIS.read(inputBuffer, 0, len)) != -1) {
+ baos.write(inputBuffer, 0, n);
+ }
+ this.in = new ByteArrayInputStream(inputBuffer);
+ originalSIS.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public byte[] getInputBuffer() {
+ return inputBuffer;
+ }
+}
More information about the logback-dev
mailing list