KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspBean


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspBean.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.16 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.jsp;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsRequestContext;
36 import org.opencms.flex.CmsFlexController;
37 import org.opencms.i18n.CmsMessageContainer;
38 import org.opencms.main.CmsLog;
39 import org.opencms.main.CmsRuntimeException;
40 import org.opencms.util.CmsRequestUtil;
41
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpServletResponse JavaDoc;
44 import javax.servlet.jsp.PageContext JavaDoc;
45
46 import org.apache.commons.logging.Log;
47
48 /**
49  * Superclass for OpenCms JSP beans that provides convient access
50  * to OpenCms core and VFS functionality.<p>
51  *
52  * If you have large chunks of code on your JSP that you want to
53  * move to a Class file, consider creating a subclass of this bean.
54  *
55  * Initialize this bean at the beginning of your JSP like this:
56  * <pre>
57  * &lt;jsp:useBean id="cmsbean" class="org.opencms.jsp.CmsJspBean"&gt;
58  * &lt% cmsbean.init(pageContext, request, response); %&gt;
59  * &lt;/jsp:useBean&gt;
60  * </pre>
61  * <p>
62  *
63  * @author Alexander Kandzior
64  *
65  * @version $Revision: 1.16 $
66  *
67  * @since 6.0.0
68  */

