Avoid exception when looking up the providerId

This is a performance optimization, as creating an exception is expensive.

Closes #20176
This commit is contained in:
Alexander Schwartz 2023-05-05 10:42:39 +02:00 committed by Michal Hajas
parent 04ab848003
commit 2758d78865

View file

@ -23,6 +23,7 @@ import org.keycloak.models.RequiredActionProviderModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -291,6 +292,13 @@ public class DefaultRequiredActions {
} }
} }
private static final HashSet<String> REQUIRED_ACTIONS = new HashSet<>();
static {
for (UserModel.RequiredAction value : UserModel.RequiredAction.values()) {
REQUIRED_ACTIONS.add(value.name());
}
}
/** /**
* Checks whether given {@code providerId} case insensitively matches any of {@link UserModel.RequiredAction} enum * Checks whether given {@code providerId} case insensitively matches any of {@link UserModel.RequiredAction} enum
* and if yes, it returns the value in correct form. * and if yes, it returns the value in correct form.
@ -303,10 +311,10 @@ public class DefaultRequiredActions {
* of {@link UserModel.RequiredAction} * of {@link UserModel.RequiredAction}
*/ */
public static String getDefaultRequiredActionCaseInsensitively(String providerId) { public static String getDefaultRequiredActionCaseInsensitively(String providerId) {
try { String upperCase = providerId.toUpperCase();
return UserModel.RequiredAction.valueOf(providerId.toUpperCase()).name(); if (REQUIRED_ACTIONS.contains(upperCase)) {
} catch (IllegalArgumentException iae) { return upperCase;
return providerId;
} }
return providerId;
} }
} }