class KeycloakRolesAuthorizationGenerator implements AuthorizationGenerator {

    private String clientId;

    /**
     * <p>Constructor for KeycloakRolesAuthorizationGenerator.</p>
     */
    public KeycloakRolesAuthorizationGenerator() {
    }

    /**
     * <p>Constructor for KeycloakRolesAuthorizationGenerator.</p>
     *
     * @param clientId a {@link String} object
     */
    public KeycloakRolesAuthorizationGenerator(final String clientId) {
        this.clientId = clientId;
    }

    /** {@inheritDoc} */
    @Override
    public Optional<UserProfile> generate(final CallContext ctx, final UserProfile profile) {<FILL_FUNCTION_BODY>}
}


        if (profile instanceof KeycloakOidcProfile) {
            try {
                val jwt = SignedJWT.parse(((KeycloakOidcProfile) profile).getAccessToken().getValue());
                val jwtClaimsSet = jwt.getJWTClaimsSet();

                val realmRolesJsonObject = jwtClaimsSet.getJSONObjectClaim("realm_access");
                if (realmRolesJsonObject != null) {
                    Iterable<String> realmRolesJsonArray = (List<String>) realmRolesJsonObject.get("roles");
                    if (realmRolesJsonArray != null) {
                        realmRolesJsonArray.forEach(role -> profile.addRole(role));
                    }
                }

                if (clientId != null) {
                    val resourceAccess = jwtClaimsSet.getJSONObjectClaim("resource_access");
                    if (resourceAccess != null) {
                        val clientRolesJsonObject = (Map) resourceAccess.get(clientId);
                        if (clientRolesJsonObject != null) {
                            Iterable<String> clientRolesJsonArray = (List<String>) clientRolesJsonObject.get("roles");
                            if (clientRolesJsonArray != null) {
                                clientRolesJsonArray.forEach(profile::addRole);
                            }
                        }
                    }
                }
            } catch (final Exception e) {
                LOGGER.warn("Cannot parse Keycloak roles", e);
            }
        }

        return Optional.of(profile);
    
<no_super_class>