View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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()),
45            true);
46      }
47      return this.teeWriter;
48    }
49  
50    @Override
51    public void flushBuffer() {
52      if (this.teeWriter != null) {
53        this.teeWriter.flush();
54      }
55    }
56  
57    byte[] getOutputBuffer() {
58      // teeServletOutputStream can be null if the getOutputStream method is never
59      // called.
60      if (teeServletOutputStream != null) {
61        return teeServletOutputStream.getOutputStreamAsByteArray();
62      } else {
63        return null;
64      }
65    }
66  
67    void finish() throws IOException {
68      if (this.teeWriter != null) {
69        this.teeWriter.close();
70      }
71      if (this.teeServletOutputStream != null) {
72        this.teeServletOutputStream.close();
73      }
74    }
75  }