001: /*
002: * $Id: Section.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 Sun Microsystems Inc., published in "Advanced Java Server Pages" by Prentice Hall PTR
005: * Copyright (c) 2001-2002 The Open For Business Project - www.ofbiz.org
006: *
007: * Permission is hereby granted, free of charge, to any person obtaining a
008: * copy of this software and associated documentation files (the "Software"),
009: * to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
011: * and/or sell copies of the Software, and to permit persons to whom the
012: * Software is furnished to do so, subject to the following conditions:
013: *
014: * The above copyright notice and this permission notice shall be included
015: * in all copies or substantial portions of the Software.
016: *
017: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
018: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
020: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
021: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
022: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
023: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
024: *
025: */
026: package org.ofbiz.content.webapp.region;
027:
028: import javax.servlet.ServletContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.jsp.JspException;
033: import javax.servlet.jsp.PageContext;
034:
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.GeneralException;
037: import org.ofbiz.base.util.UtilJ2eeCompat;
038: import org.ofbiz.content.webapp.control.RequestHandler;
039: import org.ofbiz.content.webapp.view.JPublishWrapper;
040: import org.ofbiz.content.webapp.view.ViewHandler;
041: import org.ofbiz.content.webapp.view.ViewHandlerException;
042:
043: /**
044: * A section is content with a name that implements Content.render.
045: * <p>That method renders content either by including
046: * it or by printing it directly, depending upon the direct
047: * value passed to the Section constructor.</p>
048: *
049: * <p>Note that a section's content can also be a region;if so,
050: * Region.render is called from Section.Render().</p>
051: *
052: * @author David M. Geary in the book "Advanced Java Server Pages"
053: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
054: * @version $Revision: 1.2 $
055: * @since 2.0
056: */
057: public class Section extends Content {
058:
059: protected final String name;
060: protected final String info;
061: protected RegionManager regionManager;
062:
063: public final static String module = Section.class.getName();
064:
065: public Section(String name, String info, String content,
066: String type, RegionManager regionManager) {
067: super (content, type);
068: this .name = name;
069: this .info = info;
070: this .regionManager = regionManager;
071: }
072:
073: public String getName() {
074: return name;
075: }
076:
077: public void render(PageContext pageContext) throws JspException {
078: try {
079: if (UtilJ2eeCompat.doFlushOnRender(pageContext
080: .getServletContext())) {
081: pageContext.getOut().flush();
082: }
083: render((HttpServletRequest) pageContext.getRequest(),
084: (HttpServletResponse) pageContext.getResponse());
085: } catch (java.io.IOException e) {
086: Debug.logError(e, "Error rendering section: ", module);
087: if (UtilJ2eeCompat.useNestedJspException(pageContext
088: .getServletContext()))
089: throw new JspException(e);
090: else
091: throw new JspException(e.toString());
092: } catch (ServletException e) {
093: Throwable throwable = e.getRootCause() != null ? e
094: .getRootCause() : e;
095:
096: Debug.logError(throwable, "Error rendering section: ",
097: module);
098: if (UtilJ2eeCompat.useNestedJspException(pageContext
099: .getServletContext()))
100: throw new JspException(throwable);
101: else
102: throw new JspException(throwable.toString());
103: }
104: }
105:
106: public void render(HttpServletRequest request,
107: HttpServletResponse response) throws java.io.IOException,
108: ServletException {
109: ServletContext context = (ServletContext) request
110: .getAttribute("servletContext");
111: boolean verboseOn = Debug.verboseOn();
112:
113: if (verboseOn)
114: Debug.logVerbose("Rendering " + this .toString(), module);
115:
116: // long viewStartTime = System.currentTimeMillis();
117: if (content != null) {
118: if ("direct".equals(type)) {
119: if (UtilJ2eeCompat.useOutputStreamNotWriter(context)) {
120: response.getOutputStream().print(content);
121: } else {
122: response.getWriter().print(content);
123: }
124: } else if ("default".equals(type) || "region".equals(type)
125: || "resource".equals(type)) {
126: // if type is resource then we won't even look up the region
127:
128: // if this is default or region, check to see if the content points to a valid region name
129: Region region = null;
130:
131: if ("default".equals(type) || "region".equals(type)) {
132: region = regionManager.getRegion(content);
133: }
134:
135: if ("region".equals(type) || region != null) {
136: if (region == null) {
137: throw new IllegalArgumentException(
138: "No region definition found with name: "
139: + content);
140: }
141: // render the content as a region
142: RegionStack.push(request, region);
143: region.render(request, response);
144: RegionStack.pop(request);
145: } else if ("jpublish".equals(type)) {
146: // rather then using the view handler use the wrapper directly
147: ServletContext sctx = (ServletContext) request
148: .getAttribute("servletContext");
149: if (sctx != null) {
150: JPublishWrapper jp = (JPublishWrapper) sctx
151: .getAttribute("jpublishWrapper");
152: if (jp != null) {
153: String contentStr = "<!-- " + content
154: + " Not Processed -->";
155: try {
156: contentStr = jp.render(content,
157: request, response);
158: } catch (GeneralException e) {
159: Debug
160: .logError(
161: e,
162: "Problems rendering view from JPublish",
163: module);
164: }
165: if (UtilJ2eeCompat
166: .useOutputStreamNotWriter(context)) {
167: response.getOutputStream().print(
168: contentStr);
169: } else {
170: response.getWriter().print(contentStr);
171: }
172: } else {
173: throw new IllegalStateException(
174: "No jpublishWrapper found in ServletContext");
175: }
176: } else {
177: throw new IllegalStateException(
178: "No servletContext found in request");
179: }
180: } else {
181: // default is the string that the ViewFactory expects for webapp resources
182: viewHandlerRender("default", request, response);
183: }
184: } else {
185: viewHandlerRender(type, request, response);
186: }
187: }
188: if (verboseOn)
189: Debug.logVerbose("DONE Rendering " + this .toString(),
190: module);
191: }
192:
193: protected void viewHandlerRender(String typeToUse,
194: HttpServletRequest request, HttpServletResponse response)
195: throws ServletException {
196: ServletContext context = (ServletContext) request
197: .getAttribute("servletContext");
198: RequestHandler requestHandler = (RequestHandler) context
199: .getAttribute("_REQUEST_HANDLER_");
200:
201: // see if the type is defined in the controller.xml file
202: try {
203: if (Debug.verboseOn())
204: Debug.logVerbose("Rendering view [" + content
205: + "] of type [" + typeToUse + "]", module);
206: ViewHandler vh = requestHandler.getViewFactory()
207: .getViewHandler(typeToUse);
208: // use the default content-type and encoding for the ViewHandler -- may want to change this.
209: vh.render(name, content, info, null, null, request,
210: response);
211: } catch (ViewHandlerException e) {
212: throw new ServletException(e.getNonNestedMessage(), e
213: .getNested());
214: }
215: }
216:
217: public String toString() {
218: return "Section: " + name + ", info=" + info + ", content="
219: + content + ", type=" + type;
220: }
221: }
|