Skip to content

Commit

Permalink
Remove legacy code for older versions of Keycloak JS (#520)
Browse files Browse the repository at this point in the history
* Remove legacy code for older versions of Keycloak JS

Closes #500

Signed-off-by: Jon Koops <[email protected]>

* Update src/main/java/org/keycloak/webbuilder/builders/AppBuilder.java

Signed-off-by: Stian Thorgersen <[email protected]>

---------

Signed-off-by: Jon Koops <[email protected]>
Signed-off-by: Stian Thorgersen <[email protected]>
Co-authored-by: Stian Thorgersen <[email protected]>
  • Loading branch information
jonkoops and stianst authored Nov 1, 2024
1 parent 60f2dd4 commit 255b310
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 173 deletions.
5 changes: 0 additions & 5 deletions src/main/java/org/keycloak/webbuilder/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ public String getVersionShorter() {
return split[0] + "." + split[1];
}

public int getMajorVersion() {
String[] split = version.split("\\.");
return Integer.parseInt(split[0]);
}

public Date getDate() {
return date;
}
Expand Down
83 changes: 64 additions & 19 deletions src/main/java/org/keycloak/webbuilder/builders/AppBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,90 @@
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.io.IOUtils;
import org.keycloak.webbuilder.npm.Package;
import org.keycloak.webbuilder.npm.Registry;
import org.keycloak.webbuilder.npm.Version;
import org.keycloak.webbuilder.utils.JsonParser;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class AppBuilder extends AbstractBuilder {
private final Set<String> ALLOWED_EXTENSIONS = Set.of("js");

private final Map<String, String> imports = new HashMap<>();

@Override
protected void build() throws Exception {
Package packageInfo = Registry.getPackage("keycloak-js");
Version latestVersion = packageInfo.getVersionByTag("latest");
File targetFile = new File(context.getTargetDir(), "app/keycloak.js");
// Install packages.
installPackage("keycloak-js", "latest");
// Render page template to HTML.
renderPage();
}

// Skip if target file already exists.
if (targetFile.isFile()) {
return;
}
private void installPackage(String name, String version) throws Exception {
// Resolve installation path and create directories.
Path installationPath = context.getTargetDir().toPath().resolve("vendor").resolve(name);
Files.createDirectories(installationPath);

boolean useLegacy = latestVersion.getSemanticVersion().getMajor() < 26;
String entryPoint = latestVersion.resolveEntryPoint(useLegacy);
String sourcePath = Path.of("package", entryPoint).normalize().toString();
// Get package contents as tarball stream.
Package packageInfo = Registry.getPackage(name);
Version latestVersion = packageInfo.getVersionByTag(version);
ArchiveInputStream<TarArchiveEntry> tarball = latestVersion.getDist().getTarballStream();

for (ArchiveEntry entry = tarball.getNextEntry(); entry != null; entry = tarball.getNextEntry()) {
// Loop trough until we find a file that matches the package entrypoint.
if (!entry.getName().equals(sourcePath)) {
// Copy package contents to installation path.
ArchiveEntry entry;
while ((entry = tarball.getNextEntry()) != null) {
// Skip any files not part of the package contents.
String packagePrefix = "package";
if (!entry.getName().startsWith(packagePrefix)) {
continue;
}

// Copy the file over when found.
try (OutputStream outputStream = new FileOutputStream(targetFile)) {
IOUtils.copy(tarball, outputStream);
// Resolve path without 'package' prefix.
Path entryPath = Path.of(packagePrefix).relativize(Path.of(entry.getName()));

// Skip file if it's extension is not permitted.
String extension = getFileExtension(entryPath.getFileName().toString());
if(!ALLOWED_EXTENSIONS.contains(extension)) {
continue;
}

// Resolve target path and copy file.
Path targetPath = installationPath.resolve(entryPath);
Files.createDirectories(targetPath.getParent());
Files.copy(tarball, targetPath);
}

// Add package to the imports so it can be written to the import map later.
Path importPath = Path.of("/vendor", name, latestVersion.resolveEntryPoint()).normalize();
imports.put(name, importPath.toString());
}

private void renderPage() throws Exception {
// Build import map from installed packages.
Map<String, Map<String, String>> importMap = new HashMap<>();
importMap.put("imports", imports);

// Ad the import map to the template attributes.
Map<String, Object> attributes = new HashMap<>();
attributes.put("importMap", JsonParser.writeValueAsString(importMap));

// Render the template.
File targetDir = new File(context.getTargetDir(), "app");
context.freeMarker().writeFile(attributes, "templates/app.ftl", targetDir, "index.html");
}

private String getFileExtension(String filename) {
int dotIndex = filename.lastIndexOf(".");
if (dotIndex >= 0) {
return filename.substring(dotIndex + 1);
}
return "";
}

@Override
Expand Down
44 changes: 0 additions & 44 deletions src/main/java/org/keycloak/webbuilder/npm/SemanticVersion.java

This file was deleted.

16 changes: 1 addition & 15 deletions src/main/java/org/keycloak/webbuilder/npm/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,17 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class Version {
@JsonProperty("version")
private String version;

@JsonProperty("main")
private String main;

@JsonProperty("exports")
private Map<String, Export> exports;

@JsonProperty("dist")
private Dist dist;

public SemanticVersion getSemanticVersion() {
return SemanticVersion.fromString(version);
}

public Dist getDist() {
return dist;
}

public String resolveEntryPoint(boolean useLegacy) {
if (useLegacy) {
return main;
}

public String resolveEntryPoint() {
Export defaultExport = exports.get(".");
return defaultExport != null ? defaultExport.getDefaultPath() : null;
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/keycloak/webbuilder/utils/JsonParser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.keycloak.webbuilder.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class JsonParser {

Expand All @@ -27,4 +25,12 @@ public static <T> T read(URL url, Class<T> t) {
}
}

public static String writeValueAsString(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
79 changes: 0 additions & 79 deletions static/app/app-legacy.js

This file was deleted.

2 changes: 1 addition & 1 deletion static/app/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Keycloak from './keycloak.js';
import Keycloak from 'keycloak-js';

function init() {
document.getElementById('config-form').onsubmit = function() {
Expand Down
9 changes: 2 additions & 7 deletions pages/app/index.ftl → templates/app.ftl
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<#import "/templates/template.ftl" as tmpl>

<@tmpl.page current="test-app" title="Test application" noindex=true nocsp=true>
<@tmpl.page current="test-app" title="Test application" noindex=true nocsp=true importMap=importMap>

<#if version.majorVersion < 26>
<script src="keycloak.js" type="text/javascript"></script>
<script src="app-legacy.js" type="text/javascript"></script>
<#else>
<script src="app.js" type="module"></script>
</#if>
<script src="app.js" type="module"></script>

<div class="jumbotron jumbotron-fluid bg-light kc-bg-triangles py-5 kc-app">
<div class="container">
Expand Down
3 changes: 2 additions & 1 deletion templates/template.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<#macro page current title noindex=false nocsp=false rss=false>
<#macro page current title importMap="" noindex=false nocsp=false rss=false>
<!doctype html>
<html lang="en">
<head>
Expand All @@ -21,6 +21,7 @@

<link rel="shortcut icon" href="${links.getResource('favicon.ico')}">

<#if importMap?has_content><script type="importmap">${importMap}</script></#if>
<script src="${links.getResource('js/ga.js')}" type="text/javascript"></script>
<script src="${links.getResource('bootstrap/dist/js/bootstrap.min.js')}" type="text/javascript"></script>
<script src="${links.getResource('tocbot/dist/tocbot.min.js')}" type="text/javascript"></script>
Expand Down

0 comments on commit 255b310

Please sign in to comment.