001/* 002 * (C) Copyright 2006-2008 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 * arussel 016 */ 017package org.nuxeo.ecm.platform.web.common.exceptionhandling.service; 018 019import java.io.IOException; 020import java.util.List; 021 022import javax.servlet.ServletException; 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpServletResponse; 025 026import org.nuxeo.ecm.platform.web.common.exceptionhandling.NuxeoExceptionHandler; 027import org.nuxeo.ecm.platform.web.common.exceptionhandling.NuxeoExceptionHandlerParameters; 028import org.nuxeo.ecm.platform.web.common.exceptionhandling.descriptor.ErrorHandlersDescriptor; 029import org.nuxeo.ecm.platform.web.common.exceptionhandling.descriptor.ExceptionHandlerDescriptor; 030import org.nuxeo.ecm.platform.web.common.exceptionhandling.descriptor.ListenerDescriptor; 031import org.nuxeo.ecm.platform.web.common.exceptionhandling.descriptor.RequestDumpDescriptor; 032import org.nuxeo.runtime.model.ComponentInstance; 033import org.nuxeo.runtime.model.DefaultComponent; 034 035/** 036 * @author arussel, Benjamin JALON 037 */ 038public class ExceptionHandlingComponent extends DefaultComponent implements ExceptionHandlingService { 039 040 protected NuxeoExceptionHandler exceptionHandler; 041 042 protected final NuxeoExceptionHandlerParameters exceptionHandlerParameters = new NuxeoExceptionHandlerParameters(); 043 044 public enum ExtensionPoint { 045 exceptionhandler, errorhandlers, requestdump, listener 046 } 047 048 @Override 049 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 050 ExtensionPoint ep = Enum.valueOf(ExtensionPoint.class, extensionPoint); 051 switch (ep) { 052 case exceptionhandler: 053 ExceptionHandlerDescriptor ehd = (ExceptionHandlerDescriptor) contribution; 054 exceptionHandler = newInstance(ehd.getKlass()); 055 exceptionHandler.setParameters(exceptionHandlerParameters); 056 break; 057 case errorhandlers: 058 ErrorHandlersDescriptor md = (ErrorHandlersDescriptor) contribution; 059 exceptionHandlerParameters.setBundleName(md.getBundle()); 060 exceptionHandlerParameters.setHandlers(md.getMessages()); 061 exceptionHandlerParameters.setLoggerName(md.getLoggerName()); 062 exceptionHandlerParameters.setDefaultErrorPage(md.getDefaultPage()); 063 break; 064 case requestdump: 065 RequestDumpDescriptor rdd = (RequestDumpDescriptor) contribution; 066 RequestDumper dumper = newInstance(rdd.getKlass()); 067 List<String> attributes = rdd.getAttributes(); 068 dumper.setNotListedAttributes(attributes); 069 exceptionHandlerParameters.setRequestDumper(dumper); 070 break; 071 case listener: 072 ListenerDescriptor ld = (ListenerDescriptor) contribution; 073 exceptionHandlerParameters.setListener(newInstance(ld.getKlass())); 074 break; 075 default: 076 throw new RuntimeException("error in exception handling configuration"); 077 } 078 } 079 080 protected <T> T newInstance(Class<T> klass) { 081 try { 082 return klass.newInstance(); 083 } catch (ReflectiveOperationException e) { 084 throw new RuntimeException(e); 085 } 086 } 087 088 public void forwardToErrorPage(HttpServletRequest request, HttpServletResponse response, Throwable t) 089 throws IOException, ServletException { 090 exceptionHandler.handleException(request, response, t); 091 } 092 093 public NuxeoExceptionHandler getExceptionHandler() { 094 return exceptionHandler; 095 } 096 097}