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 org.fcrepo.kernel.api.exception.ConstraintViolationException;
014
015import org.slf4j.Logger;
016
017import javax.servlet.ServletContext;
018import javax.ws.rs.core.Context;
019import javax.ws.rs.core.Link;
020import javax.ws.rs.core.Response;
021import javax.ws.rs.core.UriInfo;
022import javax.ws.rs.ext.Provider;
023
024/**
025 * @author whikloj
026 * @since 2015-06-01
027 */
028@Provider
029public class ConstraintViolationExceptionMapper extends ConstraintExceptionMapper<ConstraintViolationException>
030    implements ExceptionDebugLogging {
031
032    private static final Logger LOGGER = getLogger(ConstraintViolationExceptionMapper.class);
033
034    @Context
035    private UriInfo uriInfo;
036
037    @Context
038    private ServletContext context;
039
040    @Override
041    public Response toResponse(final ConstraintViolationException e) {
042        debugException(this, e, LOGGER);
043        final Link link = buildConstraintLink(e, context, uriInfo);
044        final String msg = e.getMessage();
045        return status(BAD_REQUEST).entity(msg).links(link).type(TEXT_PLAIN_WITH_CHARSET).build();
046    }
047
048}