1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.internal.controller.impl; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.*; |
19 | |
import static org.seasar.cubby.CubbyConstants.ATTR_CONTEXT_PATH; |
20 | |
import static org.seasar.cubby.CubbyConstants.ATTR_MESSAGES; |
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Enumeration; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
import java.util.Set; |
29 | |
import java.util.Map.Entry; |
30 | |
|
31 | |
import javax.servlet.http.HttpServletRequest; |
32 | |
import javax.servlet.http.HttpServletRequestWrapper; |
33 | |
|
34 | |
import org.seasar.cubby.CubbyConstants; |
35 | |
import org.seasar.cubby.controller.FormWrapperFactory; |
36 | |
import org.seasar.cubby.internal.controller.ThreadContext; |
37 | |
import org.seasar.cubby.internal.util.IteratorEnumeration; |
38 | |
import org.seasar.cubby.spi.beans.Attribute; |
39 | |
import org.seasar.cubby.spi.beans.BeanDesc; |
40 | |
import org.seasar.cubby.spi.beans.BeanDescFactory; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
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 | |
class CubbyHttpServletRequestWrapper extends HttpServletRequestWrapper { |
99 | |
|
100 | |
|
101 | |
private final Map<String, String[]> uriParameters; |
102 | |
|
103 | |
|
104 | |
private FormWrapperFactory formWrapperFactory; |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public CubbyHttpServletRequestWrapper(final HttpServletRequest request, |
115 | |
final Map<String, String[]> uriParameters) { |
116 | 12 | super(request); |
117 | 12 | this.uriParameters = uriParameters; |
118 | 12 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
@Override |
129 | |
public Object getAttribute(final String name) { |
130 | |
final Object value; |
131 | 21 | if (ATTR_CONTEXT_PATH.equals(name)) { |
132 | 3 | value = this.getContextPath(); |
133 | 18 | } else if (ATTR_MESSAGES.equals(name)) { |
134 | 3 | value = ThreadContext.getMessagesMap(); |
135 | 15 | } else if (ATTR_FORM_WRAPPER_FACTORY.equals(name)) { |
136 | 0 | if (this.formWrapperFactory == null) { |
137 | 0 | this.formWrapperFactory = new FormWrapperFactoryImpl(); |
138 | |
} |
139 | 0 | value = this.formWrapperFactory; |
140 | |
} else { |
141 | 15 | final Object action = super.getAttribute(ATTR_ACTION); |
142 | 15 | if (action != null) { |
143 | 12 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action |
144 | |
.getClass()); |
145 | 12 | if (beanDesc.hasPropertyAttribute(name)) { |
146 | 6 | final Attribute attribute = beanDesc |
147 | |
.getPropertyAttribute(name); |
148 | 6 | if (attribute.isReadable()) { |
149 | 3 | value = attribute.getValue(action); |
150 | |
} else { |
151 | 3 | value = super.getAttribute(name); |
152 | |
} |
153 | 6 | } else { |
154 | 6 | value = super.getAttribute(name); |
155 | |
} |
156 | 12 | } else { |
157 | 3 | value = super.getAttribute(name); |
158 | |
} |
159 | |
} |
160 | 21 | return value; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
@SuppressWarnings("unchecked") |
170 | |
@Override |
171 | |
public Enumeration getAttributeNames() { |
172 | 3 | final Set attributeNames = new HashSet(); |
173 | |
|
174 | 3 | attributeNames.add(ATTR_CONTEXT_PATH); |
175 | 3 | attributeNames.add(ATTR_ACTION); |
176 | 3 | attributeNames.add(ATTR_MESSAGES); |
177 | 1 | attributeNames.add(ATTR_FORM_WRAPPER_FACTORY); |
178 | 2 | |
179 | 3 | final Object action = super.getAttribute(ATTR_ACTION); |
180 | 3 | if (action != null) { |
181 | 1 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action |
182 | 2 | .getClass()); |
183 | 11 | for (final Attribute attribute : beanDesc.findtPropertyAttributes()) { |
184 | 13 | if (attribute.isReadable()) { |
185 | 4 | attributeNames.add(attribute.getName()); |
186 | |
} |
187 | |
} |
188 | |
} |
189 | 2 | |
190 | 7 | final Enumeration defaultAttributeNames = super.getAttributeNames(); |
191 | 7 | while (defaultAttributeNames.hasMoreElements()) { |
192 | 2 | attributeNames.add(defaultAttributeNames.nextElement()); |
193 | 2 | } |
194 | 1 | return new IteratorEnumeration(attributeNames.iterator()); |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
@Override |
208 | 8 | public String getParameter(final String name) { |
209 | 12 | final String[] parameters = this.getParameterValues(name); |
210 | 6 | if (parameters == null) { |
211 | 1 | return null; |
212 | 6 | } else { |
213 | 3 | return parameters[0]; |
214 | |
} |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
@SuppressWarnings("unchecked") |
225 | |
@Override |
226 | 2 | public Enumeration getParameterNames() { |
227 | 1 | return new IteratorEnumeration(this.getParameterMap().keySet() |
228 | |
.iterator()); |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
@SuppressWarnings("unchecked") |
242 | |
@Override |
243 | 14 | public String[] getParameterValues(final String name) { |
244 | 21 | final Map<String, String[]> parameterMap = this.getParameterMap(); |
245 | 7 | return parameterMap.get(name); |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
@SuppressWarnings("unchecked") |
258 | |
@Override |
259 | 20 | public Map getParameterMap() { |
260 | 10 | final Map<String, String[]> parameterMap = buildParameterMap( |
261 | 20 | (HttpServletRequest) getRequest(), uriParameters); |
262 | 10 | return parameterMap; |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
private Map<String, String[]> buildParameterMap( |
275 | |
final HttpServletRequest request, |
276 | 20 | final Map<String, String[]> uriParameters) { |
277 | 10 | final Map<String, List<String>> extendedParameterMap = new HashMap<String, List<String>>(); |
278 | 20 | |
279 | 30 | final Map<?, ?> originalParameterMap = request.getParameterMap(); |
280 | 46 | for (final Entry<?, ?> entry : originalParameterMap.entrySet()) { |
281 | 54 | final String name = (String) entry.getKey(); |
282 | 90 | final List<String> values = new ArrayList<String>(); |
283 | 72 | for (final String value : (String[]) entry.getValue()) { |
284 | 18 | values.add(value); |
285 | 36 | } |
286 | 54 | extendedParameterMap.put(name, values); |
287 | 38 | } |
288 | 46 | for (final Entry<String, String[]> entry : uriParameters.entrySet()) { |
289 | 54 | final String name = entry.getKey(); |
290 | 36 | if (extendedParameterMap.containsKey(name)) { |
291 | 45 | final List<String> values = extendedParameterMap.get(name); |
292 | 36 | for (final String value : entry.getValue()) { |
293 | 9 | values.add(value); |
294 | 18 | } |
295 | 27 | } else { |
296 | 45 | final List<String> values = new ArrayList<String>(); |
297 | 36 | for (final String value : entry.getValue()) { |
298 | 9 | values.add(value); |
299 | 18 | } |
300 | 9 | extendedParameterMap.put(name, values); |
301 | 36 | } |
302 | 18 | } |
303 | 20 | |
304 | 30 | final Map<String, String[]> parameterMap = new HashMap<String, String[]>(); |
305 | 10 | for (final Entry<String, List<String>> entry : extendedParameterMap |
306 | 54 | .entrySet()) { |
307 | 27 | parameterMap.put(entry.getKey(), entry.getValue().toArray( |
308 | |
new String[0])); |
309 | 20 | } |
310 | 10 | return parameterMap; |
311 | |
} |
312 | |
|
313 | |
} |