KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > application > jsp > JspTilesViewHandlerImpl


1 /*
2  * Copyright 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.myfaces.application.jsp;
17
18 import org.apache.myfaces.webapp.webxml.ServletMapping;
19 import org.apache.myfaces.webapp.webxml.WebXml;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.struts.tiles.*;
24
25 import javax.faces.FacesException;
26 import javax.faces.application.ViewHandler;
27 import javax.faces.component.UIViewRoot;
28 import javax.faces.context.ExternalContext;
29 import javax.faces.context.FacesContext;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletRequest JavaDoc;
32 import javax.servlet.ServletResponse JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Locale JavaDoc;
39
40 /**
41  * @author Thomas Spiegl (latest modification by $Author: manolito $)
42  * @version $Revision: 1.12 $ $Date: 2004/12/22 08:12:36 $
43  * $Log: JspTilesViewHandlerImpl.java,v $
44  * Revision 1.12 2004/12/22 08:12:36 manolito
45  * Use lastIndexOf instead of indexOf as suggested by sean.schofield@gmail.com on mailing list
46  *
47  * Revision 1.11 2004/12/19 11:42:36 tomsp
48  * removed Nullpointer from renderView when no tiles-definition is found
49  *
50  * Revision 1.10 2004/11/11 17:43:07 tomsp
51  * no message
52  *
53  * Revision 1.9 2004/11/11 14:50:38 bdudney
54  * made getInitParameter use the defined constant instead of the string
55  *
56  * Revision 1.8 2004/11/08 09:09:42 tomsp
57  * no message
58  *
59  * Revision 1.7 2004/11/08 08:46:45 tomsp
60  * no message
61  *
62  * Revision 1.6 2004/10/13 11:50:56 matze
63  * renamed packages to org.apache
64  *
65  * Revision 1.5 2004/10/04 11:11:46 royalts
66  * removed check on urlPattern
67  *
68  * Revision 1.4 2004/08/26 15:34:06 manolito
69  * trivial cosmetic changes
70  *
71  * Revision 1.3 2004/08/26 14:25:21 manolito
72  * JspTilesViewHandlerInitializer no longer needed, JspTilesViewHandlerImpl is initialized automatically now
73  *
74  * Revision 1.2 2004/07/19 08:18:19 royalts
75  * moved org.apache.myfaces.webapp.webxml and org.apache.util.xml to share src-tree (needed WebXml for JspTilesViewHandlerImpl)
76  *
77  * Revision 1.1 2004/07/16 17:46:46 royalts
78  * moved org.apache.myfaces.webapp.webxml and org.apache.util.xml to share src-tree (needed WebXml for JspTilesViewHandlerImpl)
79  *
80  */

