org.glassfish.jersey.server.validation.internal.LocalizationMessages Java Examples

The following examples show how to use org.glassfish.jersey.server.validation.internal.LocalizationMessages. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ValidationExceptionMapper.java    From ameba with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Response toResponse(final ConstraintViolationException exception) {
    logger.trace(LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), exception);
    Response.Status status = ValidationHelper.getResponseStatus(exception);
    ErrorMessage errorMessage = ErrorMessage.fromStatus(status.getStatusCode());
    errorMessage.setThrowable(exception);
    errorMessage.setCode(Hashing.murmur3_32().hashUnencodedChars(exception.getClass().getName()).toString());

    List<Result.Error> errors = ValidationHelper.constraintViolationToValidationErrors(exception);

    errorMessage.setErrors(errors);

    return Response.status(status)
            .entity(errorMessage)
            .type(ExceptionMapperUtils.getResponseType())
            .build();
}
 
Example #2
Source File: ValidationExceptionMapper.java    From jaxrs-beanvalidation-javaee7 with Apache License 2.0 5 votes vote down vote up
@Override
public Response toResponse(final ValidationException exception) {
    if (exception instanceof ConstraintViolationException) {
        LOGGER.log(Level.FINER, LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), exception);

        final ConstraintViolationException cve = (ConstraintViolationException) exception;
        final Response.ResponseBuilder response = Response.status(getStatus(cve));

        // Entity
        final List<Variant> variants = Variant.mediaTypes(
                MediaType.APPLICATION_XML_TYPE,
                MediaType.APPLICATION_JSON_TYPE).build();
        final Variant variant = request.get().selectVariant(variants);
        if (variant != null) {
            response.type(variant.getMediaType());
        } else {
            /*
             * default media type which will be used only when none media type from {@value variants} is in
             * accept header of original request.
             */
            response.type(MediaType.TEXT_PLAIN_TYPE);
        }
        response.entity(
                new GenericEntity<List<ValidationError>>(
                        getEntity(cve.getConstraintViolations()),
                        new GenericType<List<ValidationError>>() {}.getType()
                )
        );

        return response.build();
    } else {
        LOGGER.log(Level.WARNING, LocalizationMessages.VALIDATION_EXCEPTION_RAISED(), exception);

        return Response.serverError().entity(exception.getMessage()).build();
    }
}
 
Example #3
Source File: ParsecValidationExceptionMapper.java    From parsec-libraries with Apache License 2.0 3 votes vote down vote up
/**
 * implement toResponse with code, message.
 *
 * @param e the validation exception object
 * @param code the code
 * @param message the message
 *
 * @return the response
 */
private Response toResponse(ConstraintViolationException e, int code, String message) {
    LOGGER.log(Level.FINER, LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), e);
    List<ValidationError> errors = ValidationHelper.constraintViolationToValidationErrors(e);
    ParsecErrorResponse<ValidationError> errorResponse = ValidateUtil.buildErrorResponse(errors, code, message);
    return Response.status(Response.Status.BAD_REQUEST)
            .entity(new GenericEntity<ParsecErrorResponse<ValidationError>>(errorResponse) { }).build();
}