1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.scripting;
22
23
24 import java.util.Enumeration;
25 import java.util.Properties;
26
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32 import javax.servlet.http.HttpServletRequest;
33 import org.apache.bsf.BSFException;
34 import org.apache.bsf.BSFManager;
35
36
37
38
39
40
41
42 public class RequestToVariableFilter implements BSFManagerFilter {
43
44
45 private static final Log LOG = LogFactory.getLog(TestFilter.class);
46
47
48
49
50
51
52
53
54 public void init(String name, Properties props) { }
55
56
57
58
59
60
61
62
63 public BSFManager apply(BSFManager mgr) {
64 HttpServletRequest request =
65 (HttpServletRequest) mgr.lookupBean("request");
66 String[] values = null;
67 String name = null;
68 String newName = null;
69 Object o = null;
70 for (Enumeration e = request.getParameterNames();
71 e.hasMoreElements();) {
72 name = (String) e.nextElement();
73 o = mgr.lookupBean(name);
74 if (o == null) {
75 newName = name;
76 } else {
77 newName = "_" + name;
78 }
79
80 values = request.getParameterValues(name);
81 try {
82 if (values.length > 1) {
83 mgr.declareBean(newName, values, values.getClass());
84 if (LOG.isDebugEnabled()) {
85 LOG.debug("creating array var " + newName);
86 }
87 } else {
88 mgr.declareBean(newName, values[0], String.class);
89 if (LOG.isDebugEnabled()) {
90 LOG.debug("creating string var " + newName);
91 }
92 }
93 } catch (BSFException ex) {
94 LOG.warn("Unable to set variable " + newName, ex);
95 }
96
97 }
98 if (LOG.isDebugEnabled()) {
99 LOG.debug("Done filtering");
100 }
101 return mgr;
102 }
103 }