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 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 class AcceptDummy {
55 }
56 DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class
57 .getAnnotation(Accept.class);
58 }
59
60
61
62
63
64
65
66
67 public static String getActionDirectory(
68 final Class<? extends Action> actionClass) {
69 final String actionName;
70 final Path path = actionClass.getAnnotation(Path.class);
71 if (path != null && !StringUtil.isEmpty(path.value())) {
72 actionName = path.value();
73 } else {
74 final String name = left(actionClass.getSimpleName(), "$");
75 actionName = toFirstLower(name.replaceAll(
76 "(.*[.])*([^.]+)(Action$)", "$2"));
77 }
78 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 final int pos = text.indexOf(sep);
92 if (pos != -1) {
93 return text.substring(0, pos);
94 }
95 return text;
96 }
97
98
99
100
101
102
103
104
105 private static String toFirstLower(final String text) {
106 if (StringUtil.isEmpty(text)) {
107 throw new IllegalArgumentException("text is empty.");
108 }
109 final StringBuilder sb = new StringBuilder();
110 sb.append(text.substring(0, 1).toLowerCase());
111 if (text.length() > 1) {
112 sb.append(text.substring(1));
113 }
114 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 final String actionMethodName = getActionMethodName(method);
130 if (actionMethodName.startsWith("/")) {
131 return path = actionMethodName;
132 } else {
133 final String actionDirectory = CubbyUtils
134 .getActionDirectory(actionClass);
135 if ("/".equals(actionDirectory)) {
136 path = "/" + actionMethodName;
137 } else {
138 path = "/" + actionDirectory + "/" + actionMethodName;
139 }
140 }
141 return path;
142 }
143
144
145
146
147
148
149
150
151 private static String getActionMethodName(final Method method) {
152 final String actionName;
153 final Path path = method.getAnnotation(Path.class);
154 if (path != null && !StringUtil.isEmpty(path.value())) {
155 actionName = path.value();
156 } else {
157 final String methodName = method.getName();
158 if (INDEX_METHOD_NAME.equals(methodName)) {
159 actionName = "";
160 } else {
161 actionName = methodName;
162 }
163 }
164 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 if (method.isAnnotationPresent(Accept.class)) {
180 accept = method.getAnnotation(Accept.class);
181 } else if (actionClass.isAnnotationPresent(Accept.class)) {
182 accept = actionClass.getAnnotation(Accept.class);
183 } else {
184 accept = DEFAULT_ACCEPT_ANNOTATION;
185 }
186 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 if (value == null) {
199 size = 0;
200 } else if (value.getClass().isArray()) {
201 final Object[] array = (Object[]) value;
202 size = array.length;
203 } else if (value instanceof Collection) {
204 final Collection<?> collection = (Collection<?>) value;
205 size = collection.size();
206 } else {
207 size = 1;
208 }
209 return size;
210 }
211
212
213
214
215
216
217
218
219 public static String getPath(final HttpServletRequest request) {
220 final StringBuilder builder = new StringBuilder();
221 builder.append(request.getServletPath());
222 final String pathInfo = request.getPathInfo();
223 if (pathInfo != null) {
224 builder.append(pathInfo);
225 }
226 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 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 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 if (text == null || replace == null || with == null) {
281 return text;
282 }
283 final int index = text.indexOf(replace);
284 if (index == -1) {
285 return text;
286 }
287 final StringBuilder builder = new StringBuilder(100);
288 builder.append(text.substring(0, index));
289 builder.append(with);
290 builder.append(text.substring(index + replace.length()));
291 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 if (text == null) {
305 return null;
306 }
307 final int index = text.indexOf(delim);
308 if (index == -1) {
309 return new String[] { text };
310 }
311 final String[] tokens = new String[2];
312 tokens[0] = text.substring(0, index);
313 tokens[1] = text.substring(index + 1);
314 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 if (str == null) {
354 return "";
355 }
356 String text;
357 if (str instanceof String) {
358 text = (String) str;
359 } else {
360 text = str.toString();
361 }
362 text = StringUtil.replace(text, "&", "&");
363 text = StringUtil.replace(text, "<", "<");
364 text = StringUtil.replace(text, ">", ">");
365 text = StringUtil.replace(text, "\"", """);
366 text = StringUtil.replace(text, "'", "'");
367 return text;
368 }
369
370
371
372
373
374
375
376
377 public static int getPriority(final Method method) {
378 final Path path = method.getAnnotation(Path.class);
379 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 final Form form = getForm(actionClass, method);
401 if (form == null) {
402 return action;
403 }
404 if (!form.binding()) {
405 return null;
406 }
407 if (form.bindingType() == NONE) {
408 return null;
409 }
410 if (Form.THIS.equals(form.value())) {
411 return action;
412 }
413
414 final String propertyName = form.value();
415 final BeanDesc beanDesc = BeanDescFactory
416 .getBeanDesc(action.getClass());
417 final PropertyDesc propertyDesc = beanDesc
418 .getPropertyDesc(propertyName);
419 final Object formBean = propertyDesc.getValue(action);
420 if (formBean == null) {
421 throw new ActionRuntimeException("ECUB0102",
422 new Object[] { propertyName });
423 }
424 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 if (method.isAnnotationPresent(Form.class)) {
441 form = method.getAnnotation(Form.class);
442 } else {
443 form = actionClass.getAnnotation(Form.class);
444 }
445 return form;
446 }
447
448
449
450
451
452
453
454
455
456
457
458 public static String getOnSubmit(final Method method) {
459 final OnSubmit onSubmit = method.getAnnotation(OnSubmit.class);
460 final String parameterName;
461 if (onSubmit == null) {
462 parameterName = null;
463 } else {
464 parameterName = onSubmit.value();
465 }
466 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 return (T) request.getAttribute(name);
485 }
486
487 }