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