KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > util > CmsJspStatusBean


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/util/CmsJspStatusBean.java,v $
3  * Date : $Date: 2005/07/07 13:14:26 $
4  * Version: $Revision: 1.10 $
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.util;
33
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.i18n.CmsAcceptLanguageHeaderParser;
36 import org.opencms.i18n.CmsLocaleManager;
37 import org.opencms.i18n.CmsMessages;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.main.OpenCms;
40 import org.opencms.security.CmsRole;
41 import org.opencms.util.CmsStringUtil;
42 import org.opencms.workplace.CmsWorkplace;
43
44 import java.util.Date JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Locale JavaDoc;
48 import java.util.Map JavaDoc;
49
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51 import javax.servlet.http.HttpServletResponse JavaDoc;
52 import javax.servlet.jsp.JspException JavaDoc;
53 import javax.servlet.jsp.PageContext JavaDoc;
54
55 /**
56  * This bean provides methods to generate customized http status error pages, e.g. to handle 404 (not found) errors.<p>
57  *
58  * The JSPs using this bean are placed in the OpenCms VFS folder /system/handler/.<p>
59  *
60  * @author Andreas Zahner
61  *
62  * @version $Revision: 1.10 $
63  *
64  * @since 6.0
65  */

66 public class CmsJspStatusBean extends CmsJspActionElement {
67
68     /** Request attribute key for the error message. */
69     public static final String JavaDoc ERROR_MESSAGE = "javax.servlet.error.message";
70
71     /** Request attribute key for the error request URI. */
72     public static final String JavaDoc ERROR_REQUEST_URI = "javax.servlet.error.request_uri";
73
74     /** Request attribute key for the error servlet name. */
75     public static final String JavaDoc ERROR_SERVLET_NAME = "javax.servlet.error.servlet_name";
76
77     /** Request attribute key for the error status code. */
78     public static final String JavaDoc ERROR_STATUS_CODE = "javax.servlet.error.status_code";
79
80     /** Default name for an unknown error status code. */
81     public static final String JavaDoc UNKKNOWN_STATUS_CODE = "unknown";
82
83     /** The OpenCms VFS path containing the handler files. */
84     public static final String JavaDoc VFS_FOLDER_HANDLER = CmsWorkplace.VFS_PATH_SYSTEM + "handler/";
85
86     /** The error message. */
87     private String JavaDoc m_errorMessage;
88
89     /** The thrown exception. */
90     private Throwable JavaDoc m_exception;
91
92     /** The Locale to use for displayed messages. */
93     private Locale JavaDoc m_locale;
94
95     /** Contains all possible parameters usable by localized messages. */
96     private Object JavaDoc[] m_localizeParameters;
97
98     /** The localized messages to use on the page. */
99     private CmsMessages m_messages;
100
101     /** The request URI. */
102     private String JavaDoc m_requestUri;
103
104     /** The servlet name. */
105     private String JavaDoc m_servletName;
106
107     /** The site root of the requested resource. */
108     private String JavaDoc m_siteRoot;
109
110     /** The status code. */
111     private Integer JavaDoc m_statusCode;
112
113     /** The status code as message. */
114     private String JavaDoc m_statusCodeMessage;
115
116     /** The URI used for template part inclusion. */
117     private String JavaDoc m_templateUri;
118
119     /**
120      * Empty constructor, required for every JavaBean.
121      */

122     public CmsJspStatusBean() {
123
124         super();
125     }
126
127     /**
128      * Constructor, with parameters.
129      *
130      * @param context the JSP page context object
131      * @param req the JSP request
132      * @param res the JSP response
133      */

134     public CmsJspStatusBean(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
135
136         super(context, req, res);
137         initMembers(req, null);
138     }
139
140     /**
141      * Constructor, with parameters.
142      *
143      * @param context the JSP page context object
144      * @param req the JSP request
145      * @param res the JSP response
146      * @param t the exception that lead to the error
147      */

148     public CmsJspStatusBean(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res, Throwable JavaDoc t) {
149
150         super(context, req, res);
151         initMembers(req, t);
152     }
153
154     /**
155      * Returns the error message.<p>
156      *
157      * @return the error message
158      */

159     public String JavaDoc getErrorMessage() {
160
161         return m_errorMessage;
162     }
163
164     /**
165      * Returns the exception.<p>
166      *
167      * @return the exception
168      */

169     public Throwable JavaDoc getException() {
170
171         return m_exception;
172     }
173
174     /**
175      * Returns the locale to use for the error page.<p>
176      *
177      * @return the locale to use for the error page
178      */

179     public Locale JavaDoc getLocale() {
180
181         return m_locale;
182     }
183
184     /**
185      * Returns the processed output of the specified element of an OpenCms page.<p>
186      *
187      * The page to get the content from is looked up in the property value "template-elements".
188      * If no value is found, the page is read from the "contents/" subfolder of the handler folder.<p>
189      *
190      * For each status code, an individual page can be created by naming it "content${STATUSCODE}.html".
191      * If the individual page can not be found, the content is read from "contentunknown.html".<p>
192      *
193      * @param element name of the element
194      * @return the processed output of the specified element of an OpenCms page
195      */

196     public String JavaDoc getPageContent(String JavaDoc element) {
197
198         // Determine the folder to read the contents from
199
String JavaDoc contentFolder = property(CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS, "search", "");
200         if (CmsStringUtil.isEmpty(contentFolder)) {
201             contentFolder = VFS_FOLDER_HANDLER + "contents/";
202         }
203
204         // determine the file to read the contents from
205
String JavaDoc fileName = "content" + getStatusCodeMessage() + ".html";
206         if (!getCmsObject().existsResource(contentFolder + fileName)) {
207             // special file does not exist, use generic one
208
fileName = "content" + UNKKNOWN_STATUS_CODE + ".html";
209         }
210
211         // get the content
212
return getContent(contentFolder + fileName, element, getLocale());
213     }
214
215     /**
216      * Returns the absolute path of the requested resource in the VFS of OpenCms.<p>
217      *
218      * @return the absolute path of the requested resource in the VFS of OpenCms
219      */

220     public String JavaDoc getRequestResourceName() {
221
222         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getRequestUri())
223             && getRequestUri().startsWith(OpenCms.getSystemInfo().getOpenCmsContext())) {
224             return getRequestUri().substring(OpenCms.getSystemInfo().getOpenCmsContext().length());
225         }
226         return getRequestUri();
227     }
228
229     /**
230      * Returns the request Uri.<p>
231      *
232      * @return the request Uri
233      */

