View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.servlet;
15  
16  import java.io.ByteArrayOutputStream;
17  import java.io.IOException;
18  
19  import javax.servlet.ServletOutputStream;
20  import javax.servlet.ServletResponse;
21  
22  public class TeeServletOutputStream extends ServletOutputStream {
23  
24    final ServletOutputStream underlyingStream;
25    final ByteArrayOutputStream baosCopy;
26  
27    TeeServletOutputStream(ServletResponse httpServletResponse)
28        throws IOException {
29      // System.out.println("TeeServletOutputStream.constructor() called");
30      this.underlyingStream = httpServletResponse.getOutputStream();
31      baosCopy = new ByteArrayOutputStream();
32    }
33  
34    byte[] getOutputStreamAsByteArray() {
35      return baosCopy.toByteArray();
36    }
37  
38    @Override
39    public void write(int val) throws IOException {
40      if (underlyingStream != null) {
41        underlyingStream.write(val);
42        baosCopy.write(val);
43      }
44    }
45  
46    @Override
47    public void write(byte[] byteArray) throws IOException {
48      if (underlyingStream == null) {
49        return;
50      }
51      // System.out.println("WRITE TeeServletOutputStream.write(byte[]) called");
52      write(byteArray, 0, byteArray.length);
53    }
54  
55    @Override
56    public void write(byte byteArray[], int offset, int length)
57        throws IOException {
58      if (underlyingStream == null) {
59        return;
60      }
61      // System.out.println("WRITE TeeServletOutputStream.write(byte[], int, int)
62      // called");
63      // System.out.println(new String(byteArray, offset, length));
64      underlyingStream.write(byteArray, offset, length);
65      baosCopy.write(byteArray, offset, length);
66    }
67  
68    @Override
69    public void close() throws IOException {
70      // System.out.println("CLOSE TeeServletOutputStream.close() called");
71  
72      // If the servlet accessing the stream is using a writer instead of
73      // an OutputStream, it will probably call os.close() before calling
74      // writer.close. Thus, the underlying output stream will be called
75      // before the data sent to the writer could be flushed.
76    }
77  
78  
79    @Override
80    public void flush() throws IOException {
81      if (underlyingStream == null) {
82        return;
83      }
84      // System.out.println("FLUSH TeeServletOutputStream.flush() called");
85      underlyingStream.flush();
86      baosCopy.flush();
87    }
88  }