View Javadoc
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.servlet;
15  
16  import java.io.IOException;
17  import java.io.OutputStreamWriter;
18  import java.io.PrintWriter;
19  
20  import javax.servlet.ServletOutputStream;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.servlet.http.HttpServletResponseWrapper;
23  
24  public class TeeHttpServletResponse extends HttpServletResponseWrapper {
25  
26      TeeServletOutputStream teeServletOutputStream;
27      PrintWriter teeWriter;
28  
29      public TeeHttpServletResponse(HttpServletResponse httpServletResponse) {
30          super(httpServletResponse);
31      }
32  
33      @Override
34      public ServletOutputStream getOutputStream() throws IOException {
35          if (teeServletOutputStream == null) {
36              teeServletOutputStream = new TeeServletOutputStream(this.getResponse());
37          }
38          return teeServletOutputStream;
39      }
40  
41      @Override
42      public PrintWriter getWriter() throws IOException {
43          if (this.teeWriter == null) {
44              this.teeWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), this.getResponse().getCharacterEncoding()), true);
45          }
46          return this.teeWriter;
47      }
48  
49      @Override
50      public void flushBuffer() {
51          if (this.teeWriter != null) {
52              this.teeWriter.flush();
53          }
54      }
55  
56      byte[] getOutputBuffer() {
57          // teeServletOutputStream can be null if the getOutputStream method is never
58          // called.
59          if (teeServletOutputStream != null) {
60              return teeServletOutputStream.getOutputStreamAsByteArray();
61          } else {
62              return null;
63          }
64      }
65  
66      void finish() throws IOException {
67          if (this.teeWriter != null) {
68              this.teeWriter.close();
69          }
70          if (this.teeServletOutputStream != null) {
71              this.teeServletOutputStream.close();
72          }
73      }
74  }