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 the "servlet path" component of the request URI
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 an extension pattern like "*.execute" and
32   * then arrange that this is the default command to be executed.  In such
33   * an environment, a request for a context relative URI of "/foo.execute"
34   * would cause the "/foo.execute" command to be loaded and executed.</p>
35   *
36   * @author Craig R. McClanahan
37   */
38  
39  public class ServletPathMapper extends LookupCommand implements Command {
40  
41  
42      // ------------------------------------------------------ Instance Variables
43  
44  
45      private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
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       * @return The context key for the Catalog.
56       *
57       * @deprecated Use catalogName to specify the name of the catalog in the
58       *  catalog factory
59       */
60      public String getCatalogKey() {
61  
62          return (this.catalogKey);
63  
64      }
65  
66  
67      /**
68       * <p>Set the context key under which our {@link Catalog} has been
69       * stored.</p>
70       *
71       * @param catalogKey The new catalog key
72       *
73       * @deprecated Use catalogName to specify the name of the catalog in the
74       *  catalog factory
75       */
76      public void setCatalogKey(String catalogKey) {
77  
78          this.catalogKey = catalogKey;
79  
80      }
81  
82  
83      // --------------------------------------------------------- Command Methods
84  
85  
86      /**
87       * <p>Look up the servlet path information for this request, and use it to
88       * select an appropriate {@link Command} to be executed.
89       *
90       * @param context Context for the current request
91       * @return The name of the {@link Command} instance
92       *
93       * @since Chain 1.2
94       */
95      protected String getCommandName(Context context) {
96  
97          // Look up the servlet path for this request
98          ServletWebContext swcontext = (ServletWebContext) context;
99          HttpServletRequest request = swcontext.getRequest();
100         String servletPath = (String)
101             request.getAttribute("javax.servlet.include.servlet_path");
102         if (servletPath == null) {
103             servletPath = request.getServletPath();
104         }
105 
106         return servletPath;
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 }