View Javadoc

1   /*
2    * $Id: ModuleUtils.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.util;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.Globals;
23  import org.apache.struts.action.RequestProcessor;
24  import org.apache.struts.config.MessageResourcesConfig;
25  import org.apache.struts.config.ModuleConfig;
26  
27  import javax.servlet.ServletContext;
28  import javax.servlet.http.HttpServletRequest;
29  
30  /***
31   * General purpose utility methods related to module processing.
32   *
33   * @version $Rev: 421119 $
34   * @since Struts 1.2
35   */
36  public class ModuleUtils {
37      /***
38       * The Singleton instance.
39       */
40      private static final ModuleUtils instance = new ModuleUtils();
41  
42      /***
43       * Commons logging instance.
44       */
45      private static final Log log = LogFactory.getLog(ModuleUtils.class);
46  
47      /***
48       * Constructor for ModuleUtils.
49       */
50      protected ModuleUtils() {
51          super();
52      }
53  
54      /***
55       * Returns the Singleton instance of TagUtils.
56       */
57      public static ModuleUtils getInstance() {
58          return instance;
59      }
60  
61      /***
62       * Return the current ModuleConfig object stored in request, if it exists,
63       * null otherwise. This method can be used by plugin to retrieve the
64       * current module config object. If no moduleConfig is found, this means
65       * that the request haven't hit the server throught the struts servlet.
66       * The appropriate module config can be set and found with <code>{@link
67       * ModuleUtils#selectModule(HttpServletRequest, ServletContext)} </code>.
68       *
69       * @param request The servlet request we are processing
70       * @return the ModuleConfig object from request, or null if none is set in
71       *         the request.
72       */
73      public ModuleConfig getModuleConfig(HttpServletRequest request) {
74          return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
75      }
76  
77      /***
78       * Return the desired ModuleConfig object stored in context, if it exists,
79       * null otherwise.
80       *
81       * @param prefix  The module prefix of the desired module
82       * @param context The ServletContext for this web application
83       * @return the ModuleConfig object specified, or null if not found in the
84       *         context.
85       */
86      public ModuleConfig getModuleConfig(String prefix, ServletContext context) {
87          if ((prefix == null) || "/".equals(prefix)) {
88              return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY);
89          } else {
90              return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY
91                  + prefix);
92          }
93      }
94  
95      /***
96       * Return the desired ModuleConfig object stored in context, if it exists,
97       * otherwise return the current ModuleConfig
98       *
99       * @param prefix  The module prefix of the desired module
100      * @param request The servlet request we are processing
101      * @param context The ServletContext for this web application
102      * @return the ModuleConfig object specified, or null if not found in the
103      *         context.
104      */
105     public ModuleConfig getModuleConfig(String prefix,
106         HttpServletRequest request, ServletContext context) {
107         ModuleConfig moduleConfig = null;
108 
109         if (prefix != null) {
110             //lookup module stored with the given prefix.
111             moduleConfig = this.getModuleConfig(prefix, context);
112         } else {
113             //return the current module if no prefix was supplied.
114             moduleConfig = this.getModuleConfig(request, context);
115         }
116 
117         return moduleConfig;
118     }
119 
120     /***
121      * Return the ModuleConfig object is it exists, null otherwise.
122      *
123      * @param request The servlet request we are processing
124      * @param context The ServletContext for this web application
125      * @return the ModuleConfig object
126      */
127     public ModuleConfig getModuleConfig(HttpServletRequest request,
128         ServletContext context) {
129         ModuleConfig moduleConfig = this.getModuleConfig(request);
130 
131         if (moduleConfig == null) {
132             moduleConfig = this.getModuleConfig("", context);
133             request.setAttribute(Globals.MODULE_KEY, moduleConfig);
134         }
135 
136         return moduleConfig;
137     }
138 
139     /***
140      * Get the module name to which the specified request belong.
141      *
142      * @param request The servlet request we are processing
143      * @param context The ServletContext for this web application
144      * @return The module prefix or ""
145      */
146     public String getModuleName(HttpServletRequest request,
147         ServletContext context) {
148         // Acquire the path used to compute the module
149         String matchPath =
150             (String) request.getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
151 
152         if (matchPath == null) {
153             matchPath = request.getServletPath();
154         }
155 
156         return this.getModuleName(matchPath, context);
157     }
158 
159     /***
160      * Get the module name to which the specified uri belong.
161      *
162      * @param matchPath The uri from which we want the module name.
163      * @param context   The ServletContext for this web application
164      * @return The module prefix or ""
165      */
166     public String getModuleName(String matchPath, ServletContext context) {
167         if (log.isDebugEnabled()) {
168             log.debug("Get module name for path " + matchPath);
169         }
170 
171         String prefix = ""; // Initialize prefix before we try lookup
172         String[] prefixes = getModulePrefixes(context);
173 
174         // Get all other possible prefixes
175         int lastSlash = 0; // Initialize before loop
176 
177         while (prefix.equals("")
178             && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {
179             // We may be in a non-default module.  Try to get it's prefix.
180             matchPath = matchPath.substring(0, lastSlash);
181 
182             // Match against the list of module prefixes
183             for (int i = 0; i < prefixes.length; i++) {
184                 if (matchPath.equals(prefixes[i])) {
185                     prefix = prefixes[i];
186 
187                     break;
188                 }
189             }
190         }
191 
192         if (log.isDebugEnabled()) {
193             log.debug("Module name found: "
194                 + (prefix.equals("") ? "default" : prefix));
195         }
196 
197         return prefix;
198     }
199 
200     /***
201      * Return the list of module prefixes that are defined for this web
202      * application.  <strong>NOTE</strong> - the "" prefix for the default
203      * module is not included in this list.
204      *
205      * @param context The ServletContext for this web application.
206      * @return An array of module prefixes.
207      */
208     public String[] getModulePrefixes(ServletContext context) {
209         return (String[]) context.getAttribute(Globals.MODULE_PREFIXES_KEY);
210     }
211 
212     /***
213      * Select the module to which the specified request belongs, and add
214      * corresponding request attributes to this request.
215      *
216      * @param request The servlet request we are processing
217      * @param context The ServletContext for this web application
218      */
219     public void selectModule(HttpServletRequest request, ServletContext context) {
220         // Compute module name
221         String prefix = getModuleName(request, context);
222 
223         // Expose the resources for this module
224         this.selectModule(prefix, request, context);
225     }
226 
227     /***
228      * Select the module to which the specified request belongs, and add
229      * corresponding request attributes to this request.
230      *
231      * @param prefix  The module prefix of the desired module
232      * @param request The servlet request we are processing
233      * @param context The ServletContext for this web application
234      */
235     public void selectModule(String prefix, HttpServletRequest request,
236         ServletContext context) {
237         // Expose the resources for this module
238         ModuleConfig config = getModuleConfig(prefix, context);
239 
240         if (config != null) {
241             request.setAttribute(Globals.MODULE_KEY, config);
242 
243             MessageResourcesConfig[] mrConfig =
244                 config.findMessageResourcesConfigs();
245 
246             for (int i = 0; i < mrConfig.length; i++) {
247                 String key = mrConfig[i].getKey();
248                 MessageResources resources =
249                     (MessageResources) context.getAttribute(key + prefix);
250 
251                 if (resources != null) {
252                     request.setAttribute(key, resources);
253                 } else {
254                     request.removeAttribute(key);
255                 }
256             }
257         } else {
258             request.removeAttribute(Globals.MODULE_KEY);
259         }
260     }
261 }