001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.commons.exceptionhandlers; 019 020import static com.google.common.base.Throwables.getStackTraceAsString; 021import static javax.ws.rs.core.Response.serverError; 022import static org.slf4j.LoggerFactory.getLogger; 023import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET; 024 025import javax.jcr.RepositoryException; 026import javax.ws.rs.core.Response; 027import javax.ws.rs.ext.ExceptionMapper; 028import javax.ws.rs.ext.Provider; 029 030import org.fcrepo.kernel.api.exception.SessionMissingException; 031import org.slf4j.Logger; 032 033/** 034 * Catch all the exceptions! 035 * 036 * @author lsitu 037 * @author awoods 038 * @author cbeer 039 * @author fasseg 040 */ 041@Provider 042public class WildcardExceptionMapper implements 043 ExceptionMapper<Exception>, ExceptionDebugLogging { 044 045 Boolean showStackTrace = true; 046 047 private static final Logger LOGGER = 048 getLogger(WildcardExceptionMapper.class); 049 050 @Override 051 public Response toResponse(final Exception e) { 052 if (e.getCause() instanceof SessionMissingException) { 053 return new SessionMissingExceptionMapper() 054 .toResponse((SessionMissingException) e.getCause()); 055 } 056 057 if ( e.getCause() instanceof RepositoryException) { 058 return new RepositoryExceptionMapper() 059 .toResponse((RepositoryException)e.getCause()); 060 } 061 062 LOGGER.error("Exception intercepted by WildcardExceptionMapper: {}\n", e.getMessage()); 063 debugException(this, e, LOGGER); 064 return serverError().entity( 065 showStackTrace ? getStackTraceAsString(e) : null).type(TEXT_PLAIN_WITH_CHARSET).build(); 066 } 067 068 /** 069 * Set whether the full stack trace should be returned as part of the 070 * error response. This may be a bad idea if the stack trace is exposed 071 * to the public. 072 * @param showStackTrace the boolean value of showing stack trace 073 */ 074 public void setShowStackTrace(final Boolean showStackTrace) { 075 this.showStackTrace = showStackTrace; 076 } 077}