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