1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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.common.servlet;
15  
16  import java.io.ByteArrayOutputStream;
17  import java.io.IOException;
18  
19  import jakarta.servlet.ServletOutputStream;
20  import jakarta.servlet.ServletResponse;
21  import jakarta.servlet.WriteListener;
22  
23  public class TeeServletOutputStream extends ServletOutputStream {
24  
25      final ServletOutputStream underlyingStream;
26      final ByteArrayOutputStream baosCopy;
27  
28      TeeServletOutputStream(ServletResponse httpServletResponse) 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) throws IOException {
57          if (underlyingStream == null) {
58              return;
59          }
60          // System.out.println("WRITE TeeServletOutputStream.write(byte[], int, int)
61          // called");
62          // System.out.println(new String(byteArray, offset, length));
63          underlyingStream.write(byteArray, offset, length);
64          baosCopy.write(byteArray, offset, length);
65      }
66  
67      @Override
68      public void close() throws IOException {
69          // System.out.println("CLOSE TeeServletOutputStream.close() called");
70  
71          // If the servlet accessing the stream is using a writer instead of
72          // an OutputStream, it will probably call os.close() before calling
73          // writer.close. Thus, the underlying output stream will be called
74          // before the data sent to the writer could be flushed.
75      }
76  
77      @Override
78      public void flush() throws IOException {
79          if (underlyingStream == null) {
80              return;
81          }
82          // System.out.println("FLUSH TeeServletOutputStream.flush() called");
83          underlyingStream.flush();
84          baosCopy.flush();
85      }
86  
87      @Override
88      public boolean isReady() {
89          throw new RuntimeException("Not yet implemented");
90      }
91  
92      @Override
93      public void setWriteListener(WriteListener listener) {
94          throw new RuntimeException("Not yet implemented");
95      }
96  }