Search in sources :

Example 1 with ValidationError

use of org.summerb.approaches.validation.ValidationError in project summerb by skarpushin.

the class LoginController method handleLoginFailed.

@RequestMapping(method = RequestMethod.GET, value = SecurityActionsUrlsProviderDefaultImpl.LOGIN_FAILED)
public String handleLoginFailed(Model model, HttpServletRequest request) {
    Exception lastException = (Exception) request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
    if (lastException != null) {
        log.info("Login failed due to exception", lastException);
        model.addAttribute("lastExceptionMessage", exceptionTranslatorSimplified.buildUserMessage(lastException));
        // Delete it from session to avoid excessive memory consumption
        request.getSession().removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
    }
    model.addAttribute("loginError", true);
    // Add validation errors
    FieldValidationException validationErrors = ExceptionUtils.findExceptionOfType(lastException, FieldValidationException.class);
    if (validationErrors != null) {
        for (ValidationError error : validationErrors.getErrors()) {
            model.addAttribute("ve_" + error.getFieldToken(), msg(error.getMessageCode(), error.getMessageArgs()));
        }
    }
    // add login failed message
    return getLoginForm(model);
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) ValidationError(org.summerb.approaches.validation.ValidationError) GenericException(org.summerb.utils.exceptions.GenericException) UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ValidationError

use of org.summerb.approaches.validation.ValidationError in project summerb by skarpushin.

the class AuthTokenServiceImpl method updateToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public void updateToken(String authTokenUuid, long lastVerifiedAt, String newTokenValue) throws AuthTokenNotFoundException, FieldValidationException {
    Preconditions.checkArgument(authTokenUuid != null);
    Preconditions.checkArgument(StringUtils.hasText(newTokenValue), "TokenValue is mandatory");
    try {
        // First - check token itself
        AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
        if (newTokenValue.equals(authToken.getTokenValue())) {
            throw new FieldValidationException(new ValidationError("validation.newValueExpected", "newTokenValue"));
        }
        // Now we need to update time when token was checked
        authTokenDao.updateToken(authTokenUuid, lastVerifiedAt, newTokenValue);
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        Throwables.throwIfInstanceOf(t, AuthTokenNotFoundException.class);
        String msg = String.format("Failed to update token '%s'", authTokenUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException) ValidationError(org.summerb.approaches.validation.ValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ValidationError

use of org.summerb.approaches.validation.ValidationError in project summerb by skarpushin.

the class ValidationErrorsVm method getMsg.

/**
 * @return Map that maps field token to error msg. Message will be null if there
 *         is no error for that field.
 */
public Map<String, String> getMsg() {
    if (errorsMap == null) {
        errorsMap = new HashMap<String, String>();
        for (ValidationError validationError : validationErrors) {
            try {
                String message = I18nUtils.buildMessage(validationError, CurrentRequestUtils.getWac());
                // CHeck if there is already at least one message for that
                // field. Concatenate errors.
                String existing = errorsMap.get(validationError.getFieldToken());
                String newMessageValue = "";
                if (StringUtils.hasText(existing)) {
                    if (existing.endsWith(".")) {
                        newMessageValue = existing + " " + message;
                    } else {
                        newMessageValue = existing + ". " + message;
                    }
                } else {
                    newMessageValue = message;
                }
                errorsMap.put(validationError.getFieldToken(), newMessageValue);
            } catch (Throwable t) {
                // don't really care
                log.warn("Failed to get field validation error message", t);
            }
        }
    }
    return errorsMap;
}
Also used : ValidationError(org.summerb.approaches.validation.ValidationError)

Example 4 with ValidationError

use of org.summerb.approaches.validation.ValidationError in project summerb by skarpushin.

the class ExceptionTranslatorFveImpl method buildUserMessage.

@Override
public String buildUserMessage(Throwable t, Locale locale) {
    if (!FieldValidationException.class.equals(t.getClass())) {
        return null;
    }
    FieldValidationException fve = (FieldValidationException) t;
    StringBuilder ret = new StringBuilder();
    ret.append(I18nUtils.buildMessage(fve, messageSource, locale));
    ret.append(": ");
    boolean first = true;
    for (ValidationError ve : fve.getErrors()) {
        if (!first) {
            ret.append(", ");
        }
        ret.append(translateFieldName(ve.getFieldToken(), messageSource, locale));
        ret.append(" - ");
        ret.append(I18nUtils.buildMessage(ve, messageSource, locale));
        first = false;
    }
    return ret.toString();
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) ValidationError(org.summerb.approaches.validation.ValidationError)

Aggregations

ValidationError (org.summerb.approaches.validation.ValidationError)4 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)3 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)1 AuthTokenNotFoundException (org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException)1 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)1 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)1 GenericException (org.summerb.utils.exceptions.GenericException)1