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 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 class AcceptDummy {
53 }
54 DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class
55 .getAnnotation(Accept.class);
56 }
57
58
59
60
61
62
63
64
65 public static String getActionDirectory(
66 final Class<? extends Action> actionClass) {
67 final String actionName;
68 final Path path = actionClass.getAnnotation(Path.class);
69 if (path != null && !StringUtil.isEmpty(path.value())) {
70 actionName = path.value();
71 } else {
72 final String name = left(actionClass.getSimpleName(), "$");
73 actionName = toFirstLower(name.replaceAll(
74 "(.*[.])*([^.]+)(Action$)", "$2"));
75 }
76 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 final int pos = text.indexOf(sep);
90 if (pos != -1) {
91 return text.substring(0, pos);
92 }
93 return text;
94 }
95
96
97
98
99
100
101
102
103 private static String toFirstLower(final String text) {
104 if (StringUtil.isEmpty(text)) {
105 throw new IllegalArgumentException("text is empty.");
106 }
107 final StringBuilder sb = new StringBuilder();
108 sb.append(text.substring(0, 1).toLowerCase());
109 if (text.length() > 1) {
110 sb.append(text.substring(1));
111 }
112 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 final String actionMethodName = getActionMethodName(method);
128 if (actionMethodName.startsWith("/")) {
129 return path = actionMethodName;
130 } else {
131 final String actionDirectory = CubbyUtils
132 .getActionDirectory(actionClass);
133 if ("/".equals(actionDirectory)) {
134 path = "/" + actionMethodName;
135 } else {
136 path = "/" + actionDirectory + "/" + actionMethodName;
137 }
138 }
139 return path;
140 }
141
142
143
144
145
146
147
148
149 private static String getActionMethodName(final Method method) {
150 final String actionName;
151 final Path path = method.getAnnotation(Path.class);
152 if (path != null && !StringUtil.isEmpty(path.value())) {
153 actionName = path.value();
154 } else {
155 final String methodName = method.getName();
156 if (INDEX_METHOD_NAME.equals(methodName)) {
157 actionName = "";
158 } else {
159 actionName = methodName;
160 }
161 }
162 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 if (method.isAnnotationPresent(Accept.class)) {
178 accept = method.getAnnotation(Accept.class);
179 } else if (actionClass.isAnnotationPresent(Accept.class)) {
180 accept = actionClass.getAnnotation(Accept.class);
181 } else {
182 accept = DEFAULT_ACCEPT_ANNOTATION;
183 }
184 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 if (value == null) {
197 size = 0;
198 } else if (value.getClass().isArray()) {
199 final Object[] array = (Object[]) value;
200 size = array.length;
201 } else if (value instanceof Collection) {
202 final Collection<?> collection = (Collection<?>) value;
203 size = collection.size();
204 } else {
205 size = 1;
206 }
207 return size;
208 }
209
210
211
212
213
214
215
216
217 public static String getPath(final HttpServletRequest request) {
218 final StringBuilder builder = new StringBuilder();
219 builder.append(request.getServletPath());
220 final String pathInfo = request.getPathInfo();
221 if (pathInfo != null) {
222 builder.append(pathInfo);
223 }
224 return builder.toString();
225 }
226
227
228
229
230
231
232
233
234
235 public static boolean isActionClass(final Class<?> clazz) {
236 return Action.class.isAssignableFrom(clazz);
237 }
238
239
240
241
242
243
244
245
246
247 public static boolean isActionMethod(final Method method) {
248 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 if (text == null || replace == null || with == null) {
266 return text;
267 }
268 final int index = text.indexOf(replace);
269 if (index == -1) {
270 return text;
271 }
272 final StringBuilder builder = new StringBuilder(100);
273 builder.append(text.substring(0, index));
274 builder.append(with);
275 builder.append(text.substring(index + replace.length()));
276 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 if (text == null) {
290 return null;
291 }
292 final int index = text.indexOf(delim);
293 if (index == -1) {
294 return new String[] { text };
295 }
296 final String[] tokens = new String[2];
297 tokens[0] = text.substring(0, index);
298 tokens[1] = text.substring(index + 1);
299 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 if (str == null) {
339 return "";
340 }
341 String text;
342 if (str instanceof String) {
343 text = (String) str;
344 } else {
345 text = str.toString();
346 }
347 text = StringUtil.replace(text, "&", "&");
348 text = StringUtil.replace(text, "<", "<");
349 text = StringUtil.replace(text, ">", ">");
350 text = StringUtil.replace(text, "\"", """);
351 text = StringUtil.replace(text, "'", "'");
352 return text;
353 }
354
355
356
357
358
359
360
361
362 public static int getPriority(final Method method) {
363 final Path path = method.getAnnotation(Path.class);
364 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 final Form form = getForm(actionClass, method);
386 if (form == null) {
387 return action;
388 }
389 if (!form.binding()) {
390 return null;
391 }
392 if (form.bindingType() == NONE) {
393 return null;
394 }
395 if (Form.THIS.equals(form.value())) {
396 return action;
397 }
398
399 final String propertyName = form.value();
400 final BeanDesc beanDesc = BeanDescFactory
401 .getBeanDesc(action.getClass());
402 final PropertyDesc propertyDesc = beanDesc
403 .getPropertyDesc(propertyName);
404 final Object formBean = propertyDesc.getValue(action);
405 if (formBean == null) {
406 throw new ActionRuntimeException("ECUB0102",
407 new Object[] { propertyName });
408 }
409 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 if (method.isAnnotationPresent(Form.class)) {
426 form = method.getAnnotation(Form.class);
427 } else {
428 form = actionClass.getAnnotation(Form.class);
429 }
430 return form;
431 }
432
433
434
435
436
437
438
439
440
441
442
443 public static String getOnSubmit(final Method method) {
444 final OnSubmit onSubmit = method.getAnnotation(OnSubmit.class);
445 final String parameterName;
446 if (onSubmit == null) {
447 parameterName = null;
448 } else {
449 parameterName = onSubmit.value();
450 }
451 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 return (T) request.getAttribute(name);
470 }
471
472 }