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