Browse Source

started to implement HttpServlet

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
sqlite
Stephan Richter 6 months ago
parent
commit
65c2a2c859
  1. 4
      config/formatting.xml
  2. 4
      de.srsoftware.oidc.api/User.java
  3. 5
      de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/User.java
  4. 4
      de.srsoftware.oidc.light/build.gradle
  5. 32
      de.srsoftware.oidc.light/src/main/java/de/srsoftware/LightOICD.java
  6. 38
      de.srsoftware.oidc.light/src/main/java/de/srsoftware/Templates.java
  7. 5
      de.srsoftware.oidc.light/src/main/resources/templates/index.st

4
config/formatting.xml

@ -311,13 +311,13 @@
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="true"/> <setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.lineSplit" value="999"/> <setting id="org.eclipse.jdt.core.formatter.lineSplit" value="999"/>
<setting id="org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column" value="false"/> <setting id="org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column" value="false"/> <setting id="org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body" value="0"/> <setting id="org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body" value="0"/>
<setting id="org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve" value="3"/> <setting id="org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/> <setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="1"/> <setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="1"/>

4
de.srsoftware.oidc.api/User.java

@ -0,0 +1,4 @@
package de.srsoftware.oidc.api;
public class User {
}

5
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 {
}

4
de.srsoftware.oidc.light/build.gradle

@ -16,6 +16,9 @@ dependencies {
implementation 'org.slf4j:slf4j-api:2.0.13' implementation 'org.slf4j:slf4j-api:2.0.13'
testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter' 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 { test {
@ -24,6 +27,7 @@ test {
war { war {
archiveFileName = 'oidc.war' archiveFileName = 'oidc.war'
webAppDirName = 'src/main/resources'
} }

32
de.srsoftware.oidc.light/src/main/java/de/srsoftware/LightOICD.java

@ -1,9 +1,11 @@
/* © SRSoftware 2024 */ /* © SRSoftware 2024 */
package de.srsoftware; package de.srsoftware;
import de.srsoftware.oidc.api.User;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -11,15 +13,36 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST;
@WebServlet(urlPatterns = "/") @WebServlet(urlPatterns = "/")
public class LightOICD extends HttpServlet { public class LightOICD extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(LightOICD.class); 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 @Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
var path = Arrays.stream(req.getRequestURI().split("/")).skip(1).toList(); 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<String> path) throws IOException {
LOG.debug("helloWorld(…), path = {}", path);
resp.setContentType("text/html"); resp.setContentType("text/html");
PrintWriter out = resp.getWriter(); PrintWriter out = resp.getWriter();
out.println("<html><head><title>Hello World Servlet</title></head>"); out.println("<html><head><title>Hello World Servlet</title></head>");
@ -31,4 +54,11 @@ public class LightOICD extends HttpServlet {
out.println("</body></html>"); out.println("</body></html>");
out.close(); 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());
}
} }

38
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<File> 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;
}
}

5
de.srsoftware.oidc.light/src/main/resources/templates/index.st

@ -0,0 +1,5 @@
<html>
<body>
Dies ist die Index-Seite
</body>
</html>
Loading…
Cancel
Save