234     public String JavaDoc getRequestUri() {
235
236         return m_requestUri;
237     }
238
239     /**
240      * Returns the full Workplace resource path to the selected resource.<p>
241      *
242      * @param resourceName the name of the resource to get the resource path for
243      *
244      * @return the full Workplace resource path to the selected resource
245      */

246     public String JavaDoc getResourceUri(String JavaDoc resourceName) {
247
248         return CmsWorkplace.getResourceUri(resourceName);
249     }
250
251     /**
252      * Returns the servlet name.<p>
253      *
254      * @return the servlet name
255      */

256     public String JavaDoc getServletName() {
257
258         return m_servletName;
259     }
260
261     /**
262      * Returns the site root of the requested resource.<p>
263      *
264      * @return the site root of the requested resource
265      */

266     public String JavaDoc getSiteRoot() {
267
268         return m_siteRoot;
269     }
270
271     /**
272      * Returns the status code.<p>
273      *
274      * @return the status code
275      */

276     public Integer JavaDoc getStatusCode() {
277
278         return m_statusCode;
279     }
280
281     /**
282      * Returns the status code message.<p>
283      *
284      * @return the status code message
285      */

286     public String JavaDoc getStatusCodeMessage() {
287
288         return m_statusCodeMessage;
289     }
290
291     /**
292      * Returns the URI used for template part inclusion.<p>
293      *
294      * @return the URI used for template part inclusion
295      */

