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.TombstoneException;
009import org.slf4j.Logger;
010
011import javax.ws.rs.core.Link;
012import javax.ws.rs.core.Response;
013import javax.ws.rs.ext.ExceptionMapper;
014import javax.ws.rs.ext.Provider;
015
016import static javax.ws.rs.core.HttpHeaders.LINK;
017import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
018import static javax.ws.rs.core.Response.Status.GONE;
019import static javax.ws.rs.core.Response.status;
020import static org.slf4j.LoggerFactory.getLogger;
021
022/**
023 * @author cabeer
024 * @since 10/16/14
025 */
026@Provider
027public class TombstoneExceptionMapper implements
028        ExceptionMapper<TombstoneException>, ExceptionDebugLogging {
029
030    private static final Logger LOGGER =
031            getLogger(TombstoneExceptionMapper.class);
032
033    @Override
034    public Response toResponse(final TombstoneException e) {
035        LOGGER.debug(e.getMessage());
036        final Response.ResponseBuilder response = status(GONE)
037                .entity(e.getMessage());
038
039        if (e.getTombstoneURI() != null) {
040            response.link(e.getTombstoneURI(), "hasTombstone");
041        }
042        if (e.getTimemapUri() != null) {
043            response.header(LINK, Link.fromUri(e.getTimemapUri()).rel("timemap").build().toString());
044        }
045
046        return response.type(TEXT_PLAIN_TYPE).build();
047    }
048}