⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsjspstatusbean.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/util/CmsJspStatusBean.java,v $
 * Date   : $Date: 2005/07/07 13:14:26 $
 * Version: $Revision: 1.10 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.jsp.util;

import org.opencms.file.CmsPropertyDefinition;
import org.opencms.i18n.CmsAcceptLanguageHeaderParser;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.i18n.CmsMessages;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsRole;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.CmsWorkplace;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

/**
 * This bean provides methods to generate customized http status error pages, e.g. to handle 404 (not found) errors.<p>
 * 
 * The JSPs using this bean are placed in the OpenCms VFS folder /system/handler/.<p>
 * 
 * @author Andreas Zahner 
 * 
 * @version $Revision: 1.10 $
 * 
 * @since 6.0
 */
public class CmsJspStatusBean extends CmsJspActionElement {

    /** Request attribute key for the error message. */
    public static final String ERROR_MESSAGE = "javax.servlet.error.message";

    /** Request attribute key for the error request URI. */
    public static final String ERROR_REQUEST_URI = "javax.servlet.error.request_uri";

    /** Request attribute key for the error servlet name. */
    public static final String ERROR_SERVLET_NAME = "javax.servlet.error.servlet_name";

    /** Request attribute key for the error status code. */
    public static final String ERROR_STATUS_CODE = "javax.servlet.error.status_code";

    /** Default name for an unknown error status code. */
    public static final String UNKKNOWN_STATUS_CODE = "unknown";

    /** The OpenCms VFS path containing the handler files. */
    public static final String VFS_FOLDER_HANDLER = CmsWorkplace.VFS_PATH_SYSTEM + "handler/";

    /** The error message. */
    private String m_errorMessage;

    /** The thrown exception. */
    private Throwable m_exception;

    /** The Locale to use for displayed messages. */
    private Locale m_locale;

    /** Contains all possible parameters usable by localized messages. */
    private Object[] m_localizeParameters;

    /** The localized messages to use on the page. */
    private CmsMessages m_messages;

    /** The request URI. */
    private String m_requestUri;

    /** The servlet name. */
    private String m_servletName;

    /** The site root of the requested resource. */
    private String m_siteRoot;

    /** The status code. */
    private Integer m_statusCode;

    /** The status code as message. */
    private String m_statusCodeMessage;

    /** The URI used for template part inclusion. */
    private String m_templateUri;

    /**
     * Empty constructor, required for every JavaBean.
     */
    public CmsJspStatusBean() {

        super();
    }

    /**
     * Constructor, with parameters.
     * 
     * @param context the JSP page context object
     * @param req the JSP request 
     * @param res the JSP response 
     */
    public CmsJspStatusBean(PageContext context, HttpServletRequest req, HttpServletResponse res) {

        super(context, req, res);
        initMembers(req, null);
    }

    /**
     * Constructor, with parameters.
     * 
     * @param context the JSP page context object
     * @param req the JSP request 
     * @param res the JSP response 
     * @param t the exception that lead to the error
     */
    public CmsJspStatusBean(PageContext context, HttpServletRequest req, HttpServletResponse res, Throwable t) {

        super(context, req, res);
        initMembers(req, t);
    }

    /**
     * Returns the error message.<p>
     *
     * @return the error message
     */
    public String getErrorMessage() {

        return m_errorMessage;
    }

    /**
     * Returns the exception.<p>
     *
     * @return the exception
     */
    public Throwable getException() {

        return m_exception;
    }

    /**
     * Returns the locale to use for the error page.<p>
     *
     * @return the locale to use for the error page
     */
    public Locale getLocale() {

        return m_locale;
    }

    /**
     * Returns the processed output of the specified element of an OpenCms page.<p>
     * 
     * The page to get the content from is looked up in the property value "template-elements".
     * If no value is found, the page is read from the "contents/" subfolder of the handler folder.<p>
     * 
     * For each status code, an individual page can be created by naming it "content${STATUSCODE}.html". 
     * If the individual page can not be found, the content is read from "contentunknown.html".<p>
     * 
     * @param element name of the element
     * @return the processed output of the specified element of an OpenCms page
     */
    public String getPageContent(String element) {

        // Determine the folder to read the contents from
        String contentFolder = property(CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS, "search", "");
        if (CmsStringUtil.isEmpty(contentFolder)) {
            contentFolder = VFS_FOLDER_HANDLER + "contents/";
        }

        // determine the file to read the contents from
        String fileName = "content" + getStatusCodeMessage() + ".html";
        if (!getCmsObject().existsResource(contentFolder + fileName)) {
            // special file does not exist, use generic one
            fileName = "content" + UNKKNOWN_STATUS_CODE + ".html";
        }

        // get the content
        return getContent(contentFolder + fileName, element, getLocale());
    }

    /**
     * Returns the absolute path of the requested resource in the VFS of OpenCms.<p>
     *  
     * @return the absolute path of the requested resource in the VFS of OpenCms
     */
    public String getRequestResourceName() {

        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getRequestUri())
            && getRequestUri().startsWith(OpenCms.getSystemInfo().getOpenCmsContext())) {
            return getRequestUri().substring(OpenCms.getSystemInfo().getOpenCmsContext().length());
        }
        return getRequestUri();
    }

    /**
     * Returns the request Uri.<p>
     *
     * @return the request Uri
     */
    public String getRequestUri() {

        return m_requestUri;
    }

    /**
     * Returns the full Workplace resource path to the selected resource.<p>
     * 
     * @param resourceName the name of the resource to get the resource path for
     * 
     * @return the full Workplace resource path to the selected resource
     */
    public String getResourceUri(String resourceName) {

        return CmsWorkplace.getResourceUri(resourceName);
    }

    /**
     * Returns the servlet name.<p>
     *
     * @return the servlet name
     */
    public String getServletName() {

        return m_servletName;
    }

    /**
     * Returns the site root of the requested resource.<p>
     *
     * @return the site root of the requested resource
     */
    public String getSiteRoot() {

        return m_siteRoot;
    }

    /**
     * Returns the status code.<p>
     *
     * @return the status code
     */
    public Integer getStatusCode() {

        return m_statusCode;
    }

    /**
     * Returns the status code message.<p>
     *
     * @return the status code message
     */
    public String getStatusCodeMessage() {

        return m_statusCodeMessage;
    }

    /**
     * Returns the URI used for template part inclusion.<p>
     *
     * @return the URI used for template part inclusion
     */
    public String getTemplateUri() {

        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_templateUri)) {
            m_templateUri = "/";
        }
        return m_templateUri;
    }

    /**
     * Include a template part to display on the error page.<p>
     * 
     * @param target the target uri of the file in the OpenCms VFS (can be relative or absolute)
     * @param element the element (template selector) to display from the target
     * 
     * @throws JspException in case there were problems including the target
     */
    public void includeTemplatePart(String target, String element) throws JspException {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -