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