KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JPublishViewHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 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 java.io.OutputStreamWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletOutputStream JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.ofbiz.base.util.GeneralException;
36 import org.ofbiz.base.util.UtilJ2eeCompat;
37
38 /**
39  * Handles JPublish type view rendering
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 2.1
44  */

45 public class JPublishViewHandler implements ViewHandler {
46
47     public static final String JavaDoc module = JPublishViewHandler.class.getName();
48     
49     protected ServletContext JavaDoc servletContext = null;
50     protected JPublishWrapper wrapper = null;
51
52     /**
53      * @see org.ofbiz.webapp.view.ViewHandler#init(javax.servlet.ServletContext)
54      */

55     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
56         this.servletContext = context;
57         this.wrapper = (JPublishWrapper) context.getAttribute("jpublishWrapper");
58         if (this.wrapper == null) {
59             this.wrapper = new JPublishWrapper(servletContext);
60         }
61
62         // make sure it loaded
63
if (this.wrapper == null)
64             throw new ViewHandlerException("JPublishWrapper not created");
65     }
66
67     /**
68      * @see org.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
69      */

70     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
71         Writer JavaDoc writer = null;
72         try {
73             // use UtilJ2eeCompat to get this setup properly
74
boolean useOutputStreamNotWriter = false;
75             if (this.servletContext != null) {
76                 useOutputStreamNotWriter = UtilJ2eeCompat.useOutputStreamNotWriter(this.servletContext);
77             }
78             if (useOutputStreamNotWriter) {
79                 ServletOutputStream JavaDoc ros = response.getOutputStream();
80                 writer = new OutputStreamWriter JavaDoc(ros, "UTF-8");
81             } else {
82                 writer = response.getWriter();
83             }
84             wrapper.render(page, request, response, writer, null, true);
85         } catch (IOException JavaDoc e) {
86             throw new ViewHandlerException("Problems with the response writer/output stream", e);
87         } catch (GeneralException e) {
88             throw new ViewHandlerException("Cannot render page", e);
89         }
90     }
91 }
92
93
Popular Tags