From 65c2a2c859f8117179d4a66f098e3e8a7f72f3fe Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 21 May 2024 01:48:02 +0200 Subject: [PATCH] started to implement HttpServlet Signed-off-by: Stephan Richter --- config/formatting.xml | 4 +- de.srsoftware.oidc.api/User.java | 4 ++ .../java/de/srsoftware/oidc/api/User.java | 5 +++ de.srsoftware.oidc.light/build.gradle | 4 ++ .../main/java/de/srsoftware/LightOICD.java | 34 ++++++++++++++++- .../main/java/de/srsoftware/Templates.java | 38 +++++++++++++++++++ .../src/main/resources/templates/index.st | 5 +++ 7 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 de.srsoftware.oidc.api/User.java create mode 100644 de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/User.java create mode 100644 de.srsoftware.oidc.light/src/main/java/de/srsoftware/Templates.java create mode 100644 de.srsoftware.oidc.light/src/main/resources/templates/index.st diff --git a/config/formatting.xml b/config/formatting.xml index 036621c..cdffb90 100644 --- a/config/formatting.xml +++ b/config/formatting.xml @@ -311,13 +311,13 @@ - + - + diff --git a/de.srsoftware.oidc.api/User.java b/de.srsoftware.oidc.api/User.java new file mode 100644 index 0000000..a567297 --- /dev/null +++ b/de.srsoftware.oidc.api/User.java @@ -0,0 +1,4 @@ +package de.srsoftware.oidc.api; + +public class User { +} diff --git a/de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/User.java b/de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/User.java new file mode 100644 index 0000000..d188b46 --- /dev/null +++ b/de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/User.java @@ -0,0 +1,5 @@ +/* © SRSoftware 2024 */ +package de.srsoftware.oidc.api; + +public class User { +} diff --git a/de.srsoftware.oidc.light/build.gradle b/de.srsoftware.oidc.light/build.gradle index 5356f91..87d2639 100644 --- a/de.srsoftware.oidc.light/build.gradle +++ b/de.srsoftware.oidc.light/build.gradle @@ -16,6 +16,9 @@ dependencies { implementation 'org.slf4j:slf4j-api:2.0.13' 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 { @@ -24,6 +27,7 @@ test { war { archiveFileName = 'oidc.war' + webAppDirName = 'src/main/resources' } 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 a930fba..d558ca1 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 @@ -1,9 +1,11 @@ /* © SRSoftware 2024 */ package de.srsoftware; +import de.srsoftware.oidc.api.User; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -11,24 +13,52 @@ 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 { - private static final Logger LOG = LoggerFactory.getLogger(LightOICD.class); + private static final Templates templates; + static { + try { + templates = Templates.singleton(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { var path = Arrays.stream(req.getRequestURI().split("/")).skip(1).toList(); + User user = null; + if (path.isEmpty()) { + landingPage(req, resp, user); + return; + } + switch (path.remove(0)) { + default: + helloWorld(req, resp, path); + } + } + + private void helloWorld(HttpServletRequest req, HttpServletResponse resp, List path) throws IOException { + LOG.debug("helloWorld(…), path = {}", path); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("Hello World Servlet"); out.println(""); out.println("

Hello Guys!

"); out.println("
    "); - path.forEach(part -> out.println("
  • "+part+"
  • ")); + path.forEach(part -> out.println("
  • " + part + "
  • ")); out.println("
"); out.println(""); out.close(); } + + private void landingPage(HttpServletRequest req, HttpServletResponse resp, User user) throws IOException { + LOG.debug("landingPage(…)"); + ST st = templates.getInstanceOf("index"); + resp.setContentType("text/html"); + resp.getWriter().println(st.render()); + } } 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 new file mode 100644 index 0000000..856eed6 --- /dev/null +++ b/de.srsoftware.oidc.light/src/main/java/de/srsoftware/Templates.java @@ -0,0 +1,38 @@ +/* © SRSoftware 2024 */ +package de.srsoftware; + +import java.io.File; +import java.io.IOException; +import java.util.Optional; +import org.stringtemplate.v4.*; + +public class Templates extends STRawGroupDir { + private static Templates singleton = null; + + Templates() { + super(templateDir(), '«', '»'); + } + + private static String templateDir() { + return templateDir(new File(System.getProperty("user.dir"))).get().getAbsolutePath(); + } + + private static Optional templateDir(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); + if (inner.isPresent()) return inner; + } + } + } + return Optional.empty(); + } + + public static Templates singleton() throws IOException { + if (singleton == null) singleton = new Templates(); + return singleton; + } +} diff --git a/de.srsoftware.oidc.light/src/main/resources/templates/index.st b/de.srsoftware.oidc.light/src/main/resources/templates/index.st new file mode 100644 index 0000000..6e600a8 --- /dev/null +++ b/de.srsoftware.oidc.light/src/main/resources/templates/index.st @@ -0,0 +1,5 @@ + + +Dies ist die Index-Seite + + \ No newline at end of file