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