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; 034import java.util.Arrays; 035import java.util.HashSet; 036import java.util.Set; 037 038import javax.servlet.ServletConfig; 039import javax.servlet.ServletException; 040import javax.servlet.http.HttpServletRequest; 041import javax.servlet.http.HttpServletResponse; 042 043/** 044 * This the error handler servlet of the OpenCms system.<p> 045 * 046 * This almost 1:1 extends the "standard" {@link org.opencms.main.OpenCmsServlet}. 047 * By default, all errors are handled by this servlet, which is controlled by the 048 * setting in the shipped <code>web.xml</code>.<p> 049 * 050 * This servlet is required because certain servlet containers (eg. BEA Weblogic) 051 * can not handler the error with the same servlet that produced the error.<p> 052 * 053 * @since 6.2.0 054 * 055 * @see org.opencms.main.OpenCmsServlet 056 * @see org.opencms.staticexport.CmsStaticExportManager 057 */ 058public class OpenCmsServletErrorHandler extends OpenCmsServlet { 059 060 /** Serial version UID required for safe serialization. */ 061 private static final long serialVersionUID = 5316004893684482816L; 062 063 /** HTTP methods for which we want normal handling (rather than just setting the error code) .*/ 064 private static Set<String> defaultMethods = new HashSet<>(Arrays.asList("GET", "HEAD", "POST")); 065 066 /** 067 * OpenCms servlet main request handling method.<p> 068 * 069 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 070 */ 071 @Override 072 public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { 073 074 // check the error status 075 Integer errorStatus = (Integer)req.getAttribute(CmsJspStatusBean.ERROR_STATUS_CODE); 076 if (errorStatus != null) { 077 // only use super method if an error status code is set 078 if (OpenCmsCore.getInstance().getRunLevel() > OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 079 // use super method if servlet run level is available 080 super.doGet(req, res); 081 } else { 082 // otherwise display a simple error page 083 String errorMessage = (String)req.getAttribute(CmsJspStatusBean.ERROR_MESSAGE); 084 if (CmsStringUtil.isEmptyOrWhitespaceOnly(errorMessage)) { 085 errorMessage = ""; 086 } 087 String output = "<html><body>" 088 + CmsStringUtil.escapeHtml( 089 Messages.get().getBundle().key( 090 Messages.ERR_OPENCMS_NOT_INITIALIZED_2, 091 errorStatus, 092 errorMessage)) 093 + "</body></html>"; 094 res.setStatus(errorStatus.intValue()); 095 res.getWriter().println(output); 096 } 097 } else { 098 // no status code set, this is an invalid request 099 res.sendError(HttpServletResponse.SC_FORBIDDEN); 100 } 101 } 102 103 /** 104 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) 105 */ 106 @Override 107 public synchronized void init(ServletConfig config) { 108 109 // override super class to avoid default initialization 110 } 111 112 /** 113 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 114 */ 115 @Override 116 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 117 118 // the service() method of HttpServlet sets a status code of 405 for unimplemented / unsupported methods, 119 // which is a problem for servlets using non-standard methods like the WebDAV servlet (in the case of WebDAV, 120 // it might break clients which rely on status 404 to be returned for PROPFIND requests to nonexistent resources). 121 // So we just set the HTTP response status to the original status for all methods except GET/POST. 122 123 if (defaultMethods.contains(req.getMethod())) { 124 super.service(req, resp); 125 } else { 126 Integer errorStatus = (Integer)req.getAttribute(CmsJspStatusBean.ERROR_STATUS_CODE); 127 if (errorStatus != null) { 128 resp.setStatus(errorStatus.intValue()); 129 } 130 } 131 132 } 133 134}