diff --git a/multiuser/keycloak/che-multiuser-keycloak-server/src/main/java/org/eclipse/che/multiuser/keycloak/server/KeycloakEnvironmentInitalizationFilter.java b/multiuser/keycloak/che-multiuser-keycloak-server/src/main/java/org/eclipse/che/multiuser/keycloak/server/KeycloakEnvironmentInitalizationFilter.java index 81f5fbfe1c9..afe827a33dd 100644 --- a/multiuser/keycloak/che-multiuser-keycloak-server/src/main/java/org/eclipse/che/multiuser/keycloak/server/KeycloakEnvironmentInitalizationFilter.java +++ b/multiuser/keycloak/che-multiuser-keycloak-server/src/main/java/org/eclipse/che/multiuser/keycloak/server/KeycloakEnvironmentInitalizationFilter.java @@ -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; @@ -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 = 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 getUser(String id) throws ServerException { + try { + return Optional.of(userManager.getById(id)); + } catch (NotFoundException e) { + return Optional.empty(); + } } private HttpServletRequest addUserInRequest(