From 18ca57d2ec9cf28fb561608fff579825d93c1650 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 21 May 2024 18:49:33 +0200 Subject: [PATCH] got rid of stringtemplates in favor of lightweight solution Signed-off-by: Stephan Richter --- de.srsoftware.oidc.light/build.gradle | 5 +-- .../main/java/de/srsoftware/LightOICD.java | 6 +-- .../main/java/de/srsoftware/Templates.java | 40 ++++++++++++++----- .../src/main/resources/templates/index.html | 5 +++ .../src/main/resources/templates/index.st | 5 --- 5 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 de.srsoftware.oidc.light/src/main/resources/templates/index.html delete mode 100644 de.srsoftware.oidc.light/src/main/resources/templates/index.st diff --git a/de.srsoftware.oidc.light/build.gradle b/de.srsoftware.oidc.light/build.gradle index 87d2639..ea14b90 100644 --- a/de.srsoftware.oidc.light/build.gradle +++ b/de.srsoftware.oidc.light/build.gradle @@ -13,12 +13,11 @@ repositories { dependencies { compileOnly 'javax.servlet:javax.servlet-api:4.0.1' implementation 'ch.qos.logback:logback-core:1.5.6' + implementation 'ch.qos.logback:logback-classic:1.5.6' implementation 'org.slf4j:slf4j-api:2.0.13' + implementation project(':de.srsoftware.oidc.api') testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' - implementation 'ch.qos.logback:logback-classic:1.5.6' - implementation project(':de.srsoftware.oidc.api') - implementation('org.antlr:ST4:4.3.3') } test { diff --git a/de.srsoftware.oidc.light/src/main/java/de/srsoftware/LightOICD.java b/de.srsoftware.oidc.light/src/main/java/de/srsoftware/LightOICD.java index d558ca1..a1e4c68 100644 --- a/de.srsoftware.oidc.light/src/main/java/de/srsoftware/LightOICD.java +++ b/de.srsoftware.oidc.light/src/main/java/de/srsoftware/LightOICD.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.List; +import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -13,7 +14,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.stringtemplate.v4.ST; @WebServlet(urlPatterns = "/") public class LightOICD extends HttpServlet { @@ -57,8 +57,8 @@ public class LightOICD extends HttpServlet { private void landingPage(HttpServletRequest req, HttpServletResponse resp, User user) throws IOException { LOG.debug("landingPage(…)"); - ST st = templates.getInstanceOf("index"); + var index = templates.get("index.html", Map.of("user","Darling")); resp.setContentType("text/html"); - resp.getWriter().println(st.render()); + resp.getWriter().println(index.get()); } } diff --git a/de.srsoftware.oidc.light/src/main/java/de/srsoftware/Templates.java b/de.srsoftware.oidc.light/src/main/java/de/srsoftware/Templates.java index 856eed6..eea8dc9 100644 --- a/de.srsoftware.oidc.light/src/main/java/de/srsoftware/Templates.java +++ b/de.srsoftware.oidc.light/src/main/java/de/srsoftware/Templates.java @@ -2,28 +2,33 @@ package de.srsoftware; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; import java.util.Optional; -import org.stringtemplate.v4.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -public class Templates extends STRawGroupDir { +public class Templates { private static Templates singleton = null; + private static Logger LOG = LoggerFactory.getLogger(Templates.class); + private Path dir = searchTemplates(); - Templates() { - super(templateDir(), '«', '»'); - } + public Templates() throws FileNotFoundException {} - private static String templateDir() { - return templateDir(new File(System.getProperty("user.dir"))).get().getAbsolutePath(); + private static Path searchTemplates() throws FileNotFoundException { + return searchTemplates(new File(System.getProperty("user.dir"))).map(File::toPath).orElseThrow(() -> new FileNotFoundException("Missing template directory")); } - private static Optional templateDir(File dir) { + private static Optional searchTemplates(File dir) { if (dir.isDirectory()) { var children = dir.listFiles(); for (File child : children) { if (child.isDirectory()) { if (child.getName().equals("templates")) return Optional.of(child); - var inner = templateDir(child); + var inner = searchTemplates(child); if (inner.isPresent()) return inner; } } @@ -35,4 +40,21 @@ public class Templates extends STRawGroupDir { if (singleton == null) singleton = new Templates(); return singleton; } + + public Optional get(String path, Map replacements) { + var file = dir.resolve(path); + try { + return Optional.of(Files.readString(file)).map(s -> replaceKeys(s,replacements)); + // TODO: replacements + } catch (IOException e) { + LOG.warn("Failed to read {}", path, e); + return Optional.empty(); + } + + } + + private String replaceKeys(String text, Map replacements) { + for (Map.Entry replacement : replacements.entrySet()) text = text.replace("{"+replacement.getKey()+"}",replacement.getValue()); + return text; + } } diff --git a/de.srsoftware.oidc.light/src/main/resources/templates/index.html b/de.srsoftware.oidc.light/src/main/resources/templates/index.html new file mode 100644 index 0000000..572deaf --- /dev/null +++ b/de.srsoftware.oidc.light/src/main/resources/templates/index.html @@ -0,0 +1,5 @@ + + +Hallo {user}, dies ist die Index-Seite! + + \ No newline at end of file diff --git a/de.srsoftware.oidc.light/src/main/resources/templates/index.st b/de.srsoftware.oidc.light/src/main/resources/templates/index.st deleted file mode 100644 index 6e600a8..0000000 --- a/de.srsoftware.oidc.light/src/main/resources/templates/index.st +++ /dev/null @@ -1,5 +0,0 @@ - - -Dies ist die Index-Seite - - \ No newline at end of file