KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > servlet > RequestParameterMapper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.chain.web.servlet;
17
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
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     // ------------------------------------------------------ Instance Variables
42

43
44     private String JavaDoc catalogKey = ChainProcessor.CATALOG_DEFAULT;
45     private String JavaDoc parameter = "command";
46
47
48     // -------------------------------------------------------------- Properties
49

50
51     /**
52      * <p>Return the context key under which our {@link Catalog} has been
53      * stored.</p>
54      */

55     public String JavaDoc 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 JavaDoc catalogKey) {
69
70         this.catalogKey = catalogKey;
71
72     }
73
74
75     /**
76      * <p>Return the name of the request parameter to use for
77      * selecting the {@link Command} to be executed.</p>
78      */

79     public String JavaDoc getParameter() {
80
81         return (this.parameter);
82
83     }
84
85
86     /**
87      * <p>Set the name of the request parameter to use for
88      * selecting the {@link Command} to be executed.</p>
89      *
90      * @param parameter The new parameter name
91      */

92     public void setParameter(String JavaDoc parameter) {
93
94         this.parameter = parameter;
95
96     }
97
98
99     // --------------------------------------------------------- Command Methods
100

101
102     /**
103      * <p>Look up the specified request paramater for this request, and use it
104      * to select an appropriate {@link Command} to be executed.
105      *
106      * @param context Context for the current request
107      */

108     public boolean execute(Context context) throws Exception JavaDoc {
109
110         // Look up the specified request parameter for this request
111
ServletWebContext swcontext = (ServletWebContext) context;
112         HttpServletRequest JavaDoc request = swcontext.getRequest();
113         String JavaDoc value = request.getParameter(getParameter());
114
115         // Map to the Command specified by the extra path info
116
Catalog catalog = (Catalog) context.get(getCatalogKey());
117         Command command = catalog.getCommand(value);
118         return (command.execute(context));
119
120     }
121
122
123 }
124
Popular Tags