001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.main;
029
030import org.opencms.jsp.util.CmsJspStatusBean;
031import org.opencms.util.CmsStringUtil;
032
033import java.io.IOException;
034
035import javax.servlet.ServletConfig;
036import javax.servlet.ServletException;
037import javax.servlet.http.HttpServletRequest;
038import javax.servlet.http.HttpServletResponse;
039
040/**
041 * This the error handler servlet of the OpenCms system.<p>
042 *
043 * This almost 1:1 extends the "standard" {@link org.opencms.main.OpenCmsServlet}.
044 * By default, all errors are handled by this servlet, which is controlled by the
045 * setting in the shipped <code>web.xml</code>.<p>
046 *
047 * This servlet is required because certain servlet containers (eg. BEA Weblogic)
048 * can not handler the error with the same servlet that produced the error.<p>
049 *
050 * @since 6.2.0
051 *
052 * @see org.opencms.main.OpenCmsServlet
053 * @see org.opencms.staticexport.CmsStaticExportManager
054 */
055public class OpenCmsServletErrorHandler extends OpenCmsServlet {
056
057    /** Serial version UID required for safe serialization. */
058    private static final long serialVersionUID = 5316004893684482816L;
059
060    /**
061     * OpenCms servlet main request handling method.<p>
062     *
063     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
064     */
065    @Override
066    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
067
068        // check the error status
069        Integer errorStatus = (Integer)req.getAttribute(CmsJspStatusBean.ERROR_STATUS_CODE);
070        if (errorStatus != null) {
071            // only use super method if an error status code is set
072            if (OpenCmsCore.getInstance().getRunLevel() > OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
073                // use super method if servlet run level is available
074                super.doGet(req, res);
075            } else {
076                // otherwise display a simple error page
077                String errorMessage = (String)req.getAttribute(CmsJspStatusBean.ERROR_MESSAGE);
078                if (CmsStringUtil.isEmptyOrWhitespaceOnly(errorMessage)) {
079                    errorMessage = "";
080                }
081                String output = "<html><body>"
082                    + CmsStringUtil.escapeHtml(
083                        Messages.get().getBundle().key(
084                            Messages.ERR_OPENCMS_NOT_INITIALIZED_2,
085                            errorStatus,
086                            errorMessage))
087                    + "</body></html>";
088                res.setStatus(errorStatus.intValue());
089                res.getWriter().println(output);
090            }
091        } else {
092            // no status code set, this is an invalid request
093            res.sendError(HttpServletResponse.SC_FORBIDDEN);
094        }
095    }
096
097    /**
098     * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
099     */
100    @Override
101    public synchronized void init(ServletConfig config) {
102
103        // override super class to avoid default initialization
104    }
105}