001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.request;
016    
017    import java.util.ArrayList;
018    import java.util.Enumeration;
019    import java.util.List;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletResponse;
023    import javax.servlet.http.HttpSession;
024    
025    /**
026     * This class encapsulates all the relevant data for one request cycle of an
027     * {@link ApplicationServlet}. This includes:
028     * <ul>
029     * <li>{@link HttpServletRequest}
030     * <li>{@link HttpServletResponse}
031     * <li>{@link HttpSession}
032     * </ul>
033     * <p>
034     * This is a limited and crippled version of the RequestContext as it was available in release 3.0,
035     * that exists as a bridge for compatibility only. This saves developers from having to modify their
036     * classes to have the {@link javax.servlet.http.HttpServletRequest} or (preferrably)
037     * {@link org.apache.tapestry.web.WebRequest} injected into their pages, components, or services. It
038     * will be removed in the next release of Tapestry.
039     * 
040     * @author Howard Lewis Ship
041     * @deprecated To be removed in 4.1. Use injection to gain access to the necessary objects.
042     */
043    
044    public class RequestContext
045    {
046        private final HttpServletRequest _request;
047    
048        private final HttpServletResponse _response;
049    
050        public RequestContext(HttpServletRequest request, HttpServletResponse response)
051        {
052            _request = request;
053            _response = response;
054        }
055    
056        /**
057         * Returns the named parameter from the {@link HttpServletRequest}.
058         * <p>
059         * Use {@link #getParameters(String)}for parameters that may include multiple values.
060         */
061    
062        public String getParameter(String name)
063        {
064            return _request.getParameter(name);
065        }
066    
067        /**
068         * Convienience method for getting a {@link HttpServletRequest}attribute.
069         * 
070         * @since 2.3
071         */
072    
073        public Object getAttribute(String name)
074        {
075            return _request.getAttribute(name);
076        }
077    
078        /**
079         * For parameters that are, or are possibly, multi-valued, this method returns all the values as
080         * an array of Strings.
081         * 
082         * @see #getParameter(String)
083         */
084    
085        public String[] getParameters(String name)
086        {
087            return _request.getParameterValues(name);
088        }
089    
090        public String[] getParameterNames()
091        {
092            Enumeration e = _request.getParameterNames();
093            List names = new ArrayList();
094    
095            while (e.hasMoreElements())
096                names.add(e.nextElement());
097    
098            int count = names.size();
099    
100            String[] result = new String[count];
101    
102            return (String[]) names.toArray(result);
103        }
104    
105        /**
106         * Returns the request which initiated the current request cycle. Note that the methods
107         * {@link #getParameter(String)}and {@link #getParameters(String)}should be used, rather than
108         * obtaining parameters directly from the request (since the RequestContext handles the
109         * differences between normal and multipart/form requests).
110         */
111    
112        public HttpServletRequest getRequest()
113        {
114            return _request;
115        }
116    
117        public HttpServletResponse getResponse()
118        {
119            return _response;
120        }
121    
122        /**
123         * Returns the {@link HttpSession}, if necessary, invoking
124         * {@link HttpServletRequest#getSession(boolean)}. However, this method will <em>not</em>
125         * create a session.
126         */
127    
128        public HttpSession getSession()
129        {
130            return _request.getSession(false);
131        }
132    
133        /**
134         * Like {@link #getSession()}, but forces the creation of the {@link HttpSession}, if
135         * necessary.
136         */
137    
138        public HttpSession createSession()
139        {
140            return _request.getSession(true);
141        }
142    
143    }