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;
007import static javax.ws.rs.core.Response.status;
008import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
009import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET;
010import static org.slf4j.LoggerFactory.getLogger;
011
012import org.fcrepo.kernel.api.exception.ExternalMessageBodyException;
013import org.slf4j.Logger;
014
015import javax.servlet.ServletContext;
016import javax.ws.rs.core.Context;
017import javax.ws.rs.core.Link;
018import javax.ws.rs.core.Response;
019import javax.ws.rs.core.UriInfo;
020import javax.ws.rs.ext.Provider;
021
022/**
023 * If an External Content Link header isn't formatted correctly or missing something, return
024 * a Bad Request error.
025 *
026 * @author bseeger
027 * @since 4/18/18.
028 */
029@Provider
030public class ExternalMessageBodyExceptionMapper extends ConstraintExceptionMapper<ExternalMessageBodyException>
031        implements ExceptionDebugLogging {
032
033    private static final Logger LOGGER = getLogger(ExternalMessageBodyExceptionMapper.class);
034
035    @Context
036    private UriInfo uriInfo;
037
038    @Context
039    private ServletContext context;
040
041    @Override
042    public Response toResponse(final ExternalMessageBodyException e) {
043        debugException(this, e, LOGGER);
044        final Link link = buildConstraintLink(e, context, uriInfo);
045        final String msg = e.getMessage();
046        return status(BAD_REQUEST).entity(msg).links(link).type(TEXT_PLAIN_WITH_CHARSET).build();
047    }
048}