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