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.glassfish.jersey.server.ParamException;
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.fromResponse;
016import static org.fcrepo.http.commons.domain.RDFMediaType.TEXT_PLAIN_WITH_CHARSET;
017import static org.slf4j.LoggerFactory.getLogger;
018
019/**
020 * Handle Jersey ParamException
021 *
022 * @author awoods
023 * @since 2015-01-20
024 */
025@Provider
026public class ParamExceptionMapper implements
027        ExceptionMapper<ParamException>, ExceptionDebugLogging {
028
029    private static final Logger LOGGER = getLogger(ParamExceptionMapper.class);
030
031    @Override
032    public Response toResponse(final ParamException e) {
033        debugException(this, e, LOGGER);
034
035        final String msg = "Error parsing parameter: " + e.getParameterName() + ", of type: " +
036                e.getParameterType().getSimpleName();
037        return fromResponse(e.getResponse()).entity(msg).type(TEXT_PLAIN_WITH_CHARSET).build();
038    }
039
040}