81 public class JspTilesViewHandlerImpl
82     extends ViewHandler
83 {
84     private ViewHandler _viewHandler;
85
86     private static final Log log = LogFactory.getLog(JspTilesViewHandlerImpl.class);
87     private static final String JavaDoc TILES_EXTENSION = ".tiles";
88     private static final String JavaDoc TILES_DEF_ATTR = "tiles-definitions";
89
90     private DefinitionsFactory _definitionsFactory;
91
92     public JspTilesViewHandlerImpl(ViewHandler viewHandler)
93     {
94         _viewHandler = viewHandler;
95     }
96
97     private DefinitionsFactory getDefinitionsFactory()
98     {
99         if (_definitionsFactory == null)
100         {
101             if (log.isDebugEnabled()) log.debug("JspTilesViewHandlerImpl init");
102             ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
103             String JavaDoc tilesDefinitions = context.getInitParameter(TILES_DEF_ATTR);
104             if (tilesDefinitions == null)
105             {
106                 log.fatal("No Tiles definition found. Specify Definition files by adding "
107                           + TILES_DEF_ATTR + " param in your web.xml");
108                 return null;
109             }
110
111             DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
112             factoryConfig.setDefinitionConfigFiles(tilesDefinitions);
113             try
114             {
115                 if (log.isDebugEnabled()) log.debug("Reading tiles definitions");
116                 _definitionsFactory = TilesUtil.createDefinitionsFactory((ServletContext JavaDoc)context.getContext(),
117                                                                         factoryConfig);
118             }
119             catch (DefinitionsFactoryException e)
120             {
121                 log.fatal("Error reading tiles definitions", e);
122                 return null;
123             }
124         }
125         return _definitionsFactory;
126     }
127
128     public void renderView(FacesContext facesContext, UIViewRoot viewToRender) throws IOException JavaDoc, FacesException
129     {
130         if (viewToRender == null)
131         {
132             log.fatal("viewToRender must not be null");
133             throw new NullPointerException JavaDoc("viewToRender must not be null");
134         }
135
136         ExternalContext externalContext = facesContext.getExternalContext();
137
138         String JavaDoc viewId = facesContext.getViewRoot().getViewId();
139         ServletMapping servletMapping = getServletMapping(externalContext);
140
141         String JavaDoc defaultSuffix = externalContext.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME);
142         String JavaDoc suffix = defaultSuffix != null ? defaultSuffix : ViewHandler.DEFAULT_SUFFIX;
143         if (servletMapping.isExtensionMapping())
144         {
145             if (!viewId.endsWith(suffix))
146             {
147                 int dot = viewId.lastIndexOf('.');
148                 if (dot == -1)
149                 {
150                     if (log.isTraceEnabled()) log.trace("Current viewId has no extension, appending default suffix " + suffix);
151                     viewId = viewId + suffix;
152                 }
153                 else
154                 {
155                     if (log.isTraceEnabled()) log.trace("Replacing extension of current viewId by suffix " + suffix);
156                     viewId = viewId.substring(0, dot) + suffix;
157                 }
158                 facesContext.getViewRoot().setViewId(viewId);
159             }
160         }
161         else if (!viewId.endsWith(suffix))
162         {
163             // path-mapping used, but suffix is no default-suffix
164
dispatch(externalContext, viewToRender, viewId);
165             return;
166         }
167
168         String JavaDoc tilesId = viewId;
169         int idx = tilesId.lastIndexOf('.');
170         if (idx > 0)
171         {
172             tilesId = tilesId.substring(0, idx) + TILES_EXTENSION;
173         }
174         else
175         {
176             tilesId = tilesId + TILES_EXTENSION;
177         }
178         ServletRequest JavaDoc request = (ServletRequest JavaDoc)externalContext.getRequest();
179         ServletContext JavaDoc servletContext = (ServletContext JavaDoc)externalContext.getContext();
180
181         ComponentDefinition definition = null;
182         try
183         {
184             definition = getDefinitionsFactory().getDefinition(tilesId, request, servletContext);
185             if (definition != null)
186             {
187                 // if tiles-definition could be found set ComponentContext & viewId
188
ComponentContext tileContext = ComponentContext.getContext(request);
189                 if (tileContext == null)
190                 {
191                     tileContext = new ComponentContext(definition.getAttributes());
192                     ComponentContext.setContext(tileContext, request);
193                 }
194                 else
195                 {
196                     tileContext.addMissing(definition.getAttributes());
197                 }
198                 viewId = definition.getPage();
199             }
200         }
201         catch (DefinitionsFactoryException e)
202         {
203             throw new FacesException(e);
204         }
205
206         dispatch(externalContext, viewToRender, viewId);
207     }
208
209     private void dispatch(ExternalContext externalContext, UIViewRoot viewToRender, String JavaDoc viewId)
210         throws IOException JavaDoc
211     {
212         if (log.isTraceEnabled()) log.trace("Dispatching to " + viewId);
213
214         // handle character encoding as of section 2.5.2.2 of JSF 1.1
215
if (externalContext.getResponse() instanceof ServletResponse JavaDoc) {
216             ServletResponse JavaDoc response = (ServletResponse JavaDoc) externalContext.getResponse();
217             response.setLocale(viewToRender.getLocale());
218         }
219
220         externalContext.dispatch(viewId);
221
222         // handle character encoding as of section 2.5.2.2 of JSF 1.1
223
if (externalContext.getRequest() instanceof HttpServletRequest JavaDoc) {
224             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) externalContext.getResponse();
225             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) externalContext.getRequest();
226             HttpSession JavaDoc session = request.getSession(false);
227
228             if (session != null) {
229                 session.setAttribute(ViewHandler.CHARACTER_ENCODING_KEY, response.getCharacterEncoding());
230             }
231         }
232
233
234     }
235
236     private static ServletMapping getServletMapping(ExternalContext externalContext)
237     {
238         String JavaDoc servletPath = externalContext.getRequestServletPath();
239         String JavaDoc requestPathInfo = externalContext.getRequestPathInfo();
240
241         WebXml webxml = WebXml.getWebXml(externalContext);
242         List JavaDoc mappings = webxml.getFacesServletMappings();
243
244         boolean isExtensionMapping = requestPathInfo == null;
245
246         for (int i = 0, size = mappings.size(); i < size; i++)
247         {
248             ServletMapping servletMapping = (ServletMapping) mappings.get(i);
249             if (servletMapping.isExtensionMapping() == isExtensionMapping)
250             {
251                 String JavaDoc urlpattern = servletMapping.getUrlPattern();
252                 if (isExtensionMapping)
253                 {
254                     String JavaDoc extension = urlpattern.substring(1, urlpattern.length());
255                     if (servletPath.endsWith(extension))
256                     {
257                         return servletMapping;
258                     }
259                 }
260                 else
261                 {
262                     urlpattern = urlpattern.substring(0, urlpattern.length() - 2);
263                     // servletPath starts with "/" except in the case where the
264
// request is matched with the "/*" pattern, in which case
265
// it is the empty string (see Servlet Sepc 2.3 SRV4.4)
266
if (servletPath.equals(urlpattern))
267                     {
268                         return servletMapping;
269                     }
270                 }
271             }
272         }
273         log.error("could not find pathMapping for servletPath = " + servletPath +
274                   " requestPathInfo = " + requestPathInfo);
275         throw new IllegalArgumentException JavaDoc("could not find pathMapping for servletPath = " + servletPath +
276                   " requestPathInfo = " + requestPathInfo);
277     }
278
279
280     public Locale JavaDoc calculateLocale(FacesContext context)
281     {
282         return _viewHandler.calculateLocale(context);
283     }
284
285     public String JavaDoc calculateRenderKitId(FacesContext context)
286     {
287         return _viewHandler.calculateRenderKitId(context);
288     }
289
290     public UIViewRoot createView(FacesContext context, String JavaDoc viewId)
291     {
292         return _viewHandler.createView(context, viewId);
293     }
294
295     public String JavaDoc getActionURL(FacesContext context, String JavaDoc viewId)
296     {
297         return _viewHandler.getActionURL(context, viewId);
298     }
299
300     public String JavaDoc getResourceURL(FacesContext context, String JavaDoc path)
301     {
302         return _viewHandler.getResourceURL(context, path);
303     }
304
305     public UIViewRoot restoreView(FacesContext context, String JavaDoc viewId)
306     {
307         return _viewHandler.restoreView(context, viewId);
308     }
309
310     public void writeState(FacesContext context) throws IOException JavaDoc
311     {
312         _viewHandler.writeState(context);
313     }
314
315 }
316
Popular Tags