296     public String JavaDoc getTemplateUri() {
297
298         if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_templateUri)) {
299             m_templateUri = "/";
300         }
301         return m_templateUri;
302     }
303
304     /**
305      * Include a template part to display on the error page.<p>
306      *
307      * @param target the target uri of the file in the OpenCms VFS (can be relative or absolute)
308      * @param element the element (template selector) to display from the target
309      *
310      * @throws JspException in case there were problems including the target
311      */

312     public void includeTemplatePart(String JavaDoc target, String JavaDoc element) throws JspException JavaDoc {
313
314         includeTemplatePart(target, element, null);
315     }
316
317     /**
318      * Include a template part to display on the error page.<p>
319      *
320      * @param target the target uri of the file in the OpenCms VFS (can be relative or absolute)
321      * @param element the element (template selector) to display from the target
322      * @param parameterMap a map of the request parameters
323      *
324      * @throws JspException in case there were problems including the target
325      */

326     public void includeTemplatePart(String JavaDoc target, String JavaDoc element, Map JavaDoc parameterMap) throws JspException JavaDoc {
327
328         // store current site root and URI
329
String JavaDoc currentSiteRoot = getRequestContext().getSiteRoot();
330         String JavaDoc currentUri = getRequestContext().getUri();
331
332         // set the Locale in the request parameter Map
333
if (parameterMap == null) {
334             parameterMap = new HashMap JavaDoc(1);
335         }
336         parameterMap.put(CmsLocaleManager.PARAMETER_LOCALE, getLocale().toString());
337
338         try {
339             // set site root and URI to display template part correct
340
getRequestContext().setSiteRoot(getSiteRoot());
341             getRequestContext().setUri(getTemplateUri());
342             // include the template part
343
include(target, element, parameterMap);
344         } finally {
345             // reset site root and requested URI to status JSP
346
getRequestContext().setSiteRoot(currentSiteRoot);
347             getRequestContext().setUri(currentUri);
348         }
349     }
350
351     /**
352      * Returns the localized resource string for a given message key.<p>
353      *
354      * If the key was not found in the bundle, the return value is
355      * <code>"??? " + keyName + " ???"</code>.<p>
356      *
357      * The key can use the following parameters for formatting:
358      * <ul>
359      * <li>0: the HTTP status code</li>
360      * <li>1: the requested URI</li>
361      * <li>2: the generated error message</li>
362      * <li>3: the servlet name</li>
363      * <li>4: the date of the request</li>
364      * </ul>
365      *
366      * @param keyName the key for the desired string
367      * @return the resource string for the given key
368      */

369     public String JavaDoc key(String JavaDoc keyName) {
370
371         return key(keyName, null);
372     }
373
374     /**
375      * Returns the localized resource string for a given message key.<p>
376      *
377      * For a detailed parameter description, see {@link CmsJspStatusBean#key(String)}.<p>
378      *
379      * @param keyName the key for the desired string
380      * @param defaultKeyName the default key for the desired string, used if the keyName delivered no resource string
381      * @return the resource string for the given key
382      */

383     public String JavaDoc key(String JavaDoc keyName, String JavaDoc defaultKeyName) {
384
385         String JavaDoc value = getMessages().key(keyName, getLocalizeParameters());
386         if (value.startsWith(CmsMessages.UNKNOWN_KEY_EXTENSION) && CmsStringUtil.isNotEmpty(defaultKeyName)) {
387             value = getMessages().key(defaultKeyName, getLocalizeParameters());
388         }
389         return CmsStringUtil.escapeHtml(value);
390     }
391
392     /**
393      * Returns the localized resource string for a given message key depending on the HTTP status.<p>
394      *
395      * Automatically adds a status suffix for the key to get, eg. "keyname" gets the suffix "_404" for a 404 status.
396      * For a detailed parameter description, see {@link CmsJspStatusBean#key(String)}.<p>
397      *
398      * @param keyName the key for the desired string
399      * @return the resource string for the given key
400      */

