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: private PrintWriter writer;
029: private MockServletOutputStream outputStream;
030: private Map headers;
031: private Locale locale;
032: private String characterEncoding;
033: private int bufferSize;
034: private boolean wasErrorSent;
035: private boolean wasRedirectSent;
036: private int errorCode;
037: private int statusCode;
038: private List cookies;
039:
040: public MockHttpServletResponse() {
041: resetAll();
042: }
043:
044: /**
045: * Resets the state of this object to the default values
046: */
047: public void resetAll() {
048: headers = new CaseAwareMap();
049: characterEncoding = "ISO-8859-1";
050: bufferSize = 8192;
051: wasErrorSent = false;
052: wasRedirectSent = false;
053: errorCode = SC_OK;
054: statusCode = SC_OK;
055: cookies = new ArrayList();
056: outputStream = new MockServletOutputStream(characterEncoding);
057: try {
058: writer = new PrintWriter(new OutputStreamWriter(
059: outputStream, characterEncoding), true);
060: } catch (UnsupportedEncodingException exc) {
061: throw new NestedApplicationException(exc);
062: }
063: }
064:
065: public String encodeURL(String url) {
066: return url;
067: }
068:
069: public String encodeRedirectUrl(String url) {
070: return url;
071: }
072:
073: public String encodeRedirectURL(String url) {
074: return url;
075: }
076:
077: public String encodeUrl(String url) {
078: return url;
079: }
080:
081: public PrintWriter getWriter() throws IOException {
082: return writer;
083: }
084:
085: public ServletOutputStream getOutputStream() throws IOException {
086: return outputStream;
087: }
088:
089: public String getOutputStreamContent() {
090: return outputStream.getContent();
091: }
092:
093: public void addCookie(Cookie cookie) {
094: cookies.add(cookie);
095: }
096:
097: public void addDateHeader(String key, long date) {
098: addHeader(key, getDateString(date));
099: }
100:
101: public void addHeader(String key, String value) {
102: List valueList = (List) headers.get(key);
103: if (null == valueList) {
104: valueList = new ArrayList();
105: headers.put(key, valueList);
106: }
107: valueList.add(value);
108: }
109:
110: public void addIntHeader(String key, int value) {
111: String stringValue = new Integer(value).toString();
112: addHeader(key, stringValue);
113: }
114:
115: public boolean containsHeader(String key) {
116: return headers.containsKey(key);
117: }
118:
119: public void sendError(int code, String message) throws IOException {
120: errorCode = code;
121: wasErrorSent = true;
122: }
123:
124: public void sendError(int code) throws IOException {
125: errorCode = code;
126: wasErrorSent = true;
127: }
128:
129: public void sendRedirect(String location) throws IOException {
130: setHeader("Location", location);
131: wasRedirectSent = true;
132: }
133:
134: public void setDateHeader(String key, long date) {
135: setHeader(key, getDateString(date));
136: }
137:
138: public void setHeader(String key, String value) {
139: List valueList = new ArrayList();
140: headers.put(key, valueList);
141: valueList.add(value);
142: }
143:
144: public void setIntHeader(String key, int value) {
145: String stringValue = new Integer(value).toString();
146: setHeader(key, stringValue);
147: }
148:
149: public void setStatus(int code, String message) {
150: statusCode = code;
151: }
152:
153: public void setStatus(int code) {
154: statusCode = code;
155: }
156:
157: public void flushBuffer() throws IOException {
158: writer.flush();
159: outputStream.flush();
160: }
161:
162: public int getBufferSize() {
163: return bufferSize;
164: }
165:
166: public String getCharacterEncoding() {
167: return characterEncoding;
168: }
169:
170: public void setCharacterEncoding(String encoding) {
171: characterEncoding = encoding;
172: outputStream.setEncoding(encoding);
173: }
174:
175: public Locale getLocale() {
176: return locale;
177: }
178:
179: public void setLocale(Locale locale) {
180: this .locale = locale;
181: }
182:
183: public boolean isCommitted() {
184: return false;
185: }
186:
187: public void reset() {
188: errorCode = SC_OK;
189: statusCode = SC_OK;
190: clearHeaders();
191: resetBuffer();
192: }
193:
194: public void resetBuffer() {
195: outputStream.clearContent();
196: }
197:
198: public void clearHeaders() {
199: headers.clear();
200: }
201:
202: public void setBufferSize(int size) {
203: bufferSize = size;
204: }
205:
206: public void setContentLength(int length) {
207: setIntHeader("Content-Length", length);
208: }
209:
210: public String getContentType() {
211: return getHeader("Content-Type");
212: }
213:
214: public void setContentType(String type) {
215: setHeader("Content-Type", type);
216: }
217:
218: public Enumeration getHeaderNames() {
219: return new Vector(headers.keySet()).elements();
220: }
221:
222: public List getHeaderList(String key) {
223: return (List) headers.get(key);
224: }
225:
226: public String getHeader(String key) {
227: List list = getHeaderList(key);
228: if (null == list || 0 == list.size())
229: return null;
230: return (String) list.get(0);
231: }
232:
233: public int getStatusCode() {
234: return statusCode;
235: }
236:
237: public int getErrorCode() {
238: return errorCode;
239: }
240:
241: public List getCookies() {
242: return cookies;
243: }
244:
245: public boolean wasErrorSent() {
246: return wasErrorSent;
247: }
248:
249: public boolean wasRedirectSent() {
250: return wasRedirectSent;
251: }
252:
253: private String getDateString(long date) {
254: Date dateValue = new Date(date);
255: SimpleDateFormat dateFormat = new SimpleDateFormat(
256: WebConstants.DATE_FORMAT_HEADER, Locale.US);
257: dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
258: return dateFormat.format(dateValue);
259: }
260: }
|