Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST Forms Messages and Validations #70

Merged
merged 1 commit into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ar.edu.itba.paw.webapp.exceptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

private static final Logger LOGGER = LoggerFactory.getLogger(ConstraintViolationExceptionMapper.class);

private static final int LEAF_POSITION = 2;

@Override
public Response toResponse(final ConstraintViolationException e) {
LOGGER.warn("Exception: {}", (Object[]) e.getStackTrace());
final StringBuilder responseEntity = new StringBuilder();

responseEntity.append("{ errors: [\n");
for (ConstraintViolation<?> cv : e.getConstraintViolations()) {
final String propertyName;
final String[] propertyNodes = cv.getPropertyPath().toString().split("\\.");

if(propertyNodes.length == 3) {
propertyName = propertyNodes[LEAF_POSITION];
} else {
propertyName = propertyNodes[1];
}

LOGGER.debug("propertyname = " + propertyName);

responseEntity.append("{");
responseEntity
.append("\"")
.append(propertyName)
.append("\"")
.append(":")
.append("\"")
.append(cv.getMessage())
.append("\"")
.append("},")
.append("\n");
}
responseEntity.append("] }");
return Response.status(Response.Status.BAD_REQUEST).entity(responseEntity.toString()).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ar.edu.itba.paw.webapp.exceptions;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import static org.glassfish.jersey.server.ParamException.QueryParamException;

@Provider
public class QueryParamExceptionMapper implements ExceptionMapper<QueryParamException> {
@Override
public Response toResponse(final QueryParamException exception) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
}

This file was deleted.

14 changes: 6 additions & 8 deletions webapp/src/main/resources/ValidationMessages.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Jersey Bean Validations

javax.validation.constraints.Min.message=must be greater than or equal to {value}
javax.validation.constraints.Digits=must have {integer} digits
org.hibernate.validator.constraints.NotBlank=must not be empty
javax.validation.constraints.NotNull=must not be null
javax.validation.constraints.Size=must be between {min} and {max} characters long
javax.validation.constraints.Pattern=must follow the pattern: {regexp}
javax.validation.constraints.Min.message=debe ser igual o mayor a {value}
javax.validation.constraints.Digits.message=debe poseer {integer} dígitos
org.hibernate.validator.constraints.NotBlank.message=no debe estar vacío
javax.validation.constraints.NotNull.message=no debe ser nulo
javax.validation.constraints.Size.message=debe tener entre {min} y {max} caracteres
javax.validation.constraints.Pattern.message=debe poseer la siguiente expresión regular: {regexp}