|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
RequestContext.java | 100% | 100% | 100% | 100% |
|
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; | |
18 | import java.util.Enumeration; | |
19 | import java.util.List; | |
20 | ||
21 | import javax.servlet.http.HttpServletRequest; | |
22 | import javax.servlet.http.HttpServletResponse; | |
23 | import javax.servlet.http.HttpSession; | |
24 | ||
25 | /** | |
26 | * This class encapsulates all the relevant data for one request cycle of an | |
27 | * {@link ApplicationServlet}. This includes: | |
28 | * <ul> | |
29 | * <li>{@link HttpServletRequest} | |
30 | * <li>{@link HttpServletResponse} | |
31 | * <li>{@link HttpSession} | |
32 | * </ul> | |
33 | * <p> | |
34 | * This is a limited and crippled version of the RequestContext as it was available in release 3.0, | |
35 | * that exists as a bridge for compatibility only. This saves developers from having to modify their | |
36 | * classes to have the {@link javax.servlet.http.HttpServletRequest} or | |
37 | * {@link org.apache.tapestry.web.WebRequest}injected into their pages, components, or services. It | |
38 | * will be removed in the next release of Tapestry. | |
39 | * <p> | |
40 | * Interestingly, with the Tapestry 4.0 architecture, a single instance of RequestContext can be | |
41 | * shared by all request cycles (that's because the request and response are, in fact, threaded | |
42 | * proxies). | |
43 | * | |
44 | * @author Howard Lewis Ship | |
45 | * @deprecated To be removed in 4.1. Use injection to gain access to the necessary objects. | |
46 | */ | |
47 | ||
48 | public class RequestContext | |
49 | { | |
50 | private final HttpServletRequest _request; | |
51 | ||
52 | private final HttpServletResponse _response; | |
53 | ||
54 | 48 | public RequestContext(HttpServletRequest request, HttpServletResponse response) |
55 | { | |
56 | ||
57 | 48 | _request = request; |
58 | 48 | _response = response; |
59 | } | |
60 | ||
61 | /** | |
62 | * Returns the named parameter from the {@link HttpServletRequest}. | |
63 | * <p> | |
64 | * Use {@link #getParameters(String)}for parameters that may include multiple values. | |
65 | */ | |
66 | ||
67 | 1 | public String getParameter(String name) |
68 | { | |
69 | 1 | return _request.getParameter(name); |
70 | } | |
71 | ||
72 | /** | |
73 | * Convienience method for getting a {@link HttpServletRequest}attribute. | |
74 | * | |
75 | * @since 2.3 | |
76 | */ | |
77 | ||
78 | 1 | public Object getAttribute(String name) |
79 | { | |
80 | 1 | return _request.getAttribute(name); |
81 | } | |
82 | ||
83 | /** | |
84 | * For parameters that are, or are possibly, multi-valued, this method returns all the values as | |
85 | * an array of Strings. | |
86 | * | |
87 | * @see #getParameter(String) | |
88 | */ | |
89 | ||
90 | 1 | public String[] getParameters(String name) |
91 | { | |
92 | 1 | return _request.getParameterValues(name); |
93 | } | |
94 | ||
95 | 1 | public String[] getParameterNames() |
96 | { | |
97 | 1 | Enumeration e = _request.getParameterNames(); |
98 | 1 | List names = new ArrayList(); |
99 | ||
100 | 1 | while (e.hasMoreElements()) |
101 | 2 | names.add(e.nextElement()); |
102 | ||
103 | 1 | int count = names.size(); |
104 | ||
105 | 1 | String[] result = new String[count]; |
106 | ||
107 | 1 | return (String[]) names.toArray(result); |
108 | } | |
109 | ||
110 | /** | |
111 | * Returns the request which initiated the current request cycle. Note that the methods | |
112 | * {@link #getParameter(String)}and {@link #getParameters(String)}should be used, rather than | |
113 | * obtaining parameters directly from the request (since the RequestContext handles the | |
114 | * differences between normal and multipart/form requests). | |
115 | */ | |
116 | ||
117 | 1 | public HttpServletRequest getRequest() |
118 | { | |
119 | 1 | return _request; |
120 | } | |
121 | ||
122 | 1 | public HttpServletResponse getResponse() |
123 | { | |
124 | 1 | return _response; |
125 | } | |
126 | ||
127 | /** | |
128 | * Returns the {@link HttpSession}, if necessary, invoking | |
129 | * {@link HttpServletRequest#getSession(boolean)}. However, this method will <em>not</em> | |
130 | * create a session. | |
131 | */ | |
132 | ||
133 | 1 | public HttpSession getSession() |
134 | { | |
135 | 1 | return _request.getSession(false); |
136 | } | |
137 | ||
138 | /** | |
139 | * Like {@link #getSession()}, but forces the creation of the {@link HttpSession}, if | |
140 | * necessary. | |
141 | */ | |
142 | ||
143 | 1 | public HttpSession createSession() |
144 | { | |
145 | 1 | return _request.getSession(true); |
146 | } | |
147 | ||
148 | } |
|