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:
parent
04ab848003
commit
2758d78865
1 changed files with 12 additions and 4 deletions
|
@ -23,6 +23,7 @@ import org.keycloak.models.RequiredActionProviderModel;
|
|||
import org.keycloak.models.UserModel;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
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
|
||||
* and if yes, it returns the value in correct form.
|
||||
|
@ -303,10 +311,10 @@ public class DefaultRequiredActions {
|
|||
* of {@link UserModel.RequiredAction}
|
||||
*/
|
||||
public static String getDefaultRequiredActionCaseInsensitively(String providerId) {
|
||||
try {
|
||||
return UserModel.RequiredAction.valueOf(providerId.toUpperCase()).name();
|
||||
} catch (IllegalArgumentException iae) {
|
||||
return providerId;
|
||||
String upperCase = providerId.toUpperCase();
|
||||
if (REQUIRED_ACTIONS.contains(upperCase)) {
|
||||
return upperCase;
|
||||
}
|
||||
return providerId;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue