From 0de437708e5b12f14dc7dd7de063ba3ee226131d Mon Sep 17 00:00:00 2001 From: Garth <244253+xgp@users.noreply.github.com> Date: Tue, 20 Feb 2024 18:38:20 +0100 Subject: [PATCH] recursively delete dir after session close (#25) --- .../keycloak/themes/theme/AttributeTheme.java | 13 ++++++++--- .../theme/AttributeThemeProviderFactory.java | 22 ++++++++++++++++++- ...arkerAndMustacheEmailTemplateProvider.java | 11 +++++----- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeTheme.java b/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeTheme.java index 6683834..660139e 100644 --- a/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeTheme.java +++ b/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeTheme.java @@ -82,7 +82,7 @@ public String getName() { @Override public String getParentName() { - return getAttribute("_providerConfig.theme.email.parent", "mustache"); + return getAttribute(EMAIL_PARENT_ATTRIBUTE_PREFIX, "mustache"); } @Override @@ -95,8 +95,15 @@ public Type getType() { return type; } - public static final String EMAIL_TEMPLATE_ATTRIBUTE_PREFIX = "_providerConfig.templates.email"; - public static final String EMAIL_MESSAGE_ATTRIBUTE_PREFIX = "_providerConfig.messages.email"; + private static final String LEGACY_EMAIL_TEMPLATE_ATTRIBUTE_PREFIX = + "_providerConfig.templates.email"; + private static final String LEGACY_EMAIL_MESSAGE_ATTRIBUTE_PREFIX = + "_providerConfig.messages.email"; + private static final String EMAIL_TEMPLATE_ATTRIBUTE_PREFIX = + "_providerConfig.theme.email.templates"; + private static final String EMAIL_MESSAGE_ATTRIBUTE_PREFIX = + "_providerConfig.theme.email.messages"; + private static final String EMAIL_PARENT_ATTRIBUTE_PREFIX = "_providerConfig.theme.email.parent"; public static String templateKey(String templateName) { return String.format("%s.%s", EMAIL_TEMPLATE_ATTRIBUTE_PREFIX, templateName); diff --git a/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeThemeProviderFactory.java b/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeThemeProviderFactory.java index b3b3bb5..41769d4 100644 --- a/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeThemeProviderFactory.java +++ b/src/main/java/io/phasetwo/keycloak/themes/theme/AttributeThemeProviderFactory.java @@ -3,6 +3,9 @@ import com.google.auto.service.AutoService; import com.google.common.io.Files; import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Comparator; import lombok.extern.jbosslog.JBossLog; import org.keycloak.Config; import org.keycloak.models.KeycloakSession; @@ -33,7 +36,24 @@ public void init(Config.Scope config) {} public void postInit(KeycloakSessionFactory factory) {} @Override - public void close() {} + public void close() { + try { + deleteRecursively(tmpdir); + } catch (Exception e) { + log.warnf(e, "Error removing tmpdir", tmpdir); + } + } + + private static void deleteRecursively(File dir) throws IOException { + Path toDelete = dir.toPath(); + + java.nio.file.Files.walk(toDelete) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + + log.tracef("%s deleted? %b", toDelete, java.nio.file.Files.exists(toDelete)); + } @Override public String getId() { diff --git a/src/main/java/io/phasetwo/keycloak/themes/theme/FreeMarkerAndMustacheEmailTemplateProvider.java b/src/main/java/io/phasetwo/keycloak/themes/theme/FreeMarkerAndMustacheEmailTemplateProvider.java index 31afb25..a168fb0 100644 --- a/src/main/java/io/phasetwo/keycloak/themes/theme/FreeMarkerAndMustacheEmailTemplateProvider.java +++ b/src/main/java/io/phasetwo/keycloak/themes/theme/FreeMarkerAndMustacheEmailTemplateProvider.java @@ -61,11 +61,12 @@ private EmailTemplate processMustacheTemplate( // Expose locale_xy flags Stream stream = realm.getSupportedLocalesStream(); if (stream != null) { - stream.forEach(loc -> { - String key = String.format("locale_%s", loc); - Boolean ok = locale.getLanguage().equals(loc); - attributes.put(key, ok); - }); + stream.forEach( + loc -> { + String key = String.format("locale_%s", loc); + Boolean ok = locale.getLanguage().equals(loc); + attributes.put(key, ok); + }); } KeycloakUriInfo uriInfo = session.getContext().getUri();