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.InvalidResourceIdentifierException;
009import org.slf4j.Logger;
010
011import javax.ws.rs.core.Response;
012import javax.ws.rs.ext.ExceptionMapper;
013import javax.ws.rs.ext.Provider;
014
015import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
016import static javax.ws.rs.core.Response.status;
017import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET;
018import static org.slf4j.LoggerFactory.getLogger;
019
020/**
021 * The class translates {@link org.fcrepo.kernel.api.exception.InvalidResourceIdentifierException}s to its proper
022 * response code.
023 *
024 * @author awoods
025 * @since July 14, 2015.
026 */
027@Provider
028public class InvalidResourceIdentifierExceptionMapper implements
029        ExceptionMapper<InvalidResourceIdentifierException>, ExceptionDebugLogging {
030
031    private static final Logger LOGGER = getLogger(InvalidResourceIdentifierExceptionMapper.class);
032
033    @Override
034    public Response toResponse(final InvalidResourceIdentifierException e) {
035        debugException(this, e, LOGGER);
036        return status(BAD_REQUEST).entity(e.getMessage()).type(TEXT_PLAIN_WITH_CHARSET).build();
037    }
038
039    public Response toResponse(final String msg, final InvalidResourceIdentifierException e) {
040        debugException(this, e, LOGGER);
041        return status(BAD_REQUEST).entity(msg).type(TEXT_PLAIN_WITH_CHARSET).build();
042    }
043}