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;
021    
022    import java.io.BufferedReader;
023    import java.io.IOException;
024    import java.util.Enumeration;
025    import java.util.Locale;
026    import java.util.Map;
027    
028    /**
029     * Defines an object to provide client request information to a servlet.  The
030     * servlet container creates a <code>ServletRequest</code> object and passes
031     * it as an argument to the servlet's <code>service</code> method.
032     * <p/>
033     * <p>A <code>ServletRequest</code> object provides data including
034     * parameter name and values, attributes, and an input stream.
035     * Interfaces that extend <code>ServletRequest</code> can provide
036     * additional protocol-specific data (for example, HTTP data is
037     * provided by {@link javax.servlet.http.HttpServletRequest}.
038     *
039     * @see javax.servlet.http.HttpServletRequest
040     * @version $Rev: 835965 $ $Date: 2009-11-13 14:40:44 -0500 (Fri, 13 Nov 2009) $
041     */
042    
043    public interface ServletRequest {
044    
045        /**
046         * @return async context
047         * @since Servlet 3.0
048         */
049        AsyncContext getAsyncContext();
050    
051        /**
052         * Returns the value of the named attribute as an <code>Object</code>,
053         * or <code>null</code> if no attribute of the given name exists.
054         * <p/>
055         * <p> Attributes can be set two ways.  The servlet container may set
056         * attributes to make available custom information about a request.
057         * For example, for requests made using HTTPS, the attribute
058         * <code>javax.servlet.request.X509Certificate</code> can be used to
059         * retrieve information on the certificate of the client.  Attributes
060         * can also be set programatically using
061         * {@link ServletRequest#setAttribute}.  This allows information to be
062         * embedded into a request before a {@link RequestDispatcher} call.
063         * <p/>
064         * <p>Attribute names should follow the same conventions as package
065         * names. This specification reserves names matching <code>java.*</code>,
066         * <code>javax.*</code>, and <code>sun.*</code>.
067         *
068         * @param name a <code>String</code> specifying the name of
069         *             the attribute
070         * @return an <code>Object</code> containing the value
071         *         of the attribute, or <code>null</code> if
072         *         the attribute does not exist
073         */
074        Object getAttribute(String name);
075    
076        /**
077         * Returns an <code>Enumeration</code> containing the
078         * names of the attributes available to this request.
079         * This method returns an empty <code>Enumeration</code>
080         * if the request has no attributes available to it.
081         *
082         * @return an <code>Enumeration</code> of strings
083         *         containing the names
084         *         of the request's attributes
085         */
086        Enumeration<String> getAttributeNames();
087    
088        /**
089         * Returns the name of the character encoding used in the body of this
090         * request. This method returns <code>null</code> if the request
091         * does not specify a character encoding
092         *
093         * @return a <code>String</code> containing the name of
094         *         the character encoding, or <code>null</code>
095         *         if the request does not specify a character encoding
096         */
097        String getCharacterEncoding();
098    
099        /**
100         * Returns the length, in bytes, of the request body
101         * and made available by the input stream, or -1 if the
102         * length is not known. For HTTP servlets, same as the value
103         * of the CGI variable CONTENT_LENGTH.
104         *
105         * @return an integer containing the length of the
106         *         request body or -1 if the length is not known
107         */
108        int getContentLength();
109    
110        /**
111         * Returns the MIME type of the body of the request, or
112         * <code>null</code> if the type is not known. For HTTP servlets,
113         * same as the value of the CGI variable CONTENT_TYPE.
114         *
115         * @return a <code>String</code> containing the name
116         *         of the MIME type of
117         *         the request, or null if the type is not known
118         */
119        String getContentType();
120    
121        /**
122         * @since Servlet 3.0
123         * @return dispatcher type
124         */
125        DispatcherType getDispatcherType();
126    
127        /**
128         * Retrieves the body of the request as binary data using
129         * a {@link ServletInputStream}.  Either this method or
130         * {@link #getReader} may be called to read the body, not both.
131         *
132         * @return a {@link ServletInputStream} object containing
133         *         the body of the request
134         * @throws IllegalStateException if the {@link #getReader} method
135         *                               has already been called for this request
136         * @throws IOException           if an input or output exception occurred
137         */
138        ServletInputStream getInputStream() throws IOException;
139    
140        /**
141         * Returns the Internet Protocol (IP) address of the interface on
142         * which the request  was received.
143         *
144         * @return a <code>String</code> containing the
145         *         IP address on which the request was received.
146         * @since 2.4
147         */
148        String getLocalAddr();
149    
150        /**
151         * Returns the preferred <code>Locale</code> that the client will
152         * accept content in, based on the Accept-Language header.
153         * If the client request doesn't provide an Accept-Language header,
154         * this method returns the default locale for the server.
155         *
156         * @return the preferred <code>Locale</code> for the client
157         */
158        Locale getLocale();
159    
160        /**
161         * Returns an <code>Enumeration</code> of <code>Locale</code> objects
162         * indicating, in decreasing order starting with the preferred locale, the
163         * locales that are acceptable to the client based on the Accept-Language
164         * header.
165         * If the client request doesn't provide an Accept-Language header,
166         * this method returns an <code>Enumeration</code> containing one
167         * <code>Locale</code>, the default locale for the server.
168         *
169         * @return an <code>Enumeration</code> of preferred
170         *         <code>Locale</code> objects for the client
171         */
172        Enumeration<Locale> getLocales();
173    
174        /**
175         * Returns the host name of the Internet Protocol (IP) interface on
176         * which the request was received.
177         *
178         * @return a <code>String</code> containing the host
179         *         name of the IP on which the request was received.
180         * @since 2.4
181         */
182        String getLocalName();
183    
184        /**
185         * Returns the Internet Protocol (IP) port number of the interface
186         * on which the request was received.
187         *
188         * @return an integer specifying the port number
189         * @since 2.4
190         */
191        int getLocalPort();
192    
193         /**
194         * Returns the value of a request parameter as a <code>String</code>,
195         * or <code>null</code> if the parameter does not exist. Request parameters
196         * are extra information sent with the request.  For HTTP servlets,
197         * parameters are contained in the query string or posted form data.
198         * <p/>
199         * <p>You should only use this method when you are sure the
200         * parameter has only one value. If the parameter might have
201         * more than one value, use {@link #getParameterValues}.
202         * <p/>
203         * <p>If you use this method with a multivalued
204         * parameter, the value returned is equal to the first value
205         * in the array returned by <code>getParameterValues</code>.
206         * <p/>
207         * <p>If the parameter data was sent in the request body, such as occurs
208         * with an HTTP POST request, then reading the body directly via {@link
209         * #getInputStream} or {@link #getReader} can interfere
210         * with the execution of this method.
211         *
212         * @param name a <code>String</code> specifying the
213         *             name of the parameter
214         * @return a <code>String</code> representing the
215         *         single value of the parameter
216         * @see #getParameterValues
217         */
218        String getParameter(String name);
219    
220        /**
221         * Returns a java.util.Map of the parameters of this request.
222         * Request parameters
223         * are extra information sent with the request.  For HTTP servlets,
224         * parameters are contained in the query string or posted form data.
225         *
226         * @return an immutable java.util.Map containing parameter names as
227         *         keys and parameter values as map values. The keys in the parameter
228         *         map are of type String. The values in the parameter map are of type
229         *         String array.
230         */
231        Map<String, String[]> getParameterMap();
232    
233        /**
234         * Returns an <code>Enumeration</code> of <code>String</code>
235         * objects containing the names of the parameters contained
236         * in this request. If the request has
237         * no parameters, the method returns an
238         * empty <code>Enumeration</code>.
239         *
240         * @return an <code>Enumeration</code> of <code>String</code>
241         *         objects, each <code>String</code> containing
242         *         the name of a request parameter; or an
243         *         empty <code>Enumeration</code> if the
244         *         request has no parameters
245         */
246        Enumeration<String> getParameterNames();
247    
248        /**
249         * Returns an array of <code>String</code> objects containing
250         * all of the values the given request parameter has, or
251         * <code>null</code> if the parameter does not exist.
252         * <p/>
253         * <p>If the parameter has a single value, the array has a length
254         * of 1.
255         *
256         * @param name a <code>String</code> containing the name of
257         *             the parameter whose value is requested
258         * @return an array of <code>String</code> objects
259         *         containing the parameter's values
260         * @see #getParameter
261         */
262    
263        String[] getParameterValues(String name);
264    
265        /**
266         * Returns the name and version of the protocol the request uses
267         * in the form <i>protocol/majorVersion.minorVersion</i>, for
268         * example, HTTP/1.1. For HTTP servlets, the value
269         * returned is the same as the value of the CGI variable
270         * <code>SERVER_PROTOCOL</code>.
271         *
272         * @return a <code>String</code> containing the protocol
273         *         name and version number
274         */
275        String getProtocol();
276    
277        /**
278         * Retrieves the body of the request as character data using
279         * a <code>BufferedReader</code>.  The reader translates the character
280         * data according to the character encoding used on the body.
281         * Either this method or {@link #getInputStream} may be called to read the
282         * body, not both.
283         *
284         * @return a <code>BufferedReader</code>
285         *         containing the body of the request
286         * @throws java.io.UnsupportedEncodingException
287         *                               if the character set encoding
288         *                               used is not supported and the
289         *                               text cannot be decoded
290         * @throws IllegalStateException if {@link #getInputStream} method
291         *                               has been called on this request
292         * @throws IOException           if an input or output exception occurred
293         * @see #getInputStream
294         */
295        BufferedReader getReader() throws IOException;
296    
297        /**
298         * @deprecated As of Version 2.1 of the Java Servlet API,
299         *             use {@link ServletContext#getRealPath} instead.
300         */
301        String getRealPath(String path);
302    
303        /**
304         * Returns the Internet Protocol (IP) address of the client
305         * or last proxy that sent the request.
306         * For HTTP servlets, same as the value of the
307         * CGI variable <code>REMOTE_ADDR</code>.
308         *
309         * @return a <code>String</code> containing the
310         *         IP address of the client that sent the request
311         */
312        String getRemoteAddr();
313    
314        /**
315         * Returns the fully qualified name of the client
316         * or the last proxy that sent the request.
317         * If the engine cannot or chooses not to resolve the hostname
318         * (to improve performance), this method returns the dotted-string form of
319         * the IP address. For HTTP servlets, same as the value of the CGI variable
320         * <code>REMOTE_HOST</code>.
321         *
322         * @return a <code>String</code> containing the fully
323         *         qualified name of the client
324         */
325        String getRemoteHost();
326    
327         /**
328          * Returns the Internet Protocol (IP) source port of the client
329          * or last proxy that sent the request.
330          *
331          * @return an integer specifying the port number
332          * @since 2.4
333          */
334         int getRemotePort();
335    
336        /**
337         * Returns a {@link RequestDispatcher} object that acts as a wrapper for
338         * the resource located at the given path.
339         * A <code>RequestDispatcher</code> object can be used to forward
340         * a request to the resource or to include the resource in a response.
341         * The resource can be dynamic or static.
342         * <p/>
343         * <p>The pathname specified may be relative, although it cannot extend
344         * outside the current servlet context.  If the path begins with
345         * a "/" it is interpreted as relative to the current context root.
346         * This method returns <code>null</code> if the servlet container
347         * cannot return a <code>RequestDispatcher</code>.
348         * <p/>
349         * <p>The difference between this method and {@link
350         * ServletContext#getRequestDispatcher} is that this method can take a
351         * relative path.
352         *
353         * @param path a <code>String</code> specifying the pathname
354         *             to the resource. If it is relative, it must be
355         *             relative against the current servlet.
356         * @return a <code>RequestDispatcher</code> object
357         *         that acts as a wrapper for the resource
358         *         at the specified path, or <code>null</code>
359         *         if the servlet container cannot return a
360         *         <code>RequestDispatcher</code>
361         * @see RequestDispatcher
362         * @see ServletContext#getRequestDispatcher
363         */
364        RequestDispatcher getRequestDispatcher(String path);
365    
366        /**
367         * Returns the name of the scheme used to make this request,
368         * for example,
369         * <code>http</code>, <code>https</code>, or <code>ftp</code>.
370         * Different schemes have different rules for constructing URLs,
371         * as noted in RFC 1738.
372         *
373         * @return a <code>String</code> containing the name
374         *         of the scheme used to make this request
375         */
376        String getScheme();
377    
378        /**
379         * Returns the host name of the server to which the request was sent.
380         * It is the value of the part before ":" in the <code>Host</code>
381         * header value, if any, or the resolved server name, or the server IP address.
382         *
383         * @return a <code>String</code> containing the name
384         *         of the server
385         */
386        String getServerName();
387    
388        /**
389         * Returns the port number to which the request was sent.
390         * It is the value of the part after ":" in the <code>Host</code>
391         * header value, if any, or the server port where the client connection
392         * was accepted on.
393         *
394         * @return an integer specifying the port number
395         */
396        int getServerPort();
397    
398        /**
399          * Get the servlet context the request-response pair was last dispatched through.
400          *
401          * @return the latest ServletContext on the dispatch chain.
402          * @since 3.0
403          */
404         ServletContext getServletContext();
405    
406        /**
407         * @since Servlet 3.0
408         * @return if async is started
409         */
410        boolean isAsyncStarted();
411    
412        /**
413         * @since Servlet 3.0
414         * @return if async is supported
415         */
416        boolean isAsyncSupported();
417    
418        /**
419         * Returns a boolean indicating whether this request was made using a
420         * secure channel, such as HTTPS.
421         *
422         * @return a boolean indicating if the request was made using a
423         *         secure channel
424         */
425        boolean isSecure();
426    
427        /**
428         * Removes an attribute from this request.  This method is not
429         * generally needed as attributes only persist as long as the request
430         * is being handled.
431         * <p/>
432         * <p>Attribute names should follow the same conventions as
433         * package names. Names beginning with <code>java.*</code>,
434         * <code>javax.*</code>, and <code>com.sun.*</code>, are
435         * reserved for use by Sun Microsystems.
436         *
437         * @param name a <code>String</code> specifying
438         *             the name of the attribute to remove
439         */
440        void removeAttribute(String name);
441    
442        /**
443         * Stores an attribute in this request.
444         * Attributes are reset between requests.  This method is most
445         * often used in conjunction with {@link RequestDispatcher}.
446         * <p/>
447         * <p>Attribute names should follow the same conventions as
448         * package names. Names beginning with <code>java.*</code>,
449         * <code>javax.*</code>, and <code>com.sun.*</code>, are
450         * reserved for use by Sun Microsystems.
451         * <br> If the object passed in is null, the effect is the same as
452         * calling {@link #removeAttribute}.
453         * <br> It is warned that when the request is dispatched from the
454         * servlet resides in a different web application by
455         * <code>RequestDispatcher</code>, the object set by this method
456         * may not be correctly retrieved in the caller servlet.
457         *
458         * @param name a <code>String</code> specifying
459         *             the name of the attribute
460         * @param o    the <code>Object</code> to be stored
461         */
462        void setAttribute(String name, Object o);
463        
464        /**
465         * Overrides the name of the character encoding used in the body of this
466         * request. This method must be called prior to reading request parameters
467         * or reading input using getReader().
468         *
469         * @param env a <code>String</code> containing the name of
470         *            the character encoding.
471         * @throws java.io.UnsupportedEncodingException
472         *          if this is not a valid encoding
473         */
474        void setCharacterEncoding(String env) throws java.io.UnsupportedEncodingException;
475    
476        /**
477         *
478         * @return AsyncContext to control further work, initialized with the original request and response
479         * @since 3.0
480         */
481        AsyncContext startAsync();
482    
483        /**
484         *
485         * @param request servlet request
486         * @param response servlet response
487         * @return AsyncContext to control further work, initialized with the supplied request and response
488         * @since 3.0
489         */
490        AsyncContext startAsync(ServletRequest request, ServletResponse response);
491    
492    }
493