1 /*
2 * $Id: SelectAction.java 508312 2007-02-16 04:56:54Z pbenedict $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.struts.chain.commands.servlet;
22
23 import org.apache.struts.chain.Constants;
24 import org.apache.struts.chain.commands.AbstractSelectAction;
25 import org.apache.struts.chain.contexts.ActionContext;
26 import org.apache.struts.chain.contexts.ServletActionContext;
27 import org.apache.struts.config.ModuleConfig;
28
29 import javax.servlet.http.HttpServletRequest;
30
31 /**
32 * <p>Cache the <code>ActionConfig</code> instance for the action to be used
33 * for processing this request.</p>
34 *
35 * @version $Rev: 508312 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
36 * $
37 */
38 public class SelectAction extends AbstractSelectAction {
39 // ------------------------------------------------------- Protected Methods
40 protected String getPath(ActionContext context) {
41 ServletActionContext saContext = (ServletActionContext) context;
42 HttpServletRequest request = saContext.getRequest();
43 String path = null;
44 boolean extension = false;
45
46 // For prefix matching, match on the path info
47 path = (String) request.getAttribute(Constants.INCLUDE_PATH_INFO);
48
49 if ((path == null) || (path.length() == 0)) {
50 path = request.getPathInfo();
51 }
52
53 // For extension matching, match on the servlet path
54 if ((path == null) || (path.length() == 0)) {
55 path =
56 (String) request.getAttribute(Constants.INCLUDE_SERVLET_PATH);
57
58 if ((path == null) || (path.length() == 0)) {
59 path = request.getServletPath();
60 }
61
62 if ((path == null) || (path.length() == 0)) {
63 throw new IllegalArgumentException(
64 "No path information in request");
65 }
66
67 extension = true;
68 }
69
70 // Strip the module prefix and extension (if any)
71 ModuleConfig moduleConfig = saContext.getModuleConfig();
72 String prefix = moduleConfig.getPrefix();
73
74 if (!path.startsWith(prefix)) {
75 throw new IllegalArgumentException("Path does not start with '"
76 + prefix + "'");
77 }
78
79 path = path.substring(prefix.length());
80
81 if (extension) {
82 int slash = path.lastIndexOf("/");
83 int period = path.lastIndexOf(".");
84
85 if ((period >= 0) && (period > slash)) {
86 path = path.substring(0, period);
87 }
88 }
89
90 return (path);
91 }
92 }