Stephan Richter
7 months ago
9 changed files with 153 additions and 21 deletions
@ -1,4 +0,0 @@ |
|||||||
package de.srsoftware.oidc.api; |
|
||||||
|
|
||||||
public class User { |
|
||||||
} |
|
@ -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; |
||||||
|
} |
||||||
|
} |
@ -1,22 +1,18 @@ |
|||||||
/* © SRSoftware 2024 */ |
/* © SRSoftware 2024 */ |
||||||
package de.srsoftware.oidc.server; |
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 { |
import com.sun.net.httpserver.HttpServer; |
||||||
private static Server webserver; |
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 { |
public static void main(String[] args) throws Exception { |
||||||
webserver = new Server(8080); |
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); |
||||||
|
new StaticPages().bindPath("/static").on(server); |
||||||
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); |
new LanguageDirector("/static").bindPath("/").on(server); |
||||||
context.setContextPath("/web"); |
server.setExecutor(Executors.newCachedThreadPool()); |
||||||
context.addServlet(LightOICD.class, "/"); |
server.start(); |
||||||
|
|
||||||
webserver.setHandler(context); |
|
||||||
webserver.start(); |
|
||||||
webserver.join(); |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -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(); |
||||||
|
} |
||||||
|
} |
@ -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() |
||||||
|
} |
@ -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())); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>{title}</title> |
||||||
|
<script src="lightoidc.js" /> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
{body} |
||||||
|
</body> |
||||||
|
</html> |
Loading…
Reference in new issue