1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.seasar.cubby.filter; |
18 | |
|
19 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ACTION; |
20 | |
import static org.seasar.cubby.CubbyConstants.ATTR_CONTEXT_PATH; |
21 | |
import static org.seasar.cubby.CubbyConstants.ATTR_FORM_WRAPPER_FACTORY; |
22 | |
import static org.seasar.cubby.CubbyConstants.ATTR_MESSAGES; |
23 | |
import static org.seasar.cubby.CubbyConstants.ATTR_MESSAGES_RESOURCE_BUNDLE; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Enumeration; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.HashSet; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
import java.util.ResourceBundle; |
32 | |
import java.util.Set; |
33 | |
import java.util.Map.Entry; |
34 | |
|
35 | |
import javax.servlet.ServletRequest; |
36 | |
import javax.servlet.http.HttpServletRequest; |
37 | |
import javax.servlet.http.HttpServletRequestWrapper; |
38 | |
|
39 | |
import org.seasar.cubby.CubbyConstants; |
40 | |
import org.seasar.cubby.controller.FormWrapperFactory; |
41 | |
import org.seasar.cubby.controller.MessagesBehaviour; |
42 | |
import org.seasar.cubby.internal.controller.impl.FormWrapperFactoryImpl; |
43 | |
import org.seasar.cubby.internal.util.IteratorEnumeration; |
44 | |
import org.seasar.cubby.spi.beans.Attribute; |
45 | |
import org.seasar.cubby.spi.beans.BeanDesc; |
46 | |
import org.seasar.cubby.spi.beans.BeanDescFactory; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
class CubbyHttpServletRequestWrapper extends HttpServletRequestWrapper { |
110 | |
|
111 | |
|
112 | |
private final CubbyFilter cubbyFilter; |
113 | |
|
114 | |
|
115 | |
private final Map<String, String[]> uriParameters; |
116 | |
|
117 | |
|
118 | |
private FormWrapperFactory formWrapperFactory; |
119 | |
|
120 | |
|
121 | |
private MessagesBehaviour messagesBehaviour; |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
CubbyHttpServletRequestWrapper(final CubbyFilter cubbyFilter, |
134 | |
final HttpServletRequest request, |
135 | |
final Map<String, String[]> uriParameters) { |
136 | 9 | super(request); |
137 | 9 | this.cubbyFilter = cubbyFilter; |
138 | 9 | this.uriParameters = uriParameters; |
139 | 9 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
@Override |
150 | |
public Object getAttribute(final String name) { |
151 | |
final Object value; |
152 | 11 | if (ATTR_CONTEXT_PATH.equals(name)) { |
153 | 1 | value = this.getContextPath(); |
154 | 10 | } else if (ATTR_MESSAGES.equals(name)) { |
155 | 3 | value = getMessagesAsMap(this.getRequest(), this |
156 | |
.getMessagesBehaviour()); |
157 | 7 | } else if (ATTR_MESSAGES_RESOURCE_BUNDLE.equals(name)) { |
158 | 2 | value = getMessagesAsResourceBundle(this.getRequest(), this |
159 | |
.getMessagesBehaviour()); |
160 | 5 | } else if (ATTR_FORM_WRAPPER_FACTORY.equals(name)) { |
161 | 0 | if (this.formWrapperFactory == null) { |
162 | 0 | this.formWrapperFactory = new FormWrapperFactoryImpl(); |
163 | |
} |
164 | 0 | value = this.formWrapperFactory; |
165 | |
} else { |
166 | 5 | final Object action = super.getAttribute(ATTR_ACTION); |
167 | 5 | if (action != null) { |
168 | 4 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action |
169 | |
.getClass()); |
170 | 4 | if (beanDesc.hasPropertyAttribute(name)) { |
171 | 2 | final Attribute attribute = beanDesc |
172 | |
.getPropertyAttribute(name); |
173 | 2 | if (attribute.isReadable()) { |
174 | 1 | value = attribute.getValue(action); |
175 | |
} else { |
176 | 1 | value = super.getAttribute(name); |
177 | |
} |
178 | 2 | } else { |
179 | 2 | value = super.getAttribute(name); |
180 | |
} |
181 | 4 | } else { |
182 | 1 | value = super.getAttribute(name); |
183 | |
} |
184 | |
} |
185 | 11 | return value; |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
@SuppressWarnings("unchecked") |
195 | |
@Override |
196 | |
public Enumeration getAttributeNames() { |
197 | 1 | final Set attributeNames = new HashSet(); |
198 | |
|
199 | 1 | attributeNames.add(ATTR_CONTEXT_PATH); |
200 | 1 | attributeNames.add(ATTR_ACTION); |
201 | 1 | attributeNames.add(ATTR_MESSAGES); |
202 | 1 | attributeNames.add(ATTR_MESSAGES_RESOURCE_BUNDLE); |
203 | 1 | attributeNames.add(ATTR_FORM_WRAPPER_FACTORY); |
204 | |
|
205 | 1 | final Object action = super.getAttribute(ATTR_ACTION); |
206 | 1 | if (action != null) { |
207 | 1 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action |
208 | |
.getClass()); |
209 | 1 | for (final Attribute attribute : beanDesc.findtPropertyAttributes()) { |
210 | 5 | if (attribute.isReadable()) { |
211 | 4 | attributeNames.add(attribute.getName()); |
212 | |
} |
213 | |
} |
214 | |
} |
215 | |
|
216 | 1 | final Enumeration defaultAttributeNames = super.getAttributeNames(); |
217 | 3 | while (defaultAttributeNames.hasMoreElements()) { |
218 | 2 | attributeNames.add(defaultAttributeNames.nextElement()); |
219 | |
} |
220 | 1 | return new IteratorEnumeration(attributeNames.iterator()); |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
@Override |
234 | |
public String getParameter(final String name) { |
235 | 4 | final String[] parameters = this.getParameterValues(name); |
236 | 4 | if (parameters == null) { |
237 | 1 | return null; |
238 | |
} else { |
239 | 3 | return parameters[0]; |
240 | |
} |
241 | |
} |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
@SuppressWarnings("unchecked") |
251 | |
@Override |
252 | |
public Enumeration getParameterNames() { |
253 | 1 | return new IteratorEnumeration(this.getParameterMap().keySet() |
254 | |
.iterator()); |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
@SuppressWarnings("unchecked") |
268 | |
@Override |
269 | |
public String[] getParameterValues(final String name) { |
270 | 7 | final Map<String, String[]> parameterMap = this.getParameterMap(); |
271 | 7 | return parameterMap.get(name); |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
@SuppressWarnings("unchecked") |
284 | |
@Override |
285 | |
public Map getParameterMap() { |
286 | 10 | final Map<String, String[]> parameterMap = buildParameterMap( |
287 | |
(HttpServletRequest) getRequest(), uriParameters); |
288 | 10 | return parameterMap; |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
private Map<String, String[]> buildParameterMap( |
301 | |
final HttpServletRequest request, |
302 | |
final Map<String, String[]> uriParameters) { |
303 | 10 | final Map<String, List<String>> extendedParameterMap = new HashMap<String, List<String>>(); |
304 | |
|
305 | 10 | final Map<?, ?> originalParameterMap = request.getParameterMap(); |
306 | 10 | for (final Entry<?, ?> entry : originalParameterMap.entrySet()) { |
307 | 18 | final String name = (String) entry.getKey(); |
308 | 18 | final List<String> values = new ArrayList<String>(); |
309 | 36 | for (final String value : (String[]) entry.getValue()) { |
310 | 18 | values.add(value); |
311 | |
} |
312 | 18 | extendedParameterMap.put(name, values); |
313 | 18 | } |
314 | 10 | for (final Entry<String, String[]> entry : uriParameters.entrySet()) { |
315 | 18 | final String name = entry.getKey(); |
316 | 18 | if (extendedParameterMap.containsKey(name)) { |
317 | 9 | final List<String> values = extendedParameterMap.get(name); |
318 | 18 | for (final String value : entry.getValue()) { |
319 | 9 | values.add(value); |
320 | |
} |
321 | 9 | } else { |
322 | 9 | final List<String> values = new ArrayList<String>(); |
323 | 18 | for (final String value : entry.getValue()) { |
324 | 9 | values.add(value); |
325 | |
} |
326 | 9 | extendedParameterMap.put(name, values); |
327 | |
} |
328 | 18 | } |
329 | |
|
330 | 10 | final Map<String, String[]> parameterMap = new HashMap<String, String[]>(); |
331 | 10 | for (final Entry<String, List<String>> entry : extendedParameterMap |
332 | |
.entrySet()) { |
333 | 27 | parameterMap.put(entry.getKey(), entry.getValue().toArray( |
334 | |
new String[0])); |
335 | |
} |
336 | 10 | return parameterMap; |
337 | |
} |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
private static ResourceBundle getMessagesAsResourceBundle( |
345 | |
final ServletRequest request, |
346 | |
final MessagesBehaviour messagesBehaviour) { |
347 | 5 | final ResourceBundle bundle = (ResourceBundle) request |
348 | |
.getAttribute(ATTR_MESSAGES_RESOURCE_BUNDLE); |
349 | 5 | if (bundle != null) { |
350 | 0 | return bundle; |
351 | |
} |
352 | |
|
353 | 5 | final ResourceBundle newBundle = messagesBehaviour.getBundle(request |
354 | |
.getLocale()); |
355 | 5 | request.setAttribute(ATTR_MESSAGES_RESOURCE_BUNDLE, newBundle); |
356 | 5 | return newBundle; |
357 | |
} |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
private static Map<String, Object> getMessagesAsMap( |
366 | |
final ServletRequest request, |
367 | |
final MessagesBehaviour messagesBehaviour) { |
368 | |
@SuppressWarnings("unchecked") |
369 | 3 | final Map<String, Object> messages = (Map<String, Object>) request |
370 | |
.getAttribute(ATTR_MESSAGES); |
371 | 3 | if (messages != null) { |
372 | 0 | return messages; |
373 | |
} |
374 | |
|
375 | 3 | final ResourceBundle bundle = getMessagesAsResourceBundle(request, |
376 | |
messagesBehaviour); |
377 | 3 | final Map<String, Object> newMessages = messagesBehaviour.toMap(bundle); |
378 | 3 | request.setAttribute(ATTR_MESSAGES, newMessages); |
379 | 3 | return newMessages; |
380 | |
} |
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
private MessagesBehaviour getMessagesBehaviour() { |
390 | 5 | if (this.messagesBehaviour == null) { |
391 | 5 | this.messagesBehaviour = cubbyFilter.createMessagesBehaviour(); |
392 | |
} |
393 | 5 | return this.messagesBehaviour; |
394 | |
} |
395 | |
|
396 | |
} |