001: /*
002: * $Id: RegionViewHandler.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.view;
026:
027: import java.io.IOException;
028: import java.net.URL;
029:
030: import javax.servlet.ServletContext;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.servlet.jsp.JspException;
035:
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.content.webapp.control.ContextFilter;
038: import org.ofbiz.content.webapp.region.Region;
039: import org.ofbiz.content.webapp.region.RegionManager;
040: import org.ofbiz.content.webapp.region.RegionStack;
041:
042: /**
043: * Handles Region type view rendering
044: *
045: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
046: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
047: * @version $Revision: 1.2 $
048: * @since 2.0
049: */
050: public class RegionViewHandler implements ViewHandler {
051:
052: public static final String module = RegionViewHandler.class
053: .getName();
054:
055: protected ServletContext context;
056: protected RegionManager regionManager = null;
057:
058: public void init(ServletContext context)
059: throws ViewHandlerException {
060: this .context = context;
061:
062: URL regionFile = null;
063: try {
064: regionFile = context.getResource("/WEB-INF/regions.xml");
065: } catch (java.net.MalformedURLException e) {
066: throw new IllegalArgumentException(
067: "regions.xml file URL invalid: " + e.getMessage());
068: }
069:
070: if (regionFile == null) {
071: Debug.logWarning("No " + "/WEB-INF/regions.xml"
072: + " file found in this webapp", module);
073: } else {
074: Debug.logVerbose("Loading regions from XML file in: "
075: + regionFile, module);
076: regionManager = new RegionManager(regionFile);
077: }
078: }
079:
080: public void render(String name, String page, String info,
081: String contentType, String encoding,
082: HttpServletRequest request, HttpServletResponse response)
083: throws ViewHandlerException {
084: // some containers call filters on EVERY request, even forwarded ones,
085: // so let it know that it came from the control servlet
086:
087: if (request == null) {
088: throw new ViewHandlerException(
089: "The HttpServletRequest object was null, how did that happen?");
090: }
091: if (page == null || page.length() == 0) {
092: throw new ViewHandlerException(
093: "Page name was null or empty, but must be specified");
094: }
095:
096: // tell the ContextFilter we are forwarding
097: request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET,
098: new Boolean(true));
099:
100: Region region = regionManager.getRegion(page);
101: if (region == null) {
102: throw new ViewHandlerException(
103: "Error: could not find region with name " + page);
104: }
105:
106: try {
107: // this render method does not come from a page tag so some setup needs to happen here
108: RegionStack.push(request, region);
109:
110: region.render(request, response);
111: } catch (IOException ie) {
112: throw new ViewHandlerException("IO Error in region", ie);
113: } catch (ServletException e) {
114: Throwable throwable = e.getRootCause() != null ? e
115: .getRootCause() : e;
116:
117: if (throwable instanceof JspException) {
118: JspException jspe = (JspException) throwable;
119:
120: throwable = jspe.getRootCause() != null ? jspe
121: .getRootCause() : jspe;
122: }
123: Debug.logError(throwable,
124: "ServletException rendering JSP view", module);
125: throw new ViewHandlerException(e.getMessage(), throwable);
126: }
127: RegionStack.pop(request);
128: }
129: }
|