001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.services;
014
015import java.util.List;
016import java.util.Locale;
017
018/**
019 * Generic version of {@link javax.servlet.http.HttpServletRequest}, used to encapsulate the Servlet API version, and to
020 * help bridge the differences between Servlet API and Porlet API.
021 *
022 * The Request service is a {@linkplain org.apache.tapestry5.ioc.services.PropertyShadowBuilder shadow} of the current
023 * thread's request.
024 */
025public interface Request
026{
027    /**
028     * Gets the {@link Session}. If create is false and the session has not be created previously, returns null. Also,
029     * if the session is invalidated and create is false, returns null. Invoking this method with true, when the session exists but has
030     * been invalidated, will force the creation of a new session.
031     *
032     * @param create
033     *         true to force the creation of the session
034     * @return the session (or null if create is false the session has not been previously created)
035     */
036    Session getSession(boolean create);
037
038    /**
039     * Returns the context path. This always starts with a "/" character and does not end with one, with the exception
040     * of servlets in the root context, which return the empty string.
041     *
042     * @deprecated in 5.4, inject the value for symbol {@link org.apache.tapestry5.SymbolConstants#CONTEXT_PATH} instead
043     */
044    String getContextPath();
045
046    /**
047     * Returns a list of query parameter names, in alphabetical order.
048     */
049    List<String> getParameterNames();
050
051    /**
052     * Returns the query parameter value for the given name. Returns null if no such parameter is in the request. For a
053     * multi-valued parameter, returns just the first value.
054     */
055    String getParameter(String name);
056
057    /**
058     * Returns the parameter values for the given name. Returns null if no such parameter is in the request.
059     */
060    String[] getParameters(String name);
061
062    /**
063     * Returns the path portion of the request, which starts with a "/" and contains everything up to the start of the
064     * query parameters. It doesn't include the context path.
065     */
066    String getPath();
067
068    /**
069     * Returns the locale of the client as determined from the request headers.
070     */
071    Locale getLocale();
072
073    /**
074     * Returns the names of all headers in the request.
075     */
076    List<String> getHeaderNames();
077
078    /**
079     * Returns the value of the specified request header as a <code>long</code> value that represents a
080     * <code>Date</code> object. Use this method with headers that contain dates, such as <code>If-Modified-Since</code>
081     * .
082     *
083     * The date is returned as the number of milliseconds since January 1, 1970 GMT. The header name is case
084     * insensitive.
085     *
086     * If the request did not have a header of the specified name, this method returns -1. If the header can't be
087     * converted to a date, the method throws an <code>IllegalArgumentException</code>.
088     *
089     * @param name
090     *         a <code>String</code> specifying the name of the header
091     * @return a <code>long</code> value representing the date specified in the header expressed as the number of
092     *         milliseconds since January 1, 1970 GMT, or -1 if the named header was not included with the reqest
093     * @throws IllegalArgumentException
094     *         If the header value can't be converted to a date
095     */
096    long getDateHeader(String name);
097
098    /**
099     * Returns the named header as a string, or null if not found.
100     */
101    String getHeader(String name);
102
103    /**
104     * Returns true if the request originated on the client using XmlHttpRequest (the core of any Ajax behavior). Ajax
105     * action requests may behave quite differently than ordinary, page-based requests. This implementation currently
106     * depends on the client side setting a header: <strong>X-Requested-With=XMLHttpRequest</strong> (this is what
107     * Prototype does).
108     *
109     * @return true if the request has an XmlHttpRequest origin
110     */
111    boolean isXHR();
112
113    /**
114     * Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
115     *
116     * @return a boolean indicating if the request was made using a secure channel
117     */
118    public boolean isSecure();
119
120    /**
121     * Returns the host name of the server to which the request was sent. It is the value of the part before ":" in the
122     * <code>Host</code> header, if any, or the resolved server name, or the server IP address.
123     *
124     * @return the name of the server
125     */
126    public String getServerName();
127
128    /**
129     * Checks whether the requested session ID is still valid.
130     *
131     * @return true if the request included a session id that is still active, false if the included session id has
132     *         expired
133     */
134    boolean isRequestedSessionIdValid();
135
136    /**
137     * Returns the value of the named attribute as an <code>Object</code>, or <code>null</code> if no attribute of the
138     * given name exists. Because this method is a wrapper around
139     * {@link javax.servlet.ServletRequest#getAttribute(String)},
140     * it is case <em>sensitive</em> (unlike most of Tapestry).
141     *
142     * @param name
143     *         a <code>String</code> specifying the name of the attribute
144     * @return an <code>Object</code> containing the value of the attribute, or <code>null</code> if the attribute does
145     *         not exist
146     */
147    Object getAttribute(String name);
148
149    /**
150     * Returns a sorted list of attribute names.
151     *
152     * @since 5.4
153     */
154    List<String> getAttributeNames();
155
156
157    /**
158     * Stores an attribute in this request. Attributes are reset between requests (and remember that in Tapestry, there
159     * is usually two requests per operation: the action request that redirects to a render request).
160     *
161     * @param name
162     *         a <code>String</code> specifying the name of the attribute
163     * @param value
164     *         the <code>Object</code> to be stored, or null to remove the attribute
165     */
166    void setAttribute(String name, Object value);
167
168    /**
169     * Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
170     *
171     * @return a string specifying the name of the method with which this request was made
172     */
173    String getMethod();
174
175    /**
176     * Returns the Internet Protocol (IP) port number of the interface
177     * on which the request was received.
178     *
179     * @return an integer specifying the port number
180     * @since 5.2.0
181     */
182    int getLocalPort();
183
184    /**
185     * Returns the port number to which the request was sent.
186     * It is the value of the part after ":" in the <code>Host</code> header, if any, or the server port where the
187     * client connection
188     * was accepted on.
189     *
190     * @return an integer specifying the port number
191     * @since 5.2.5
192     */
193    int getServerPort();
194
195    /**
196     * Returns the fully qualified name of the client
197     * or the last proxy that sent the request.
198     * If the engine cannot or chooses not to resolve the hostname
199     * (to improve performance), this method returns the dotted-string form of
200     * the IP address.
201     *
202     * @return a <code>String</code> containing the fully
203     *         qualified name of the client
204     * @since 5.3
205     */
206    String getRemoteHost();
207
208    /**
209     * Returns true if the request specified a session, and that session has been invalidated.
210     *
211     * @return true if session was invalidated during this request
212     * @since 5.4
213     */
214    boolean isSessionInvalidated();
215}