implemented cookies, implemented local file delivery option (--base /path/to/static/content), refactoring static files
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -6,11 +6,21 @@ import de.srsoftware.oidc.api.PathHandler;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
public class StaticPages extends PathHandler {
|
||||
private static final String DEFAULT_LANGUAGE = "en";
|
||||
private ClassLoader loader;
|
||||
private final Optional<Path> base;
|
||||
private ClassLoader loader;
|
||||
|
||||
public StaticPages(Optional<Path> basePath) {
|
||||
super();
|
||||
base = basePath;
|
||||
}
|
||||
|
||||
private record Response(String contentType, byte[] content) {
|
||||
}
|
||||
@@ -18,15 +28,15 @@ public class StaticPages extends PathHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange ex) throws IOException {
|
||||
String path = relativePath(ex);
|
||||
String lang = language(ex).orElse(DEFAULT_LANGUAGE);
|
||||
String method = ex.getRequestMethod();
|
||||
String relativePath = relativePath(ex);
|
||||
String lang = language(ex).orElse(DEFAULT_LANGUAGE);
|
||||
String method = ex.getRequestMethod();
|
||||
|
||||
if (path.isBlank()) path = INDEX;
|
||||
if (relativePath.isBlank()) relativePath = INDEX;
|
||||
System.out.printf("%s %s: ", method, ex.getRequestURI());
|
||||
try {
|
||||
System.out.printf("Loading %s for lagnuage %s…", path, lang);
|
||||
var response = loadTemplate(lang, path).orElseThrow(() -> new FileNotFoundException());
|
||||
System.out.printf("Loading %s for lagnuage %s…", relativePath, lang);
|
||||
Response response = loadFile(lang, relativePath).orElseThrow(() -> new FileNotFoundException());
|
||||
|
||||
ex.getResponseHeaders().add(CONTENT_TYPE, response.contentType);
|
||||
ex.sendResponseHeaders(200, response.content.length);
|
||||
@@ -41,15 +51,37 @@ public class StaticPages extends PathHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<Response> loadTemplate(String language, String path) throws IOException {
|
||||
private URL getLocalUrl(Path base, String language, String path) {
|
||||
var file = base.resolve(language).resolve(path);
|
||||
if (!Files.isRegularFile(file)) {
|
||||
file = base.resolve(DEFAULT_LANGUAGE).resolve(path);
|
||||
if (!Files.isRegularFile(file)) return null;
|
||||
}
|
||||
try {
|
||||
return file.toUri().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private URL getResource(String language, String path) {
|
||||
if (loader == null) loader = getClass().getClassLoader();
|
||||
var resource = loader.getResource(String.join("/", language, path));
|
||||
if (resource == null) resource = loader.getResource(String.join("/", DEFAULT_LANGUAGE, 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()));
|
||||
return resource;
|
||||
}
|
||||
|
||||
private Optional<Response> loadFile(String language, String path) {
|
||||
try {
|
||||
var resource = base.map(b -> getLocalUrl(b, language, path)).orElseGet(() -> getResource(language, 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()));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,7 @@
|
||||
<head>
|
||||
<title>Light OIDC</title>
|
||||
<script src="config.js"></script>
|
||||
<script src="lightoidc.js"></script>
|
||||
<script>
|
||||
checkUser();
|
||||
</script>
|
||||
<script src="index.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome!</h1>
|
||||
|
||||
7
de.srsoftware.oidc.web/src/main/resources/en/index.js
Normal file
7
de.srsoftware.oidc.web/src/main/resources/en/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const UNAUTHORIZED = 401;
|
||||
|
||||
function handleUser(response){
|
||||
console.log(response);
|
||||
}
|
||||
|
||||
fetch(api+"/user").then(handleUser);
|
||||
@@ -1,43 +0,0 @@
|
||||
const UNAUTHORIZED = 401;
|
||||
|
||||
function handleCheckUser(response){
|
||||
console.log(window.location.href);
|
||||
if (response.status == UNAUTHORIZED){
|
||||
window.location.href = "login.html";
|
||||
return;
|
||||
}
|
||||
}
|
||||
function checkUser(){
|
||||
fetch(api+"/user")
|
||||
.then(handleCheckUser)
|
||||
.catch((err) => console.log(err));
|
||||
}
|
||||
|
||||
function handleLogin(response){
|
||||
if (response.status == 401){
|
||||
loadError("login-failed");
|
||||
return;
|
||||
}
|
||||
console.log(response);
|
||||
}
|
||||
|
||||
function loadError(page){
|
||||
fetch(web+"/"+page+".txt").then(resp => resp.text()).then(showError);
|
||||
}
|
||||
|
||||
function showError(content){
|
||||
document.getElementById("error").innerHTML = content;
|
||||
}
|
||||
|
||||
function tryLogin(){
|
||||
document.getElementById("error").innerHTML = "";
|
||||
var data = Object.fromEntries(new FormData(document.getElementById('login')));
|
||||
fetch(api+"/login",{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
}).then(handleLogin);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<head>
|
||||
<title>Light OIDC</title>
|
||||
<script src="config.js"></script>
|
||||
<script src="lightoidc.js"></script>
|
||||
<script src="index.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Login</h1>
|
||||
|
||||
Reference in New Issue
Block a user