001 package com.mockrunner.mock.web;
002
003 import java.io.IOException;
004 import java.io.OutputStreamWriter;
005 import java.io.PrintWriter;
006 import java.io.UnsupportedEncodingException;
007 import java.text.SimpleDateFormat;
008 import java.util.ArrayList;
009 import java.util.Date;
010 import java.util.Enumeration;
011 import java.util.List;
012 import java.util.Locale;
013 import java.util.Map;
014 import java.util.TimeZone;
015 import java.util.Vector;
016
017 import javax.servlet.ServletOutputStream;
018 import javax.servlet.http.Cookie;
019 import javax.servlet.http.HttpServletResponse;
020
021 import com.mockrunner.base.NestedApplicationException;
022 import com.mockrunner.util.common.CaseAwareMap;
023
024 /**
025 * Mock implementation of <code>HttpServletResponse</code>.
026 */
027 public class MockHttpServletResponse implements HttpServletResponse
028 {
029 private PrintWriter writer;
030 private MockServletOutputStream outputStream;
031 private Map headers;
032 private Locale locale;
033 private String characterEncoding;
034 private int bufferSize;
035 private boolean wasErrorSent;
036 private boolean wasRedirectSent;
037 private int errorCode;
038 private int statusCode;
039 private List cookies;
040
041 public MockHttpServletResponse()
042 {
043 resetAll();
044 }
045
046 /**
047 * Resets the state of this object to the default values
048 */
049 public void resetAll()
050 {
051 headers = new CaseAwareMap();
052 characterEncoding = "ISO-8859-1";
053 bufferSize = 8192;
054 wasErrorSent = false;
055 wasRedirectSent = false;
056 errorCode = SC_OK;
057 statusCode = SC_OK;
058 cookies = new ArrayList();
059 outputStream = new MockServletOutputStream(characterEncoding);
060 try
061 {
062 writer = new PrintWriter(new OutputStreamWriter(outputStream, characterEncoding), true);
063 }
064 catch(UnsupportedEncodingException exc)
065 {
066 throw new NestedApplicationException(exc);
067 }
068 }
069
070 public String encodeURL(String url)
071 {
072 return url;
073 }
074
075 public String encodeRedirectUrl(String url)
076 {
077 return url;
078 }
079
080 public String encodeRedirectURL(String url)
081 {
082 return url;
083 }
084
085 public String encodeUrl(String url)
086 {
087 return url;
088 }
089
090 public PrintWriter getWriter() throws IOException
091 {
092 return writer;
093 }
094
095 public ServletOutputStream getOutputStream() throws IOException
096 {
097 return outputStream;
098 }
099
100 public String getOutputStreamContent()
101 {
102 return outputStream.getContent();
103 }
104
105 public void addCookie(Cookie cookie)
106 {
107 cookies.add(cookie);
108 }
109
110 public void addDateHeader(String key, long date)
111 {
112 addHeader(key, getDateString(date));
113 }
114
115 public void addHeader(String key, String value)
116 {
117 List valueList = (List) headers.get(key);
118 if (null == valueList)
119 {
120 valueList = new ArrayList();
121 headers.put(key, valueList);
122 }
123 valueList.add(value);
124 }
125
126 public void addIntHeader(String key, int value)
127 {
128 String stringValue = new Integer(value).toString();
129 addHeader(key, stringValue);
130 }
131
132 public boolean containsHeader(String key)
133 {
134 return headers.containsKey(key);
135 }
136
137 public void sendError(int code, String message) throws IOException
138 {
139 errorCode = code;
140 wasErrorSent = true;
141 }
142
143 public void sendError(int code) throws IOException
144 {
145 errorCode = code;
146 wasErrorSent = true;
147 }
148
149 public void sendRedirect(String location) throws IOException
150 {
151 setHeader("Location", location);
152 wasRedirectSent = true;
153 }
154
155 public void setDateHeader(String key, long date)
156 {
157 setHeader(key, getDateString(date));
158 }
159
160 public void setHeader(String key, String value)
161 {
162 List valueList = new ArrayList();
163 headers.put(key, valueList);
164 valueList.add(value);
165 }
166
167 public void setIntHeader(String key, int value)
168 {
169 String stringValue = new Integer(value).toString();
170 setHeader(key, stringValue);
171 }
172
173 public void setStatus(int code, String message)
174 {
175 statusCode = code;
176 }
177
178 public void setStatus(int code)
179 {
180 statusCode = code;
181 }
182
183 public void flushBuffer() throws IOException
184 {
185 writer.flush();
186 outputStream.flush();
187 }
188
189 public int getBufferSize()
190 {
191 return bufferSize;
192 }
193
194 public String getCharacterEncoding()
195 {
196 return characterEncoding;
197 }
198
199 public void setCharacterEncoding(String encoding)
200 {
201 characterEncoding = encoding;
202 outputStream.setEncoding(encoding);
203 }
204
205 public Locale getLocale()
206 {
207 return locale;
208 }
209
210 public void setLocale(Locale locale)
211 {
212 this.locale = locale;
213 }
214
215 public boolean isCommitted()
216 {
217 return false;
218 }
219
220 public void reset()
221 {
222 errorCode = SC_OK;
223 statusCode = SC_OK;
224 clearHeaders();
225 resetBuffer();
226 }
227
228 public void resetBuffer()
229 {
230 outputStream.clearContent();
231 }
232
233 public void clearHeaders()
234 {
235 headers.clear();
236 }
237
238 public void setBufferSize(int size)
239 {
240 bufferSize = size;
241 }
242
243 public void setContentLength(int length)
244 {
245 setIntHeader("Content-Length", length);
246 }
247
248 public String getContentType()
249 {
250 return getHeader("Content-Type");
251 }
252
253 public void setContentType(String type)
254 {
255 setHeader("Content-Type", type);
256 }
257
258 public Enumeration getHeaderNames()
259 {
260 return new Vector(headers.keySet()).elements();
261 }
262
263 public List getHeaderList(String key)
264 {
265 return (List)headers.get(key);
266 }
267
268 public String getHeader(String key)
269 {
270 List list = getHeaderList(key);
271 if(null == list || 0 == list.size()) return null;
272 return (String)list.get(0);
273 }
274
275 public int getStatusCode()
276 {
277 return statusCode;
278 }
279
280 public int getErrorCode()
281 {
282 return errorCode;
283 }
284
285 public List getCookies()
286 {
287 return cookies;
288 }
289
290 public boolean wasErrorSent()
291 {
292 return wasErrorSent;
293 }
294
295 public boolean wasRedirectSent()
296 {
297 return wasRedirectSent;
298 }
299
300 private String getDateString(long date)
301 {
302 Date dateValue = new Date(date);
303 SimpleDateFormat dateFormat = new SimpleDateFormat(WebConstants.DATE_FORMAT_HEADER, Locale.US);
304 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
305 return dateFormat.format(dateValue);
306 }
307 }