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 org.fcrepo.kernel.api.exception.CannotCreateMementoException;
009import org.slf4j.Logger;
010
011import javax.servlet.ServletContext;
012import javax.ws.rs.core.Context;
013import javax.ws.rs.core.Link;
014import javax.ws.rs.core.Response;
015import javax.ws.rs.core.UriInfo;
016import javax.ws.rs.ext.Provider;
017
018import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
019import static javax.ws.rs.core.Response.status;
020import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET;
021import static org.slf4j.LoggerFactory.getLogger;
022
023/**
024 * A mapper for memento creation exceptions
025 *
026 * @author dbernstein
027 */
028@Provider
029public class CannotCreateMementoExceptionMapper extends ConstraintExceptionMapper<CannotCreateMementoException>
030        implements ExceptionDebugLogging {
031
032    private static final Logger LOGGER = getLogger(CannotCreateMementoExceptionMapper.class);
033
034    @Context
035    private UriInfo uriInfo;
036
037    @Context
038    private ServletContext context;
039
040    @Override
041    public Response toResponse(final CannotCreateMementoException 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}