Search in sources :

Example 1 with ScriptProviderMetadata

use of org.keycloak.representations.provider.ScriptProviderMetadata in project keycloak by keycloak.

the class ScriptProviderDeploymentProcessor method deploy.

static void deploy(DeploymentUnit deploymentUnit, KeycloakDeploymentInfo info) {
    ResourceRoot resourceRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
    if (resourceRoot == null) {
        return;
    }
    VirtualFile jarFile = resourceRoot.getRoot();
    if (jarFile == null || !jarFile.exists() || !jarFile.getName().endsWith(".jar")) {
        return;
    }
    ScriptProviderDescriptor descriptor = readScriptProviderDescriptor(jarFile);
    if (descriptor == null) {
        return;
    }
    for (Map.Entry<String, List<ScriptProviderMetadata>> entry : descriptor.getProviders().entrySet()) {
        for (ScriptProviderMetadata metadata : entry.getValue()) {
            String fileName = metadata.getFileName();
            if (fileName == null) {
                throw new RuntimeException("You must provide the script file name");
            }
            try (InputStream in = jarFile.getChild(fileName).openStream()) {
                metadata.setCode(StreamUtil.readString(in, StandardCharsets.UTF_8));
            } catch (IOException cause) {
                throw new RuntimeException("Failed to read script file [" + fileName + "]", cause);
            }
            metadata.setId(new StringBuilder("script").append("-").append(fileName).toString());
            String name = metadata.getName();
            if (name == null) {
                name = fileName;
            }
            metadata.setName(name);
            PROVIDERS.get(entry.getKey()).accept(info, metadata);
        }
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) ScriptProviderMetadata(org.keycloak.representations.provider.ScriptProviderMetadata) InputStream(java.io.InputStream) ScriptProviderDescriptor(org.keycloak.representations.provider.ScriptProviderDescriptor) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ScriptProviderMetadata

use of org.keycloak.representations.provider.ScriptProviderMetadata in project keycloak by keycloak.

the class KeycloakProcessor method loadDeployedScriptProviders.

private Map<String, ProviderFactory> loadDeployedScriptProviders(ClassLoader classLoader, Spi spi) {
    Map<String, ProviderFactory> providers = new HashMap<>();
    if (supportsDeployeableScripts(spi)) {
        try {
            Enumeration<URL> urls = classLoader.getResources(KEYCLOAK_SCRIPTS_JSON_PATH);
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                int fileSeparator = url.getFile().indexOf(JAR_FILE_SEPARATOR);
                if (fileSeparator != -1) {
                    JarFile jarFile = new JarFile(url.getFile().substring("file:".length(), fileSeparator));
                    JarEntry descriptorEntry = jarFile.getJarEntry(KEYCLOAK_SCRIPTS_JSON_PATH);
                    ScriptProviderDescriptor descriptor;
                    try (InputStream is = jarFile.getInputStream(descriptorEntry)) {
                        descriptor = JsonSerialization.readValue(is, ScriptProviderDescriptor.class);
                    }
                    for (Entry<String, List<ScriptProviderMetadata>> entry : descriptor.getProviders().entrySet()) {
                        if (isScriptForSpi(spi, entry.getKey())) {
                            for (ScriptProviderMetadata metadata : entry.getValue()) {
                                ProviderFactory provider = createDeployableScriptProvider(jarFile, entry, metadata);
                                providers.put(metadata.getId(), provider);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Failed to discover script providers", e);
        }
    }
    return providers;
}
Also used : ScriptProviderMetadata(org.keycloak.representations.provider.ScriptProviderMetadata) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ScriptProviderDescriptor(org.keycloak.representations.provider.ScriptProviderDescriptor) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) FolderThemeProviderFactory(org.keycloak.theme.FolderThemeProviderFactory) RequestHostnameProviderFactory(org.keycloak.url.RequestHostnameProviderFactory) DefaultJpaConnectionProviderFactory(org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory) FilesPlainTextVaultProviderFactory(org.keycloak.vault.FilesPlainTextVaultProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) FixedHostnameProviderFactory(org.keycloak.url.FixedHostnameProviderFactory) DefaultHostnameProviderFactory(org.keycloak.url.DefaultHostnameProviderFactory) EnvironmentDependentProviderFactory(org.keycloak.provider.EnvironmentDependentProviderFactory) LiquibaseJpaUpdaterProviderFactory(org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProviderFactory) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ScriptProviderDescriptor (org.keycloak.representations.provider.ScriptProviderDescriptor)2 ScriptProviderMetadata (org.keycloak.representations.provider.ScriptProviderMetadata)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)1 VirtualFile (org.jboss.vfs.VirtualFile)1 DefaultJpaConnectionProviderFactory (org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory)1 LiquibaseJpaUpdaterProviderFactory (org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProviderFactory)1 EnvironmentDependentProviderFactory (org.keycloak.provider.EnvironmentDependentProviderFactory)1 ProviderFactory (org.keycloak.provider.ProviderFactory)1 FolderThemeProviderFactory (org.keycloak.theme.FolderThemeProviderFactory)1 DefaultHostnameProviderFactory (org.keycloak.url.DefaultHostnameProviderFactory)1 FixedHostnameProviderFactory (org.keycloak.url.FixedHostnameProviderFactory)1