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 the "path info" component of the request URI
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 URI "/execute/foo"
034 * would cause the "/foo" command to be loaded and executed.</p>
035 *
036 * @author Craig R. McClanahan
037 */
038
039 public class PathInfoMapper extends LookupCommand implements Command {
040
041
042 // ------------------------------------------------------ Instance Variables
043
044
045 private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
046
047
048 // -------------------------------------------------------------- Properties
049
050
051 /**
052 * <p>Return the context key under which our {@link Catalog} has been
053 * stored.</p>
054 *
055 * @return The context key for the Catalog.
056 *
057 * @deprecated Use catalogName to specify the name of the catalog in the
058 * catalog factory
059 */
060 public String getCatalogKey() {
061
062 return (this.catalogKey);
063
064 }
065
066
067 /**
068 * <p>Set the context key under which our {@link Catalog} has been
069 * stored.</p>
070 *
071 * @param catalogKey The new catalog key
072 *
073 * @deprecated Use catalogName to specify the name of the catalog in the
074 * catalog factory
075 */
076 public void setCatalogKey(String catalogKey) {
077
078 this.catalogKey = catalogKey;
079
080 }
081
082
083 // --------------------------------------------------------- Command Methods
084
085
086 /**
087 * <p>Look up the extra path information for this request, and use it to
088 * select an appropriate {@link Command} to be executed.
089 *
090 * @param context Context for the current request
091 * @return The name of the {@link Command} instance
092 *
093 * @since Chain 1.2
094 */
095 protected String getCommandName(Context context) {
096
097 // Look up the extra path information for this request
098 ServletWebContext swcontext = (ServletWebContext) context;
099 HttpServletRequest request = swcontext.getRequest();
100 String pathInfo = (String)
101 request.getAttribute("javax.servlet.include.path_info");
102 if (pathInfo == null) {
103 pathInfo = request.getPathInfo();
104 }
105
106 return pathInfo;
107
108 }
109
110 /**
111 * <p>Return the {@link Catalog} to look up the {@link Command} in.</p>
112 *
113 * @param context {@link Context} for this request
114 * @return The catalog.
115 * @exception IllegalArgumentException if no {@link Catalog}
116 * can be found
117 *
118 * @since Chain 1.2
119 */
120 protected Catalog getCatalog(Context context) {
121 Catalog catalog = (Catalog) context.get(getCatalogKey());
122 if (catalog == null) {
123 catalog = super.getCatalog(context);
124 }
125 return catalog;
126 }
127
128 }