001/* 002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 * bstefanescu 016 * vpasquier 017 */ 018package org.nuxeo.ecm.webengine.app; 019 020import org.nuxeo.ecm.webengine.WebException; 021 022import javax.ws.rs.Produces; 023import javax.ws.rs.WebApplicationException; 024import javax.ws.rs.core.MediaType; 025import javax.ws.rs.core.MultivaluedMap; 026import javax.ws.rs.ext.MessageBodyWriter; 027import javax.ws.rs.ext.Provider; 028import java.io.IOException; 029import java.io.OutputStream; 030import java.lang.annotation.Annotation; 031import java.lang.reflect.Type; 032 033/** 034 * @since 6.0 035 */ 036@Provider 037@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON + "+nxentity" }) 038public class JsonExceptionWriter implements MessageBodyWriter<WebException> { 039 040 @Override 041 public long getSize(WebException arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) { 042 return -1; 043 } 044 045 @Override 046 public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) { 047 return WebException.class.isAssignableFrom(arg0); 048 } 049 050 @Override 051 public void writeTo(WebException webException, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, 052 MultivaluedMap<String, Object> arg5, OutputStream outputStream) throws IOException, WebApplicationException { 053 JsonWebengineWriter.writeException(outputStream, webException, mediaType); 054 } 055 056}