1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.access.common.servlet;
15
16 import java.io.IOException;
17 import java.io.OutputStreamWriter;
18 import java.io.PrintWriter;
19
20 import jakarta.servlet.ServletOutputStream;
21 import jakarta.servlet.http.HttpServletResponse;
22 import jakarta.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(
45 new OutputStreamWriter(getOutputStream(), this.getResponse().getCharacterEncoding()), 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
59
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 }