Source code: com/prolifics/servlet/FilterHttpServletResponse.java
1 /*************************************************/
2 /* Copyright (c) 1999 */
3 /* by */
4 /* JYACC, Inc., New York NY USA */
5 /* and contributors. */
6 /* Use of this program is governed by the */
7 /* JYACC Public License Version 1.0, a copy of */
8 /* which can be obtained at */
9 /* http://www.possl.org/jyacc-license.html */
10 /*************************************************/
11
12 /* @(#)FilterHttpServletResponse.java 77.1 99/04/22 15:03:04 */
13
14 package com.prolifics.servlet;
15
16 import javax.servlet.http.*;
17 import javax.servlet.*;
18 import java.io.IOException;
19
20 import java.io.PrintWriter;
21 import java.io.UnsupportedEncodingException;
22
23 /**
24 * An HTTP servlet response. This interface allows a servlet's
25 * <code>service</code> method to manipulate HTTP-protocol specified
26 * header information and return data to its client. It is implemented
27 * by network service developers for use within servlets.
28 *
29 *
30 * @version @(#)FilterHttpServletResponse.java 77.1 99/04/22 15:03:04
31 */
32 public
33 class FilterHttpServletResponse implements HttpServletResponse
34 {
35 private static String sccsid = "@(#)FilterHttpServletResponse.java 77.1 99/04/22 15:03:04";
36
37 private HttpServletResponse res;
38
39 public FilterHttpServletResponse(HttpServletResponse res)
40 {
41 this.res = res;
42 }
43
44 /**
45 * Adds the specified cookie to the response. It can be called
46 * multiple times to set more than one cookie.
47 *
48 * @param cookie the Cookie to return to the client
49 */
50 public void addCookie(Cookie cookie)
51 {
52 res.addCookie(cookie);
53 }
54
55 /**
56 * Checks whether the response message header has a field with
57 * the specified name.
58 *
59 * @param name the header field name
60 * @return true if the response message header has a field with
61 * the specified name; false otherwise
62 */
63 public boolean containsHeader(String name)
64 {
65 return res.containsHeader(name);
66 }
67
68 /**
69 * Sets the status code and message for this response. If the
70 * field had already been set, the new value overwrites the
71 * previous one. The message is sent as the body of an HTML
72 * page, which is returned to the user to describe the problem.
73 * The page is sent with a default HTML header; the message
74 * is enclosed in simple body tags (<body></body>).
75 *
76 * @param sc the status code
77 * @param sm the status message
78 */
79 public void setStatus(int sc, String sm)
80 {
81 res.setStatus(sc, sm);
82 }
83
84 /**
85 * Sets the status code for this response. This method is used to
86 * set the return status code when there is no error (for example,
87 * for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there
88 * is an error, the <code>sendError</code> method should be used
89 * instead.
90 *
91 * @param sc the status code
92 *
93 * @see #sendError
94 */
95 public void setStatus(int sc)
96 {
97 res.setStatus(sc);
98 }
99
100 /**
101 *
102 * Adds a field to the response header with the given name and value.
103 * If the field had already been set, the new value overwrites the
104 * previous one. The <code>containsHeader</code> method can be
105 * used to test for the presence of a header before setting its
106 * value.
107 *
108 * @param name the name of the header field
109 * @param value the header field's value
110 *
111 * @see #containsHeader
112 */
113 public void setHeader(String name, String value)
114 {
115 res.setHeader(name, value);
116 }
117
118 /**
119 * Adds a field to the response header with the given name and
120 * integer value. If the field had already been set, the new value
121 * overwrites the previous one. The <code>containsHeader</code>
122 * method can be used to test for the presence of a header before
123 * setting its value.
124 *
125 * @param name the name of the header field
126 * @param value the header field's integer value
127 *
128 * @see #containsHeader
129 */
130 public void setIntHeader(String name, int value)
131 {
132 res.setIntHeader(name, value);
133 }
134
135 /**
136 *
137 * Adds a field to the response header with the given name and
138 * date-valued field. The date is specified in terms of
139 * milliseconds since the epoch. If the date field had already
140 * been set, the new value overwrites the previous one. The
141 * <code>containsHeader</code> method can be used to test for the
142 * presence of a header before setting its value.
143 *
144 * @param name the name of the header field
145 * @param value the header field's date value
146 *
147 * @see #containsHeader
148 */
149 public void setDateHeader(String name, long date)
150 {
151 res.setDateHeader(name, date);
152 }
153
154 /**
155 * Sends an error response to the client using the specified status
156 * code and descriptive message. If setStatus has previously been
157 * called, it is reset to the error status code. The message is
158 * sent as the body of an HTML page, which is returned to the user
159 * to describe the problem. The page is sent with a default HTML
160 * header; the message is enclosed in simple body tags
161 * (<body></body>).
162 *
163 * @param sc the status code
164 * @param msg the detail message
165 * @exception IOException If an I/O error has occurred. */
166 public void sendError(int sc, String msg) throws IOException
167 {
168 res.sendError(sc, msg);
169 }
170
171 /**
172 * Sends an error response to the client using the specified
173 * status code and a default message.
174 * @param sc the status code
175 * @exception IOException If an I/O error has occurred.
176 */
177 public void sendError(int sc) throws IOException
178 {
179 res.sendError(sc);
180 }
181
182 /**
183 * Sends a temporary redirect response to the client using the
184 * specified redirect location URL. The URL must be absolute (for
185 * example, <code><em>https://hostname/path/file.html</em></code>).
186 * Relative URLs are not permitted here.
187 *
188 * @param location the redirect location URL
189 * @exception IOException If an I/O error has occurred.
190 */
191 public void sendRedirect(String location) throws IOException
192 {
193 res.sendRedirect(location);
194 }
195
196 /**
197 * Encodes the specified URL by including the session ID in it,
198 * or, if encoding is not needed, returns the URL unchanged.
199 * The implementation of this method should include the logic to
200 * determine whether the session ID needs to be encoded in the URL.
201 * For example, if the browser supports cookies, or session
202 * tracking is turned off, URL encoding is unnecessary.
203 *
204 * <p>All URLs emitted by a Servlet should be run through this
205 * method. Otherwise, URL rewriting cannot be used with browsers
206 * which do not support cookies.
207 *
208 * @param url the url to be encoded.
209 * @return the encoded URL if encoding is needed; the unchanged URL
210 * otherwise.
211 */
212 public String encodeUrl (String url)
213 {
214 return res.encodeUrl(url);
215 }
216
217 /**
218 * Encodes the specified URL by including the session ID in it,
219 * or, if encoding is not needed, returns the URL unchanged.
220 * The implementation of this method should include the logic to
221 * determine whether the session ID needs to be encoded in the URL.
222 * For example, if the browser supports cookies, or session
223 * tracking is turned off, URL encoding is unnecessary.
224 *
225 * <p>All URLs emitted by a Servlet should be run through this
226 * method. Otherwise, URL rewriting cannot be used with browsers
227 * which do not support cookies.
228 *
229 * @param url the url to be encoded.
230 * @return the encoded URL if encoding is needed; the unchanged URL
231 * otherwise.
232 */
233 public String encodeURL (String url)
234 {
235 /* new return res.encodeURL(url); */
236 return res.encodeUrl(url);
237 }
238
239 /**
240 * Encodes the specified URL for use in the
241 * <code>sendRedirect</code> method or, if encoding is not needed,
242 * returns the URL unchanged. The implementation of this method
243 * should include the logic to determine whether the session ID
244 * needs to be encoded in the URL. Because the rules for making
245 * this determination differ from those used to decide whether to
246 * encode a normal link, this method is seperate from the
247 * <code>encodeUrl</code> method.
248 *
249 * <p>All URLs sent to the HttpServletResponse.sendRedirect
250 * method should be run through this method. Otherwise, URL
251 * rewriting canont be used with browsers which do not support
252 * cookies.
253 *
254 * @param url the url to be encoded.
255 * @return the encoded URL if encoding is needed; the unchanged URL
256 * otherwise.
257 *
258 * @see #sendRedirect
259 * @see #encodeUrl
260 */
261 public String encodeRedirectUrl (String url)
262 {
263 return res.encodeRedirectUrl(url);
264 }
265
266 /**
267 * Encodes the specified URL for use in the
268 * <code>sendRedirect</code> method or, if encoding is not needed,
269 * returns the URL unchanged. The implementation of this method
270 * should include the logic to determine whether the session ID
271 * needs to be encoded in the URL. Because the rules for making
272 * this determination differ from those used to decide whether to
273 * encode a normal link, this method is seperate from the
274 * <code>encodeUrl</code> method.
275 *
276 * <p>All URLs sent to the HttpServletResponse.sendRedirect
277 * method should be run through this method. Otherwise, URL
278 * rewriting canont be used with browsers which do not support
279 * cookies.
280 *
281 * @param url the url to be encoded.
282 * @return the encoded URL if encoding is needed; the unchanged URL
283 * otherwise.
284 *
285 * @see #sendRedirect
286 * @see #encodeURL
287 */
288 public String encodeRedirectURL (String url)
289 {
290 /* new return res.encodeRedirectURL(url); */
291 return res.encodeRedirectUrl(url);
292 }
293
294 /**
295 * Sets the content length for this response.
296 *
297 * @param len the content length
298 */
299 public void setContentLength(int len)
300 {
301 res.setContentLength(len);
302 }
303
304 /**
305 * Sets the content type for this response. This type may later
306 * be implicitly modified by addition of properties such as the MIME
307 * <em>charset=<value></em> if the service finds it necessary,
308 * and the appropriate media type property has not been set.
309 *
310 * <p>This response property may only be assigned one time. If a
311 * writer is to be used to write a text response, this method must
312 * be called before the method <code>getWriter</code>. If an
313 * output stream will be used to write a response, this method must
314 * be called before the output stream is used to write response
315 * data.
316 *
317 * @param type the content's MIME type
318 * @see getOutputStream
319 * @see getWriter */
320 public void setContentType(String type)
321 {
322 res.setContentType(type);
323 }
324
325 /**
326 * Returns an output stream for writing binary response data.
327 *
328 * @see getWriter
329 * @exception IllegalStateException if getWriter has been
330 * called on this same request.
331 * @exception IOException if an I/O exception has occurred
332 */
333 public ServletOutputStream getOutputStream() throws IOException
334 {
335 return res.getOutputStream();
336 }
337
338 /**
339 * Returns a print writer for writing formatted text responses. The
340 * MIME type of the response will be modified, if necessary, to reflect
341 * the character encoding used, through the <em>charset=...</em>
342 * property. This means that the content type must be set before
343 * calling this method.
344 *
345 * @see getOutputStream
346 * @see setContentType
347 *
348 * @exception UnsupportedEncodingException if no such encoding can
349 * be provided
350 * @exception IllegalStateException if getOutputStream has been
351 * called on this same request.
352 * @exception IOException on other errors.
353 */
354 public PrintWriter getWriter () throws IOException
355 {
356 return res.getWriter();
357 }
358
359 /**
360 * Returns the character set encoding used for this MIME body.
361 * The character encoding is either the one specified in the
362 * assigned content type, or one which the client understands.
363 * If no content type has yet been assigned, it is implicitly
364 * set to <em>text/plain</em>
365 */
366 public String getCharacterEncoding ()
367 {
368 return res.getCharacterEncoding();
369 }
370 }