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.jakarta.servlet; 18 19 import org.apache.commons.chain.Catalog; 20 import org.apache.commons.chain.Command; 21 import org.apache.commons.chain.Context; 22 import org.apache.commons.chain.generic.LookupCommand; 23 24 import jakarta.servlet.http.HttpServletRequest; 25 26 /** 27 * {@link Command} that uses a specified request parameter 28 * to select a {@link Command} from the appropriate {@link Catalog}, and 29 * execute it. To use this command, you would typically map an instance 30 * of {@link ChainProcessor} to a wildcard pattern like "*.execute" and 31 * then arrange that this is the default command to be executed. In such 32 * an environment, a request for the context-relative path 33 * "/foo.execute?command=bar" would cause the "/bar" command to be loaded 34 * and executed. 35 * 36 * @author Craig R. McClanahan 37 */ 38 public class RequestParameterMapper extends LookupCommand<ServletWebContext> { 39 40 // ------------------------------------------------------ Instance Variables 41 42 private String catalogKey = ChainProcessor.CATALOG_DEFAULT; 43 private String parameter = "command"; 44 45 // ------------------------------------------------------------ Constructors 46 47 /** 48 * The Default-Constructor for this class. 49 */ 50 public RequestParameterMapper() { 51 } 52 53 // -------------------------------------------------------------- Properties 54 55 /** 56 * Return the context key under which our {@link Catalog} has been 57 * stored. 58 * 59 * @return The context key for the Catalog. 60 */ 61 public String getCatalogKey() { 62 return this.catalogKey; 63 } 64 65 /** 66 * Set the context key under which our {@link Catalog} has been 67 * stored. 68 * 69 * @param catalogKey The new catalog key 70 * 71 * @deprecated Use catalogName to specify the name of the catalog in the 72 * catalog factory 73 */ 74 @Deprecated 75 public void setCatalogKey(String catalogKey) { 76 this.catalogKey = catalogKey; 77 } 78 79 /** 80 * Return the name of the request parameter to use for 81 * selecting the {@link Command} to be executed. 82 * 83 * @return The name of the request parameter. 84 * 85 * @deprecated Use catalogName to specify the name of the catalog in the 86 * catalog factory 87 */ 88 @Deprecated 89 public String getParameter() { 90 return this.parameter; 91 } 92 93 /** 94 * Set the name of the request parameter to use for 95 * selecting the {@link Command} to be executed. 96 * 97 * @param parameter The new parameter name 98 */ 99 public void setParameter(String parameter) { 100 this.parameter = parameter; 101 } 102 103 // --------------------------------------------------------- Command Methods 104 105 /** 106 * Look up the specified request parameter for this request, and use it 107 * to select an appropriate {@link Command} to be executed. 108 * 109 * @param context Context for the current request 110 * 111 * @return The name of the {@link Command} instance 112 * 113 * @since Chain 1.2 114 */ 115 @Override 116 protected String getCommandName(ServletWebContext context) { 117 // Look up the specified request parameter for this request 118 HttpServletRequest request = context.getRequest(); 119 String value = request.getParameter(getParameter()); 120 return value; 121 } 122 123 /** 124 * Return the {@link Catalog} to look up the {@link Command} in. 125 * 126 * @param context {@link Context} for this request 127 * 128 * @return The catalog. 129 * 130 * @throws IllegalArgumentException if no {@link Catalog} 131 * can be found 132 * 133 * @since Chain 1.2 134 */ 135 @Override 136 protected Catalog<ServletWebContext> getCatalog(ServletWebContext context) { 137 /* If the object returned from the passed context is not a valid catalog 138 * then we use the super class's catalog extraction logic to pull it 139 * or to error gracefully. 140 */ 141 Object testCatalog = context.get(getCatalogKey()); 142 143 /* Assume that the underlying implementation is following convention and 144 * returning a catalog with the current context. 145 */ 146 @SuppressWarnings("unchecked") 147 Catalog<ServletWebContext> catalog = testCatalog instanceof Catalog 148 ? (Catalog<ServletWebContext>) testCatalog 149 : super.getCatalog(context); 150 151 return catalog; 152 } 153 }