use of org.summerb.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);
}
use of org.summerb.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(), LocaleContextHolder.getLocale());
// 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;
}
use of org.summerb.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);
}
}
use of org.summerb.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();
}
Aggregations