001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.web.common.exceptionhandling; 023 024import java.io.IOException; 025 026import javax.servlet.Filter; 027import javax.servlet.FilterChain; 028import javax.servlet.FilterConfig; 029import javax.servlet.ServletException; 030import javax.servlet.ServletRequest; 031import javax.servlet.ServletResponse; 032import javax.servlet.http.HttpServletRequest; 033import javax.servlet.http.HttpServletResponse; 034 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.nuxeo.common.utils.ExceptionUtils; 038import org.nuxeo.ecm.platform.web.common.exceptionhandling.service.ExceptionHandlingService; 039import org.nuxeo.runtime.api.Framework; 040 041public class NuxeoExceptionFilter implements Filter { 042 043 /** 044 * @deprecated use {@link NuxeoExceptionHandler#EXCEPTION_HANDLER_MARKER} 045 */ 046 @Deprecated 047 public static final String EXCEPTION_FILTER_ATTRIBUTE = "NuxeoExceptionFilter"; 048 049 private NuxeoExceptionHandler exceptionHandler; 050 051 private static final Log log = LogFactory.getLog(NuxeoExceptionFilter.class); 052 053 public void init(FilterConfig filterConfig) throws ServletException { 054 try { 055 getHandler(); 056 } catch (ServletException e) { 057 log.info("NuxeoExceptionHandler will be lazy loaded"); 058 } 059 } 060 061 protected NuxeoExceptionHandler getHandler() throws ServletException { 062 if (exceptionHandler == null) { 063 ExceptionHandlingService service = Framework.getService(ExceptionHandlingService.class); 064 exceptionHandler = service.getExceptionHandler(); 065 } 066 return exceptionHandler; 067 } 068 069 private void handleException(HttpServletRequest request, HttpServletResponse response, Exception e) 070 throws IOException, ServletException { 071 getHandler().handleException(request, response, e); 072 } 073 074 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 075 ServletException { 076 try { 077 chain.doFilter(request, response); 078 } catch (RuntimeException | IOException | ServletException e) { 079 try { 080 handleException((HttpServletRequest) request, (HttpServletResponse) response, e); 081 } catch (ServletException ee) { 082 throw ee; 083 } catch (RuntimeException | IOException ee) { 084 throw new ServletException(ee); 085 } 086 } 087 } 088 089 public void destroy() { 090 } 091 092}