1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.tiles.request.servlet; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.io.OutputStream; |
25 | |
import java.io.PrintWriter; |
26 | |
import java.io.Writer; |
27 | |
import java.util.Arrays; |
28 | |
import java.util.Collections; |
29 | |
import java.util.List; |
30 | |
import java.util.Locale; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
import javax.servlet.RequestDispatcher; |
34 | |
import javax.servlet.ServletException; |
35 | |
import javax.servlet.http.HttpServletRequest; |
36 | |
import javax.servlet.http.HttpServletResponse; |
37 | |
|
38 | |
import org.apache.tiles.request.AbstractClientRequest; |
39 | |
import org.apache.tiles.request.ApplicationContext; |
40 | |
import org.apache.tiles.request.attribute.Addable; |
41 | |
import org.apache.tiles.request.collection.HeaderValuesMap; |
42 | |
import org.apache.tiles.request.collection.ReadOnlyEnumerationMap; |
43 | |
import org.apache.tiles.request.collection.ScopeMap; |
44 | |
import org.apache.tiles.request.servlet.extractor.ParameterExtractor; |
45 | |
import org.apache.tiles.request.servlet.extractor.RequestScopeExtractor; |
46 | |
import org.apache.tiles.request.servlet.extractor.HeaderExtractor; |
47 | |
import org.apache.tiles.request.servlet.extractor.SessionScopeExtractor; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public class ServletRequest extends AbstractClientRequest { |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 1 | private static final List<String> SCOPES |
60 | |
= Collections.unmodifiableList(Arrays.asList(REQUEST_SCOPE, "session", APPLICATION_SCOPE)); |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private HttpServletRequest request; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private HttpServletResponse response; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private OutputStream outputStream; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
private PrintWriter writer; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 23 | private Map<String, String> header = null; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 23 | private Addable<String> responseHeaders = null; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | 23 | private Map<String, String[]> headerValues = null; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | 23 | private Map<String, String> param = null; |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | 23 | private Map<String, Object> requestScope = null; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 23 | private Map<String, Object> sessionScope = null; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public ServletRequest( |
130 | |
ApplicationContext applicationContext, |
131 | |
HttpServletRequest request, HttpServletResponse response) { |
132 | 23 | super(applicationContext); |
133 | 23 | this.request = request; |
134 | 23 | this.response = response; |
135 | 23 | } |
136 | |
|
137 | |
|
138 | |
public Map<String, String> getHeader() { |
139 | |
|
140 | 1 | if ((header == null) && (request != null)) { |
141 | 1 | header = new ReadOnlyEnumerationMap<String>(new HeaderExtractor(request, null)); |
142 | |
} |
143 | 1 | return (header); |
144 | |
|
145 | |
} |
146 | |
|
147 | |
|
148 | |
public Addable<String> getResponseHeaders() { |
149 | |
|
150 | 1 | if ((responseHeaders == null) && (response != null)) { |
151 | 1 | responseHeaders = new HeaderExtractor(null, response); |
152 | |
} |
153 | 1 | return (responseHeaders); |
154 | |
|
155 | |
} |
156 | |
|
157 | |
|
158 | |
public Map<String, String[]> getHeaderValues() { |
159 | |
|
160 | 1 | if ((headerValues == null) && (request != null)) { |
161 | 1 | headerValues = new HeaderValuesMap(new HeaderExtractor(request, response)); |
162 | |
} |
163 | 1 | return (headerValues); |
164 | |
|
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
public Map<String, String> getParam() { |
170 | |
|
171 | 1 | if ((param == null) && (request != null)) { |
172 | 1 | param = new ReadOnlyEnumerationMap<String>(new ParameterExtractor(request)); |
173 | |
} |
174 | 1 | return (param); |
175 | |
|
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
@SuppressWarnings("unchecked") |
181 | |
public Map<String, String[]> getParamValues() { |
182 | 1 | return request.getParameterMap(); |
183 | |
} |
184 | |
|
185 | |
@Override |
186 | |
public Map<String, Object> getContext(String scope) { |
187 | 0 | if(REQUEST_SCOPE.equals(scope)){ |
188 | 0 | return getRequestScope(); |
189 | 0 | }else if("session".equals(scope)){ |
190 | 0 | return getSessionScope(); |
191 | 0 | }else if(APPLICATION_SCOPE.equals(scope)){ |
192 | 0 | return getApplicationScope(); |
193 | |
} |
194 | 0 | throw new IllegalArgumentException(scope + " does not exist. Call getAvailableScopes() first to check."); |
195 | |
} |
196 | |
|
197 | |
|
198 | |
public Map<String, Object> getRequestScope() { |
199 | |
|
200 | 1 | if ((requestScope == null) && (request != null)) { |
201 | 1 | requestScope = new ScopeMap(new RequestScopeExtractor(request)); |
202 | |
} |
203 | 1 | return (requestScope); |
204 | |
|
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
public Map<String, Object> getSessionScope() { |
210 | |
|
211 | 1 | if ((sessionScope == null) && (request != null)) { |
212 | 1 | sessionScope = new ScopeMap(new SessionScopeExtractor(request)); |
213 | |
} |
214 | 1 | return (sessionScope); |
215 | |
|
216 | |
} |
217 | |
|
218 | |
@Override |
219 | |
public List<String> getAvailableScopes() { |
220 | 0 | return SCOPES; |
221 | |
} |
222 | |
|
223 | |
|
224 | |
public void doForward(String path) throws IOException { |
225 | 4 | if (response.isCommitted()) { |
226 | 1 | doInclude(path); |
227 | |
} else { |
228 | 3 | forward(path); |
229 | |
} |
230 | 2 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
public void doInclude(String path) throws IOException { |
235 | 4 | RequestDispatcher rd = request.getRequestDispatcher(path); |
236 | |
|
237 | 4 | if (rd == null) { |
238 | 1 | throw new IOException("No request dispatcher returned for path '" |
239 | |
+ path + "'"); |
240 | |
} |
241 | |
|
242 | |
try { |
243 | 3 | rd.include(request, response); |
244 | 1 | } catch (ServletException ex) { |
245 | 1 | throw ServletUtil.wrapServletException(ex, "ServletException including path '" |
246 | |
+ path + "'."); |
247 | 2 | } |
248 | 2 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
private void forward(String path) throws IOException { |
257 | 3 | RequestDispatcher rd = request.getRequestDispatcher(path); |
258 | |
|
259 | 3 | if (rd == null) { |
260 | 1 | throw new IOException("No request dispatcher returned for path '" |
261 | |
+ path + "'"); |
262 | |
} |
263 | |
|
264 | |
try { |
265 | 2 | rd.forward(request, response); |
266 | 1 | } catch (ServletException ex) { |
267 | 1 | throw ServletUtil.wrapServletException(ex, "ServletException including path '" |
268 | |
+ path + "'."); |
269 | 1 | } |
270 | 1 | } |
271 | |
|
272 | |
|
273 | |
public OutputStream getOutputStream() throws IOException { |
274 | 1 | if (outputStream == null) { |
275 | 1 | outputStream = response.getOutputStream(); |
276 | |
} |
277 | 1 | return outputStream; |
278 | |
} |
279 | |
|
280 | |
|
281 | |
public Writer getWriter() throws IOException { |
282 | 1 | return getPrintWriter(); |
283 | |
} |
284 | |
|
285 | |
|
286 | |
public PrintWriter getPrintWriter() throws IOException { |
287 | 2 | if (writer == null) { |
288 | 2 | writer = response.getWriter(); |
289 | |
} |
290 | 2 | return writer; |
291 | |
} |
292 | |
|
293 | |
|
294 | |
public boolean isResponseCommitted() { |
295 | 1 | return response.isCommitted(); |
296 | |
} |
297 | |
|
298 | |
|
299 | |
public void setContentType(String contentType) { |
300 | 1 | response.setContentType(contentType); |
301 | 1 | } |
302 | |
|
303 | |
|
304 | |
public Locale getRequestLocale() { |
305 | 1 | return request.getLocale(); |
306 | |
} |
307 | |
|
308 | |
public HttpServletRequest getRequest() { |
309 | 1 | return request; |
310 | |
} |
311 | |
|
312 | |
public HttpServletResponse getResponse() { |
313 | 1 | return response; |
314 | |
} |
315 | |
|
316 | |
|
317 | |
public boolean isUserInRole(String role) { |
318 | 1 | return request.isUserInRole(role); |
319 | |
} |
320 | |
} |