001/* 002 * (C) Copyright 2006-2010 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 * bstefanescu 018 */ 019package org.nuxeo.ecm.webengine.app; 020 021import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; 022 023import javax.ws.rs.core.Context; 024import javax.ws.rs.core.HttpHeaders; 025import javax.ws.rs.core.Response; 026import javax.ws.rs.core.Response.Status; 027import javax.ws.rs.ext.ExceptionMapper; 028import javax.ws.rs.ext.Provider; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.ecm.core.api.validation.DocumentValidationException; 033import org.nuxeo.ecm.webengine.WebException; 034import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 035import org.nuxeo.runtime.transaction.TransactionHelper; 036 037import com.sun.jersey.api.NotFoundException; 038 039/** 040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 041 */ 042@Provider 043public class WebEngineExceptionMapper implements ExceptionMapper<Throwable> { 044 045 @Context 046 HttpHeaders headers; 047 048 protected static final Log log = LogFactory.getLog(WebEngineExceptionMapper.class); 049 050 @Override 051 public Response toResponse(Throwable cause) { 052 TransactionHelper.setTransactionRollbackOnly(); 053 if (headers.getAcceptableMediaTypes().contains(APPLICATION_JSON_TYPE)) { 054 if (cause instanceof DocumentValidationException) { 055 DocumentValidationException dve = (DocumentValidationException) cause; 056 return Response.status(Status.BAD_REQUEST).entity(dve.getReport()).build(); 057 } 058 } 059 if (cause instanceof NotFoundException) { 060 NotFoundException nfe = (NotFoundException) cause; 061 log.debug("JAX-RS 404 Not Found: " + nfe.getNotFoundUri()); 062 } else if (cause instanceof WebResourceNotFoundException) { 063 WebResourceNotFoundException nfe = (WebResourceNotFoundException) cause; 064 log.debug("JAX-RS 404 Not Found: " + nfe.getMessage()); 065 } else { 066 log.warn("Exception in JAX-RS processing", cause); 067 } 068 return WebException.newException(cause.getMessage(), WebException.wrap(cause)).toResponse(); 069 } 070 071}