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.CubbyConstants.INTERNAL_FORWARD_DIRECTORY; |
19 | |
|
20 | |
import java.lang.reflect.Method; |
21 | |
import java.util.Collection; |
22 | |
|
23 | |
import javax.servlet.http.HttpServletRequest; |
24 | |
|
25 | |
import org.seasar.cubby.action.Accept; |
26 | |
import org.seasar.cubby.action.Action; |
27 | |
import org.seasar.cubby.action.ActionResult; |
28 | |
import org.seasar.cubby.action.Path; |
29 | |
import org.seasar.cubby.action.RequestMethod; |
30 | |
import org.seasar.framework.util.StringUtil; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class CubbyUtils { |
39 | |
|
40 | |
|
41 | |
private static final String INDEX_METHOD_NAME = "index"; |
42 | |
|
43 | |
|
44 | |
private static Accept DEFAULT_ACCEPT_ANNOTATION; |
45 | |
static { |
46 | |
@Accept |
47 | 0 | class AcceptDummy { |
48 | |
} |
49 | 1 | DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class |
50 | |
.getAnnotation(Accept.class); |
51 | 1 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public static String getActionDirectory(final Class<?> actionClass) { |
61 | |
final String actionName; |
62 | 173 | final Path path = actionClass.getAnnotation(Path.class); |
63 | 173 | if (path != null && !StringUtil.isEmpty(path.value())) { |
64 | 105 | actionName = path.value(); |
65 | |
} else { |
66 | 68 | final String name = left(actionClass.getSimpleName(), "$"); |
67 | 68 | actionName = toFirstLower(name.replaceAll( |
68 | |
"(.*[.])*([^.]+)(Action$)", "$2")); |
69 | |
} |
70 | 173 | return actionName; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private static String left(final String text, final String sep) { |
83 | 68 | final int pos = text.indexOf(sep); |
84 | 68 | if (pos != -1) { |
85 | 0 | return text.substring(0, pos); |
86 | |
} |
87 | 68 | return text; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private static String toFirstLower(final String text) { |
98 | 68 | if (StringUtil.isEmpty(text)) { |
99 | 0 | throw new IllegalArgumentException("text is empty."); |
100 | |
} |
101 | 68 | final StringBuilder sb = new StringBuilder(); |
102 | 68 | sb.append(text.substring(0, 1).toLowerCase()); |
103 | 68 | if (text.length() > 1) { |
104 | 68 | sb.append(text.substring(1)); |
105 | |
} |
106 | 68 | return sb.toString(); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public static String getActionPath(final Class<?> actionClass, |
119 | |
final Method method) { |
120 | |
final String path; |
121 | 167 | final String actionMethodName = getActionMethodName(method); |
122 | 167 | if (actionMethodName.startsWith("/")) { |
123 | 2 | return path = actionMethodName; |
124 | |
} else { |
125 | 165 | final String actionDirectory = CubbyUtils |
126 | |
.getActionDirectory(actionClass); |
127 | 165 | if ("/".equals(actionDirectory)) { |
128 | 40 | path = "/" + actionMethodName; |
129 | |
} else { |
130 | 125 | path = "/" + actionDirectory + "/" + actionMethodName; |
131 | |
} |
132 | |
} |
133 | 165 | return path; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
private static String getActionMethodName(final Method method) { |
144 | |
final String actionName; |
145 | 167 | final Path path = method.getAnnotation(Path.class); |
146 | 167 | if (path != null && !StringUtil.isEmpty(path.value())) { |
147 | 112 | actionName = path.value(); |
148 | |
} else { |
149 | 55 | final String methodName = method.getName(); |
150 | 55 | if (INDEX_METHOD_NAME.equals(methodName)) { |
151 | 14 | actionName = ""; |
152 | |
} else { |
153 | 41 | actionName = methodName; |
154 | |
} |
155 | |
} |
156 | 167 | return actionName; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public static RequestMethod[] getAcceptableRequestMethods( |
169 | |
final Class<?> actionClass, final Method method) { |
170 | 156 | Accept accept = method.getAnnotation(Accept.class); |
171 | 156 | if (accept == null) { |
172 | 120 | accept = actionClass.getAnnotation(Accept.class); |
173 | 120 | if (accept == null) { |
174 | 120 | accept = DEFAULT_ACCEPT_ANNOTATION; |
175 | |
} |
176 | |
} |
177 | 156 | return accept.value(); |
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public static int getObjectSize(final Object value) { |
188 | |
final int size; |
189 | 8 | if (value == null) { |
190 | 1 | size = 0; |
191 | 7 | } else if (value.getClass().isArray()) { |
192 | 3 | final Object[] array = (Object[]) value; |
193 | 3 | size = array.length; |
194 | 3 | } else if (value instanceof Collection) { |
195 | 3 | final Collection<?> collection = (Collection<?>) value; |
196 | 3 | size = collection.size(); |
197 | 3 | } else { |
198 | 1 | size = 1; |
199 | |
} |
200 | 8 | return size; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public static String getPath(final HttpServletRequest request) { |
211 | 5 | final String uri = request.getRequestURI(); |
212 | 5 | final String contextPath = request.getContextPath(); |
213 | |
final String path; |
214 | 5 | if ("/".equals(contextPath)) { |
215 | 5 | path = uri; |
216 | |
} else { |
217 | 0 | path = uri.substring(contextPath.length()); |
218 | |
} |
219 | 5 | return path; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
public static String getInternalForwardPath( |
232 | |
final Class<? extends Action> actionClass, final String methodName) { |
233 | 24 | return "/" + INTERNAL_FORWARD_DIRECTORY + "/" |
234 | |
+ actionClass.getCanonicalName() + "/" + methodName; |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
public static boolean isActionClass(final Class<?> clazz) { |
246 | 36 | return Action.class.isAssignableFrom(clazz); |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
public static boolean isActionMethod(final Method method) { |
258 | 732 | return method.getReturnType().isAssignableFrom(ActionResult.class) |
259 | |
&& method.getParameterTypes().length == 0; |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public static String replaceFirst(final String text, final String replace, |
274 | |
final String with) { |
275 | 12 | if (text == null || replace == null || with == null) { |
276 | 6 | return text; |
277 | |
} |
278 | 6 | final int index = text.indexOf(replace); |
279 | 6 | if (index == -1) { |
280 | 0 | return text; |
281 | |
} |
282 | 6 | final StringBuilder builder = new StringBuilder(100); |
283 | 6 | builder.append(text.substring(0, index)); |
284 | 6 | builder.append(with); |
285 | 6 | builder.append(text.substring(index + replace.length())); |
286 | 6 | return builder.toString(); |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public static String[] split2(final String text, final char delim) { |
299 | 113 | if (text == null) { |
300 | 1 | return null; |
301 | |
} |
302 | 112 | int index = text.indexOf(delim); |
303 | 112 | if (index == -1) { |
304 | 86 | return new String[] { text }; |
305 | |
} |
306 | 26 | String[] tokens = new String[2]; |
307 | 26 | tokens[0] = text.substring(0, index); |
308 | 26 | tokens[1] = text.substring(index + 1); |
309 | 26 | return tokens; |
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 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public static String escapeHtml(final Object str) { |
348 | 108 | if (str == null) { |
349 | 0 | return ""; |
350 | |
} |
351 | |
String text; |
352 | 108 | if (str instanceof String) { |
353 | 107 | text = (String) str; |
354 | |
} else { |
355 | 1 | text = str.toString(); |
356 | |
} |
357 | 108 | text = StringUtil.replace(text, "&", "&"); |
358 | 108 | text = StringUtil.replace(text, "<", "<"); |
359 | 108 | text = StringUtil.replace(text, ">", ">"); |
360 | 108 | text = StringUtil.replace(text, "\"", """); |
361 | 108 | text = StringUtil.replace(text, "'", "'"); |
362 | 108 | return text; |
363 | |
} |
364 | |
|
365 | |
} |