1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.statelessfilter.wrappers;
17
18 import java.io.IOException;
19 import java.io.OutputStreamWriter;
20 import java.io.PrintWriter;
21 import java.io.Writer;
22
23 import javax.servlet.ServletOutputStream;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.servlet.http.HttpServletResponseWrapper;
26
27
28
29
30
31
32
33 public class BufferedHttpResponseWrapper extends HttpServletResponseWrapper {
34 private final BufferedServletOutputStream bufferedServletOut = new BufferedServletOutputStream();
35
36 private Integer errorCode = null;
37 private String errorMsg = null;
38 private String location = null;
39 private ServletOutputStream outputStream = null;
40 private PrintWriter printWriter = null;
41 boolean committed = false;
42
43 @Override
44 public boolean isCommitted() {
45 if (committed)
46 return true;
47 else
48 return super.isCommitted();
49 }
50
51 public BufferedHttpResponseWrapper(HttpServletResponse origResponse) {
52 super(origResponse);
53 }
54
55 @Override
56 public void flushBuffer() throws IOException {
57
58 if (this.outputStream != null) {
59 this.outputStream.flush();
60 } else if (this.printWriter != null) {
61 this.printWriter.flush();
62 bufferedServletOut.flush();
63 }
64
65 }
66
67 public byte[] getBuffer() {
68 return this.bufferedServletOut.getBuffer();
69 }
70
71 @Override
72 public int getBufferSize() {
73 return this.bufferedServletOut.getBuffer().length;
74 }
75
76 @Override
77 public ServletOutputStream getOutputStream() throws IOException {
78
79 if (this.printWriter != null) {
80 throw new IllegalStateException("The Servlet API forbids calling getOutputStream( ) after"
81 + " getWriter( ) has been called");
82 }
83
84 if (this.outputStream == null) {
85 this.outputStream = this.bufferedServletOut;
86 }
87
88 return this.outputStream;
89 }
90
91 @Override
92 public PrintWriter getWriter() throws IOException {
93
94 if (this.outputStream != null) {
95 throw new IllegalStateException("The Servlet API forbids calling getWriter( ) after"
96 + " getOutputStream( ) has been called");
97 }
98
99 if (this.printWriter == null) {
100
101 Writer writer = new OutputStreamWriter(this.bufferedServletOut, this.getResponse().getCharacterEncoding());
102
103 this.printWriter = new PrintWriter(writer);
104 }
105
106 return this.printWriter;
107 }
108
109 public boolean performSend() throws IOException {
110 if (errorCode != null && errorMsg != null) {
111 super.sendError(errorCode.intValue(), errorMsg);
112 return true;
113 } else if (errorCode != null) {
114 super.sendError(errorCode.intValue());
115 return true;
116 } else if (location != null) {
117 super.sendRedirect(location);
118 return true;
119 }
120
121 return false;
122 }
123
124 @Override
125 public void reset() {
126 this.bufferedServletOut.reset();
127 errorCode = null;
128 errorMsg = null;
129 location = null;
130 }
131
132 @Override
133 public void resetBuffer() {
134 this.bufferedServletOut.reset();
135 }
136
137 @Override
138 public void sendError(int sc) throws IOException {
139 if (location == null) {
140 errorCode = new Integer(sc);
141 }
142 committed = true;
143 }
144
145 @Override
146 public void sendError(int sc, String msg) throws IOException {
147 if (location == null) {
148 errorCode = new Integer(sc);
149 errorMsg = msg;
150 }
151 committed = true;
152 }
153
154 @Override
155 public void sendRedirect(String location) throws IOException {
156 if (errorCode == null) {
157 this.location = location;
158 }
159 committed = true;
160 }
161
162 @Override
163 public void setBufferSize(int size) {
164 if (this.bufferedServletOut.hasData()) {
165 throw new IllegalStateException("Content has already been sent. No Setbuffer allowed");
166 }
167 this.bufferedServletOut.setBufferSize(size);
168 }
169 }