working on configuration
This commit is contained in:
138
src/main/java/de/srsoftware/widerhall/web/Web.java
Normal file
138
src/main/java/de/srsoftware/widerhall/web/Web.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package de.srsoftware.widerhall.web;
|
||||
|
||||
import de.srsoftware.widerhall.Configuration;
|
||||
import de.srsoftware.widerhall.Util;
|
||||
import de.srsoftware.widerhall.data.User;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.stringtemplate.v4.STGroup;
|
||||
import org.stringtemplate.v4.STRawGroupDir;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.srsoftware.widerhall.Util.t;
|
||||
|
||||
public class Web extends HttpServlet {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Web.class);
|
||||
private static final String LOGIN = "login";
|
||||
private final String baseDir;
|
||||
private final STGroup templates;
|
||||
private static final String WEB_ROOT = "/web";
|
||||
|
||||
public Web(){
|
||||
var config = Configuration.instance();
|
||||
baseDir = config.baseDir();
|
||||
var templateDir = String.join(File.separator,baseDir,"static","templates");
|
||||
templates = new STRawGroupDir(templateDir,'«','»');
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String error = handleGet(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
private String handleGet(HttpServletRequest req, HttpServletResponse resp) {
|
||||
var path = req.getPathInfo();
|
||||
path = path == null ? "index" : path.substring(1);
|
||||
switch (path){
|
||||
case "css":
|
||||
case "js":
|
||||
case "login":
|
||||
return loadTemplate(path,null,resp);
|
||||
case "jquery":
|
||||
return loadFile("jquery-3.6.0.min.js",resp);
|
||||
}
|
||||
|
||||
var u = req.getSession().getAttribute("user");
|
||||
if (u instanceof User user){
|
||||
Map<String,Object> data = Map.of("user",user.map());
|
||||
return loadTemplate(path,data,resp);
|
||||
}
|
||||
return loginRedirect(resp);
|
||||
}
|
||||
|
||||
private String loadTemplate(String path, Map<String, ? extends Object> data, HttpServletResponse resp) {
|
||||
var template = templates.getInstanceOf(path);
|
||||
if (template != null){
|
||||
try {
|
||||
template.add("data",data);
|
||||
resp.getWriter().println(template.render());
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
return t("Failed to load template '{}'",path);
|
||||
}
|
||||
}
|
||||
return t("No template for path {}!",path);
|
||||
}
|
||||
|
||||
|
||||
private String loginRedirect(HttpServletResponse resp) {
|
||||
try {
|
||||
resp.sendRedirect(String.join("/",WEB_ROOT,LOGIN));
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
return t("Was not able to redirect to login page: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String loadFile(String filename, HttpServletResponse resp) {
|
||||
var path = String.join(File.separator,baseDir,filename);
|
||||
LOG.debug("loading {}",path);
|
||||
var file = new File(path);
|
||||
if (!file.exists()) return t("File {} does not exist!",filename);
|
||||
try {
|
||||
var content = Files.readString(file.toPath());
|
||||
resp.getWriter().println(content);
|
||||
} catch (IOException e) {
|
||||
return t("Failed to load file '{}'!",filename);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String error = handlePost(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
private String handlePost(HttpServletRequest req, HttpServletResponse resp) {
|
||||
var path = req.getPathInfo();
|
||||
if (path == null) path = "/";
|
||||
switch (path){
|
||||
case "/login":
|
||||
return handleLogin(req,resp);
|
||||
}
|
||||
|
||||
return t("No handler for path {}!",path);
|
||||
}
|
||||
|
||||
private String handleLogin(HttpServletRequest req, HttpServletResponse resp) {
|
||||
var email = req.getParameter("email");
|
||||
var pass = req.getParameter("pass");
|
||||
if (email == null || pass == null) return loginRedirect(resp);
|
||||
if (!Util.isEmail(email)) return loadTemplate("login", Map.of("error",t("'{}' is not a valid email address!",email)), resp);
|
||||
try {
|
||||
var user = User.load(email,pass);
|
||||
req.getSession().setAttribute("user",user);
|
||||
resp.sendRedirect(String.join("/",WEB_ROOT,"index"));
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
LOG.warn("Static.handleLogin failed:",e);
|
||||
Thread.sleep(10000);
|
||||
} finally {
|
||||
return loadTemplate("login", Map.of("error",t("Invalid username/password")), resp);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user