View Javadoc

1   /*
2    * $Id: TilesUtilStrutsModulesImpl.java 421151 2006-07-12 06:07:14Z wsmoak $
3    *
4    * Copyright 1999-2004 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  
19  package org.apache.struts.tiles;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletContext;
24  import javax.servlet.ServletException;
25  import javax.servlet.ServletRequest;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts.Globals;
30  import org.apache.struts.config.ModuleConfig;
31  import org.apache.struts.util.ModuleUtils;
32  
33  /***
34   * Implementation of TilesUtil for Struts multi modules.
35   * Methods in this implementation are aware of the Struts module context.
36   * <br>
37   * <ul>
38   * <li>The method getFactory(...) returns the factory for the current Struts
39   * module.</li>
40   * <li>Methods doForward() and doInclude() use their counterparts in the
41   * current RequestProcessor (todo).</li>
42   * <li>The method createFactory(...) creates a factory for the current module and
43   * stores it under the appropriate property name.</li>
44   * </ul>
45   */
46  public class TilesUtilStrutsModulesImpl extends TilesUtilStrutsImpl {
47  
48      /***
49       * Do a forward using request dispatcher.
50       *
51       * This method is used by the Tiles package anytime a forward is required.
52       * @param uri Uri or Definition name to forward.
53       * @param request Current page request.
54       * @param response Current page response.
55       * @param servletContext Current servlet context.
56       */
57      public void doForward(
58          String uri,
59          HttpServletRequest request,
60          HttpServletResponse response,
61          ServletContext servletContext)
62          throws IOException, ServletException {
63  
64          request.getRequestDispatcher(uri).forward(request, response);
65      }
66  
67      /***
68       * Do an include using request dispatcher.
69       *
70       * This method is used by the Tiles package anytime an include is required.
71       * @param uri Uri or Definition name to forward.
72       * @param request Current page request.
73       * @param response Current page response.
74       * @param servletContext Current servlet context.
75       */
76      public void doInclude(
77          String uri,
78          HttpServletRequest request,
79          HttpServletResponse response,
80          ServletContext servletContext)
81          throws IOException, ServletException {
82  
83          request.getRequestDispatcher(uri).include(request, response);
84      }
85  
86      /***
87       * Get the definition factory from appropriate servlet context.
88       * @param request Current request.
89       * @param servletContext Current servlet context.
90       * @return Definitions factory or null if not found.
91       */
92      public DefinitionsFactory getDefinitionsFactory(
93          ServletRequest request,
94          ServletContext servletContext) {
95  
96          return getDefinitionsFactory(
97              servletContext,
98              getModuleConfig((HttpServletRequest) request, servletContext));
99      }
100 
101     /***
102      * Get definition factory for the module attached to specified moduleConfig.
103      * @param servletContext Current servlet context.
104      * @param moduleConfig Module config of the module for which the factory is requested.
105      * @return Definitions factory or null if not found.
106      */
107     public DefinitionsFactory getDefinitionsFactory(
108         ServletContext servletContext,
109         ModuleConfig moduleConfig) {
110 
111         return (DefinitionsFactory) servletContext.getAttribute(
112             DEFINITIONS_FACTORY + moduleConfig.getPrefix());
113     }
114 
115     /***
116      * Make definition factory accessible to tags.
117      * Factory is stored in servlet context.
118      * @param factory Factory to be made accessible.
119      * @param servletContext Current servlet context.
120      */
121     protected void makeDefinitionsFactoryAccessible(
122         DefinitionsFactory factory,
123         ServletContext servletContext) {
124 
125         String prefix = factory.getConfig().getFactoryName();
126         servletContext.setAttribute(DEFINITIONS_FACTORY + prefix, factory);
127     }
128 
129     /***
130      * Get Tiles RequestProcessor associated to the current module.
131      * @param request Current request.
132      * @param servletContext Current servlet context.
133      * @return The {@link TilesRequestProcessor} for the current request.
134      */
135     protected TilesRequestProcessor getRequestProcessor(
136         HttpServletRequest request,
137         ServletContext servletContext) {
138 
139         ModuleConfig moduleConfig = getModuleConfig(request, servletContext);
140 
141         return (TilesRequestProcessor) servletContext.getAttribute(
142             Globals.REQUEST_PROCESSOR_KEY + moduleConfig.getPrefix());
143     }
144 
145     /***
146      * Get the current ModuleConfig.
147      * <br>
148      * Lookup in the request and do selectModule if not found. The side effect
149      * is, that the ModuleConfig object is set in the request if it was not present.
150      * @param request Current request.
151      * @param servletContext Current servlet context*.
152      * @return The ModuleConfig for current request.
153      */
154     protected ModuleConfig getModuleConfig(
155         HttpServletRequest request,
156         ServletContext servletContext) {
157 
158         ModuleConfig moduleConfig =
159             ModuleUtils.getInstance().getModuleConfig(request);
160 
161         if (moduleConfig == null) {
162             // ModuleConfig not found in current request. Select it.
163             ModuleUtils.getInstance().selectModule(request, servletContext);
164             moduleConfig = ModuleUtils.getInstance().getModuleConfig(request);
165         }
166 
167         return moduleConfig;
168     }
169 
170 }