001 // Copyright 2007, 2008, 2009, 2010 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.test;
016
017 import java.io.ByteArrayOutputStream;
018 import java.io.IOException;
019 import java.io.OutputStream;
020 import java.io.OutputStreamWriter;
021 import java.io.PrintWriter;
022 import java.util.Map;
023
024 import javax.servlet.ServletOutputStream;
025 import javax.servlet.http.HttpServletResponse;
026
027 import org.apache.tapestry5.Link;
028 import org.apache.tapestry5.dom.Document;
029 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
030
031 public class TestableResponseImpl implements TestableResponse
032 {
033 private Link link;
034
035 private boolean committed;
036
037 private Document renderedDocument;
038
039 private Map<String, Object> headers;
040
041 private String redirectURL;
042
043 private int status = HttpServletResponse.SC_OK;
044
045 private String errorMessage;
046
047 private int contentLength = 0;
048
049 private String contentType;
050
051 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
052
053 private ServletOutputStream outputStream = new TesableServletOutputStream(output);
054
055 private PrintWriter printWriter;
056
057 public TestableResponseImpl()
058 {
059 headers = CollectionFactory.newMap();
060 }
061
062 public OutputStream getOutputStream(String contentType) throws IOException
063 {
064 this.contentType = contentType;
065
066 return this.outputStream;
067 }
068
069 public PrintWriter getPrintWriter(String contentType) throws IOException
070 {
071 committed = true;
072
073 this.contentType = contentType;
074
075 if (printWriter == null)
076 {
077 this.printWriter = new PrintWriter(new OutputStreamWriter(output));
078 }
079
080 return this.printWriter;
081 }
082
083 public void sendError(int sc, String message) throws IOException
084 {
085 setCommitted();
086
087 this.status = sc;
088 this.errorMessage = message;
089 }
090
091 public void sendRedirect(String URL) throws IOException
092 {
093 setCommitted();
094
095 this.redirectURL = URL;
096 }
097
098 public void setContentLength(int length)
099 {
100 this.contentLength = length;
101 }
102
103 public void setDateHeader(String name, long date)
104 {
105 headers.put(name, date);
106 }
107
108 public void setHeader(String name, String value)
109 {
110 headers.put(name, value);
111 }
112
113 public void setIntHeader(String name, int value)
114 {
115 headers.put(name, value);
116 }
117
118 public void sendRedirect(Link link) throws IOException
119 {
120 setCommitted();
121
122 this.link = link;
123 }
124
125 public void setStatus(int sc)
126 {
127 this.status = sc;
128 }
129
130 public String encodeRedirectURL(String URL)
131 {
132 return URL;
133 }
134
135 public String encodeURL(String URL)
136 {
137 return URL;
138 }
139
140 public Link getRedirectLink()
141 {
142 return link;
143 }
144
145 public boolean isCommitted()
146 {
147 return committed;
148 }
149
150 public void clear()
151 {
152 committed = false;
153 link = null;
154 renderedDocument = null;
155 headers.clear();
156 status = HttpServletResponse.SC_OK;
157 errorMessage = null;
158 contentLength = 0;
159 contentType = null;
160 output.reset();
161 }
162
163 public Document getRenderedDocument()
164 {
165 return renderedDocument;
166 }
167
168 public void setRenderedDocument(Document document)
169 {
170 renderedDocument = document;
171 }
172
173 public void disableCompression()
174 {
175 }
176
177 public Object getHeader(String name)
178 {
179 return headers.get(name);
180 }
181
182 public String getRedirectURL()
183 {
184 return this.redirectURL;
185 }
186
187 public int getStatus()
188 {
189 return status;
190 }
191
192 public String getErrorMessage()
193 {
194 return errorMessage;
195 }
196
197 public int getContentLength()
198 {
199 return contentLength;
200 }
201
202 private void setCommitted()
203 {
204 this.committed = true;
205 }
206
207 public String getContentType()
208 {
209 return this.contentType;
210 }
211
212 public String getOutput()
213 {
214 return output.toString();
215 }
216
217 private class TesableServletOutputStream extends ServletOutputStream
218 {
219 private OutputStream delegate;
220
221 public TesableServletOutputStream(OutputStream delegate)
222 {
223 super();
224 this.delegate = delegate;
225 }
226
227 @Override
228 public void write(int b) throws IOException
229 {
230 delegate.write(b);
231 }
232
233 @Override
234 public void flush() throws IOException
235 {
236 super.flush();
237
238 this.delegate.flush();
239
240 setCommitted();
241 }
242
243 @Override
244 public void close() throws IOException
245 {
246 super.close();
247
248 this.delegate.close();
249 }
250
251 }
252 }