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