Skip to content

Commit

Permalink
Fixed concurrent user registration (#6703)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Kabashniuk <skabashniuk@redhat.com>
  • Loading branch information
skabashnyuk committed Oct 12, 2017
1 parent c35d27e commit 14061f5
Showing 1 changed file with 21 additions and 9 deletions.
Expand Up @@ -17,6 +17,7 @@
import io.jsonwebtoken.Jwt;
import java.io.IOException;
import java.security.Principal;
import java.util.Optional;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.FilterChain;
Expand Down Expand Up @@ -107,19 +108,30 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

private User getOrCreateUser(String id, String email, String username)
throws ServerException, ConflictException {
try {
return userManager.getById(id);
} catch (NotFoundException e) {
Optional<User> user = getUser(id);
if (!user.isPresent()) {
synchronized (this) {
final UserImpl cheUser = new UserImpl(id, email, username, generate("", 12), emptyList());
try {
return userManager.create(cheUser, false);
} catch (ConflictException ex) {
cheUser.setName(generate(cheUser.getName(), 4));
return userManager.create(cheUser, false);
user = getUser(id);
if (!user.isPresent()) {
final UserImpl cheUser = new UserImpl(id, email, username, generate("", 12), emptyList());
try {
return userManager.create(cheUser, false);
} catch (ConflictException ex) {
cheUser.setName(generate(cheUser.getName(), 4));
return userManager.create(cheUser, false);
}
}
}
}
return user.get();
}

private Optional<User> getUser(String id) throws ServerException {
try {
return Optional.of(userManager.getById(id));
} catch (NotFoundException e) {
return Optional.empty();
}
}

private HttpServletRequest addUserInRequest(
Expand Down

0 comments on commit 14061f5

Please sign in to comment.