use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class RestExceptionTranslator method determineFailureResult.
protected DtoBase determineFailureResult(Exception ex, HttpServletRequest request, HttpServletResponse response) {
// first see if it is FVE
FieldValidationException fve = ExceptionUtils.findExceptionOfType(ex, FieldValidationException.class);
if (fve != null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return fve.getErrorDescriptionObject();
}
boolean translateAuthErrors = Boolean.TRUE.equals(Boolean.valueOf(request.getHeader(X_TRANSLATE_AUTHORIZATION_ERRORS)));
GenericServerErrorResult ret = null;
if (translateAuthErrors) {
ret = new GenericServerErrorResult(buildUserMessage(ex, request), new ExceptionInfo(ex));
}
NotAuthorizedException naex = ExceptionUtils.findExceptionOfType(ex, NotAuthorizedException.class);
if (naex != null) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return ret != null ? ret : naex.getResult();
}
AuthenticationException ae = ExceptionUtils.findExceptionOfType(ex, AuthenticationException.class);
if (ae != null) {
// NOTE: See how we did that in AuthenticationFailureHandlerImpl...
// Looks like we need to augment our custom RestLoginFilter so it
// will put username to request
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return ret != null ? ret : new NotAuthorizedResult("(username not resolved)", SecurityMessageCodes.AUTH_FATAL);
}
AccessDeniedException ade = ExceptionUtils.findExceptionOfType(ex, AccessDeniedException.class);
if (ade != null) {
if (authenticationTrustResolver.isAnonymous(SecurityContextHolder.getContext().getAuthentication())) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return ret != null ? ret : new NotAuthorizedResult(getCurrentUser(null), SecurityMessageCodes.LOGIN_REQUIRED);
}
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return ret != null ? ret : new NotAuthorizedResult(getCurrentUser(null), SecurityMessageCodes.ACCESS_DENIED);
}
CurrentUserNotFoundException cunfe = ExceptionUtils.findExceptionOfType(ex, CurrentUserNotFoundException.class);
if (cunfe != null) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return ret != null ? ret : new NotAuthorizedResult(getCurrentUser(null), SecurityMessageCodes.LOGIN_REQUIRED);
}
// TBD: Do we really need to send whole stack trace to client ??? I think we
// should do it only during development
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return new GenericServerErrorResult(buildUserMessage(ex, request), new ExceptionInfo(ex));
}
use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class EasyCrudValidationStrategyAbstract method validateForCreate.
@Override
public void validateForCreate(TDto dto) throws FieldValidationException {
ValidationContext ctx = new ValidationContext();
doValidateForCreate(dto, ctx);
if (ctx.getHasErrors()) {
throw new FieldValidationException(ctx.getErrors());
}
}
use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class EasyCrudValidationStrategyAbstract method validateForUpdate.
@Override
public void validateForUpdate(TDto existingVersion, TDto newVersion) throws FieldValidationException {
ValidationContext ctx = new ValidationContext();
doValidateForUpdate(existingVersion, newVersion, ctx);
if (ctx.getHasErrors()) {
throw new FieldValidationException(ctx.getErrors());
}
}
use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class UserServiceImpl method updateUser.
@Override
@Transactional(rollbackFor = Throwable.class)
public void updateUser(User user) throws FieldValidationException, UserNotFoundException {
Preconditions.checkArgument(user != null, "User reference required");
Preconditions.checkArgument(StringUtils.hasText(user.getUuid()), "User uuid must be provided");
validateUser(user);
boolean isUpdatedSuccessfully;
try {
isUpdatedSuccessfully = userDao.updateUser(user);
eventBus.post(EntityChangedEvent.updated(user));
} catch (DuplicateKeyException dke) {
throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL));
} catch (Throwable t) {
String msg = String.format("Failed to update user '%s'", user.getUuid());
throw new UserServiceUnexpectedException(msg, t);
}
if (!isUpdatedSuccessfully) {
throw new UserNotFoundException(user.getUuid());
}
}
use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class AuthTokenServiceImpl method validateAndGetUser.
private User validateAndGetUser(String userEmail, String passwordPlain) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
try {
User user = userService.getUserByEmail(userEmail);
boolean isPasswordValid = passwordService.isUserPasswordValid(user.getUuid(), passwordPlain);
if (!isPasswordValid) {
throw new InvalidPasswordException();
}
return user;
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
Throwables.throwIfInstanceOf(t, InvalidPasswordException.class);
String msg = String.format("Failed to validate user '%s' and password '%s'", userEmail, passwordPlain);
throw new UserServiceUnexpectedException(msg, t);
}
}
Aggregations