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