Created
March 13, 2015 20:14
-
-
Save mp911de/96991d30dbaa2604830e to your computer and use it in GitHub Desktop.
JAX-RS Exception Mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.servlet.http.HttpServletRequest; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Request; | |
import javax.ws.rs.core.Variant; | |
import java.util.List; | |
/** | |
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a> | |
* @since 13.11.14 14:19 | |
*/ | |
public abstract class AbstractExceptionMapper { | |
// Hint: RestConstants is part of my application code | |
protected String getType(Request request) { | |
List<Variant> variants = | |
Variant.mediaTypes(MediaType.valueOf(RestConstants.STATUS_REPRESENTATION_JSON), | |
MediaType.valueOf(RestConstants.STATUS_REPRESENTATION_XML), MediaType.APPLICATION_XML_TYPE, | |
MediaType.APPLICATION_JSON_TYPE).build(); | |
Variant variant = request.selectVariant(variants); | |
if (variant != null && variant.getMediaType() != null) { | |
return variant.getMediaType().toString(); | |
} | |
return MediaType.APPLICATION_XML; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.servlet.http.HttpServletRequest; | |
import javax.ws.rs.WebApplicationException; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.Request; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.ExceptionMapper; | |
import javax.ws.rs.ext.Provider; | |
import org.apache.log4j.Logger; | |
import org.jboss.resteasy.spi.Failure; | |
/** | |
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a> | |
* @since 11.02.14 11:12 | |
*/ | |
@Provider | |
public class DefaultExceptionMapper extends AbstractExceptionMapper implements ExceptionMapper<Exception> { | |
@Context | |
private Request request; | |
@Context | |
private HttpServletRequest servletRequest; | |
@Override | |
public Response toResponse(Exception exception) { | |
Logger LOG = Logger.getLogger(getClass()); | |
if (exception instanceof WebApplicationException) { | |
WebApplicationException wae = (WebApplicationException) exception; | |
if (wae.getResponse().getStatus() >= 400 && wae.getResponse().getStatus() <= 499) { | |
LOG.warn(exception.toString(), exception); | |
} | |
if (wae.getResponse().getStatus() >= 500 && wae.getResponse().getStatus() <= 500) { | |
LOG.error(exception.toString(), exception); | |
} | |
return wae.getResponse(); | |
} | |
if (exception instanceof Failure) { | |
Failure failure = (Failure) exception; | |
int status = failure.getErrorCode(); | |
if (failure.getResponse() != null && failure.getResponse().getStatus() != 0) { | |
status = failure.getResponse().getStatus(); | |
} | |
if (status >= 400 && status <= 499 && failure.isLoggable()) { | |
LOG.warn(exception.toString(), exception); | |
} | |
if (status >= 500 && status <= 500 && failure.isLoggable()) { | |
LOG.error(exception.toString(), exception); | |
} | |
if (failure.getResponse() != null) { | |
return failure.getResponse(); | |
} | |
return Response.status(status).build(); | |
} | |
LOG.error(exception.toString(), exception); | |
// Hint: RestConstants is part of my application code | |
Response.Status status = Response.Status.INTERNAL_SERVER_ERROR; | |
return Response.status(status).entity(new StatusRepresentation(status.getStatusCode(), exception)).type(getType(request)) | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.servlet.http.HttpServletRequest; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.Request; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.ExceptionMapper; | |
import javax.ws.rs.ext.Provider; | |
import org.apache.log4j.Logger; | |
/** | |
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a> | |
* @since 11.02.14 11:12 | |
*/ | |
@Provider | |
public class IllegalArgumentExceptionMapper extends AbstractExceptionMapper implements ExceptionMapper<IllegalArgumentException> { | |
private final static Logger LOG = Logger.getLogger(IllegalArgumentExceptionMapper.class); | |
@Context | |
private Request request; | |
@Context | |
private HttpServletRequest servletRequest; | |
@Override | |
public Response toResponse(IllegalArgumentException exception) { | |
Response.Status status = Response.Status.BAD_REQUEST; | |
LOG.warn(exception.getMessage(), exception); | |
// Hint: StatusRepresentation is part of my application code | |
return Response.status(status).entity(new StatusRepresentation(status.getStatusCode(), exception.getMessage())) | |
.type(getType(request)).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment