1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.util; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.util.Collection; |
20 | |
|
21 | |
import static org.seasar.cubby.action.RequestParameterBindingType.*; |
22 | |
import javax.servlet.http.HttpServletRequest; |
23 | |
|
24 | |
import org.seasar.cubby.action.Accept; |
25 | |
import org.seasar.cubby.action.Action; |
26 | |
import org.seasar.cubby.action.ActionResult; |
27 | |
import org.seasar.cubby.action.Form; |
28 | |
import org.seasar.cubby.action.OnSubmit; |
29 | |
import org.seasar.cubby.action.Path; |
30 | |
import org.seasar.cubby.action.RequestMethod; |
31 | |
import org.seasar.cubby.exception.ActionRuntimeException; |
32 | |
import org.seasar.framework.beans.BeanDesc; |
33 | |
import org.seasar.framework.beans.PropertyDesc; |
34 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
35 | |
import org.seasar.framework.util.StringUtil; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class CubbyUtils { |
44 | |
|
45 | |
|
46 | |
private static final String INDEX_METHOD_NAME = "index"; |
47 | |
|
48 | |
|
49 | |
public static Accept DEFAULT_ACCEPT_ANNOTATION; |
50 | |
static { |
51 | |
@Accept |
52 | 0 | class AcceptDummy { |
53 | |
} |
54 | 1 | DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class |
55 | |
.getAnnotation(Accept.class); |
56 | 1 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static String getActionDirectory( |
66 | |
final Class<? extends Action> actionClass) { |
67 | |
final String actionName; |
68 | 217 | final Path path = actionClass.getAnnotation(Path.class); |
69 | 217 | if (path != null && !StringUtil.isEmpty(path.value())) { |
70 | 123 | actionName = path.value(); |
71 | |
} else { |
72 | 94 | final String name = left(actionClass.getSimpleName(), "$"); |
73 | 94 | actionName = toFirstLower(name.replaceAll( |
74 | |
"(.*[.])*([^.]+)(Action$)", "$2")); |
75 | |
} |
76 | 217 | return actionName; |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private static String left(final String text, final String sep) { |
89 | 94 | final int pos = text.indexOf(sep); |
90 | 94 | if (pos != -1) { |
91 | 0 | return text.substring(0, pos); |
92 | |
} |
93 | 94 | return text; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
private static String toFirstLower(final String text) { |
104 | 94 | if (StringUtil.isEmpty(text)) { |
105 | 0 | throw new IllegalArgumentException("text is empty."); |
106 | |
} |
107 | 94 | final StringBuilder sb = new StringBuilder(); |
108 | 94 | sb.append(text.substring(0, 1).toLowerCase()); |
109 | 94 | if (text.length() > 1) { |
110 | 94 | sb.append(text.substring(1)); |
111 | |
} |
112 | 94 | return sb.toString(); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public static String getActionPath( |
125 | |
final Class<? extends Action> actionClass, final Method method) { |
126 | |
final String path; |
127 | 209 | final String actionMethodName = getActionMethodName(method); |
128 | 209 | if (actionMethodName.startsWith("/")) { |
129 | 2 | return path = actionMethodName; |
130 | |
} else { |
131 | 207 | final String actionDirectory = CubbyUtils |
132 | |
.getActionDirectory(actionClass); |
133 | 207 | if ("/".equals(actionDirectory)) { |
134 | 46 | path = "/" + actionMethodName; |
135 | |
} else { |
136 | 161 | path = "/" + actionDirectory + "/" + actionMethodName; |
137 | |
} |
138 | |
} |
139 | 207 | return path; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
private static String getActionMethodName(final Method method) { |
150 | |
final String actionName; |
151 | 209 | final Path path = method.getAnnotation(Path.class); |
152 | 209 | if (path != null && !StringUtil.isEmpty(path.value())) { |
153 | 130 | actionName = path.value(); |
154 | |
} else { |
155 | 79 | final String methodName = method.getName(); |
156 | 79 | if (INDEX_METHOD_NAME.equals(methodName)) { |
157 | 16 | actionName = ""; |
158 | |
} else { |
159 | 63 | actionName = methodName; |
160 | |
} |
161 | |
} |
162 | 209 | return actionName; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public static RequestMethod[] getAcceptableRequestMethods( |
175 | |
final Class<?> actionClass, final Method method) { |
176 | |
final Accept accept; |
177 | 198 | if (method.isAnnotationPresent(Accept.class)) { |
178 | 56 | accept = method.getAnnotation(Accept.class); |
179 | 142 | } else if (actionClass.isAnnotationPresent(Accept.class)) { |
180 | 0 | accept = actionClass.getAnnotation(Accept.class); |
181 | |
} else { |
182 | 142 | accept = DEFAULT_ACCEPT_ANNOTATION; |
183 | |
} |
184 | 198 | return accept.value(); |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
public static int getObjectSize(final Object value) { |
195 | |
final int size; |
196 | 8 | if (value == null) { |
197 | 1 | size = 0; |
198 | 7 | } else if (value.getClass().isArray()) { |
199 | 3 | final Object[] array = (Object[]) value; |
200 | 3 | size = array.length; |
201 | 3 | } else if (value instanceof Collection) { |
202 | 3 | final Collection<?> collection = (Collection<?>) value; |
203 | 3 | size = collection.size(); |
204 | 3 | } else { |
205 | 1 | size = 1; |
206 | |
} |
207 | 8 | return size; |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
public static String getPath(final HttpServletRequest request) { |
218 | 1 | final StringBuilder builder = new StringBuilder(); |
219 | 1 | builder.append(request.getServletPath()); |
220 | 1 | final String pathInfo = request.getPathInfo(); |
221 | 1 | if (pathInfo != null) { |
222 | 0 | builder.append(pathInfo); |
223 | |
} |
224 | 1 | return builder.toString(); |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
public static boolean isActionClass(final Class<?> clazz) { |
236 | 71 | return Action.class.isAssignableFrom(clazz); |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
public static boolean isActionMethod(final Method method) { |
248 | 1110 | return method.getReturnType().isAssignableFrom(ActionResult.class) |
249 | |
&& method.getParameterTypes().length == 0; |
250 | |
} |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
public static String replaceFirst(final String text, final String replace, |
264 | |
final String with) { |
265 | 12 | if (text == null || replace == null || with == null) { |
266 | 6 | return text; |
267 | |
} |
268 | 6 | final int index = text.indexOf(replace); |
269 | 6 | if (index == -1) { |
270 | 0 | return text; |
271 | |
} |
272 | 6 | final StringBuilder builder = new StringBuilder(100); |
273 | 6 | builder.append(text.substring(0, index)); |
274 | 6 | builder.append(with); |
275 | 6 | builder.append(text.substring(index + replace.length())); |
276 | 6 | return builder.toString(); |
277 | |
} |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
public static String[] split2(final String text, final char delim) { |
289 | 357 | if (text == null) { |
290 | 1 | return null; |
291 | |
} |
292 | 356 | final int index = text.indexOf(delim); |
293 | 356 | if (index == -1) { |
294 | 250 | return new String[] { text }; |
295 | |
} |
296 | 106 | final String[] tokens = new String[2]; |
297 | 106 | tokens[0] = text.substring(0, index); |
298 | 106 | tokens[1] = text.substring(index + 1); |
299 | 106 | return tokens; |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public static String escapeHtml(final Object str) { |
338 | 115 | if (str == null) { |
339 | 0 | return ""; |
340 | |
} |
341 | |
String text; |
342 | 115 | if (str instanceof String) { |
343 | 114 | text = (String) str; |
344 | |
} else { |
345 | 1 | text = str.toString(); |
346 | |
} |
347 | 115 | text = StringUtil.replace(text, "&", "&"); |
348 | 115 | text = StringUtil.replace(text, "<", "<"); |
349 | 115 | text = StringUtil.replace(text, ">", ">"); |
350 | 115 | text = StringUtil.replace(text, "\"", """); |
351 | 115 | text = StringUtil.replace(text, "'", "'"); |
352 | 115 | return text; |
353 | |
} |
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
public static int getPriority(final Method method) { |
363 | 357 | final Path path = method.getAnnotation(Path.class); |
364 | 357 | return path != null ? path.priority() : Integer.MAX_VALUE; |
365 | |
} |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
@SuppressWarnings("deprecation") |
383 | |
public static Object getFormBean(final Action action, |
384 | |
final Class<?> actionClass, final Method method) { |
385 | 9 | final Form form = getForm(actionClass, method); |
386 | 9 | if (form == null) { |
387 | 7 | return action; |
388 | |
} |
389 | 2 | if (!form.binding()) { |
390 | 0 | return null; |
391 | |
} |
392 | 2 | if (form.bindingType() == NONE) { |
393 | 0 | return null; |
394 | |
} |
395 | 2 | if (Form.THIS.equals(form.value())) { |
396 | 0 | return action; |
397 | |
} |
398 | |
|
399 | 2 | final String propertyName = form.value(); |
400 | 2 | final BeanDesc beanDesc = BeanDescFactory |
401 | |
.getBeanDesc(action.getClass()); |
402 | 2 | final PropertyDesc propertyDesc = beanDesc |
403 | |
.getPropertyDesc(propertyName); |
404 | 2 | final Object formBean = propertyDesc.getValue(action); |
405 | 2 | if (formBean == null) { |
406 | 1 | throw new ActionRuntimeException("ECUB0102", |
407 | |
new Object[] { propertyName }); |
408 | |
} |
409 | 1 | return formBean; |
410 | |
} |
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
public static Form getForm(final Class<?> actionClass, final Method method) { |
424 | |
final Form form; |
425 | 80 | if (method.isAnnotationPresent(Form.class)) { |
426 | 69 | form = method.getAnnotation(Form.class); |
427 | |
} else { |
428 | 11 | form = actionClass.getAnnotation(Form.class); |
429 | |
} |
430 | 80 | return form; |
431 | |
} |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
public static String getOnSubmit(final Method method) { |
444 | 478 | final OnSubmit onSubmit = method.getAnnotation(OnSubmit.class); |
445 | |
final String parameterName; |
446 | 478 | if (onSubmit == null) { |
447 | 478 | parameterName = null; |
448 | |
} else { |
449 | 0 | parameterName = onSubmit.value(); |
450 | |
} |
451 | 478 | return parameterName; |
452 | |
} |
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
@SuppressWarnings("unchecked") |
467 | |
public static <T> T getAttribute(final HttpServletRequest request, |
468 | |
final String name) { |
469 | 7 | return (T) request.getAttribute(name); |
470 | |
} |
471 | |
|
472 | |
} |