401     public String JavaDoc keyStatus(String JavaDoc keyName) {
402
403         keyName += "_";
404         return key(keyName + getStatusCodeMessage(), keyName + UNKKNOWN_STATUS_CODE);
405     }
406
407     /**
408      * Sets the URI used for template part inclusion.<p>
409      *
410      * @param templateUri the URI used for template part inclusion
411      */

412     public void setTemplateUri(String JavaDoc templateUri) {
413
414         m_templateUri = templateUri;
415     }
416
417     /**
418      * Returns true if the current user has the "DEVELOPER" role and can view the exception stacktrace.<p>
419      *
420      * @return true if the current user has the "DEVELOPER" role and can view the exception stacktrace
421      */

422     public boolean showException() {
423
424         return getCmsObject().hasRole(CmsRole.DEVELOPER);
425     }
426
427     /**
428      * Returns the parameter object for localization.<p>
429      *
430      * @return the parameter object for localization
431      *
432      * @see #key(String) for a more detailed object description
433      */

434     protected Object JavaDoc[] getLocalizeParameters() {
435
436         if (m_localizeParameters == null) {
437             m_localizeParameters = new Object JavaDoc[] {
438                 getStatusCodeMessage(),
439                 getRequestUri(),
440                 getErrorMessage(),
441                 getServletName(),
442                 new Date JavaDoc(getRequestContext().getRequestTime())};
443         }
444         return m_localizeParameters;
445     }
446
447     /**
448      * Returns the initialized messages object to read localized messages from.<p>
449      *
450      * @return the initialized messages object to read localized messages from
451      */

452     protected CmsMessages getMessages() {
453
454         if (m_messages == null) {
455             // initialize the localized messages
456
m_messages = new CmsMessages(Messages.get().getBundleName(), getLocale().toString());
457         }
458         return m_messages;
459     }
460
461     /**
462      * Initializes the members of this bean with the information retrieved from the current request.<p>
463      *
464      * @param req the JSP request
465      * @param t the exception that lead to the error
466      */

467     protected void initMembers(HttpServletRequest JavaDoc req, Throwable JavaDoc t) {
468
469         // get the status error attribute values from the request
470
m_servletName = (String JavaDoc)req.getAttribute(ERROR_SERVLET_NAME);
471         m_errorMessage = (String JavaDoc)req.getAttribute(ERROR_MESSAGE);
472         m_requestUri = (String JavaDoc)req.getAttribute(ERROR_REQUEST_URI);
473         m_statusCode = (Integer JavaDoc)req.getAttribute(ERROR_STATUS_CODE);
474
475         if (m_statusCode == null || m_requestUri == null) {
476             // check if the error request is invoked via Apache/HTTPd ErrorDocument and mod_jk
477

478             // to use this you need to add the following to "jk.conf":
479

480             // JkEnvVar REDIRECT_URL none
481
// JkEnvVar REDIRECT_STATUS none
482
// JkEnvVar REDIRECT_SERVLET_NAME OpenCmsServlet
483

484             String JavaDoc jkUri = (String JavaDoc)req.getAttribute("REDIRECT_URL");
485             String JavaDoc jkStatusCode = (String JavaDoc)req.getAttribute("REDIRECT_STATUS");
486             String JavaDoc jkServletName = (String JavaDoc)req.getAttribute("REDIRECT_SERVLET_NAME");
487             try {
488                 if (!"none".equals(jkStatusCode) && !"none".equals(jkUri)) {
489                     m_servletName = jkServletName;
490                     m_requestUri = jkUri;
491                     m_statusCode = new Integer JavaDoc(jkStatusCode);
492                 }
493             } catch (NullPointerException JavaDoc e) {
494                 // attibute not set, ignore
495
} catch (NumberFormatException JavaDoc e) {
496                 // status code not a number, ignore
497
}
498         }
499
500         // get the status code as String
501
if (m_statusCode != null) {
502             m_statusCodeMessage = String.valueOf(m_statusCode.intValue());
503         } else {
504             m_statusCodeMessage = UNKKNOWN_STATUS_CODE;
505         }
506
507         m_exception = t;
508
509         // determine the best locale to use from the users browser settings
510
CmsAcceptLanguageHeaderParser parser = new CmsAcceptLanguageHeaderParser(
511             req,
512             OpenCms.getWorkplaceManager().getDefaultLocale());
513         List JavaDoc acceptedLocales = parser.getAcceptedLocales();
514         List JavaDoc workplaceLocales = OpenCms.getWorkplaceManager().getLocales();
515         m_locale = OpenCms.getLocaleManager().getFirstMatchingLocale(acceptedLocales, workplaceLocales);
516         if (m_locale == null) {
517             // no match found - use OpenCms default locale
518
m_locale = OpenCms.getWorkplaceManager().getDefaultLocale();
519         }
520
521         // store the site root of the request
522
m_siteRoot = OpenCms.getSiteManager().matchRequest(req).getSiteRoot();
523     }
524
525     /**
526      * Sets the error message.<p>
527      *
528      * @param errorMessage the error message to set
529      */

