com.uid2.shared.auth.Roles Maven / Gradle / Ivy
package com.uid2.shared.auth;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class Roles {
public static > Set getRoles(Class type, JsonObject clientKeyJson) {
JsonArray rolesArr = clientKeyJson.getJsonArray("roles");
return rolesArr != null ? getRoles(type, rolesArr) : null;
}
public static > Set getRoles(Class type, JsonArray rolesJsonArray) {
Set roles = new HashSet<>();
for (int i = 0; i < rolesJsonArray.size(); ++i) {
String value = rolesJsonArray.getString(i).toUpperCase();
roles.add(Enum.valueOf(type, value));
}
return roles;
}
public static > Set getRoles(Class type, String rolesSpec) {
return Arrays.stream(rolesSpec.split(","))
.map(r -> r.trim().toUpperCase())
.map(r -> Enum.valueOf(type, r))
.collect(Collectors.toSet());
}
public static String getRolesString(Set roles) {
return String.join(",", roles.stream().map(Object::toString).collect(Collectors.toList()));
}
}