KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > region > Section


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

26 package org.ofbiz.webapp.region;
27
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.jsp.JspException JavaDoc;
33 import javax.servlet.jsp.PageContext JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.GeneralException;
37 import org.ofbiz.base.util.UtilJ2eeCompat;
38 import org.ofbiz.webapp.control.RequestHandler;
39 import org.ofbiz.webapp.view.JPublishWrapper;
40 import org.ofbiz.webapp.view.ViewHandler;
41 import org.ofbiz.webapp.view.ViewHandlerException;
42
43 /**
44  * A section is content with a name that implements Content.render.
45  * <p>That method renders content either by including
46  * it or by printing it directly, depending upon the direct
47  * value passed to the Section constructor.</p>
48  *
49  * <p>Note that a section's content can also be a region;if so,
50  * Region.render is called from Section.Render().</p>
51  *
52  * @author David M. Geary in the book "Advanced Java Server Pages"
53  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
54  * @version $Rev: 5462 $
55  * @since 2.0
56  */

57 public class Section extends Content {
58
59     protected final String JavaDoc name;
60     protected final String JavaDoc info;
61     protected RegionManager regionManager;
62     
63     public final static String JavaDoc module = Section.class.getName();
64
65     public Section(String JavaDoc name, String JavaDoc info, String JavaDoc content, String JavaDoc type, RegionManager regionManager) {
66         super(content, type);
67         this.name = name;
68         this.info = info;
69         this.regionManager = regionManager;
70     }
71
72     public String JavaDoc getName() {
73         return name;
74     }
75
76     public void render(PageContext JavaDoc pageContext) throws JspException JavaDoc {
77         try {
78             if (UtilJ2eeCompat.doFlushOnRender(pageContext.getServletContext())) {
79                 pageContext.getOut().flush();
80             }
81             render((HttpServletRequest JavaDoc) pageContext.getRequest(), (HttpServletResponse JavaDoc) pageContext.getResponse());
82         } catch (java.io.IOException JavaDoc e) {
83             Debug.logError(e, "Error rendering section: ", module);
84             if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext()))
85                 throw new JspException JavaDoc(e);
86             else
87                 throw new JspException JavaDoc(e.toString());
88         } catch (ServletException JavaDoc e) {
89             Throwable JavaDoc throwable = e.getRootCause() != null ? e.getRootCause() : e;
90
91             Debug.logError(throwable, "Error rendering section: ", module);
92             if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext()))
93                 throw new JspException JavaDoc(throwable);
94             else
95                 throw new JspException JavaDoc(throwable.toString());
96         }
97     }
98
99     public void render(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws java.io.IOException JavaDoc, ServletException JavaDoc {
100         ServletContext JavaDoc context = (ServletContext JavaDoc) request.getAttribute("servletContext");
101         boolean verboseOn = Debug.verboseOn();
102
103         if (verboseOn) Debug.logVerbose("Rendering " + this.toString(), module);
104
105         // long viewStartTime = System.currentTimeMillis();
106
if (content != null) {
107             if ("direct".equals(type)) {
108                 if (UtilJ2eeCompat.useOutputStreamNotWriter(context)) {
109                     response.getOutputStream().print(content);
110                 } else {
111                     response.getWriter().print(content);
112                 }
113             } else if ("default".equals(type) || "region".equals(type) || "resource".equals(type) || "jpublish".equals(type)) {
114                 // if type is resource then we won't even look up the region
115

116                 // if this is default or region, check to see if the content points to a valid region name
117
Region region = null;
118
119                 if ("default".equals(type) || "region".equals(type)) {
120                     region = regionManager.getRegion(content);
121                 }
122
123                 if ("region".equals(type) || region != null) {
124                     if (region == null) {
125                         throw new IllegalArgumentException JavaDoc("No region definition found with name: " + content);
126                     }
127                     // render the content as a region
128
RegionStack.push(request, region);
129                     region.render(request, response);
130                     RegionStack.pop(request);
131                 } else if ("jpublish".equals(type)) {
132                     // rather then using the view handler use the wrapper directly
133
ServletContext JavaDoc sctx = (ServletContext JavaDoc) request.getAttribute("servletContext");
134                     if (sctx != null) {
135                         JPublishWrapper jp = this.getJPublishWrapper(sctx);
136                         if (jp != null) {
137                             String JavaDoc contentStr = "<!-- " + content + " Not Processed -->";
138                             try {
139                                 contentStr = jp.render(content, request, response);
140                             } catch (GeneralException e) {
141                                 Debug.logError(e, "Problems rendering view from JPublish", module);
142                             }
143                             if (UtilJ2eeCompat.useOutputStreamNotWriter(context)) {
144                                 response.getOutputStream().print(contentStr);
145                             } else {
146                                 response.getWriter().print(contentStr);
147                             }
148                         } else {
149                             throw new IllegalStateException JavaDoc("No jpublishWrapper found in ServletContext");
150                         }
151                     } else {
152                         throw new IllegalStateException JavaDoc("No servletContext found in request");
153                     }
154                 } else {
155                     // default is the string that the ViewFactory expects for webapp resources
156
viewHandlerRender("default", request, response);
157                 }
158             } else {
159                 viewHandlerRender(type, request, response);
160             }
161         }
162         if (verboseOn) Debug.logVerbose("DONE Rendering " + this.toString(), module);
163     }
164
165     protected void viewHandlerRender(String JavaDoc typeToUse, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
166         ServletContext JavaDoc context = (ServletContext JavaDoc) request.getAttribute("servletContext");
167         RequestHandler requestHandler = (RequestHandler) context.getAttribute("_REQUEST_HANDLER_");
168
169         // see if the type is defined in the controller.xml file
170
try {
171             if (Debug.verboseOn()) Debug.logVerbose("Rendering view [" + content + "] of type [" + typeToUse + "]", module);
172             ViewHandler vh = requestHandler.getViewFactory().getViewHandler(typeToUse);
173             // use the default content-type and encoding for the ViewHandler -- may want to change this.
174
vh.render(name, content, info, null, null, request, response);
175         } catch (ViewHandlerException e) {
176             throw new ServletException JavaDoc(e.getNonNestedMessage(), e.getNested());
177         }
178     }
179
180     protected JPublishWrapper getJPublishWrapper(ServletContext JavaDoc ctx) {
181         JPublishWrapper wrapper = (JPublishWrapper) ctx.getAttribute("jpublishWrapper");
182         if (wrapper == null) {
183             wrapper = new JPublishWrapper(ctx);
184         }
185         return wrapper;
186     }
187
188     public String JavaDoc toString() {
189         return "Section: " + name + ", info=" + info + ", content=" + content + ", type=" + type;
190     }
191 }
192
Popular Tags