69 public class CmsJspBean {
70
71     /** The log object for this class. */
72     private static final Log LOG = CmsLog.getLog(CmsJspBean.class);
73
74     /** JSP page context. */
75     private PageContext JavaDoc m_context;
76
77     /** OpenCms core CmsObject. */
78     private CmsFlexController m_controller;
79
80     /** Flag to indicate that this bean was properly initialized. */
81     private boolean m_isNotInitialized;
82
83     /** Flag to indicate if we want default or custom Exception handling. */
84     private boolean m_isSupressingExceptions;
85
86     /** OpenCms JSP request. */
87     private HttpServletRequest JavaDoc m_request;
88
89     /** OpenCms JSP response. */
90     private HttpServletResponse JavaDoc m_response;
91
92     /**
93      * Empty constructor, required for every JavaBean.<p>
94      */

95     public CmsJspBean() {
96
97         // noop, call init() to get going
98
m_isSupressingExceptions = true;
99         m_isNotInitialized = true;
100     }
101
102     /**
103      * Returns the CmsObject from the wrapped request.<p>
104      *
105      * This is a convenience method in case you need access to
106      * the CmsObject in your JSP scriplets.
107      *
108      * @return the CmsObject from the wrapped request
109      */

110     public CmsObject getCmsObject() {
111
112         if (m_isNotInitialized) {
113             return null;
114         }
115         return m_controller.getCmsObject();
116     }
117
118     /**
119      * Returns the JSP page context this bean was initilized with.<p>
120      *
121      * @return the JSP page context this bean was initilized with
122      */

123     public PageContext JavaDoc getJspContext() {
124
125         return m_context;
126     }
127
128     /**
129      * Returns the request this bean was initilized with.<p>
130      *
131      * @return the request this bean was initilized with
132      */

133     public HttpServletRequest JavaDoc getRequest() {
134
135         return m_request;
136     }
137
138     /**
139      * Returns the current users OpenCms request context.<p>
140      *
141      * @return the current users OpenCms request context
142      */

143     public CmsRequestContext getRequestContext() {
144
145         return getCmsObject().getRequestContext();
146     }
147
148     /**
149      * Returns the reponse wrapped by this element.<p>
150      *
151      * @return the reponse wrapped by this element
152      */

153     public HttpServletResponse JavaDoc getResponse() {
154
155         return m_response;
156     }
157
158     /**
159      * Initialize this bean with the current page context, request and response.<p>
160      *
161      * It is required to call one of the init() methods before you can use the
162      * instance of this bean.
163      *
164      * @param context the JSP page context object
165      * @param req the JSP request
166      * @param res the JSP response
167      */

168     public void init(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
169
170         m_controller = CmsFlexController.getController(req);
171         if (m_controller == null) {
172             // controller not found - this request was not initialized properly
173
throw new CmsRuntimeException(Messages.get().container(
174                 Messages.ERR_MISSING_CMS_CONTROLLER_1,
175                 CmsJspBean.class.getName()));
176         }
177         m_context = context;
178         m_request = req;
179         m_response = res;
180         m_isNotInitialized = false;
181     }
182
183     /**
184      * Returns <code>true</code> if Exceptions are handled by the class instace and supressed on the
185      * output page, or <code>false</code> if they will be thrown and have to be handled by the calling class.<p>
186      *
187      * The default is <code>true</code>.
188      * If set to <code>false</code> Exceptions that occur internally will be wrapped into
189      * a RuntimeException and thrown to the calling class instance.<p>
190      *
191      * @return <code>true</code> if Exceptions are supressed, or
192      * <code>false</code> if they will be thrown and have to be handled by the calling class
193      */

194     public boolean isSupressingExceptions() {
195
196         return m_isSupressingExceptions;
197     }
198
199     /**
200      * Sets the content type for the HTTP response.<p>
201      *
202      * This method is required since JSP's are handled in a special way for included template elements,
203      * so {@link javax.servlet.ServletResponse#setContentType(java.lang.String)} won't work.<p>
204      *
205      * Please note that the content type set this way is never cached in the Flex cache,
206      * so you must make sure to not cache the element when you use this method.<p>
207      *
208      * @param type the type to set
209      *
210      * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
211      */

212     public void setContentType(String JavaDoc type) {
213
214         // set the content type on the top level response
215
m_controller.getTopResponse().setContentType(type);
216     }
217
218     /**
219      * Sets the status code for the HTTP response.<p>
220      *
221      * This method is required since JSP's are handled in a special way for included template elements,
222      * so {@link javax.servlet.http.HttpServletResponseWrapper#setStatus(int)} won't work.<p>
223      *
224      * Please note that the status code set this way is never cached in the Flex cache,
225      * so you must make sure to not cache the element when you use this method.<p>
226      *
227      * @param status the status code to set
228      *
229      * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
230      */

231     public void setStatus(int status) {
232
233         // use the request attribute to store the status
234
m_request.setAttribute(CmsRequestUtil.ATTRIBUTE_ERRORCODE, new Integer JavaDoc(status));
235     }
236
237     /**
238      * Controls if Exceptions that occur in methods of this class are supressed (true)
239      * or not (false).<p>
240      *
241      * The default is <code>true</code>.
242      * If set to <code>false</code> all Exceptions that occur internally will be wrapped into
243      * a RuntimeException and thrown to the calling class instance.<p>
244      *
245      * @param value the value to set the Exception handing to
246      */

247     public void setSupressingExceptions(boolean value) {
248
249         m_isSupressingExceptions = value;
250     }
251
252     /**
253      * Returns the Flex controller derived from the request this bean was initilized with.<p>
254      *
255      * This is protected since the CmsFlexController this is really an internal OpenCms
256      * helper function, not part of the public OpenCms API. It must not be used on JSP pages,
257      * only from subclasses of this bean.<p>
258      *
259      * @return the Flex controller derived from the request this bean was initilized with
260      */

261     protected CmsFlexController getController() {
262
263         return m_controller;
264     }
265
266     /**
267      * Internally localizes the given <code>CmsMessageContainer</code> to a String. <p>
268      *
269      * If the user request context is at hand, the user's locale will be chosen. If
270      * no user request context is available, the default locale is used.
271      * <p>
272      *
273      * @param container the message container that allows localization of the represented message.
274      * @return the message String of the container argument localized to the user's locale (if available) or
275      * to the default locale.
276      */

277     protected String JavaDoc getMessage(CmsMessageContainer container) {
278
279         CmsObject cms = getCmsObject();
280         String JavaDoc result;
281         if ((cms == null) || (cms.getRequestContext().getLocale() == null)) {
282             result = container.key();
283         } else {
284             result = container.key(cms.getRequestContext().getLocale());
285         }
286         return result;
287     }
288
289     /**
290      * Handles any exception that might occur in the context of this element to
291      * ensure that templates are not disturbed.<p>
292      *
293      * @param t the Throwable that was catched
294      */

295     protected void handleException(Throwable JavaDoc t) {
296
297         if (LOG.isErrorEnabled()) {
298             LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_JSP_BEAN_0), t);
299         }
300         if (!(m_isSupressingExceptions || getRequestContext().currentProject().isOnlineProject())) {
301             if (LOG.isDebugEnabled()) {
302                 // no stack trace needed since it was already logged with the "error" log message above
303
LOG.debug(Messages.get().getBundle().key(
304                     Messages.LOG_DEBUG_INTERRUPTED_EXCEPTION_1,
305                     getClass().getName()));
306             }
307             String JavaDoc uri = null;
308             Throwable JavaDoc u = getController().getThrowable();
309             if (u != null) {
310                 uri = getController().getThrowableResourceUri();
311             } else {
312                 u = t;
313             }
314             throw new CmsRuntimeException(Messages.get().container(
315                 Messages.ERR_RUNTIME_1,
316                 (uri != null) ? uri : getClass().getName()), t);
317         }
318     }
319
320     /**
321      * Returns true if this bean has not been initilized (i.e. init() has not been called so far), false otherwise.<p>
322      *
323      * @return true if this bean has not been initilized
324      */

325     protected boolean isNotInitialized() {
326
327         return m_isNotInitialized;
328     }
329 }
Popular Tags