View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.servlet;
18  
19  
20  import javax.servlet.http.HttpServletRequest;
21  import org.apache.commons.chain.Catalog;
22  import org.apache.commons.chain.Context;
23  import org.apache.commons.chain.Command;
24  import org.apache.commons.chain.generic.LookupCommand;
25  
26  
27  /**
28   * <p>{@link Command} that uses a specified request parameter
29   * to select a {@link Command} from the appropriate {@link Catalog}, and
30   * execute it.  To use this command, you would typically map an instance
31   * of {@link ChainProcessor} to a wildcard pattern like "*.execute" and
32   * then arrange that this is the default command to be executed.  In such
33   * an environment, a request for the context-relative path
34   * "/foo.execute?command=bar" would cause the "/bar" command to be loaded
35   * and executed.</p>
36   *
37   * @author Craig R. McClanahan
38   */
39  
40  public class RequestParameterMapper extends LookupCommand implements Command {
41  
42  
43      // ------------------------------------------------------ Instance Variables
44  
45  
46      private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
47      private String parameter = "command";
48  
49  
50      // -------------------------------------------------------------- Properties
51  
52  
53      /**
54       * <p>Return the context key under which our {@link Catalog} has been
55       * stored.</p>
56       *
57       * @return The context key for the Catalog.
58       */
59      public String getCatalogKey() {
60  
61          return (this.catalogKey);
62  
63      }
64  
65  
66      /**
67       * <p>Set the context key under which our {@link Catalog} has been
68       * stored.</p>
69       *
70       * @param catalogKey The new catalog key
71       *
72       * @deprecated Use catalogName to specify the name of the catalog in the
73       *  catalog factory
74       */
75      public void setCatalogKey(String catalogKey) {
76  
77          this.catalogKey = catalogKey;
78  
79      }
80  
81  
82      /**
83       * <p>Return the name of the request parameter to use for
84       * selecting the {@link Command} to be executed.</p>
85       *
86       * @return The name of the request parameter.
87       *
88       * @deprecated Use catalogName to specify the name of the catalog in the
89       *  catalog factory
90       */
91      public String getParameter() {
92  
93          return (this.parameter);
94  
95      }
96  
97  
98      /**
99       * <p>Set the name of the request parameter to use for
100      * selecting the {@link Command} to be executed.</p>
101      *
102      * @param parameter The new parameter name
103      */
104     public void setParameter(String parameter) {
105 
106         this.parameter = parameter;
107 
108     }
109 
110 
111     // --------------------------------------------------------- Command Methods
112 
113 
114     /**
115      * <p>Look up the specified request paramater for this request, and use it
116      * to select an appropriate {@link Command} to be executed.
117      *
118      * @param context Context for the current request
119      * @return The name of the {@link Command} instance
120      *
121      * @since Chain 1.2
122      */
123     protected String getCommandName(Context context) {
124 
125         // Look up the specified request parameter for this request
126         ServletWebContext swcontext = (ServletWebContext) context;
127         HttpServletRequest request = swcontext.getRequest();
128         String value = request.getParameter(getParameter());
129         return value;
130 
131     }
132 
133 
134     /**
135      * <p>Return the {@link Catalog} to look up the {@link Command} in.</p>
136      *
137      * @param context {@link Context} for this request
138      * @return The catalog.
139      * @exception IllegalArgumentException if no {@link Catalog}
140      *  can be found
141      *
142      * @since Chain 1.2
143      */
144     protected Catalog getCatalog(Context context) {
145         Catalog catalog = (Catalog) context.get(getCatalogKey());
146         if (catalog == null) {
147             catalog = super.getCatalog(context);
148         }
149         return catalog;
150     }
151 
152 
153 }