Search in sources :

Example 1 with DuplicateUserException

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";
}
Also used : Locale(java.util.Locale) FieldError(org.springframework.validation.FieldError) UserAccount(com.jappstart.model.auth.UserAccount) DuplicateUserException(com.jappstart.exception.DuplicateUserException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with DuplicateUserException

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);
}
Also used : TaskOptions(com.google.appengine.api.taskqueue.TaskOptions) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) List(java.util.List) UserAccount(com.jappstart.model.auth.UserAccount) DuplicateUserException(com.jappstart.exception.DuplicateUserException) Queue(com.google.appengine.api.taskqueue.Queue) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DuplicateUserException (com.jappstart.exception.DuplicateUserException)2 UserAccount (com.jappstart.model.auth.UserAccount)2 Queue (com.google.appengine.api.taskqueue.Queue)1 TaskOptions (com.google.appengine.api.taskqueue.TaskOptions)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Query (javax.persistence.Query)1 Transactional (org.springframework.transaction.annotation.Transactional)1 FieldError (org.springframework.validation.FieldError)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1