in services/src/main/java/org/apache/custos/service/federated/client/keycloak/KeycloakClient.java [1216:1283]
public List<GroupRepresentation> createGroups(String realmId, String clientId, String clientSec, List<GroupRepresentation> groupRepresentations) {
try (Keycloak client = getClient(iamServerURL, superAdminRealmID, superAdminUserName, superAdminPassword)) {
List<GroupRepresentation> representationList = new ArrayList<>();
for (GroupRepresentation representation : groupRepresentations) {
Response response = client.realm(realmId).groups().add(representation);
if (response.getStatus() == HttpStatus.SC_CREATED) {
String id = getCreatedId(response);
if (representation.getRealmRoles() != null && !representation.getRealmRoles().isEmpty()) {
List<RoleRepresentation> roleRepresentation = new ArrayList<>();
for (String role : representation.getRealmRoles()) {
RoleResource resource = client.realm(realmId).roles().get(role);
if (resource != null) {
roleRepresentation.add(resource.toRepresentation());
}
}
if (!roleRepresentation.isEmpty()) {
client.realm(realmId).groups().group(id).roles().realmLevel().add(roleRepresentation);
}
}
if (representation.getClientRoles() != null && !representation.getClientRoles().isEmpty()) {
List<RoleRepresentation> clientRepresentations = new ArrayList<>();
ClientRepresentation clientRepresentation =
client.realm(realmId).clients().findByClientId(clientId).get(0);
for (String role : representation.getClientRoles().get(clientId)) {
RoleResource resource = client.realm(realmId).clients().get(clientRepresentation.getId()).roles().get(role);
if (resource != null) {
clientRepresentations.add(resource.toRepresentation());
}
}
if (!clientRepresentations.isEmpty()) {
client.realm(realmId).groups().group(id).roles().
clientLevel(clientRepresentation.getId()).add(clientRepresentations);
}
}
representation.setId(id);
this.createGroup(client, realmId, clientId, representation);
response.close();
GroupRepresentation savedRep = client.realm(realmId).groups().group(representation.getId()).toRepresentation();
representationList.add(savedRep);
return representationList;
} else if (response.getStatus() == HttpStatus.SC_UNAUTHORIZED) {
String msg = "Error occurred while creating group, reason: HTTP " + response.getStatus() + " Unauthorized";
LOGGER.error(msg);
throw new RuntimeException(msg);
} else {
String msg = "Error occurred while creating group, reason: HTTP " + response.getStatus();
LOGGER.error(msg);
throw new RuntimeException(msg);
}
}
} catch (Exception ex) {
String msg = "Error occurred while creating group, reason: " + ex.getMessage();
LOGGER.error(msg, ex);
throw new RuntimeException(msg, ex);
}
return null;
}