diff --git a/pom.xml b/pom.xml index f949725..455fe2e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.0.8 + 0.0.9 diff --git a/src/main/java/de/srsoftware/widerhall/Application.java b/src/main/java/de/srsoftware/widerhall/Application.java index 40d6009..59a5454 100644 --- a/src/main/java/de/srsoftware/widerhall/Application.java +++ b/src/main/java/de/srsoftware/widerhall/Application.java @@ -1,5 +1,6 @@ package de.srsoftware.widerhall; +import de.srsoftware.widerhall.web.Front; import de.srsoftware.widerhall.web.Rest; import de.srsoftware.widerhall.web.Web; import org.eclipse.jetty.server.Connector; @@ -34,6 +35,7 @@ public class Application { ServletContextHandler context = new ServletContextHandler(server, "/",sh,null,null,null); context.addServlet(Rest.class,"/api/*"); context.addServlet(Web.class,"/web/*"); + context.addServlet(Front.class,"/*"); server.start(); } diff --git a/src/main/java/de/srsoftware/widerhall/web/Front.java b/src/main/java/de/srsoftware/widerhall/web/Front.java new file mode 100644 index 0000000..8cc6b32 --- /dev/null +++ b/src/main/java/de/srsoftware/widerhall/web/Front.java @@ -0,0 +1,19 @@ +package de.srsoftware.widerhall.web; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; + +public class Front extends TemplateServlet { + + private static final String FRONT = "frontpage"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + loadTemplates(); + String error = loadTemplate(FRONT, Map.of(),resp); + if (error != null) resp.sendError(400,error); + } +} diff --git a/src/main/java/de/srsoftware/widerhall/web/TemplateServlet.java b/src/main/java/de/srsoftware/widerhall/web/TemplateServlet.java new file mode 100644 index 0000000..79b24a7 --- /dev/null +++ b/src/main/java/de/srsoftware/widerhall/web/TemplateServlet.java @@ -0,0 +1,57 @@ +package de.srsoftware.widerhall.web; + +import de.srsoftware.widerhall.Configuration; +import org.stringtemplate.v4.STGroup; +import org.stringtemplate.v4.STRawGroupDir; + +import javax.servlet.http.HttpServlet; +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 abstract class TemplateServlet extends HttpServlet { + private STGroup templates; + private final String baseDir; + + public TemplateServlet(){ + var config = Configuration.instance(); + baseDir = config.baseDir(); + loadTemplates(); + } + + protected String loadFile(String filename, HttpServletResponse resp) { + var path = String.join(File.separator,baseDir,"static",filename); + 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; + } + + protected String loadTemplate(String path, Map 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); + } + + protected void loadTemplates() { + var templateDir = String.join(File.separator,baseDir,"static","templates"); + templates = new STRawGroupDir(templateDir,'«','»'); + } +} diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 41ddbf2..8d6b861 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -28,7 +28,8 @@ import java.util.Map; import static de.srsoftware.widerhall.Constants.*; import static de.srsoftware.widerhall.Util.t; -public class Web extends HttpServlet { +public class Web extends TemplateServlet { + public static final String WEB_ROOT = "/web"; private static final String ADD_LIST = "add_list"; private static final String CONFIRM = "confirm"; private static final Logger LOG = LoggerFactory.getLogger(Web.class); @@ -48,15 +49,6 @@ public class Web extends HttpServlet { private static final String SMTP_USER = "smtp_user"; private static final String SMTP_PASS = "smtp_pass"; private static final int PRIMARY_KEY_CONSTRAINT = 19; - private final String baseDir; - private STGroup templates; - private static final String WEB_ROOT = "/web"; - - public Web(){ - var config = Configuration.instance(); - baseDir = config.baseDir(); - loadTemplates(); - } private String addList(HttpServletRequest req, HttpServletResponse resp) { @@ -154,7 +146,6 @@ public class Web extends HttpServlet { } } - @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { String error = handleGet(req, resp); @@ -181,12 +172,13 @@ public class Web extends HttpServlet { } private String handleGet(HttpServletRequest req, HttpServletResponse resp) { + var path = req.getPathInfo(); + if (path == null) return redirectTo(INDEX,resp); var o = req.getSession().getAttribute("user"); User user = o instanceof User ? (User) o : null; var data = new HashMap(); if (user != null) data.put(USER,user.safeMap()); - var path = req.getPathInfo(); - path = (path == null || path.equals("/")) ? INDEX : path.substring(1); + path = path.equals("/") ? INDEX : path.substring(1); String notes = null; var listEmail = req.getParameter(LIST); var list = MailingList.load(listEmail); @@ -276,37 +268,9 @@ public class Web extends HttpServlet { return t("No handler for path {}!",path); } - private String loadFile(String filename, HttpServletResponse resp) { - var path = String.join(File.separator,baseDir,"static",filename); - 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; - } - private String loadTemplate(String path, Map 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 void loadTemplates() { - var templateDir = String.join(File.separator,baseDir,"static","templates"); - templates = new STRawGroupDir(templateDir,'«','»'); - } + private String redirectTo(String page, HttpServletResponse resp) { try { diff --git a/static/templates/frontpage.st b/static/templates/frontpage.st new file mode 100644 index 0000000..2f7cbe6 --- /dev/null +++ b/static/templates/frontpage.st @@ -0,0 +1,40 @@ + + + + + + + +

Widerhall front page

+ If you are looking for you mailing lists, Go to the /web page... +
+ What is Widerhall? +

+ Widerhall is a mailing list system written in Java. + It allows to maintain a set of mailing lists with the option to make (some of) them publicy subscribable. +

+

+ Widerhall is very lightweight, as does not include a full email server stack. +

+
+
+ Why should I use Widerhall? +

+ Compared to other mailing list systems, widerhall is very lightweight: +

+ It contains not mailserver stack. It does not even require you to set up a mailserver! +

+
+
+ How does it work? +

+ Widerhall works by just letting you set up any IMAP/SMTP enabled email address with a provider of your choice. + It then connects to the inbox of you selected email address and watches for incoming messages. + Upon message reception, it forwards the message to all subscribers of the mailing list. +

+

+ That's it. +

+
+ + \ No newline at end of file