001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.web.common.exceptionhandling; 021 022import java.io.IOException; 023 024import javax.servlet.Filter; 025import javax.servlet.FilterChain; 026import javax.servlet.FilterConfig; 027import javax.servlet.ServletException; 028import javax.servlet.ServletRequest; 029import javax.servlet.ServletResponse; 030import javax.servlet.http.HttpServletRequest; 031import javax.servlet.http.HttpServletResponse; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.common.utils.ExceptionUtils; 036import org.nuxeo.ecm.platform.web.common.exceptionhandling.service.ExceptionHandlingService; 037import org.nuxeo.runtime.api.Framework; 038 039public class NuxeoExceptionFilter implements Filter { 040 041 /** 042 * @deprecated use {@link NuxeoExceptionHandler#EXCEPTION_HANDLER_MARKER} 043 */ 044 @Deprecated 045 public static final String EXCEPTION_FILTER_ATTRIBUTE = "NuxeoExceptionFilter"; 046 047 private NuxeoExceptionHandler exceptionHandler; 048 049 private static final Log log = LogFactory.getLog(NuxeoExceptionFilter.class); 050 051 public void init(FilterConfig filterConfig) throws ServletException { 052 try { 053 getHandler(); 054 } catch (ServletException e) { 055 log.info("NuxeoExceptionHandler will be lazy loaded"); 056 } 057 } 058 059 protected NuxeoExceptionHandler getHandler() throws ServletException { 060 if (exceptionHandler == null) { 061 ExceptionHandlingService service = Framework.getService(ExceptionHandlingService.class); 062 exceptionHandler = service.getExceptionHandler(); 063 } 064 return exceptionHandler; 065 } 066 067 private void handleException(HttpServletRequest request, HttpServletResponse response, Exception e) 068 throws IOException, ServletException { 069 getHandler().handleException(request, response, e); 070 } 071 072 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 073 ServletException { 074 try { 075 chain.doFilter(request, response); 076 } catch (RuntimeException | IOException | ServletException e) { 077 try { 078 handleException((HttpServletRequest) request, (HttpServletResponse) response, e); 079 } catch (ServletException ee) { 080 throw ee; 081 } catch (RuntimeException | IOException ee) { 082 throw new ServletException(ee); 083 } 084 } 085 } 086 087 public void destroy() { 088 } 089 090}