01: /*
02: * $Id: JspViewHandler.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
03: *
04: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * 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.content.webapp.view;
26:
27: import java.io.IOException;
28:
29: import javax.servlet.RequestDispatcher;
30: import javax.servlet.ServletContext;
31: import javax.servlet.ServletException;
32: import javax.servlet.http.HttpServletRequest;
33: import javax.servlet.http.HttpServletResponse;
34: import javax.servlet.jsp.JspException;
35:
36: import org.ofbiz.base.util.Debug;
37: import org.ofbiz.content.webapp.control.ContextFilter;
38:
39: /**
40: * ViewHandlerException - View Handler Exception
41: *
42: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43: * @version $Revision: 1.2 $
44: * @since 2.0
45: */
46: public class JspViewHandler implements ViewHandler {
47:
48: public static final String module = JspViewHandler.class.getName();
49:
50: protected ServletContext context;
51:
52: public void init(ServletContext context)
53: throws ViewHandlerException {
54: this .context = context;
55: }
56:
57: public void render(String name, String page, String contentType,
58: String encoding, String info, HttpServletRequest request,
59: HttpServletResponse response) throws ViewHandlerException {
60: // some containers call filters on EVERY request, even forwarded ones,
61: // so let it know that it came from the control servlet
62:
63: if (request == null) {
64: throw new ViewHandlerException(
65: "Null HttpServletRequest object");
66: }
67: if (page == null || page.length() == 0) {
68: throw new ViewHandlerException("Null or empty source");
69: }
70:
71: // tell the ContextFilter we are forwarding
72: request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET,
73: new Boolean(true));
74: RequestDispatcher rd = request.getRequestDispatcher(page);
75:
76: if (rd == null)
77: throw new ViewHandlerException(
78: "Source returned a null dispatcher (" + page + ")");
79: try {
80: rd.include(request, response);
81: } catch (IOException ie) {
82: throw new ViewHandlerException("IO Error in view", ie);
83: } catch (ServletException e) {
84: Throwable throwable = e.getRootCause() != null ? e
85: .getRootCause() : e;
86:
87: if (throwable instanceof JspException) {
88: JspException jspe = (JspException) throwable;
89:
90: throwable = jspe.getRootCause() != null ? jspe
91: .getRootCause() : jspe;
92: }
93: Debug.logError(throwable,
94: "ServletException rendering JSP view", module);
95: throw new ViewHandlerException(e.getMessage(), throwable);
96: }
97: }
98: }
|