KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > RegionViewHandler


1 /*
2  * $Id: RegionViewHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.view;
26
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.jsp.JspException JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.webapp.control.ContextFilter;
37 import org.ofbiz.webapp.region.Region;
38 import org.ofbiz.webapp.region.RegionManager;
39 import org.ofbiz.webapp.region.RegionStack;
40
41 /**
42  * Handles Region type view rendering
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
46  * @version $Rev: 5462 $
47  * @since 2.0
48  */

49 public class RegionViewHandler implements ViewHandler {
50     
51     public static final String JavaDoc module = RegionViewHandler.class.getName();
52
53     protected ServletContext JavaDoc context;
54     protected RegionManager regionManager = null;
55
56     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
57         this.context = context;
58
59         URL JavaDoc regionFile = null;
60         try {
61             regionFile = context.getResource("/WEB-INF/regions.xml");
62         } catch (java.net.MalformedURLException JavaDoc e) {
63             throw new IllegalArgumentException JavaDoc("regions.xml file URL invalid: " + e.getMessage());
64         }
65
66         if (regionFile == null) {
67             Debug.logWarning("No " + "/WEB-INF/regions.xml" + " file found in this webapp", module);
68         } else {
69             Debug.logVerbose("Loading regions from XML file in: " + regionFile, module);
70             regionManager = new RegionManager(regionFile);
71         }
72     }
73
74     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
75         // some containers call filters on EVERY request, even forwarded ones,
76
// so let it know that it came from the control servlet
77

78         if (request == null) {
79             throw new ViewHandlerException("The HttpServletRequest object was null, how did that happen?");
80         }
81         if (page == null || page.length() == 0) {
82             throw new ViewHandlerException("Page name was null or empty, but must be specified");
83         }
84
85         // tell the ContextFilter we are forwarding
86
request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean JavaDoc(true));
87
88         Region region = regionManager.getRegion(page);
89         if (region == null) {
90             throw new ViewHandlerException("Error: could not find region with name " + page);
91         }
92
93         try {
94             // this render method does not come from a page tag so some setup needs to happen here
95
RegionStack.push(request, region);
96
97             region.render(request, response);
98         } catch (IOException JavaDoc ie) {
99             throw new ViewHandlerException("IO Error in region", ie);
100         } catch (ServletException JavaDoc e) {
101             Throwable JavaDoc throwable = e.getRootCause() != null ? e.getRootCause() : e;
102
103             if (throwable instanceof JspException JavaDoc) {
104                 JspException JavaDoc jspe = (JspException JavaDoc) throwable;
105
106                 throwable = jspe.getRootCause() != null ? jspe.getRootCause() : jspe;
107             }
108             Debug.logError(throwable, "ServletException rendering JSP view", module);
109             throw new ViewHandlerException(e.getMessage(), throwable);
110         }
111         RegionStack.pop(request);
112     }
113 }
114
Popular Tags