View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.web.servlet;
17  
18  
19  import javax.servlet.http.HttpServletRequest;
20  import org.apache.commons.chain.Catalog;
21  import org.apache.commons.chain.Command;
22  import org.apache.commons.chain.Context;
23  
24  
25  /***
26   * <p>{@link Command} that uses the "path info" component of the request URI
27   * to select a {@link Command} from the appropriate {@link Catalog}, and
28   * execute it.  To use this command, you would typically map an instance
29   * of {@link ChainProcessor} to a wildcard pattern like "/execute/*" and
30   * then arrange that this is the default command to be executed.  In such
31   * an environment, a request for the context-relative URI "/execute/foo"
32   * would cause the "/foo" command to be loaded and executed.</p>
33   *
34   * @author Craig R. McClanahan
35   */
36  
37  public class PathInfoMapper implements Command {
38  
39  
40      // ------------------------------------------------------ Instance Variables
41  
42  
43      private String catalogKey = ChainProcessor.CATALOG_DEFAULT;
44  
45  
46      // -------------------------------------------------------------- Properties
47  
48  
49      /***
50       * <p>Return the context key under which our {@link Catalog} has been
51       * stored.</p>
52       *
53       * @return The context key for the Catalog.
54       */
55      public String getCatalogKey() {
56  
57          return (this.catalogKey);
58  
59      }
60  
61  
62      /***
63       * <p>Set the context key under which our {@link Catalog} has been
64       * stored.</p>
65       *
66       * @param catalogKey The new catalog key
67       */
68      public void setCatalogKey(String catalogKey) {
69  
70          this.catalogKey = catalogKey;
71  
72      }
73  
74  
75      // --------------------------------------------------------- Command Methods
76  
77  
78      /***
79       * <p>Look up the extra path information for this request, and use it to
80       * select an appropriate {@link Command} to be executed.
81       *
82       * @param context Context for the current request
83       * @return The result of executing the Command for the request URI.
84       * @throws Exception if there is a problem executing the Command for
85       *  the request URI.
86       */
87      public boolean execute(Context context) throws Exception {
88  
89          // Look up the extra path information for this request
90          ServletWebContext swcontext = (ServletWebContext) context;
91          HttpServletRequest request = swcontext.getRequest();
92          String pathInfo = (String)
93              request.getAttribute("javax.servlet.include.path_info");
94          if (pathInfo == null) {
95              pathInfo = request.getPathInfo();
96          }
97  
98          // Map to the Command specified by the extra path info
99          Catalog catalog = (Catalog) context.get(getCatalogKey());
100         Command command = catalog.getCommand(pathInfo);
101         return (command.execute(context));
102 
103     }
104 
105 
106 }