1 package org.apache.turbine.modules;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.annotation.Annotation;
23
24
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.Arrays;
28
29 import org.apache.commons.collections.map.MultiKeyMap;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.fulcrum.parser.ParameterParser;
34 import org.apache.fulcrum.parser.ValueParser.URLCaseFolding;
35 import org.apache.turbine.Turbine;
36 import org.apache.turbine.TurbineConstants;
37 import org.apache.turbine.annotation.AnnotationProcessor;
38 import org.apache.turbine.annotation.TurbineActionEvent;
39 import org.apache.turbine.annotation.TurbineConfiguration;
40 import org.apache.turbine.pipeline.PipelineData;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public abstract class ActionEvent extends Action
88 {
89
90 protected Log log = LogFactory.getLog(this.getClass());
91
92
93 protected static final String BUTTON = "eventSubmit_";
94
95 protected static final int BUTTON_LENGTH = BUTTON.length();
96
97 protected static final String DEFAULT_METHOD = "doPerform";
98
99 protected static final String METHOD_NAME_PREFIX = "do";
100
101 protected static final int METHOD_NAME_LENGTH = METHOD_NAME_PREFIX.length();
102
103 protected static final int LENGTH = BUTTON.length();
104
105
106
107
108
109 @TurbineConfiguration( TurbineConstants.ACTION_EVENTSUBMIT_NEEDSVALUE_KEY )
110 private boolean submitValueKey = TurbineConstants.ACTION_EVENTSUBMIT_NEEDSVALUE_DEFAULT;
111
112
113
114
115
116
117 @TurbineConfiguration( TurbineConstants.ACTION_EVENT_BUBBLE_EXCEPTION_UP )
118 protected boolean bubbleUpException = TurbineConstants.ACTION_EVENT_BUBBLE_EXCEPTION_UP_DEFAULT;
119
120
121
122
123 private MultiKeyMap methodCache = new MultiKeyMap();
124
125
126
127
128
129
130
131
132
133
134
135 protected Method getMethod(String name, Class<?>[] signature, ParameterParser pp) throws NoSuchMethodException
136 {
137 Method method = (Method) this.methodCache.get(name, signature);
138
139 if (method == null)
140 {
141
142 Method[] methods = getClass().getMethods();
143
144 methodLoop:
145 for (Method m : methods)
146 {
147 Annotation[] annotations = AnnotationProcessor.getAnnotations(m);
148 for (Annotation a : annotations)
149 {
150 if (a instanceof TurbineActionEvent)
151 {
152 TurbineActionEvent tae = (TurbineActionEvent) a;
153 if (name.equals(pp.convert(tae.value()))
154 && Arrays.equals(signature, m.getParameterTypes()))
155 {
156 method = m;
157 break methodLoop;
158 }
159 }
160 }
161 }
162
163
164 if (method == null)
165 {
166 String tmp = name.toLowerCase().substring(METHOD_NAME_LENGTH);
167 method = getClass().getMethod(METHOD_NAME_PREFIX + StringUtils.capitalize(tmp), signature);
168 }
169
170 this.methodCache.put(name, signature, method);
171 }
172
173 return method;
174 }
175
176
177
178
179
180
181
182
183
184 @Override
185 public void doPerform(PipelineData pipelineData)
186 throws Exception
187 {
188 ParameterParser pp = pipelineData.get(Turbine.class, ParameterParser.class);
189 executeEvents(pp, new Class<?>[]{ PipelineData.class }, new Object[]{ pipelineData });
190 }
191
192
193
194
195
196
197
198
199
200
201 protected void executeEvents(ParameterParser pp, Class<?>[] signature, Object[] parameters)
202 throws Exception
203 {
204
205 String theButton = null;
206
207 String button = pp.convert(BUTTON);
208 String key = null;
209
210
211 for (String k : pp)
212 {
213 key = k;
214 if (key.startsWith(button))
215 {
216 if (considerKey(key, pp))
217 {
218 theButton = key;
219 break;
220 }
221 }
222 }
223
224 if (theButton == null)
225 {
226 theButton = BUTTON + DEFAULT_METHOD;
227 key = null;
228 }
229
230 theButton = formatString(theButton, pp);
231 Method method = null;
232
233 try
234 {
235 method = getMethod(theButton, signature, pp);
236 }
237 catch (NoSuchMethodException e)
238 {
239 method = getMethod(DEFAULT_METHOD, signature, pp);
240 }
241 finally
242 {
243 if (key != null)
244 {
245 pp.remove(key);
246 }
247 }
248
249 try
250 {
251 if (log.isDebugEnabled())
252 {
253 log.debug("Invoking " + method);
254 }
255
256 method.invoke(this, parameters);
257 }
258 catch (InvocationTargetException ite)
259 {
260 Throwable t = ite.getTargetException();
261 if (bubbleUpException)
262 {
263 if (t instanceof Exception)
264 {
265 throw (Exception) t;
266 }
267 else
268 {
269 throw ite;
270 }
271 }
272 else
273 {
274 log.error("Invokation of " + method , t);
275 }
276 }
277 }
278
279
280
281
282
283
284
285
286
287 protected String formatString(String input, ParameterParser pp)
288 {
289 String tmp = input;
290
291 if (StringUtils.isNotEmpty(input))
292 {
293 tmp = input.toLowerCase();
294
295
296 String methodName = (tmp.endsWith(".x") || tmp.endsWith(".y"))
297 ? input.substring(0, input.length() - 2)
298 : input;
299
300 if (pp.getUrlFolding() == URLCaseFolding.NONE)
301 {
302 tmp = methodName.substring(BUTTON_LENGTH);
303 }
304 else
305 {
306 tmp = methodName.toLowerCase().substring(BUTTON_LENGTH);
307 }
308 }
309
310 return tmp;
311 }
312
313
314
315
316
317
318
319
320
321 protected boolean considerKey(String key, ParameterParser pp)
322 {
323 if (!submitValueKey)
324 {
325 log.debug("No Value required, accepting " + key);
326 return true;
327 }
328 else
329 {
330
331
332
333
334
335
336
337
338
339 String keyValue = pp.getString(key);
340 log.debug("Key Value is " + keyValue);
341 if (StringUtils.isEmpty(keyValue))
342 {
343 log.debug("Key is empty, rejecting " + key);
344 return false;
345 }
346
347 try
348 {
349 if (Integer.parseInt(keyValue) != 0)
350 {
351 log.debug("Integer != 0, accepting " + key);
352 return true;
353 }
354 }
355 catch (NumberFormatException nfe)
356 {
357
358
359
360 log.debug("Not a number, accepting " + key);
361 return true;
362 }
363 }
364 log.debug("Rejecting " + key);
365 return false;
366 }
367 }