@POST @ValidateOnExecution(type = ExecutableType.NONE) public Response formPost(@Valid @BeanParam FormDataBean form) { final BindingResult vr = getVr(); if (vr.isFailed()) { ValidationError validationError = vr.getAllValidationErrors().iterator().next(); final ConstraintViolation<?> cv = validationError.getViolation(); final String property = cv.getPropertyPath().toString(); error.setProperty(property.substring(property.lastIndexOf('.') + 1)); error.setValue(cv.getInvalidValue()); error.setMessage(cv.getMessage()); error.setParam(validationError.getParamName()); return Response.status(BAD_REQUEST).entity("error.jsp").build(); } return Response.status(OK).entity("data.jsp").build(); }
@POST @Controller @ValidateOnExecution(type = ExecutableType.NONE) public Response formPost(@Valid @BeanParam FormDataBean form) { if (br.isFailed()) { ValidationError validationError = br.getAllValidationErrors().iterator().next(); final ConstraintViolation<?> cv = validationError.getViolation(); final String property = cv.getPropertyPath().toString(); error.setProperty(property.substring(property.lastIndexOf('.') + 1)); error.setValue(cv.getInvalidValue()); error.setMessage(cv.getMessage()); error.setParam(validationError.getParamName()); return Response.status(BAD_REQUEST).entity("error.jsp").build(); } return Response.status(OK).entity("data.jsp").build(); }
@POST @Path("/person") @ValidateOnExecution(type = ExecutableType.NONE) @CsrfValid public String addPerson(@BeanParam @Valid Person person) { if (bindingResult.isFailed()) { models.put("messages", bindingResult.getAllMessages()); } else { dataStore.addPerson(person); } List<Person> personList = dataStore.getPersonList(); models.put("personList", personList); return "person.jsp"; }
@RolesAllowed({"user", "admin"}) @Path("/{id}") @GET @Produces(MediaType.APPLICATION_JSON) @ValidateOnExecution public User getUser(@NotNull @PathParam("id") long id) throws EntityNotFoundException { if (securityContext.isUserInRole("admin")) { return dao.getUser(id); } else { User user = dao.getUser(id); if (user.getUsername().equals(securityContext.getUserPrincipal().getName())) { return user; } else { throw new NotAllowedException("Not allowed "); } } }
@POST @ValidateOnExecution(type = ExecutableType.NONE) public Response formPost(@Valid @BeanParam HelloBean form) { if (validationResult.isFailed()) { validationResult.getAllViolations().stream() .forEach(v -> { final String p = v.getPropertyPath().toString(); models.put(p.substring(p.lastIndexOf('.') + 1), v.getMessage()); }); models.put("form", form); return Response.status(BAD_REQUEST).entity("form.jsp").build(); } models.put("name", form.getFirstName() + " " + form.getLastName()); return Response.status(OK).entity("hello.jsp").build(); }
@POST @Path("new") @Controller @ValidateOnExecution(type = ExecutableType.NONE) @CsrfValid public String createProduct(@Valid @BeanParam Product entity) { if (validationResult.isFailed()) { return ValidationUtil.getResponse(validationResult, error); } facade.create(entity); return "redirect:product/list"; }
@POST @Path("update") @Controller @ValidateOnExecution(type = ExecutableType.NONE) @CsrfValid public String updateProduct(@Valid @BeanParam Product entity) { if (validationResult.isFailed()) { return ValidationUtil.getResponse(validationResult, error); } facade.edit(entity); return "redirect:product/list"; }
@POST @Path("new") @Controller @ValidateOnExecution(type = ExecutableType.NONE) @CsrfValid public String createProductOrder(@Valid @BeanParam ProductOrder entity) { if (validationResult.isFailed()) { return ValidationUtil.getResponse(validationResult, error); } facade.create(entity); return "redirect:productOrder/list"; }
@POST @Path("update") @Controller @ValidateOnExecution(type = ExecutableType.NONE) @CsrfValid public String updateProductOrder(@Valid @BeanParam ProductOrder entity) { if (validationResult.isFailed()) { return ValidationUtil.getResponse(validationResult, error); } facade.edit(entity); return "redirect:productOrder/list"; }
@POST @Path("new") @Controller @ValidateOnExecution(type = ExecutableType.NONE) @CsrfValid public String createCustomer(@Valid @BeanParam Customer entity) { if (validationResult.isFailed()) { return ValidationUtil.getResponse(validationResult, error); } facade.create(entity); return "redirect:customer/list"; }
@POST @Path("update") @Controller @ValidateOnExecution(type = ExecutableType.NONE) @CsrfValid public String updateCustomer(@Valid @BeanParam Customer entity) { if (validationResult.isFailed()) { return ValidationUtil.getResponse(validationResult, error); } facade.edit(entity); return "redirect:customer/list"; }
@POST @CsrfValid @ValidateOnExecution(type = ExecutableType.NONE) public Response save(@Valid @BeanParam TaskForm form) { log.log(Level.INFO, "saving new task @{0}", form); if (validationResult.isFailed()) { AlertMessage alert = AlertMessage.danger("Validation voilations!"); validationResult.getAllViolations() .stream() .forEach((ConstraintViolation t) -> { String path = t.getPropertyPath().toString(); alert.addError(path.substring(path.lastIndexOf(".") + 1), "", t.getMessage()); }); models.put("errors", alert); return Response.status(BAD_REQUEST).entity("add.jspx").build(); } Task task = new Task(); task.setName(form.getName()); task.setDescription(form.getDescription()); task.setDueDate(form.getDueDate()); taskRepository.save(task); flashMessage.notify(Type.success, "Task was created successfully!"); //models.put("flashMessage", flashMessage); return Response.ok("redirect:tasks").build(); }
@POST //@CsrfValid @ValidateOnExecution(type = ExecutableType.NONE) public Response save(@Valid @BeanParam TaskForm form) { log.log(Level.INFO, "saving new task @{0}", form); if (validationResult.isFailed()) { AlertMessage alert = AlertMessage.danger("Validation voilations!"); validationResult.getAllViolations() .stream() .forEach((ConstraintViolation t) -> { String path = t.getPropertyPath().toString(); alert.addError(path.substring(path.lastIndexOf(".") + 1), "", t.getMessage()); }); models.put("errors", alert); return Response.status(BAD_REQUEST).entity("add.xhtml").build(); } Task task = new Task(); task.setName(form.getName()); task.setDescription(form.getDescription()); taskRepository.save(task); flashMessage.notify(Type.success, "Task was created successfully!"); //models.put("flashMessage", flashMessage); return Response.ok("redirect:tasks").build(); }
@RolesAllowed({"admin"}) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @ValidateOnExecution public User addUser(@Valid @NotNull User user) { return dao.addUser(user); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @ValidateOnExecution public Response addCoffee(@Valid Coffee coffee) { int order = CoffeeService.addCoffee(coffee); Coffee c = CoffeeService.getCoffee(order); return Response.created(uriInfo.getAbsolutePath()) .entity(c).build(); }
@Override protected void configureAop(final ValidationMethodInterceptor interceptor) { /* Using explicit annotation matching because annotations may be used just for compile time checks (see hibernate validator apt lib). Moreover it makes "automatic" validation more explicit. Such annotation usage is contradict with its javadoc, but annotation name is ideal for use case, so why introduce new one).*/ bindInterceptor(Matchers.any(), Matchers.annotatedWith(ValidateOnExecution.class), interceptor); bindInterceptor(Matchers.annotatedWith(ValidateOnExecution.class), Matchers.any(), interceptor); }
@POST @ValidateOnExecution(type = ExecutableType.NONE) public String post(@Valid @BeanParam FormBean form) { if (bindingResult.isFailed()) { List<String> errors = bindingResult.getAllValidationErrors().stream() .map(ValidationError::getMessage) .collect(Collectors.toList()); models.put("errors", errors); } return "form.jsp"; }
@ValidateOnExecution public void enabledValidation(@NotNull Object object) { }
@ValidateOnExecution public void doAction(@Valid ComplexBean bean) { }