001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.chain.web.servlet;
018
019
020 import javax.servlet.http.HttpServletRequest;
021 import org.apache.commons.chain.Catalog;
022 import org.apache.commons.chain.Context;
023 import org.apache.commons.chain.Command;
024 import org.apache.commons.chain.generic.LookupCommand;
025
026
027 /**
028 * <p>{@link Command} that uses a specified request parameter
029 * to select a {@link Command} from the appropriate {@link Catalog}, and
030 * execute it. To use this command, you would typically map an instance
031 * of {@link ChainProcessor} to a wildcard pattern like "*.execute" and
032 * then arrange that this is the default command to be executed. In such
033 * an environment, a request for the context-relative path
034 * "/foo.execute?command=bar" would cause the "/bar" command to be loaded
035 * and executed.</p>
036 *
037 * @author Craig R. McClanahan
038 */
039
040 public class RequestParameterMapper extends LookupCommand implements Command {
041
042
043 // ------------------------------------------------------ Instance Variables
044
045
046 private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
047 private String parameter = "command";
048
049
050 // -------------------------------------------------------------- Properties
051
052
053 /**
054 * <p>Return the context key under which our {@link Catalog} has been
055 * stored.</p>
056 *
057 * @return The context key for the Catalog.
058 */
059 public String getCatalogKey() {
060
061 return (this.catalogKey);
062
063 }
064
065
066 /**
067 * <p>Set the context key under which our {@link Catalog} has been
068 * stored.</p>
069 *
070 * @param catalogKey The new catalog key
071 *
072 * @deprecated Use catalogName to specify the name of the catalog in the
073 * catalog factory
074 */
075 public void setCatalogKey(String catalogKey) {
076
077 this.catalogKey = catalogKey;
078
079 }
080
081
082 /**
083 * <p>Return the name of the request parameter to use for
084 * selecting the {@link Command} to be executed.</p>
085 *
086 * @return The name of the request parameter.
087 *
088 * @deprecated Use catalogName to specify the name of the catalog in the
089 * catalog factory
090 */
091 public String getParameter() {
092
093 return (this.parameter);
094
095 }
096
097
098 /**
099 * <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 }