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 the "servlet path" component of the request URI
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 an extension pattern like "*.execute" and
30 * then arrange that this is the default command to be executed. In such
31 * an environment, a request for a context relative URI of "/foo.execute"
32 * would cause the "/foo.execute" command to be loaded and executed.</p>
33 *
34 * @author Craig R. McClanahan
35 */
36
37 public class ServletPathMapper implements Command {
38
39
40
41
42
43 private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
44
45
46
47
48
49 /***
50 * <p>Return the context key under which our {@link Catalog} has been
51 * stored.</p>
52 *
53 * @return The context key for the Catalog.
54 */
55 public String getCatalogKey() {
56
57 return (this.catalogKey);
58
59 }
60
61
62 /***
63 * <p>Set the context key under which our {@link Catalog} has been
64 * stored.</p>
65 *
66 * @param catalogKey The new catalog key
67 */
68 public void setCatalogKey(String catalogKey) {
69
70 this.catalogKey = catalogKey;
71
72 }
73
74
75
76
77
78 /***
79 * <p>Look up the servlet path information for this request, and use it to
80 * select an appropriate {@link Command} to be executed.
81 *
82 * @param context Context for the current request
83 * @return The result of executing the Command for the servlet path.
84 * @throws Exception if there is a problem executing the Command for
85 * the servlet path.
86 */
87 public boolean execute(Context context) throws Exception {
88
89
90 ServletWebContext swcontext = (ServletWebContext) context;
91 HttpServletRequest request = swcontext.getRequest();
92 String servletPath = (String)
93 request.getAttribute("javax.servlet.include.servlet_path");
94 if (servletPath == null) {
95 servletPath = request.getServletPath();
96 }
97
98
99 Catalog catalog = (Catalog) context.get(getCatalogKey());
100 Command command = catalog.getCommand(servletPath);
101 return (command.execute(context));
102
103 }
104
105
106 }