Browse Source

working on slimmer implementation

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
sqlite
Stephan Richter 7 months ago
parent
commit
5627db16b5
  1. 4
      de.srsoftware.oidc.api/User.java
  2. 31
      de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/PathHandler.java
  3. 5
      de.srsoftware.oidc.server/build.gradle
  4. 24
      de.srsoftware.oidc.server/src/main/java/de/srsoftware/oidc/server/Application.java
  5. 27
      de.srsoftware.oidc.server/src/main/java/de/srsoftware/oidc/server/LanguageDirector.java
  6. 20
      de.srsoftware.oidc.web/build.gradle
  7. 53
      de.srsoftware.oidc.web/src/main/java/de/srsoftware/oidc/web/StaticPages.java
  8. 9
      de.srsoftware.oidc.web/src/main/resources/en/index.html
  9. 1
      settings.gradle

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

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

31
de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/PathHandler.java

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.api;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public abstract class PathHandler implements HttpHandler {
private String path;
public class Bond {
Bond(String p) {
path = p;
}
public HttpServer on(HttpServer server) {
server.createContext(path, PathHandler.this);
return server;
}
}
public Bond bindPath(String path) {
return new Bond(path);
}
public String relativePath(HttpExchange ex) {
var path = ex.getRequestURI().toString();
if (path.startsWith(this.path)) path = path.substring(this.path.length());
if (path.startsWith("/")) path = path.substring(1);
return path;
}
}

5
de.srsoftware.oidc.server/build.gradle

@ -12,9 +12,8 @@ repositories { @@ -12,9 +12,8 @@ repositories {
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'org.eclipse.jetty:jetty-server:11.0.14'
implementation 'org.eclipse.jetty:jetty-webapp:11.0.22'
implementation project(':de.srsoftware.oidc.light')
implementation project(':de.srsoftware.oidc.web')
implementation project(':de.srsoftware.oidc.api')
}
test {

24
de.srsoftware.oidc.server/src/main/java/de/srsoftware/oidc/server/Application.java

@ -1,22 +1,18 @@ @@ -1,22 +1,18 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.server;
import de.srsoftware.oidc.light.LightOICD;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
public class Application {
private static Server webserver;
import com.sun.net.httpserver.HttpServer;
import de.srsoftware.oidc.web.StaticPages;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
public class Application {
public static void main(String[] args) throws Exception {
webserver = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/web");
context.addServlet(LightOICD.class, "/");
webserver.setHandler(context);
webserver.start();
webserver.join();
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
new StaticPages().bindPath("/static").on(server);
new LanguageDirector("/static").bindPath("/").on(server);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
}
}

27
de.srsoftware.oidc.server/src/main/java/de/srsoftware/oidc/server/LanguageDirector.java

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.server;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.oidc.api.PathHandler;
import java.io.IOException;
import java.util.Arrays;
public class LanguageDirector extends PathHandler {
private static final String DEFAULT_LANG = "de";
private final String path;
public LanguageDirector(String pathTo) {
path = pathTo;
}
@Override
public void handle(HttpExchange t) throws IOException {
Headers headers = t.getRequestHeaders();
String lang = headers.get("Accept-Language").stream().flatMap(s -> Arrays.stream(s.split(","))).findFirst().orElse(DEFAULT_LANG);
t.getResponseHeaders().add("Location", String.join(lang, "/static/", "/de/index.html"));
t.sendResponseHeaders(301, 0);
t.getResponseBody().close();
}
}

20
de.srsoftware.oidc.web/build.gradle

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
plugins {
id 'java'
}
group = 'de.srsoftware'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(':de.srsoftware.oidc.api')
}
test {
useJUnitPlatform()
}

53
de.srsoftware.oidc.web/src/main/java/de/srsoftware/oidc/web/StaticPages.java

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.web;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.oidc.api.PathHandler;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Optional;
public class StaticPages extends PathHandler {
private static final String DEFAULT_LANG = "en";
private ClassLoader loader;
private record Response(String contentType, byte[] content) {
}
private static final String INDEX = "de/index.html";
@Override
public void handle(HttpExchange ex) throws IOException {
String path = relativePath(ex);
if (path.isBlank()) path = INDEX;
try {
var response = loadTemplate(path).orElseThrow(() -> new FileNotFoundException());
ex.getResponseHeaders().add("Content-Type", response.contentType);
ex.sendResponseHeaders(200, response.content.length);
OutputStream os = ex.getResponseBody();
os.write(response.content);
os.close();
} catch (FileNotFoundException fnf) {
ex.sendResponseHeaders(404, 0);
ex.getResponseBody().close();
}
}
private Optional<Response> loadTemplate(String path) throws IOException {
if (loader == null) loader = getClass().getClassLoader();
var resource = loader.getResource(path);
if (resource == null) {
var parts = path.split("/");
parts[0] = DEFAULT_LANG;
path = String.join("/", parts);
resource = loader.getResource(path);
}
if (resource == null) return Optional.empty();
var connection = resource.openConnection();
var contentType = connection.getContentType();
try (var in = connection.getInputStream()) {
return Optional.of(new Response(contentType, in.readAllBytes()));
}
}
}

9
de.srsoftware.oidc.web/src/main/resources/en/index.html

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<html>
<head>
<title>{title}</title>
<script src="lightoidc.js" />
</head>
<body>
{body}
</body>
</html>

1
settings.gradle

@ -2,4 +2,5 @@ rootProject.name = 'LightOIDC' @@ -2,4 +2,5 @@ rootProject.name = 'LightOIDC'
include 'de.srsoftware.oidc.api'
include 'de.srsoftware.oidc.light'
include 'de.srsoftware.oidc.server'
include 'de.srsoftware.oidc.web'

Loading…
Cancel
Save