package org.keycloak.adapters.springsecurity;

import org.keycloak.adapters.AdapterDeploymentContext;
import org.keycloak.adapters.KeycloakDeployment;
import org.keycloak.adapters.KeycloakDeploymentBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

import java.io.InputStream;

/**
 * Bean holding the {@link KeycloakDeployment} and {@link AdapterDeploymentContext} for this
 * Spring application context. The Keycloak deployment is loaded from the required
 * <code>WEB-INF/keycloak.json</code> file generated by Keycloak.
 *
 * @author <a href="mailto:srossillo@smartling.com">Scott Rossillo</a>
 * @version $Revision: 1 $
 */
public class AdapterDeploymentContextBean implements ApplicationContextAware, InitializingBean {

    private ApplicationContext applicationContext;
    private AdapterDeploymentContext deploymentContext;
    private KeycloakDeployment deployment;

    @Override
    public void afterPropertiesSet() throws Exception {
        Resource resource = applicationContext.getResource("WEB-INF/keycloak.json");
        InputStream is = resource.getInputStream();
        this.deployment = KeycloakDeploymentBuilder.build(is);
        this.deploymentContext = new AdapterDeploymentContext(deployment);
    }

    /**
     * Returns the Keycloak {@link AdapterDeploymentContext} for this application context.
     *
     * @return the Keycloak {@link AdapterDeploymentContext} for this application context
     */
    public AdapterDeploymentContext getDeploymentContext() {
        return deploymentContext;
    }

    /**
     * Returns the {@link KeycloakDeployment} for this application context.
     *
     * @return the {@link KeycloakDeployment} for this application context
     */
    public KeycloakDeployment getDeployment() {
        return deployment;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

@@ -484,27 +484,65 @@
 java.io.
-InputStream
+FileNotFoundException;%0Aimport java.io.IOException
 ;%0A%0A/**%0A 
@@ -725,24 +725,16 @@
 * %3Ccode%3E
-WEB-INF/
 keycloak
@@ -750,34 +750,21 @@
 de%3E 
-file generated by Keycloak
+resource file
 .%0A *
@@ -964,16 +964,293 @@
 Bean %7B%0A%0A
+    private static final String KEYCLOAK_CONFIG_FILE = %22keycloak.json%22;%0A    private static final String KEYCLOAK_CONFIG_WEB_RESOURCE = %22WEB-INF/%22 + KEYCLOAK_CONFIG_FILE;%0A    private static final String KEYCLOAK_CONFIG_CLASSPATH_RESOURCE = %22classpath:%22 + KEYCLOAK_CONFIG_FILE;%0A%0A
     priv
@@ -1454,32 +1454,244 @@
 ows Exception %7B%0A
+        this.deployment = loadKeycloakDeployment();%0A        this.deploymentContext = new AdapterDeploymentContext(deployment);%0A    %7D%0A%0A    private KeycloakDeployment loadKeycloakDeployment() throws IOException %7B%0A%0A
         Resource
@@ -1737,42 +1737,48 @@
 rce(
-%22WEB-INF/keycloak.json%22
+KEYCLOAK_CONFIG_WEB_RESOURCE
 );%0A
+%0A
         
 Inpu
@@ -1777,187 +1777,430 @@
     
-InputStream is = resource.getInputStream();%0A        this.deployment = KeycloakDeploymentBuilder.build(is);%0A        this.deploymentContext = new AdapterDeploymentContext(deployment
+if (!resource.isReadable()) %7B%0A            resource=  applicationContext.getResource(KEYCLOAK_CONFIG_CLASSPATH_RESOURCE);%0A        %7D%0A%0A        if (!resource.isReadable()) %7B%0A            throw new FileNotFoundException(String.format(%22Unable to locate Keycloak from %25s or %25s%22, KEYCLOAK_CONFIG_WEB_RESOURCE, KEYCLOAK_CONFIG_CLASSPATH_RESOURCE));%0A        %7D%0A%0A        return KeycloakDeploymentBuilder.build(resource.getInputStream()
 );%0A 

Support for loading keycloak.json from the classpath