001/* 002 * (C) Copyright 2006-2011 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.jaxrs.session; 020 021import java.io.PrintWriter; 022import java.io.StringWriter; 023 024import javax.ws.rs.WebApplicationException; 025import javax.ws.rs.core.Response; 026import javax.ws.rs.ext.ExceptionMapper; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.core.api.DocumentNotFoundException; 031import org.nuxeo.ecm.core.api.DocumentSecurityException; 032 033/** 034 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 035 * @deprecated since 9.3. 036 */ 037@Deprecated 038public class CoreExceptionMapper implements ExceptionMapper<Throwable> { 039 040 protected static final Log log = LogFactory.getLog(CoreExceptionMapper.class); 041 042 @Override 043 public Response toResponse(Throwable t) { 044 log.error("Exception in JAX-RS processing", t); 045 if (t instanceof WebApplicationException) { 046 return ((WebApplicationException) t).getResponse(); 047 } else if (t instanceof DocumentSecurityException) { 048 return getResponse(t, 401); 049 } else if (t instanceof DocumentNotFoundException) { 050 return getResponse(t, 404); 051 } 052 return getResponse(t, 500); 053 } 054 055 public static Response getResponse(Throwable t, int status) { 056 String message = status == 500 ? getStackTrace(t) : null; 057 return Response.status(status).entity(message).build(); 058 } 059 060 public static String getStackTrace(Throwable t) { 061 StringWriter sw = new StringWriter(); 062 PrintWriter pw = new PrintWriter(sw); 063 t.printStackTrace(pw); 064 pw.close(); 065 return sw.toString(); 066 } 067 068}