001    /*
002    * Copyright 2004 The Apache Software Foundation
003    *
004    * Licensed under the Apache License, Version 2.0 (the "License");
005    * you may not use this file except in compliance with the License.
006    * You may obtain a copy of the License at
007    *
008    *     http://www.apache.org/licenses/LICENSE-2.0
009    *
010    * Unless required by applicable law or agreed to in writing, software
011    * distributed under the License is distributed on an "AS IS" BASIS,
012    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    * See the License for the specific language governing permissions and
014    * limitations under the License.
015    */
016    package javax.servlet.http;
017    
018    import java.io.IOException;
019    
020    import javax.servlet.ServletResponse;
021    
022    /**
023     *
024     * Extends the {@link ServletResponse} interface to provide HTTP-specific
025     * functionality in sending a response.  For example, it has methods
026     * to access HTTP headers and cookies.
027     *
028     * <p>The servlet container creates an <code>HttpServletResponse</code> object
029     * and passes it as an argument to the servlet's service methods
030     * (<code>doGet</code>, <code>doPost</code>, etc).
031     *
032     * 
033     * @author      Various
034     * @version     $Version$
035     *
036     * @see         javax.servlet.ServletResponse
037     *
038     */
039    
040    
041    
042    public interface HttpServletResponse extends ServletResponse {
043    
044        /**
045         * Adds the specified cookie to the response.  This method can be called
046         * multiple times to set more than one cookie.
047         *
048         * @param cookie the Cookie to return to the client
049         *
050         */
051    
052        public void addCookie(Cookie cookie);
053    
054        /**
055         * Returns a boolean indicating whether the named response header 
056         * has already been set.
057         * 
058         * @param   name    the header name
059         * @return          <code>true</code> if the named response header 
060         *                  has already been set; 
061         *                  <code>false</code> otherwise
062         */
063    
064        public boolean containsHeader(String name);
065    
066        /**
067         * Encodes the specified URL by including the session ID in it,
068         * or, if encoding is not needed, returns the URL unchanged.
069         * The implementation of this method includes the logic to
070         * determine whether the session ID needs to be encoded in the URL.
071         * For example, if the browser supports cookies, or session
072         * tracking is turned off, URL encoding is unnecessary.
073         * 
074         * <p>For robust session tracking, all URLs emitted by a servlet 
075         * should be run through this
076         * method.  Otherwise, URL rewriting cannot be used with browsers
077         * which do not support cookies.
078         *
079         * @param   url     the url to be encoded.
080         * @return          the encoded URL if encoding is needed;
081         *                  the unchanged URL otherwise.
082         */
083    
084        public String encodeURL(String url);
085    
086        /**
087         * Encodes the specified URL for use in the
088         * <code>sendRedirect</code> method or, if encoding is not needed,
089         * returns the URL unchanged.  The implementation of this method
090         * includes the logic to determine whether the session ID
091         * needs to be encoded in the URL.  Because the rules for making
092         * this determination can differ from those used to decide whether to
093         * encode a normal link, this method is separated from the
094         * <code>encodeURL</code> method.
095         * 
096         * <p>All URLs sent to the <code>HttpServletResponse.sendRedirect</code>
097         * method should be run through this method.  Otherwise, URL
098         * rewriting cannot be used with browsers which do not support
099         * cookies.
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         * @see #sendRedirect
106         * @see #encodeUrl
107         */
108    
109        public String encodeRedirectURL(String url);
110    
111        /**
112         * @deprecated      As of version 2.1, use encodeURL(String url) instead
113         *
114         * @param   url     the url to be encoded.
115         * @return          the encoded URL if encoding is needed; 
116         *                  the unchanged URL otherwise.
117         */
118    
119        public String encodeUrl(String url);
120        
121        /**
122         * @deprecated      As of version 2.1, use 
123         *                  encodeRedirectURL(String url) instead
124         *
125         * @param   url     the url to be encoded.
126         * @return          the encoded URL if encoding is needed; 
127         *                  the unchanged URL otherwise.
128         */
129    
130        public String encodeRedirectUrl(String url);
131    
132        /**
133         * Sends an error response to the client using the specified
134         * status.  The server defaults to creating the
135         * response to look like an HTML-formatted server error page
136         * containing the specified message, setting the content type
137         * to "text/html", leaving cookies and other headers unmodified.
138         *
139         * If an error-page declaration has been made for the web application
140         * corresponding to the status code passed in, it will be served back in 
141         * preference to the suggested msg parameter. 
142         *
143         * <p>If the response has already been committed, this method throws 
144         * an IllegalStateException.
145         * After using this method, the response should be considered
146         * to be committed and should not be written to.
147         *
148         * @param   sc      the error status code
149         * @param   msg     the descriptive message
150         * @exception       IOException     If an input or output exception occurs
151         * @exception       IllegalStateException   If the response was committed
152         */
153       
154        public void sendError(int sc, String msg) throws IOException;
155    
156        /**
157         * Sends an error response to the client using the specified status
158         * code and clearing the buffer. 
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   sc      the error status code
165         * @exception       IOException     If an input or output exception occurs
166         * @exception       IllegalStateException   If the response was committed
167         *                                          before this method call
168         */
169    
170        public void sendError(int sc) throws IOException;
171    
172        /**
173         * Sends a temporary redirect response to the client using the
174         * specified redirect location URL.  This method can accept relative URLs;
175         * the servlet container must convert the relative URL to an absolute URL
176         * before sending the response to the client. If the location is relative 
177         * without a leading '/' the container interprets it as relative to
178         * the current request URI. If the location is relative with a leading
179         * '/' the container interprets it as relative to the servlet container root.
180         *
181         * <p>If the response has already been committed, this method throws 
182         * an IllegalStateException.
183         * After using this method, the response should be considered
184         * to be committed and should not be written to.
185         *
186         * @param           location        the redirect location URL
187         * @exception       IOException     If an input or output exception occurs
188         * @exception       IllegalStateException   If the response was committed or
189     if a partial URL is given and cannot be converted into a valid URL
190         */
191    
192        public void sendRedirect(String location) throws IOException;
193        
194        /**
195         * 
196         * Sets a response header with the given name and
197         * date-value.  The date is specified in terms of
198         * milliseconds since the epoch.  If the header had already
199         * been set, the new value overwrites the previous one.  The
200         * <code>containsHeader</code> method can be used to test for the
201         * presence of a header before setting its value.
202         * 
203         * @param   name    the name of the header to set
204         * @param   date    the assigned date value
205         * 
206         * @see #containsHeader
207         * @see #addDateHeader
208         */
209    
210        public void setDateHeader(String name, long date);
211        
212        /**
213         * 
214         * Adds a response header with the given name and
215         * date-value.  The date is specified in terms of
216         * milliseconds since the epoch.  This method allows response headers 
217         * to have multiple values.
218         * 
219         * @param   name    the name of the header to set
220         * @param   date    the additional date value
221         * 
222         * @see #setDateHeader
223         */
224    
225        public void addDateHeader(String name, long date);
226        
227        /**
228         *
229         * Sets a response header with the given name and value.
230         * If the header had already been set, the new value overwrites the
231         * previous one.  The <code>containsHeader</code> method can be
232         * used to test for the presence of a header before setting its
233         * value.
234         * 
235         * @param   name    the name of the header
236         * @param   value   the header value  If it contains octet string,
237         *          it should be encoded according to RFC 2047
238         *          (http://www.ietf.org/rfc/rfc2047.txt)
239         *
240         * @see #containsHeader
241         * @see #addHeader
242         */
243    
244        public void setHeader(String name, String value);
245        
246        /**
247         * Adds a response header with the given name and value.
248         * This method allows response headers to have multiple values.
249         * 
250         * @param   name    the name of the header
251         * @param   value   the additional header value   If it contains
252         *          octet string, it should be encoded
253         *          according to RFC 2047
254         *          (http://www.ietf.org/rfc/rfc2047.txt)
255         *
256         * @see #setHeader
257         */
258    
259        public void addHeader(String name, String value);
260    
261        /**
262         * Sets a response header with the given name and
263         * integer value.  If the header had already been set, the new value
264         * overwrites the previous one.  The <code>containsHeader</code>
265         * method can be used to test for the presence of a header before
266         * setting its value.
267         *
268         * @param   name    the name of the header
269         * @param   value   the assigned integer value
270         *
271         * @see #containsHeader
272         * @see #addIntHeader
273         */
274    
275        public void setIntHeader(String name, int value);
276    
277        /**
278         * Adds a response header with the given name and
279         * integer value.  This method allows response headers to have multiple
280         * values.
281         *
282         * @param   name    the name of the header
283         * @param   value   the assigned integer value
284         *
285         * @see #setIntHeader
286         */
287    
288        public void addIntHeader(String name, int value);
289    
290    
291        
292        /**
293         * Sets the status code for this response.  This method is used to
294         * set the return status code when there is no error (for example,
295         * for the status codes SC_OK or SC_MOVED_TEMPORARILY).  If there
296         * is an error, and the caller wishes to invoke an error-page defined
297         * in the web application, the <code>sendError</code> method should be used
298         * instead.
299         * <p> The container clears the buffer and sets the Location header, preserving
300         * cookies and other headers.
301         *
302         * @param   sc      the status code
303         *
304         * @see #sendError
305         */
306    
307        public void setStatus(int sc);
308      
309        /**
310         * @deprecated As of version 2.1, due to ambiguous meaning of the 
311         * message parameter. To set a status code 
312         * use <code>setStatus(int)</code>, to send an error with a description
313         * use <code>sendError(int, String)</code>.
314         *
315         * Sets the status code and message for this response.
316         * 
317         * @param   sc      the status code
318         * @param   sm      the status message
319         */
320    
321        public void setStatus(int sc, String sm);
322    
323        
324        /*
325         * Server status codes; see RFC 2068.
326         */
327    
328        /**
329         * Status code (100) indicating the client can continue.
330         */
331    
332        public static final int SC_CONTINUE = 100;
333    
334        
335        /**
336         * Status code (101) indicating the server is switching protocols
337         * according to Upgrade header.
338         */
339    
340        public static final int SC_SWITCHING_PROTOCOLS = 101;
341    
342        /**
343         * Status code (200) indicating the request succeeded normally.
344         */
345    
346        public static final int SC_OK = 200;
347    
348        /**
349         * Status code (201) indicating the request succeeded and created
350         * a new resource on the server.
351         */
352    
353        public static final int SC_CREATED = 201;
354    
355        /**
356         * Status code (202) indicating that a request was accepted for
357         * processing, but was not completed.
358         */
359    
360        public static final int SC_ACCEPTED = 202;
361    
362        /**
363         * Status code (203) indicating that the meta information presented
364         * by the client did not originate from the server.
365         */
366    
367        public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
368    
369        /**
370         * Status code (204) indicating that the request succeeded but that
371         * there was no new information to return.
372         */
373    
374        public static final int SC_NO_CONTENT = 204;
375    
376        /**
377         * Status code (205) indicating that the agent <em>SHOULD</em> reset
378         * the document view which caused the request to be sent.
379         */
380    
381        public static final int SC_RESET_CONTENT = 205;
382    
383        /**
384         * Status code (206) indicating that the server has fulfilled
385         * the partial GET request for the resource.
386         */
387    
388        public static final int SC_PARTIAL_CONTENT = 206;
389    
390        /**
391         * Status code (300) indicating that the requested resource
392         * corresponds to any one of a set of representations, each with
393         * its own specific location.
394         */
395    
396        public static final int SC_MULTIPLE_CHOICES = 300;
397    
398        /**
399         * Status code (301) indicating that the resource has permanently
400         * moved to a new location, and that future references should use a
401         * new URI with their requests.
402         */
403    
404        public static final int SC_MOVED_PERMANENTLY = 301;
405    
406        /**
407         * Status code (302) indicating that the resource has temporarily
408         * moved to another location, but that future references should
409         * still use the original URI to access the resource.
410         *
411         * This definition is being retained for backwards compatibility.
412         * SC_FOUND is now the preferred definition.
413         */
414    
415        public static final int SC_MOVED_TEMPORARILY = 302;
416    
417        /**
418        * Status code (302) indicating that the resource reside
419        * temporarily under a different URI. Since the redirection might
420        * be altered on occasion, the client should continue to use the
421        * Request-URI for future requests.(HTTP/1.1) To represent the
422        * status code (302), it is recommended to use this variable.
423        */
424    
425        public static final int SC_FOUND = 302;
426    
427        /**
428         * Status code (303) indicating that the response to the request
429         * can be found under a different URI.
430         */
431    
432        public static final int SC_SEE_OTHER = 303;
433    
434        /**
435         * Status code (304) indicating that a conditional GET operation
436         * found that the resource was available and not modified.
437         */
438    
439        public static final int SC_NOT_MODIFIED = 304;
440    
441        /**
442         * Status code (305) indicating that the requested resource
443         * <em>MUST</em> be accessed through the proxy given by the
444         * <code><em>Location</em></code> field.
445         */
446    
447        public static final int SC_USE_PROXY = 305;
448    
449         /**
450         * Status code (307) indicating that the requested resource 
451         * resides temporarily under a different URI. The temporary URI
452         * <em>SHOULD</em> be given by the <code><em>Location</em></code> 
453         * field in the response.
454         */
455    
456         public static final int SC_TEMPORARY_REDIRECT = 307;
457    
458        /**
459         * Status code (400) indicating the request sent by the client was
460         * syntactically incorrect.
461         */
462    
463        public static final int SC_BAD_REQUEST = 400;
464    
465        /**
466         * Status code (401) indicating that the request requires HTTP
467         * authentication.
468         */
469    
470        public static final int SC_UNAUTHORIZED = 401;
471    
472        /**
473         * Status code (402) reserved for future use.
474         */
475    
476        public static final int SC_PAYMENT_REQUIRED = 402;
477    
478        /**
479         * Status code (403) indicating the server understood the request
480         * but refused to fulfill it.
481         */
482    
483        public static final int SC_FORBIDDEN = 403;
484    
485        /**
486         * Status code (404) indicating that the requested resource is not
487         * available.
488         */
489    
490        public static final int SC_NOT_FOUND = 404;
491    
492        /**
493         * Status code (405) indicating that the method specified in the
494         * <code><em>Request-Line</em></code> is not allowed for the resource
495         * identified by the <code><em>Request-URI</em></code>.
496         */
497    
498        public static final int SC_METHOD_NOT_ALLOWED = 405;
499    
500        /**
501         * Status code (406) indicating that the resource identified by the
502         * request is only capable of generating response entities which have
503         * content characteristics not acceptable according to the accept
504         * headers sent in the request.
505         */
506    
507        public static final int SC_NOT_ACCEPTABLE = 406;
508    
509        /**
510         * Status code (407) indicating that the client <em>MUST</em> first
511         * authenticate itself with the proxy.
512         */
513    
514        public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
515    
516        /**
517         * Status code (408) indicating that the client did not produce a
518         * request within the time that the server was prepared to wait.
519         */
520    
521        public static final int SC_REQUEST_TIMEOUT = 408;
522    
523        /**
524         * Status code (409) indicating that the request could not be
525         * completed due to a conflict with the current state of the
526         * resource.
527         */
528    
529        public static final int SC_CONFLICT = 409;
530    
531        /**
532         * Status code (410) indicating that the resource is no longer
533         * available at the server and no forwarding address is known.
534         * This condition <em>SHOULD</em> be considered permanent.
535         */
536    
537        public static final int SC_GONE = 410;
538    
539        /**
540         * Status code (411) indicating that the request cannot be handled
541         * without a defined <code><em>Content-Length</em></code>.
542         */
543    
544        public static final int SC_LENGTH_REQUIRED = 411;
545    
546        /**
547         * Status code (412) indicating that the precondition given in one
548         * or more of the request-header fields evaluated to false when it
549         * was tested on the server.
550         */
551    
552        public static final int SC_PRECONDITION_FAILED = 412;
553    
554        /**
555         * Status code (413) indicating that the server is refusing to process
556         * the request because the request entity is larger than the server is
557         * willing or able to process.
558         */
559    
560        public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;
561    
562        /**
563         * Status code (414) indicating that the server is refusing to service
564         * the request because the <code><em>Request-URI</em></code> is longer
565         * than the server is willing to interpret.
566         */
567    
568        public static final int SC_REQUEST_URI_TOO_LONG = 414;
569    
570        /**
571         * Status code (415) indicating that the server is refusing to service
572         * the request because the entity of the request is in a format not
573         * supported by the requested resource for the requested method.
574         */
575    
576        public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
577    
578        /**
579         * Status code (416) indicating that the server cannot serve the
580         * requested byte range.
581         */
582    
583        public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
584    
585        /**
586         * Status code (417) indicating that the server could not meet the
587         * expectation given in the Expect request header.
588         */
589    
590        public static final int SC_EXPECTATION_FAILED = 417;
591    
592        /**
593         * Status code (500) indicating an error inside the HTTP server
594         * which prevented it from fulfilling the request.
595         */
596    
597        public static final int SC_INTERNAL_SERVER_ERROR = 500;
598    
599        /**
600         * Status code (501) indicating the HTTP server does not support
601         * the functionality needed to fulfill the request.
602         */
603    
604        public static final int SC_NOT_IMPLEMENTED = 501;
605    
606        /**
607         * Status code (502) indicating that the HTTP server received an
608         * invalid response from a server it consulted when acting as a
609         * proxy or gateway.
610         */
611    
612        public static final int SC_BAD_GATEWAY = 502;
613    
614        /**
615         * Status code (503) indicating that the HTTP server is
616         * temporarily overloaded, and unable to handle the request.
617         */
618    
619        public static final int SC_SERVICE_UNAVAILABLE = 503;
620    
621        /**
622         * Status code (504) indicating that the server did not receive
623         * a timely response from the upstream server while acting as
624         * a gateway or proxy.
625         */
626    
627        public static final int SC_GATEWAY_TIMEOUT = 504;
628    
629        /**
630         * Status code (505) indicating that the server does not support
631         * or refuses to support the HTTP protocol version that was used
632         * in the request message.
633         */
634    
635        public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
636    }