2018-08-16 16:53:20 +02:00
|
|
|
package sonia.scm.api.v2;
|
|
|
|
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import org.jboss.resteasy.api.validation.ResteasyViolationException;
|
2018-10-26 09:47:30 +02:00
|
|
|
import org.slf4j.MDC;
|
|
|
|
|
import sonia.scm.api.v2.resources.ErrorDto;
|
2018-08-16 16:53:20 +02:00
|
|
|
|
|
|
|
|
import javax.validation.ConstraintViolation;
|
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
import javax.ws.rs.ext.ExceptionMapper;
|
|
|
|
|
import javax.ws.rs.ext.Provider;
|
|
|
|
|
import javax.xml.bind.annotation.XmlElement;
|
|
|
|
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
|
|
|
|
import javax.xml.bind.annotation.XmlRootElement;
|
2018-10-26 09:47:30 +02:00
|
|
|
import java.util.Collections;
|
2018-08-16 16:53:20 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
2018-10-26 09:47:30 +02:00
|
|
|
import static java.util.Collections.emptyList;
|
|
|
|
|
|
2018-08-16 16:53:20 +02:00
|
|
|
@Provider
|
|
|
|
|
public class ValidationExceptionMapper implements ExceptionMapper<ResteasyViolationException> {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Response toResponse(ResteasyViolationException exception) {
|
|
|
|
|
|
2018-10-26 09:47:30 +02:00
|
|
|
List<ConstraintViolationDto> violations =
|
2018-08-16 16:53:20 +02:00
|
|
|
exception.getConstraintViolations()
|
|
|
|
|
.stream()
|
2018-10-26 09:47:30 +02:00
|
|
|
.map(ConstraintViolationDto::new)
|
2018-08-16 16:53:20 +02:00
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
return Response
|
|
|
|
|
.status(Response.Status.BAD_REQUEST)
|
|
|
|
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
2018-10-26 09:47:30 +02:00
|
|
|
.entity(new ValidationErrorDto(violations))
|
2018-08-16 16:53:20 +02:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Getter
|
2018-10-26 09:47:30 +02:00
|
|
|
public static class ValidationErrorDto extends ErrorDto {
|
2018-08-16 16:53:20 +02:00
|
|
|
@XmlElement(name = "violation")
|
|
|
|
|
@XmlElementWrapper(name = "violations")
|
2018-10-26 09:47:30 +02:00
|
|
|
private List<ConstraintViolationDto> violations;
|
2018-08-16 16:53:20 +02:00
|
|
|
|
2018-10-26 09:47:30 +02:00
|
|
|
public ValidationErrorDto(List<ConstraintViolationDto> violations) {
|
|
|
|
|
super(MDC.get("transaction_id"), "1wR7ZBe7H1", emptyList(), "input violates conditions (see violation list)");
|
2018-08-27 14:19:07 +02:00
|
|
|
this.violations = violations;
|
2018-08-16 16:53:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@XmlRootElement(name = "violation")
|
|
|
|
|
@Getter
|
2018-10-26 09:47:30 +02:00
|
|
|
public static class ConstraintViolationDto {
|
2018-08-16 16:53:20 +02:00
|
|
|
private String path;
|
|
|
|
|
private String message;
|
|
|
|
|
|
2018-10-26 09:47:30 +02:00
|
|
|
public ConstraintViolationDto(ConstraintViolation<?> violation) {
|
2018-08-16 16:53:20 +02:00
|
|
|
message = violation.getMessage();
|
|
|
|
|
path = violation.getPropertyPath().toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|