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 "path info" 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 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 URI "/execute/foo"
32 * would cause the "/foo" command to be loaded and executed.</p>
33 *
34 * @author Craig R. McClanahan
35 */
36
37 public class PathInfoMapper 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 public String getCatalogKey() {
54
55 return (this.catalogKey);
56
57 }
58
59
60 /***
61 * <p>Set the context key under which our {@link Catalog} has been
62 * stored.</p>
63 *
64 * @param catalogKey The new catalog key
65 */
66 public void setCatalogKey(String catalogKey) {
67
68 this.catalogKey = catalogKey;
69
70 }
71
72
73
74
75
76 /***
77 * <p>Look up the extra path information for this request, and use it to
78 * select an appropriate {@link Command} to be executed.
79 *
80 * @param context Context for the current request
81 */
82 public boolean execute(Context context) throws Exception {
83
84
85 ServletWebContext swcontext = (ServletWebContext) context;
86 HttpServletRequest request = swcontext.getRequest();
87 String pathInfo = (String)
88 request.getAttribute("javax.servlet.include.path_info");
89 if (pathInfo == null) {
90 pathInfo = request.getPathInfo();
91 }
92
93
94 Catalog catalog = (Catalog) context.get(getCatalogKey());
95 Command command = catalog.getCommand(pathInfo);
96 return (command.execute(context));
97
98 }
99
100
101 }