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 | |
import java.util.Iterator; |
21 | |
|
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.Path; |
28 | |
import org.seasar.cubby.action.RequestMethod; |
29 | |
import org.seasar.framework.util.StringUtil; |
30 | |
|
31 | 0 | public class CubbyUtils { |
32 | |
|
33 | |
private static Accept DEFAULT_ACCEPT_ANNOTATION; |
34 | |
static { |
35 | |
@Accept |
36 | 0 | class AcceptDummy { |
37 | |
} |
38 | 1 | DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class.getAnnotation(Accept.class); |
39 | 1 | } |
40 | |
|
41 | |
public static String getActionClassName(final Class<?> c) { |
42 | |
final String actionName; |
43 | 27 | final Path path = c.getAnnotation(Path.class); |
44 | 27 | if (path != null && !StringUtil.isEmpty(path.value())) { |
45 | 18 | actionName = path.value(); |
46 | |
} else { |
47 | 9 | final String name = left(c.getSimpleName(), "$"); |
48 | 9 | actionName = toFirstLower(name.replaceAll( |
49 | |
"(.*[.])*([^.]+)(Action$)", "$2")); |
50 | |
} |
51 | 27 | return actionName; |
52 | |
} |
53 | |
|
54 | |
static String getActionMethodName(final Method m) { |
55 | |
final String actionName; |
56 | 23 | final Path path = m.getAnnotation(Path.class); |
57 | 23 | if (path != null && !StringUtil.isEmpty(path.value())) { |
58 | 8 | actionName = path.value(); |
59 | |
} else { |
60 | 15 | final String methodName = m.getName(); |
61 | 15 | if ("index".equals(methodName)) { |
62 | 4 | actionName = ""; |
63 | |
} else { |
64 | 11 | actionName = methodName; |
65 | |
} |
66 | |
} |
67 | 23 | return actionName; |
68 | |
} |
69 | |
|
70 | |
public static String getActionUrl(final Class<?> c, final Method m) { |
71 | 23 | final String actionMethodName = getActionMethodName(m); |
72 | 23 | if (actionMethodName.startsWith("/")) { |
73 | 4 | return actionMethodName; |
74 | |
} else { |
75 | 19 | final String actionName = CubbyUtils.getActionClassName(c); |
76 | 19 | if ("/".equals(actionName)) { |
77 | 9 | return "/" + actionMethodName; |
78 | |
} else { |
79 | 10 | return "/" + actionName + "/" + actionMethodName; |
80 | |
} |
81 | |
} |
82 | |
} |
83 | |
|
84 | |
public static RequestMethod[] getAcceptableRequestMethods( |
85 | |
final Class<?> c, final Method m) { |
86 | 12 | Accept accept = m.getAnnotation(Accept.class); |
87 | 12 | if (accept == null) { |
88 | 12 | accept = c.getAnnotation(Accept.class); |
89 | 12 | if (accept == null) { |
90 | 12 | accept = DEFAULT_ACCEPT_ANNOTATION; |
91 | |
} |
92 | |
} |
93 | 12 | return accept.value(); |
94 | |
} |
95 | |
|
96 | |
public static boolean isActionMethod(final Method m) { |
97 | 92 | return m.getReturnType().isAssignableFrom(ActionResult.class) |
98 | |
&& m.getParameterTypes().length == 0; |
99 | |
} |
100 | |
|
101 | |
public static int getObjectSize(final Object value) { |
102 | |
final int size; |
103 | 8 | if (value == null) { |
104 | 1 | size = 0; |
105 | 7 | } else if (value.getClass().isArray()) { |
106 | 3 | final Object[] array = (Object[]) value; |
107 | 3 | size = array.length; |
108 | 3 | } else if (value instanceof Collection) { |
109 | 3 | final Collection<?> collection = (Collection<?>) value; |
110 | 3 | size = collection.size(); |
111 | 3 | } else { |
112 | 1 | size = 1; |
113 | |
} |
114 | 8 | return size; |
115 | |
} |
116 | |
|
117 | |
public static String getPath(final HttpServletRequest request) { |
118 | 1 | final String uri = request.getRequestURI(); |
119 | 1 | final String contextPath = request.getContextPath(); |
120 | |
final String path; |
121 | 1 | if ("/".equals(contextPath)) { |
122 | 1 | path = uri; |
123 | |
} else { |
124 | 0 | path = uri.substring(contextPath.length()); |
125 | |
} |
126 | 1 | return path; |
127 | |
} |
128 | |
|
129 | |
public static boolean isActionClass(final Class<?> c) { |
130 | 0 | return Action.class.isAssignableFrom(c); |
131 | |
} |
132 | |
|
133 | |
static String toFirstLower(final String propertyName) { |
134 | 9 | if (StringUtil.isEmpty(propertyName)) { |
135 | 0 | throw new IllegalArgumentException("properyName is empty."); |
136 | |
} |
137 | 9 | final StringBuilder sb = new StringBuilder(); |
138 | 9 | sb.append(propertyName.substring(0, 1).toLowerCase()); |
139 | 9 | if (propertyName.length() > 1) { |
140 | 9 | sb.append(propertyName.substring(1)); |
141 | |
} |
142 | 9 | return sb.toString(); |
143 | |
} |
144 | |
|
145 | |
static String left(final String str, final String sep) { |
146 | 9 | final int pos = str.indexOf(sep); |
147 | 9 | if (pos != -1) { |
148 | 0 | return str.substring(0, pos); |
149 | |
} |
150 | 9 | return str; |
151 | |
} |
152 | |
|
153 | |
public static String join(final Iterator<?> iterator, final char separator) { |
154 | 1 | if (iterator == null) { |
155 | 0 | return null; |
156 | |
} |
157 | 1 | if (!iterator.hasNext()) { |
158 | 0 | return ""; |
159 | |
} |
160 | 1 | final Object first = iterator.next(); |
161 | 1 | if (!iterator.hasNext()) { |
162 | 1 | return first != null ? first.toString() : ""; |
163 | |
} |
164 | 0 | final StringBuffer buf = new StringBuffer(256); |
165 | 0 | if (first != null) { |
166 | 0 | buf.append(first); |
167 | |
} |
168 | 0 | while (iterator.hasNext()) { |
169 | 0 | buf.append(separator); |
170 | 0 | final Object obj = iterator.next(); |
171 | 0 | if (obj != null) { |
172 | 0 | buf.append(obj); |
173 | |
} |
174 | 0 | } |
175 | 0 | return buf.toString(); |
176 | |
} |
177 | |
} |