KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JspViewHandler.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 javax.servlet.RequestDispatcher 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
38 /**
39  * ViewHandlerException - View Handler Exception
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 2.0
44  */

45 public class JspViewHandler implements ViewHandler {
46     
47     public static final String JavaDoc module = JspViewHandler.class.getName();
48
49     protected ServletContext JavaDoc context;
50
51     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
52         this.context = context;
53     }
54
55     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc contentType, String JavaDoc encoding, String JavaDoc info, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
56         // some containers call filters on EVERY request, even forwarded ones,
57
// so let it know that it came from the control servlet
58

59         if (request == null) {
60             throw new ViewHandlerException("Null HttpServletRequest object");
61         }
62         if (page == null || page.length() == 0) {
63             throw new ViewHandlerException("Null or empty source");
64         }
65
66         //Debug.log("Requested Page : " + page, module);
67
//Debug.log("Physical Path : " + context.getRealPath(page));
68

69         // tell the ContextFilter we are forwarding
70
request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean JavaDoc(true));
71         RequestDispatcher JavaDoc rd = request.getRequestDispatcher(page);
72         
73         if (rd == null) {
74             Debug.logInfo("HttpServletRequest.getRequestDispatcher() failed; trying ServletContext", module);
75             rd = context.getRequestDispatcher(page);
76             if (rd == null) {
77                 Debug.logInfo("ServletContext.getRequestDispatcher() failed; trying ServletContext.getNamedDispatcher(\"jsp\")", module);
78                 rd = context.getNamedDispatcher("jsp");
79                 if (rd == null) {
80                     throw new ViewHandlerException("Source returned a null dispatcher (" + page + ")");
81                 }
82             }
83         }
84
85         try {
86             rd.include(request, response);
87         } catch (IOException JavaDoc ie) {
88             throw new ViewHandlerException("IO Error in view", ie);
89         } catch (ServletException JavaDoc e) {
90             Throwable JavaDoc throwable = e.getRootCause() != null ? e.getRootCause() : e;
91
92             if (throwable instanceof JspException JavaDoc) {
93                 JspException JavaDoc jspe = (JspException JavaDoc) throwable;
94
95                 throwable = jspe.getRootCause() != null ? jspe.getRootCause() : jspe;
96             }
97             Debug.logError(throwable, "ServletException rendering JSP view", module);
98             throw new ViewHandlerException(e.getMessage(), throwable);
99         }
100     }
101 }
102
Popular Tags