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.Globals;
19  import org.apache.struts.chain.Constants;
20  import org.apache.struts.chain.commands.AbstractSelectModule;
21  import org.apache.struts.chain.contexts.ActionContext;
22  import org.apache.struts.chain.contexts.ServletActionContext;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  /***
27   * <p>Cache the <code>ModuleConfig</code> and <code>MessageResources</code>
28   * instances for the sub-application module to be used for processing this
29   * request.</p>
30   *
31   * @version $Rev: 421119 $ $Date: 2005-06-04 10:58:46 -0400 (Sat, 04 Jun 2005)
32   *          $
33   */
34  public class SelectModule extends AbstractSelectModule {
35      // ------------------------------------------------------- Protected Methods
36      protected String getPrefix(ActionContext context) {
37          // Identify the URI from which we will match a module prefix
38          ServletActionContext sacontext = (ServletActionContext) context;
39          HttpServletRequest request = sacontext.getRequest();
40          String uri =
41              (String) request.getAttribute(Constants.INCLUDE_SERVLET_PATH);
42  
43          if (uri == null) {
44              uri = request.getServletPath();
45          }
46  
47          if (uri == null) {
48              throw new IllegalArgumentException("No path information in request");
49          }
50  
51          // Identify the module prefix for the current module
52          String prefix = ""; // Initialize to default prefix
53          String[] prefixes =
54              (String[]) sacontext.getApplicationScope().get(Globals.MODULE_PREFIXES_KEY);
55          int lastSlash = 0;
56  
57          while (prefix.equals("") && ((lastSlash = uri.lastIndexOf("/")) > 0)) {
58              uri = uri.substring(0, lastSlash);
59  
60              for (int i = 0; i < prefixes.length; i++) {
61                  if (uri.equals(prefixes[i])) {
62                      prefix = prefixes[i];
63  
64                      break;
65                  }
66              }
67          }
68  
69          return (prefix);
70      }
71  }