001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.http.commons.exceptionhandlers;
007
008import static javax.ws.rs.core.Response.status;
009import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
010import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET;
011import static org.slf4j.LoggerFactory.getLogger;
012
013import javax.ws.rs.core.Response;
014import javax.ws.rs.ext.ExceptionMapper;
015import javax.ws.rs.ext.Provider;
016
017import org.slf4j.Logger;
018
019import com.fasterxml.jackson.core.JsonParseException;
020
021/**
022 * If an injected JSON resource fails to parse, return an HTTP 400 Bad Request.
023 *
024 * @author Esme Cowles
025 * @since 2014-05-21
026 */
027@Provider
028public class JsonParseExceptionMapper implements
029        ExceptionMapper<JsonParseException>, ExceptionDebugLogging {
030
031    private static final Logger LOGGER = getLogger(JsonParseExceptionMapper.class);
032
033    @Override
034    public Response toResponse(final JsonParseException e) {
035        debugException(this, e, LOGGER);
036        return status(BAD_REQUEST).entity(e.getMessage()).type(TEXT_PLAIN_WITH_CHARSET).build();
037    }
038}