1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.onehippo.forge.hst.pdf.renderer.servlet;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.CharArrayWriter;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.util.Locale;
26
27 import javax.servlet.ServletOutputStream;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpServletResponseWrapper;
30
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.lang.ArrayUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39 public class ContentCapturingHttpServletResponse extends HttpServletResponseWrapper {
40
41 private static Logger log = LoggerFactory.getLogger(ContentCapturingHttpServletResponse.class);
42
43 private static class CharArrayWriterBuffer extends CharArrayWriter {
44 @Override
45 public void close() {
46 }
47
48 public void closeWriter() {
49 super.close();
50 }
51 }
52
53 private static class ByteArrayOutputStreamBuffer extends ByteArrayOutputStream {
54 @Override
55 public void close() {
56 }
57
58 public void closeOutputStream() {
59 try {
60 super.close();
61 } catch (IOException e) {
62 log.error("Failed to close byte array output stream", e);
63 }
64 }
65 }
66
67 private CharArrayWriterBuffer charOutputBuffer;
68 private PrintWriter printWriter;
69
70 private ByteArrayOutputStreamBuffer byteOutputBuffer;
71 private ServletOutputStream servletOutputStream;
72
73 private String characterEncoding;
74 private String contentType;
75 private int bufferSize;
76 private int contentLength;
77 private boolean committed;
78 private Locale locale;
79
80 public ContentCapturingHttpServletResponse(HttpServletResponse response) {
81 super(response);
82 }
83
84
85
86
87
88
89 public PrintWriter getWriter() throws IOException {
90 if (printWriter == null) {
91 if (servletOutputStream != null) {
92 throw new IllegalStateException("getOutputStream() has already been called on this response");
93 }
94
95 charOutputBuffer = new CharArrayWriterBuffer();
96 printWriter = new PrintWriter(charOutputBuffer);
97 }
98
99 return printWriter;
100 }
101
102
103
104
105
106
107 public ServletOutputStream getOutputStream() throws IOException {
108 if (servletOutputStream == null) {
109 if (printWriter != null) {
110 throw new IllegalStateException("getWriter() has already been called on this response");
111 }
112
113 byteOutputBuffer = new ByteArrayOutputStreamBuffer();
114 servletOutputStream = new ServletOutputStream() {
115 @Override
116 public void write(int b) throws IOException {
117 byteOutputBuffer.write(b);
118 }
119 };
120 }
121
122 return servletOutputStream;
123 }
124
125 public boolean isWrittenToPrintWriter() {
126 return (printWriter != null);
127 }
128
129 public char [] toCharArray() {
130 if (charOutputBuffer != null) {
131 return charOutputBuffer.toCharArray();
132 }
133
134 return ArrayUtils.EMPTY_CHAR_ARRAY;
135 }
136
137 public byte [] toByteArray() {
138 if (byteOutputBuffer != null) {
139 return byteOutputBuffer.toByteArray();
140 }
141
142 return ArrayUtils.EMPTY_BYTE_ARRAY;
143 }
144
145 public void close() {
146 IOUtils.closeQuietly(servletOutputStream);
147
148 if (byteOutputBuffer != null) {
149 byteOutputBuffer.closeOutputStream();
150 }
151
152 IOUtils.closeQuietly(printWriter);
153
154 if (charOutputBuffer != null) {
155 charOutputBuffer.closeWriter();
156 }
157 }
158
159 @Override
160 public void setCharacterEncoding(String characterEncoding) {
161 this.characterEncoding = characterEncoding;
162 }
163
164 @Override
165 public String getCharacterEncoding() {
166 return characterEncoding;
167 }
168
169 @Override
170 public void setContentLength(int contentLength) {
171 this.contentLength = contentLength;
172 }
173
174 public int getContentLength() {
175 return contentLength;
176 }
177
178 @Override
179 public void setContentType(String contentType) {
180 this.contentType = contentType;
181 }
182
183 @Override
184 public String getContentType() {
185 return contentType;
186 }
187
188 @Override
189 public void setBufferSize(int bufferSize) {
190 this.bufferSize = bufferSize;
191 }
192
193 @Override
194 public int getBufferSize() {
195 return bufferSize;
196 }
197
198 @Override
199 public void flushBuffer() throws IOException {
200
201 }
202
203 @Override
204 public void resetBuffer() {
205
206 }
207
208 @Override
209 public boolean isCommitted() {
210 if (getResponse().isCommitted()) {
211 return true;
212 }
213
214 return committed;
215 }
216
217 @Override
218 public void reset() {
219
220 }
221
222 @Override
223 public void setLocale(Locale locale) {
224 this.locale = locale;
225 }
226
227 @Override
228 public Locale getLocale() {
229 return locale;
230 }
231 }