001/* 002 * (C) Copyright 2006-2009 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 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.webdav; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024 025import javax.ws.rs.WebApplicationException; 026import javax.ws.rs.core.Response; 027import javax.ws.rs.ext.ExceptionMapper; 028import javax.ws.rs.ext.Provider; 029 030import java.io.PrintWriter; 031import java.io.StringWriter; 032 033/** 034 * Simple error handler to give back a user-readable status, and log it to the console. 035 * <p> 036 * This is a convenience for trouble-shouting. 037 */ 038@Provider 039public class ExceptionHandler implements ExceptionMapper<Exception> { 040 041 private static final Log log = LogFactory.getLog(ExceptionHandler.class); 042 043 @Override 044 public Response toResponse(Exception e) { 045 StringWriter sw = new StringWriter(); 046 e.printStackTrace(new PrintWriter(sw)); 047 int status = 500; 048 // String msg; 049 if (e instanceof WebApplicationException) { 050 status = ((WebApplicationException) e).getResponse().getStatus(); 051 if (status < 400 || status >= 500) { 052 log.error("Status = " + status); 053 log.error(e, e); 054 } 055 // msg = "Error " + status + "\n" + e.getMessage() + "\n" + sw; 056 } else { 057 log.error(e, e); 058 // msg = "Error\n\n" + e.getMessage() + "\n\n" + sw; 059 } 060 return Response.status(status).build(); 061 } 062 063}