KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > request > RequestContext


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.request;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24
25 import org.apache.tapestry.ApplicationServlet;
26
27 /**
28  * This class encapsulates all the relevant data for one request cycle of an
29  * {@link ApplicationServlet}. This includes:
30  * <ul>
31  * <li>{@link HttpServletRequest}
32  * <li>{@link HttpServletResponse}
33  * <li>{@link HttpSession}
34  * </ul>
35  * <p>
36  * This is a limited and crippled version of the RequestContext as it was available in release 3.0,
37  * that exists as a bridge for compatibility only. This saves developers from having to modify their
38  * classes to have the {@link javax.servlet.http.HttpServletRequest}&nbsp;or
39  * {@link org.apache.tapestry.web.WebRequest}injected into their pages, components, or services. It
40  * will be removed in the next release of Tapestry.
41  * <p>
42  * Interestingly, with the Tapestry 4.0 architecture, a single instance of RequestContext can be
43  * shared by all request cycles (that's because the request and response are, in fact, threaded
44  * proxies).
45  *
46  * @author Howard Lewis Ship
47  * @deprecated To be removed in 4.1. Use injection to gain access to the necessary objects.
48  */

49
50 public class RequestContext
51 {
52     private final HttpServletRequest JavaDoc _request;
53
54     private final HttpServletResponse JavaDoc _response;
55
56     public RequestContext(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
57     {
58
59         _request = request;
60         _response = response;
61     }
62
63     /**
64      * Returns the named parameter from the {@link HttpServletRequest}.
65      * <p>
66      * Use {@link #getParameters(String)}for parameters that may include multiple values.
67      */

68
69     public String JavaDoc getParameter(String JavaDoc name)
70     {
71         return _request.getParameter(name);
72     }
73
74     /**
75      * Convienience method for getting a {@link HttpServletRequest}attribute.
76      *
77      * @since 2.3
78      */

79
80     public Object JavaDoc getAttribute(String JavaDoc name)
81     {
82         return _request.getAttribute(name);
83     }
84
85     /**
86      * For parameters that are, or are possibly, multi-valued, this method returns all the values as
87      * an array of Strings.
88      *
89      * @see #getParameter(String)
90      */

91
92     public String JavaDoc[] getParameters(String JavaDoc name)
93     {
94         return _request.getParameterValues(name);
95     }
96
97     public String JavaDoc[] getParameterNames()
98     {
99         Enumeration JavaDoc e = _request.getParameterNames();
100         List JavaDoc names = new ArrayList JavaDoc();
101
102         while (e.hasMoreElements())
103             names.add(e.nextElement());
104
105         int count = names.size();
106
107         String JavaDoc[] result = new String JavaDoc[count];
108
109         return (String JavaDoc[]) names.toArray(result);
110     }
111
112     /**
113      * Returns the request which initiated the current request cycle. Note that the methods
114      * {@link #getParameter(String)}and {@link #getParameters(String)}should be used, rather than
115      * obtaining parameters directly from the request (since the RequestContext handles the
116      * differences between normal and multipart/form requests).
117      */

118
119     public HttpServletRequest JavaDoc getRequest()
120     {
121         return _request;
122     }
123
124     public HttpServletResponse JavaDoc getResponse()
125     {
126         return _response;
127     }
128
129     /**
130      * Returns the {@link HttpSession}, if necessary, invoking
131      * {@link HttpServletRequest#getSession(boolean)}. However, this method will <em>not</em>
132      * create a session.
133      */

134
135     public HttpSession JavaDoc getSession()
136     {
137         return _request.getSession(false);
138     }
139
140     /**
141      * Like {@link #getSession()}, but forces the creation of the {@link HttpSession}, if
142      * necessary.
143      */

144
145     public HttpSession JavaDoc createSession()
146     {
147         return _request.getSession(true);
148     }
149
150 }
Popular Tags