View Javadoc

1   /*
2    * Copyright 2003,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.struts.chain.commands.servlet;
17  
18  import org.apache.struts.chain.Constants;
19  import org.apache.struts.chain.commands.AbstractSelectAction;
20  import org.apache.struts.chain.contexts.ActionContext;
21  import org.apache.struts.chain.contexts.ServletActionContext;
22  import org.apache.struts.config.ModuleConfig;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  /***
27   * <p>Cache the <code>ActionConfig</code> instance for the action to be used
28   * for processing this request.</p>
29   *
30   * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
31   *          $
32   */
33  public class SelectAction extends AbstractSelectAction {
34      // ------------------------------------------------------- Protected Methods
35      protected String getPath(ActionContext context) {
36          ServletActionContext saContext = (ServletActionContext) context;
37          HttpServletRequest request = saContext.getRequest();
38          String path = null;
39          boolean extension = false;
40  
41          // For prefix matching, match on the path info
42          path = (String) request.getAttribute(Constants.INCLUDE_PATH_INFO);
43  
44          if (path == null) {
45              path = request.getPathInfo();
46          }
47  
48          // For extension matching, match on the servlet path
49          if (path == null) {
50              path =
51                  (String) request.getAttribute(Constants.INCLUDE_SERVLET_PATH);
52  
53              if (path == null) {
54                  path = request.getServletPath();
55              }
56  
57              if (path == null) {
58                  throw new IllegalArgumentException(
59                      "No path information in request");
60              }
61  
62              extension = true;
63          }
64  
65          // Strip the module prefix and extension (if any)
66          ModuleConfig moduleConfig = saContext.getModuleConfig();
67          String prefix = moduleConfig.getPrefix();
68  
69          if (!path.startsWith(prefix)) {
70              throw new IllegalArgumentException("Path does not start with '"
71                  + prefix + "'");
72          }
73  
74          path = path.substring(prefix.length());
75  
76          if (extension) {
77              int slash = path.lastIndexOf("/");
78              int period = path.lastIndexOf(".");
79  
80              if ((period >= 0) && (period > slash)) {
81                  path = path.substring(0, period);
82              }
83          }
84  
85          return (path);
86      }
87  }