530     protected void setErrorMessage(String JavaDoc errorMessage) {
531
532         m_errorMessage = errorMessage;
533     }
534
535     /**
536      * Sets the exception.<p>
537      *
538      * @param exception the exception to set
539      */

540     protected void setException(Throwable JavaDoc exception) {
541
542         m_exception = exception;
543     }
544
545     /**
546      * Sets the locale to use for the error page.<p>
547      *
548      * @param locale the locale to use for the error page
549      */

550     protected void setLocale(Locale JavaDoc locale) {
551
552         m_locale = locale;
553     }
554
555     /**
556      * Sets the parameter object for localization.<p>
557      *
558      * @param localizeParameters the parameter object for localization
559      */

560     protected void setLocalizeParameters(Object JavaDoc[] localizeParameters) {
561
562         m_localizeParameters = localizeParameters;
563     }
564
565     /**
566      * Sets the initialized messages object to read localized messages from.<p>
567      *
568      * @param messages the initialized messages object to read localized messages from
569      */

570     protected void setMessages(CmsMessages messages) {
571
572         m_messages = messages;
573     }
574
575     /**
576      * Sets the request Uri.<p>
577      *
578      * @param requestUri the request Uri to set
579      */

580     protected void setRequestUri(String JavaDoc requestUri) {
581
582         m_requestUri = requestUri;
583     }
584
585     /**
586      * Sets the servlet name.<p>
587      *
588      * @param servletName the servlet name to set
589      */

590     protected void setServletName(String JavaDoc servletName) {
591
592         m_servletName = servletName;
593     }
594
595     /**
596      * Sets the site root of the requested resource.<p>
597      *
598      * @param siteRoot the site root of the requested resource
599      */

600     protected void setSiteRoot(String JavaDoc siteRoot) {
601
602         m_siteRoot = siteRoot;
603     }
604
605     /**
606      * Sets the status code.<p>
607      *
608      * @param statusCode the status code to set
609      */

610     protected void setStatusCode(Integer JavaDoc statusCode) {
611
612         m_statusCode = statusCode;
613     }
614
615     /**
616      * Sets the status code message.<p>
617      *
618      * @param statusCodeMessage the status code message to set
619      */

620     protected void setStatusCodeMessage(String JavaDoc statusCodeMessage) {
621
622         m_statusCodeMessage = statusCodeMessage;
623     }
624 }
Popular Tags