Ejemplo n.º 1
0
 public static BaseResponse createResponse(HttpStatus status, BindingResult result) {
   BaseResponse response = new BaseResponse();
   if (result.hasErrors()) {
     response.setError(true);
     response.setHttpErrorCode(status.value());
     /* create an array of total error count */
     ArrayList<ErrorDTO> errors = new ArrayList<ErrorDTO>(result.getErrorCount());
     /* append all field errors */
     for (FieldError err : result.getFieldErrors()) {
       System.out.println(
           "I got field error for: "
               + err.getField()
               + ", message: "
               + err.getDefaultMessage()
               + ", code: "
               + err.getCode());
       ErrorDTO dto = new ErrorDTO(err.getField(), err.getCode(), err.getDefaultMessage());
       errors.add(dto);
     }
     /* append global errors now */
     for (ObjectError err : result.getGlobalErrors()) {
       System.out.println(
           "I got global error for: "
               + err.getObjectName()
               + ", message: "
               + err.getDefaultMessage()
               + ", code: "
               + err.getCode());
       ErrorDTO dto = new ErrorDTO(err.getObjectName(), err.getCode(), err.getDefaultMessage());
       errors.add(dto);
     }
     response.setErrors(errors);
   }
   return response;
 }
Ejemplo n.º 2
0
  private ErrorDTO processFieldErrors(List<FieldError> fieldErrors) {
    ErrorDTO dto = new ErrorDTO(ErrorConstants.ERR_VALIDATION);

    for (FieldError fieldError : fieldErrors) {
      dto.add(fieldError.getObjectName(), fieldError.getField(), fieldError.getCode());
    }

    return dto;
  }
 @Test
 public void shouldAddErrorWhenEmptySourceIds() throws Exception {
   InvitationSentRecord record = InvitationSentRecord.toInvitationSentRecord("SLOTS", "IOS", "");
   Errors errors = new BeanPropertyBindingResult(record, "record");
   validator.validate(record, errors);
   FieldError fieldError = errors.getFieldError("sourceIds");
   assertEquals("sourceIds must be present", fieldError.getDefaultMessage());
   assertEquals("empty", fieldError.getCode());
 }
 @Test
 public void shouldAddErrorWhenNullPlatform() throws Exception {
   InvitationSentRecord record =
       InvitationSentRecord.toInvitationSentRecord("SLOTS", null, "*****@*****.**");
   Errors errors = new BeanPropertyBindingResult(record, "record");
   validator.validate(record, errors);
   FieldError fieldError = errors.getFieldError("platform");
   assertEquals("platform must be present", fieldError.getDefaultMessage());
   assertEquals("empty", fieldError.getCode());
 }
 @Test
 public void shouldAddErrorWhenUnknownGame() throws Exception {
   InvitationSentRecord record =
       InvitationSentRecord.toInvitationSentRecord("BLACKJACK", "IOS", "*****@*****.**");
   Errors errors = new BeanPropertyBindingResult(record, "record");
   validator.validate(record, errors);
   FieldError fieldError = errors.getFieldError("gameType");
   assertEquals("gameType is not supported", fieldError.getDefaultMessage());
   assertEquals("unsupported", fieldError.getCode());
 }
 @Test
 public void shouldAddErrorWhenSourceIDIsBlank() {
   InvitationSentRecord record =
       InvitationSentRecord.toInvitationSentRecord("SLOTS", "IOS", ",1002222223");
   Errors errors = new BeanPropertyBindingResult(record, "record");
   validator.validate(record, errors);
   FieldError fieldError = errors.getFieldError("sourceIds");
   assertEquals("sourceIds cannot be blank", fieldError.getDefaultMessage());
   assertEquals("nullOrBlankSourceId", fieldError.getCode());
 }
 @Test
 public void shouldAddErrorWhenInvalidPlatform() throws Exception {
   InvitationSentRecord record =
       InvitationSentRecord.toInvitationSentRecord("SLOTS", "PLAYSTATION", "*****@*****.**");
   Errors errors = new BeanPropertyBindingResult(record, "record");
   validator.validate(record, errors);
   FieldError fieldError = errors.getFieldError("platform");
   assertEquals("platform is not supported", fieldError.getDefaultMessage());
   assertEquals("unsupported", fieldError.getCode());
 }
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public @ResponseBody UserJsonResponse handleRegistration(
      @Valid @RequestBody User user,
      BindingResult result,
      Locale locale,
      Model model,
      HttpServletRequest request) {
    logger.info(user.toString());
    UserJsonResponse userJsonResponse = new UserJsonResponse();
    if (request.getSession().getAttribute("loggedIn") != null) {
      userJsonResponse.setStatus("SUCCESS");
      return userJsonResponse;
    }

    if (result.hasErrors()) {

      Map<String, String> errors = new HashMap<String, String>();
      List<FieldError> fieldErrors = result.getFieldErrors();
      for (FieldError fieldError : fieldErrors) {
        String[] resolveMessageCodes = result.resolveMessageCodes(fieldError.getCode());
        String string = resolveMessageCodes[0];
        // System.out.println("resolveMessageCodes : "+string);
        String message =
            messageSource.getMessage(
                string + "." + fieldError.getField(),
                new Object[] {fieldError.getRejectedValue()},
                locale);
        // System.out.println("Meassage : "+message);
        errors.put(fieldError.getField(), message);
      }
      userJsonResponse.setStatus("ERROR");
      userJsonResponse.setErrorsMap(errors);
      return userJsonResponse;
    }

    UserDAO userDao = (UserDAO) context.getBean("userDao");
    if (userDao.get(user.getUsername()) != null) {
      return handleError(userJsonResponse, "username", locale);
    } else if (userDao.getUserBySerialNumber(user.getSerialNumber()) != null) {
      return handleError(userJsonResponse, "serialNumber", locale);
    } else {
      String hash = DaoHelper.getInstance().getPasswordHashing().getHashAndSalt(user.getPassword());
      user.setHash(hash);
      user.setRole("Student");
      Student student = new Student(user);
      student.setSubscriptionDate(new Date());
      logger.info(student.toString());
      userDao.create(student);
      userJsonResponse.setStatus("SUCCESS");
      return userJsonResponse;
    }
  }
 @ExceptionHandler(InvalidRequestException.class)
 public ResponseEntity<Object> handleInvalidRequest(RuntimeException e, WebRequest request) {
   InvalidRequestException ire = (InvalidRequestException) e;
   List<FieldErrorResource> fieldErrorResources = new ArrayList<>();
   List<FieldError> fieldErrors = ire.getErrors().getFieldErrors();
   for (FieldError fieldError : fieldErrors) {
     FieldErrorResource fieldErrorResource = new FieldErrorResource();
     fieldErrorResource.setResource(fieldError.getObjectName());
     fieldErrorResource.setField(fieldError.getField());
     fieldErrorResource.setCode(fieldError.getCode());
     fieldErrorResource.setMessage(fieldError.getDefaultMessage());
     fieldErrorResources.add(fieldErrorResource);
   }
   ErrorResource error = errorResource("InvalidRequest", ire.getMessage());
   error.setFieldErrors(fieldErrorResources);
   return handleExceptionInternal(
       e, error, getHeaders(), HttpStatus.UNPROCESSABLE_ENTITY, request);
 }