001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006package org.fcrepo.http.commons.exceptionhandlers; 007 008import org.slf4j.Logger; 009 010import javax.ws.rs.WebApplicationException; 011import javax.ws.rs.core.Response; 012import javax.ws.rs.ext.ExceptionMapper; 013import javax.ws.rs.ext.Provider; 014 015import static javax.ws.rs.core.Response.fromResponse; 016import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET; 017import static org.slf4j.LoggerFactory.getLogger; 018 019/** 020 * Handle Jersey WebApplicationException 021 * 022 * @author lsitu 023 */ 024@Provider 025public class WebApplicationExceptionMapper implements 026 ExceptionMapper<WebApplicationException>, ExceptionDebugLogging { 027 028 private static final Logger LOGGER = 029 getLogger(WebApplicationExceptionMapper.class); 030 031 @Override 032 public Response toResponse(final WebApplicationException e) { 033 LOGGER.warn("Web application error", e); 034 final String msg = null == e.getCause() ? e.getMessage() : e.getCause().getMessage(); 035 // 204, 205, 304 MUST NOT contain an entity body - RFC2616 036 switch (e.getResponse().getStatus()) { 037 case 204: 038 case 205: 039 case 304: 040 return fromResponse(e.getResponse()).entity(null).build(); 041 default: 042 return fromResponse(e.getResponse()).entity(msg).type(TEXT_PLAIN_WITH_CHARSET).build(); 043 } 044 } 045}