/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package it.necsi.keycloak; import java.io.InputStream; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.keycloak.adapters.KeycloakConfigResolver; import org.keycloak.adapters.KeycloakDeployment; import org.keycloak.adapters.KeycloakDeploymentBuilder; import org.keycloak.adapters.spi.HttpFacade; /** * * @author Mattia */ public class PathBasedKeycloakConfigResolver implements KeycloakConfigResolver { private final Map cache = new ConcurrentHashMap<>(); @Override public KeycloakDeployment resolve(HttpFacade.Request request) { String path = request.getURI(); int multitenantIndex = path.indexOf("risolvo/"); if (multitenantIndex == -1) { throw new IllegalStateException("Not able to resolve realm from the request path!"); } String realm = path.substring(path.indexOf("risolvo/")).split("/")[1]; if (realm.contains("?")) { realm = realm.split("\\?")[0]; } KeycloakDeployment deployment = cache.get(realm); if (null == deployment) { // not found on the simple cache, try to load it from the file system InputStream is = getClass().getResourceAsStream("/" + realm + "-keycloak.json"); if (is == null) { throw new IllegalStateException("Not able to find the file /" + realm + "-keycloak.json"); } deployment = KeycloakDeploymentBuilder.build(is); cache.put(realm, deployment); } return deployment; } }