use of com.jappstart.exception.DuplicateUserException in project google-app-engine-jappstart by taylorleese.
the class RegisterController method submit.
/**
* Handle the create account form submission.
*
* @param register the register form bean
* @param binding the binding result
* @param request the HTTP servlet request
* @return the path
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
public final String submit(@ModelAttribute(REGISTER) @Valid final Register register, final BindingResult binding, final HttpServletRequest request) {
final Locale locale = localeResolver.resolveLocale(request);
if (binding.hasErrors()) {
return "create";
}
final UserAccount user = new UserAccount(register.getUsername());
user.setDisplayName(register.getDisplayName());
user.setEmail(register.getEmail());
user.setPassword(passwordEncoder.encodePassword(register.getPassword(), user.getSalt()));
try {
userDetailsService.addUser(user, locale);
} catch (DuplicateUserException e) {
binding.addError(new FieldError(REGISTER, "username", messageSource.getMessage("create.error.username", null, locale)));
return "create";
}
return "redirect:/register/success";
}
use of com.jappstart.exception.DuplicateUserException in project google-app-engine-jappstart by taylorleese.
the class UserDetailsServiceImpl method addUser.
/**
* Adds a user.
*
* @param user the user
* @param locale the locale
*/
@Override
@Transactional
public final void addUser(final UserAccount user, final Locale locale) {
final UserAccount cachedUser = (UserAccount) memcacheService.get(user.getUsername());
if (cachedUser != null) {
throw new DuplicateUserException();
}
final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE username = :username");
query.setParameter(USERNAME, user.getUsername());
@SuppressWarnings("unchecked") final List results = query.getResultList();
if (results != null && !results.isEmpty()) {
throw new DuplicateUserException();
}
entityManager.persist(user);
memcacheService.put(user.getUsername(), user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
final TaskOptions taskOptions = TaskOptions.Builder.withUrl(mailTaskUrl).param("username", user.getUsername()).param("locale", locale.toString());
final Queue queue = QueueFactory.getQueue(mailTaskName);
queue.add(datastoreService.getCurrentTransaction(), taskOptions);
}
Aggregations