1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.web.servlet;
17
18
19 import javax.servlet.http.HttpServletRequest;
20 import org.apache.commons.chain.Catalog;
21 import org.apache.commons.chain.Command;
22 import org.apache.commons.chain.Context;
23
24
25 /***
26 * <p>{@link Command} that uses a specified request parameter
27 * to select a {@link Command} from the appropriate {@link Catalog}, and
28 * execute it. To use this command, you would typically map an instance
29 * of {@link ChainProcessor} to a wildcard pattern like "*.execute" and
30 * then arrange that this is the default command to be executed. In such
31 * an environment, a request for the context-relative path
32 * "/foo.execute?command=bar" would cause the "/bar" command to be loaded
33 * and executed.</p>
34 *
35 * @author Craig R. McClanahan
36 */
37
38 public class RequestParameterMapper implements Command {
39
40
41
42
43
44 private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
45 private String parameter = "command";
46
47
48
49
50
51 /***
52 * <p>Return the context key under which our {@link Catalog} has been
53 * stored.</p>
54 *
55 * @return The context key for the Catalog.
56 */
57 public String getCatalogKey() {
58
59 return (this.catalogKey);
60
61 }
62
63
64 /***
65 * <p>Set the context key under which our {@link Catalog} has been
66 * stored.</p>
67 *
68 * @param catalogKey The new catalog key
69 */
70 public void setCatalogKey(String catalogKey) {
71
72 this.catalogKey = catalogKey;
73
74 }
75
76
77 /***
78 * <p>Return the name of the request parameter to use for
79 * selecting the {@link Command} to be executed.</p>
80 *
81 * @return The name of the request parameter.
82 */
83 public String getParameter() {
84
85 return (this.parameter);
86
87 }
88
89
90 /***
91 * <p>Set the name of the request parameter to use for
92 * selecting the {@link Command} to be executed.</p>
93 *
94 * @param parameter The new parameter name
95 */
96 public void setParameter(String parameter) {
97
98 this.parameter = parameter;
99
100 }
101
102
103
104
105
106 /***
107 * <p>Look up the specified request paramater for this request, and use it
108 * to select an appropriate {@link Command} to be executed.
109 *
110 * @param context Context for the current request
111 * @return The result of executing the Command for the request parameter.
112 * @throws Exception if there is a problem executing the Command for
113 * the request parameter.
114 */
115 public boolean execute(Context context) throws Exception {
116
117
118 ServletWebContext swcontext = (ServletWebContext) context;
119 HttpServletRequest request = swcontext.getRequest();
120 String value = request.getParameter(getParameter());
121
122
123 Catalog catalog = (Catalog) context.get(getCatalogKey());
124 Command command = catalog.getCommand(value);
125 return (command.execute(context));
126
127 }
128
129
130 }