1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, Steve Meyfroidt
4 * All rights reserved.
5 * Email: smeyfroi@users.sourceforge.net
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name "Scope" nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * $Id: DummyHTTPResponse.java,v 1.6 2002/06/17 09:56:00 ludovicc Exp $
38 */
39
40
41 package test.controller.servlet;
42
43
44 import java.io.ByteArrayOutputStream;
45 import java.io.IOException;
46 import java.io.PrintWriter;
47 import java.util.Locale;
48 import javax.servlet.ServletOutputStream;
49 import javax.servlet.http.Cookie;
50 import javax.servlet.http.HttpServletResponse;
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53 import org.scopemvc.util.Debug;
54
55
56 /***
57 * <P>
58 * An HTTPResponse used in testing the servlet controller/view impl.
59 * Collects streamed output to be returned as a String from getContent().
60 * </P>
61 *
62 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
63 * @version $Revision: 1.6 $ $Date: 2002/06/17 09:56:00 $
64 */
65 class DummyHTTPResponse implements HttpServletResponse {
66 /***
67 * @deprecated As of version 2.1, use encodeURL(String url) instead
68 *
69 * @param url the url to be encoded.
70 * @return the encoded URL if encoding is needed;
71 * the unchanged URL otherwise.
72 */
73 public String encodeUrl(String url) {
74 return url;
75 }
76 /***
77 * Adds the specified cookie to the response. This method can be called
78 * multiple times to set more than one cookie.
79 *
80 * @param cookie the Cookie to return to the client
81 *
82 */
83 public void addCookie(Cookie cookie) {
84 }
85 /***
86 * Returns a boolean indicating whether the named response header
87 * has already been set.
88 *
89 * @param name the header name
90 * @return <code>true</code> if the named response header
91 * has already been set;
92 * <code>false</code> otherwise
93 */
94 public boolean containsHeader(String name) {
95 return false;
96 }
97 /***
98 * @deprecated As of version 2.1, use
99 * encodeRedirectURL(String url) instead
100 *
101 * @param url the url to be encoded.
102 * @return the encoded URL if encoding is needed;
103 * the unchanged URL otherwise.
104 */
105 public String encodeRedirectUrl(String url) {
106 return url;
107 }
108 /***
109 * Encodes the specified URL by including the session ID in it,
110 * or, if encoding is not needed, returns the URL unchanged.
111 * The implementation of this method includes the logic to
112 * determine whether the session ID needs to be encoded in the URL.
113 * For example, if the browser supports cookies, or session
114 * tracking is turned off, URL encoding is unnecessary.
115 *
116 * <p>For robust session tracking, all URLs emitted by a servlet
117 * should be run through this
118 * method. Otherwise, URL rewriting cannot be used with browsers
119 * which do not support cookies.
120 *
121 * @param url the url to be encoded.
122 * @return the encoded URL if encoding is needed;
123 * the unchanged URL otherwise.
124 */
125 public String encodeURL(String url) {
126 return url;
127 }
128 /***
129 * Encodes the specified URL for use in the
130 * <code>sendRedirect</code> method or, if encoding is not needed,
131 * returns the URL unchanged. The implementation of this method
132 * includes the logic to determine whether the session ID
133 * needs to be encoded in the URL. Because the rules for making
134 * this determination can differ from those used to decide whether to
135 * encode a normal link, this method is seperate from the
136 * <code>encodeURL</code> method.
137 *
138 * <p>All URLs sent to the <code>HttpServletResponse.sendRedirect</code>
139 * method should be run through this method. Otherwise, URL
140 * rewriting cannot be used with browsers which do not support
141 * cookies.
142 *
143 * @param url the url to be encoded.
144 * @return the encoded URL if encoding is needed;
145 * the unchanged URL otherwise.
146 *
147 * @see #sendRedirect
148 * @see #encodeUrl
149 */
150 public String encodeRedirectURL(String url) {
151 return url;
152 }
153 /***
154 * Sends a temporary redirect response to the client using the
155 * specified redirect location URL. This method can accept relative URLs;
156 * the servlet container will convert the relative URL to an absolute URL
157 * before sending the response to the client.
158 *
159 * <p>If the response has already been committed, this method throws
160 * an IllegalStateException.
161 * After using this method, the response should be considered
162 * to be committed and should not be written to.
163 *
164 * @param location the redirect location URL
165 * @exception IOException If an input or output exception occurs
166 * @exception IllegalStateException If the response was committed
167 */
168 public void sendRedirect(String location) throws IOException {
169 }
170 /***
171 *
172 * Sets a response header with the given name and
173 * date-value. The date is specified in terms of
174 * milliseconds since the epoch. If the header had already
175 * been set, the new value overwrites the previous one. The
176 * <code>containsHeader</code> method can be used to test for the
177 * presence of a header before setting its value.
178 *
179 * @param name the name of the header to set
180 * @param value the assigned date value
181 *
182 * @see #containsHeader
183 * @see #addDateHeader
184 */
185 public void setDateHeader(String name, long date) {
186 }
187 /***
188 * Sends an error response to the client using the specified status
189 * code and descriptive message. The server generally creates the
190 * response to look like a normal server error page.
191 *
192 * <p>If the response has already been committed, this method throws
193 * an IllegalStateException.
194 * After using this method, the response should be considered
195 * to be committed and should not be written to.
196 *
197 * @param sc the error status code
198 * @param msg the descriptive message
199 * @exception IOException If an input or output exception occurs
200 * @exception IllegalStateException If the response was committed
201 * before this method call
202 */
203 public void sendError(int sc, String msg) throws IOException {
204 statusCode = sc;
205 errorMessage = msg;
206 }
207 /***
208 * Sends an error response to the client using the specified
209 * status. The server generally creates the
210 * response to look like a normal server error page.
211 *
212 * <p>If the response has already been committed, this method throws
213 * an IllegalStateException.
214 * After using this method, the response should be considered
215 * to be committed and should not be written to.
216 *
217 * @param sc the error status code
218 * @exception IOException If an input or output exception occurs
219 * @exception IllegalStateException If the response was committed
220 */
221
222 public void sendError(int sc) throws IOException {
223 statusCode = sc;
224 }
225 /***
226 * Adds a response header with the given name and value.
227 * This method allows response headers to have multiple values.
228 *
229 * @param name the name of the header
230 * @param value the additional header value
231 *
232 * @see #setHeader
233 */
234 public void addHeader(String name, String value) {
235 }
236 /***
237 * Sets a response header with the given name and
238 * integer value. If the header had already been set, the new value
239 * overwrites the previous one. The <code>containsHeader</code>
240 * method can be used to test for the presence of a header before
241 * setting its value.
242 *
243 * @param name the name of the header
244 * @param value the assigned integer value
245 *
246 * @see #containsHeader
247 * @see #addIntHeader
248 */
249 public void setIntHeader(String name, int value) {
250 }
251 /***
252 *
253 * Adds a response header with the given name and
254 * date-value. The date is specified in terms of
255 * milliseconds since the epoch. This method allows response headers
256 * to have multiple values.
257 *
258 * @param name the name of the header to set
259 * @param value the additional date value
260 *
261 * @see #setDateHeader
262 */
263 public void addDateHeader(String name, long date) {
264 }
265 /***
266 *
267 * Sets a response header with the given name and value.
268 * If the header had already been set, the new value overwrites the
269 * previous one. The <code>containsHeader</code> method can be
270 * used to test for the presence of a header before setting its
271 * value.
272 *
273 * @param name the name of the header
274 * @param value the header value
275 *
276 * @see #containsHeader
277 * @see #addHeader
278 */
279 public void setHeader(String name, String value) {
280 }
281 /***
282 * @deprecated As of version 2.1, due to ambiguous meaning of the
283 * message parameter. To set a status code
284 * use <code>setStatus(int)</code>, to send an error with a description
285 * use <code>sendError(int, String)</code>.
286 *
287 * Sets the status code and message for this response.
288 *
289 * @param sc the status code
290 * @param sm the status message
291 */
292 public void setStatus(int sc, String sm) {
293 statusCode = sc;
294 errorMessage = sm;
295 }
296 /***
297 * Sets the status code for this response. This method is used to
298 * set the return status code when there is no error (for example,
299 * for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there
300 * is an error, the <code>sendError</code> method should be used
301 * instead.
302 *
303 * @param sc the status code
304 *
305 * @see #sendError
306 */
307
308 public void setStatus(int sc) {
309 statusCode = sc;
310 }
311 /***
312 * Adds a response header with the given name and
313 * integer value. This method allows response headers to have multiple
314 * values.
315 *
316 * @param name the name of the header
317 * @param value the assigned integer value
318 *
319 * @see #setIntHeader
320 */
321 public void addIntHeader(String name, int value) {
322 }
323 /***
324 * Sets the length of the content body in the response
325 * In HTTP servlets, this method sets the HTTP Content-Length header.
326 *
327 *
328 * @param len an integer specifying the length of the
329 * content being returned to the client; sets
330 * the Content-Length header
331 *
332 */
333 public void setContentLength(int len) {
334 contentLength = len;
335 }
336 /***
337 * Returns the name of the charset used for
338 * the MIME body sent in this response.
339 *
340 * <p>If no charset has been assigned, it is implicitly
341 * set to <code>ISO-8859-1</code> (<code>Latin-1</code>).
342 *
343 * <p>See RFC 2047 (http://ds.internic.net/rfc/rfc2045.txt)
344 * for more information about character encoding and MIME.
345 *
346 * @return a <code>String</code> specifying the
347 * name of the charset, for
348 * example, <code>ISO-8859-1</code>
349 *
350 */
351 public String getCharacterEncoding() {
352 return "ISO-8859-1";
353 }
354 /***
355 * Returns a {@link ServletOutputStream} suitable for writing binary
356 * data in the response. The servlet container does not encode the
357 * binary data. Either this method or {@link #getWriter} may
358 * be called to write the body, not both.
359 *
360 * @return a {@link ServletOutputStream} for writing binary data
361 *
362 * @exception IllegalStateException if the <code>getWriter</code> method
363 * has been called on this response
364 *
365 * @exception IOException if an input or output exception occurred
366 *
367 * @see #getWriter
368 *
369 */
370 public ServletOutputStream getOutputStream() throws IOException {
371 return stream;
372 }
373 /***
374 * Sets the preferred buffer size for the body of the response.
375 * The servlet container will use a buffer at least as large as
376 * the size requested. The actual buffer size used can be found
377 * using <code>getBufferSize</code>.
378 *
379 * <p>A larger buffer allows more content to be written before anything is
380 * actually sent, thus providing the servlet with more time to set
381 * appropriate status codes and headers. A smaller buffer decreases
382 * server memory load and allows the client to start receiving data more
383 * quickly.
384 *
385 * <p>This method must be called before any response body content is
386 * written; if content has been written, this method throws an
387 * <code>IllegalStateException</code>.
388 *
389 * @param size the preferred buffer size
390 *
391 * @exception IllegalStateException if this method is called after
392 * content has been written
393 *
394 * @see #getBufferSize
395 * @see #flushBuffer
396 * @see #isCommitted
397 * @see #reset
398 *
399 */
400 public void setBufferSize(int size) {
401 }
402 /***
403 * Returns a <code>PrintWriter</code> object that
404 * can send character text to the client.
405 * The character encoding used is the one specified
406 * in the <code>charset=</code> property of the
407 * {@link #setContentType} method, which must be called
408 * <i>before</i> calling this method for the charset to take effect.
409 *
410 * <p>If necessary, the MIME type of the response is
411 * modified to reflect the character encoding used.
412 *
413 * <p>Either this method or {@link #getOutputStream} may be called
414 * to write the body, not both.
415 *
416 *
417 * @return a <code>PrintWriter</code> object that
418 * can return character data to the client
419 *
420 * @exception UnsupportedEncodingException if the charset specified in
421 * <code>setContentType</code> cannot be
422 * used
423 *
424 * @exception IllegalStateException if the <code>getOutputStream</code>
425 * method has already been called for this
426 * response object
427 *
428 * @exception IOException if an input or output exception occurred
429 *
430 * @see #getOutputStream
431 * @see #setContentType
432 *
433 */
434 public PrintWriter getWriter() throws IOException {
435 if (Debug.ON) Debug.assertTrue(1==0, "getWriter not supported in this test class");
436 return null;
437 }
438 /***
439 * Forces any content in the buffer to be written to the client. A call
440 * to this method automatically commits the response, meaning the status
441 * code and headers will be written.
442 *
443 * @see #setBufferSize
444 * @see #getBufferSize
445 * @see #isCommitted
446 * @see #reset
447 *
448 */
449 public void flushBuffer() throws IOException {
450 stream.flush();
451 }
452 /***
453 * Sets the content type of the response being sent to
454 * the client. The content type may include the type of character
455 * encoding used, for example, <code>text/html; charset=ISO-8859-4</code>.
456 *
457 * <p>If obtaining a <code>PrintWriter</code>, this method should be
458 * called first.
459 *
460 *
461 * @param type a <code>String</code> specifying the MIME
462 * type of the content
463 *
464 * @see #getOutputStream
465 * @see #getWriter
466 *
467 */
468 public void setContentType(String type) {
469 contentType = type;
470 }
471 /***
472 * Clears any data that exists in the buffer as well as the status code and
473 * headers. If the response has been committed, this method throws an
474 * <code>IllegalStateException</code>.
475 *
476 * @exception IllegalStateException if the response has already been
477 * committed
478 *
479 * @see #setBufferSize
480 * @see #getBufferSize
481 * @see #flushBuffer
482 * @see #isCommitted
483 *
484 */
485 public void reset() {
486 }
487 /***
488 * Returns the actual buffer size used for the response. If no buffering
489 * is used, this method returns 0.
490 *
491 * @return the actual buffer size used
492 *
493 * @see #setBufferSize
494 * @see #flushBuffer
495 * @see #isCommitted
496 * @see #reset
497 *
498 */
499 public int getBufferSize() {
500 return 0;
501 }
502 /***
503 * Returns the locale assigned to the response.
504 *
505 *
506 * @see #setLocale
507 *
508 */
509 public Locale getLocale() {
510 return Locale.getDefault();
511 }
512 /***
513 * Returns a boolean indicating if the response has been
514 * committed. A commited response has already had its status
515 * code and headers written.
516 *
517 * @return a boolean indicating if the response has been
518 * committed
519 *
520 * @see #setBufferSize
521 * @see #getBufferSize
522 * @see #flushBuffer
523 * @see #reset
524 *
525 */
526 public boolean isCommitted() {
527 return true;
528 }
529 /***
530 * Sets the locale of the response, setting the headers (including the
531 * Content-Type's charset) as appropriate. This method should be called
532 * before a call to {@link #getWriter}. By default, the response locale
533 * is the default locale for the server.
534 *
535 * @param loc the locale of the response
536 *
537 * @see #getLocale
538 *
539 */
540 public void setLocale(Locale loc) {
541 }
542
543 private String errorMessage;
544 String getErrorMessage() {
545 return errorMessage;
546 }
547
548 private int statusCode;
549 int getStatusCode() {
550 return statusCode;
551 }
552
553 private int contentLength;
554 int getContentLength() {
555 return contentLength;
556 }
557
558 private String contentType;
559 String getContentType() {
560 return contentType;
561 }
562
563 private ServletOutputStream stream = new DummyServletOutputStream();
564 // private StringWriter stringWriter = new StringWriter();
565 // private PrintWriter writer = new PrintWriter(stringWriter);
566
567 String getContent() {
568 return stream.toString();
569 }
570 }
571
572 class DummyServletOutputStream extends ServletOutputStream {
573
574 private static final Log LOG = LogFactory.getLog(DummyServletOutputStream.class);
575
576 private ByteArrayOutputStream delegate = new ByteArrayOutputStream();
577
578 /***
579 * Writes the specified byte to this output stream. The general
580 * contract for <code>write</code> is that one byte is written
581 * to the output stream. The byte to be written is the eight
582 * low-order bits of the argument <code>b</code>. The 24
583 * high-order bits of <code>b</code> are ignored.
584 * <p>
585 * Subclasses of <code>OutputStream</code> must provide an
586 * implementation for this method.
587 *
588 * @param b the <code>byte</code>.
589 * @exception IOException if an I/O error occurs. In particular,
590 * an <code>IOException</code> may be thrown if the
591 * output stream has been closed.
592 */
593 public void write(int b) throws IOException {
594 if (LOG.isDebugEnabled()) LOG.debug("write: " + b);
595 delegate.write(b);
596 }
597
598 public String toString() {
599 return delegate.toString();
600 }
601 }
This page was automatically generated by Maven