package io.dropwizard.jersey.validation;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import org.glassfish.jersey.server.model.Invocable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.ConstraintViolation;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.Set;
@Provider
public class JerseyViolationExceptionMapper implements ExceptionMapper<JerseyViolationException> {
private static final Logger LOGGER = LoggerFactory.getLogger(JerseyViolationExceptionMapper.class);
@Override
public Response toResponse(final JerseyViolationException exception) {
// Provide a way to log if desired, Issue #2128, PR #2129
LOGGER.debug("Object validation failure", exception);
final Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
final Invocable invocable = exception.getInvocable();
final ImmutableList<String> errors = FluentIterable.from(exception.getConstraintViolations())
.transform(violation -> ConstraintMessage.getMessage(violation, invocable)).toList();
final int status = ConstraintMessage.determineStatus(violations, invocable);
return Response.status(status)
.entity(new ValidationErrorMessage(errors))
.build();
}
}