2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.mock;
19  
20  import java.io.ByteArrayOutputStream;
21  import java.io.CharArrayWriter;
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import java.text.SimpleDateFormat;
25  import java.util.ArrayList;
26  import java.util.Date;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Locale;
30  import java.util.TimeZone;
31  
32  import javax.servlet.ServletOutputStream;
33  import javax.servlet.http.Cookie;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /***
37   * <p>Mock implementation of <code>HttpServletResponse</code>.</p>
38   *
39   * $Id$
40   */
41  
42  public class MockHttpServletResponse implements HttpServletResponse {
43  
44  
45      // ------------------------------------------------------------ Constructors
46  
47  
48      /***
49       * <p>Return a default instance.</p>
50       */
51      public MockHttpServletResponse() { }
52  
53  
54      // ----------------------------------------------------- Mock Object Methods
55  
56  
57      /***
58       * <p>Retrieve the first value that was set for the specified header,
59       * if any.  Otherwise, return <code>null</code>.</p>
60       *
61       * @param name Header name to look up
62       */
63      public String getHeader(String name) {
64          String match = name + ":";
65          Iterator headers = this.headers.iterator();
66          while (headers.hasNext()) {
67              String header = (String) headers.next();
68              if (header.startsWith(match)) {
69                  return header.substring(match.length() + 1).trim();
70              }
71          }
72          return null;
73      }
74  
75  
76      /***
77       * <p>Return the text message for the HTTP status that was set.</p>
78       */
79      public String getMessage() {
80          return this.message;
81      }
82  
83  
84      /***
85       * <p>Return the HTTP status code that was set.</p>
86       */
87      public int getStatus() {
88          return this.status;
89      }
90  
91  
92      /***
93       * <p>Set the <code>ServletOutputStream</code> to be returned by a call to
94       * <code>getOutputStream()</code>.</p>
95       *
96       * @param stream The <code>ServletOutputStream</code> instance to use
97       *
98       * @deprecated Let the <code>getOutputStream()</code> method create and
99       *  return an instance of <code>MockServletOutputStream</code> for you
100      */
101     public void setOutputStream(ServletOutputStream stream) {
102         this.stream = stream;
103     }
104 
105 
106     /***
107      * <p>Set the <code>PrintWriter</code> to be returned by a call to
108      * <code>getWriter()</code>.</p>
109      *
110      * @param writer The <code>PrintWriter</code> instance to use
111      *
112      * @deprecated Let the <code>getWriter()</code> method create and return
113      *  an instance of <code>MockPrintWriter</code> for you
114      */
115     public void setWriter(PrintWriter writer) {
116         this.writer = writer;
117     }
118 
119 
120     // ------------------------------------------------------ Instance Variables
121 
122 
123     private String encoding = "ISO-8859-1";
124     private String contentType = "text/html";
125     private List headers = new ArrayList();
126     private String message = null;
127     private int status = HttpServletResponse.SC_OK;
128     private ServletOutputStream stream = null;
129     private PrintWriter writer = null;
130 
131 
132     // -------------------------------------------- HttpServletResponse Methods
133 
134 
135     /*** {@inheritDoc} */
136     public void addCookie(Cookie cookie) {
137 
138         throw new UnsupportedOperationException();
139 
140     }
141 
142 
143     /*** {@inheritDoc} */
144     public void addDateHeader(String name, long value) {
145 
146         headers.add(name + ": " + formatDate(value));
147 
148     }
149 
150 
151     /*** {@inheritDoc} */
152     public void addHeader(String name, String value) {
153 
154         headers.add(name + ": " + value);
155 
156     }
157 
158 
159     /*** {@inheritDoc} */
160     public void addIntHeader(String name, int value) {
161 
162         headers.add(name + ": " + value);
163 
164     }
165 
166 
167     /*** {@inheritDoc} */
168     public boolean containsHeader(String name) {
169 
170         return getHeader(name) != null;
171 
172     }
173 
174 
175     /*** {@inheritDoc} */
176     public String encodeRedirectUrl(String url) {
177 
178         return encodeRedirectURL(url);
179 
180     }
181 
182 
183     /*** {@inheritDoc} */
184     public String encodeRedirectURL(String url) {
185 
186         return url;
187 
188     }
189 
190 
191     /*** {@inheritDoc} */
192     public String encodeUrl(String url) {
193 
194         return encodeURL(url);
195 
196     }
197 
198 
199     /*** {@inheritDoc} */
200     public String encodeURL(String url) {
201 
202         return url;
203 
204     }
205 
206 
207     /*** {@inheritDoc} */
208     public void sendError(int status) {
209 
210         this.status = status;
211 
212     }
213 
214 
215     /*** {@inheritDoc} */
216     public void sendError(int status, String message) {
217 
218         this.status = status;
219         this.message = message;
220 
221     }
222 
223 
224     /*** {@inheritDoc} */
225     public void sendRedirect(String location) {
226 
227         this.status = HttpServletResponse.SC_MOVED_TEMPORARILY;
228         this.message = location;
229 
230     }
231 
232 
233     /*** {@inheritDoc} */
234     public void setDateHeader(String name, long value) {
235 
236         removeHeader(name);
237         addDateHeader(name, value);
238 
239     }
240 
241 
242     /*** {@inheritDoc} */
243     public void setHeader(String name, String value) {
244 
245         removeHeader(name);
246         addHeader(name, value);
247 
248     }
249 
250 
251     /*** {@inheritDoc} */
252     public void setIntHeader(String name, int value) {
253 
254         removeHeader(name);
255         addIntHeader(name, value);
256 
257     }
258 
259 
260     /*** {@inheritDoc} */
261     public void setStatus(int status) {
262 
263         throw new UnsupportedOperationException();
264 
265     }
266 
267 
268     /*** {@inheritDoc} */
269     public void setStatus(int status, String message) {
270 
271         throw new UnsupportedOperationException();
272 
273     }
274 
275 
276     // ------------------------------------------------ ServletResponse Methods
277 
278 
279     /*** {@inheritDoc} */
280     public void flushBuffer() {
281 
282         throw new UnsupportedOperationException();
283 
284     }
285 
286 
287     /*** {@inheritDoc} */
288     public int getBufferSize() {
289 
290         throw new UnsupportedOperationException();
291 
292     }
293 
294 
295     /*** {@inheritDoc} */
296     public String getCharacterEncoding() {
297 
298         return this.encoding;
299 
300     }
301 
302 
303     /*** {@inheritDoc} */
304     public String getContentType() {
305 
306         return this.contentType;
307 
308     }
309 
310 
311     /*** {@inheritDoc} */
312     public Locale getLocale() {
313 
314         throw new UnsupportedOperationException();
315 
316     }
317 
318 
319     /*** {@inheritDoc} */
320     public ServletOutputStream getOutputStream() throws IOException {
321 
322         if (stream == null) {
323             if (writer != null) {
324                 throw new IllegalStateException("Cannot call getOutputStream() after getWriter() has been called");
325             }
326             stream = new MockServletOutputStream(new ByteArrayOutputStream());
327         }
328         return stream;
329 
330     }
331 
332 
333     /*** {@inheritDoc} */
334     public PrintWriter getWriter() throws IOException {
335 
336         if (writer == null) {
337             if (stream != null) {
338                 throw new IllegalStateException("Cannot call getWriter() after getOutputStream() was called");
339             }
340             writer = new MockPrintWriter(new CharArrayWriter());
341         }
342         return writer;
343 
344     }
345 
346 
347     /*** {@inheritDoc} */
348     public boolean isCommitted() {
349 
350         throw new UnsupportedOperationException();
351 
352     }
353 
354 
355     /*** {@inheritDoc} */
356     public void reset() {
357 
358         throw new UnsupportedOperationException();
359 
360     }
361 
362 
363     /*** {@inheritDoc} */
364     public void resetBuffer() {
365 
366         throw new UnsupportedOperationException();
367 
368     }
369 
370 
371     /*** {@inheritDoc} */
372     public void setBufferSize(int size) {
373 
374         throw new UnsupportedOperationException();
375 
376     }
377 
378 
379     /*** {@inheritDoc} */
380     public void setCharacterEncoding(String charset) {
381 
382         this.encoding = charset;
383 
384     }
385 
386 
387     /*** {@inheritDoc} */
388     public void setContentLength(int length) {
389 
390         throw new UnsupportedOperationException();
391 
392     }
393 
394 
395     /*** {@inheritDoc} */
396     public void setContentType(String type) {
397 
398         contentType = type;
399 
400     }
401 
402 
403     /*** {@inheritDoc} */
404     public void setLocale(Locale locale) {
405 
406         throw new UnsupportedOperationException();
407 
408     }
409 
410 
411     // --------------------------------------------------------- Private Methods
412 
413 
414     /***
415      * <p>The date formatting helper we will use in <code>httpTimestamp()</code>.
416      * Note that usage of this helper must be synchronized.</p>
417      */
418     private static SimpleDateFormat format =
419             new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
420     static {
421         format.setTimeZone(TimeZone.getTimeZone("GMT"));
422     }
423 
424 
425     /***
426      * <p>Return a properly formatted String version of the specified
427      * date/time, formatted as required by the HTTP specification.</p>
428      *
429      * @param date Date/time, expressed as milliseconds since the epoch
430      */
431     private String formatDate(long date) {
432         return format.format(new Date(date));
433     }
434 
435 
436     /***
437      * <p>Remove any header that has been set with the specific name.</p>
438      *
439      * @param name Header name to look up
440      */
441     private void removeHeader(String name) {
442         String match = name + ":";
443         Iterator headers = this.headers.iterator();
444         while (headers.hasNext()) {
445             String header = (String) headers.next();
446             if (header.startsWith(match)) {
447                 headers.remove();
448                 return;
449             }
450         }
451     }
452 
453 
454 }