From b9f1ac0516f94a3c54ab24980a7748ea0d54bc40 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Fri, 15 Apr 2022 15:56:00 +0200 Subject: [PATCH] working on list creation --- .../de/srsoftware/widerhall/Constants.java | 1 + .../widerhall/data/MailingList.java | 24 +++- .../java/de/srsoftware/widerhall/web/Web.java | 109 +++++++++--------- static/templates/add_list.st | 69 +++++++++++ static/templates/listlist.st | 4 + 5 files changed, 148 insertions(+), 59 deletions(-) create mode 100644 static/templates/add_list.st diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java index a687e5e..9ec1e0c 100644 --- a/src/main/java/de/srsoftware/widerhall/Constants.java +++ b/src/main/java/de/srsoftware/widerhall/Constants.java @@ -8,6 +8,7 @@ public class Constants { public static final String IMAPS = "imaps"; public static final String INBOX = "inbox"; public static final String INDEX = "index"; + public static final String INT = "INT"; public static final String NAME = "name"; public static final String NOTES = "notes"; public static final String PASSWORD = "password"; diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index f91856d..694d036 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -3,17 +3,21 @@ package de.srsoftware.widerhall.data; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.validation.constraints.Email; - -import static de.srsoftware.widerhall.Constants.*; -import static de.srsoftware.widerhall.data.Database.HASHED_PASS; -import static de.srsoftware.widerhall.data.Database.SALT; - import java.sql.SQLException; import java.util.Map; +import static de.srsoftware.widerhall.Constants.*; + public class MailingList { private static final Logger LOG = LoggerFactory.getLogger(MailingList.class); + private static final String IMAP_HOST = "imap_host"; + private static final String IMAP_PORT = "imap_port"; + private static final String IMAP_USER = "imap_user"; + private static final String IMAP_PASS = "imap_pass"; + private static final String SMTP_HOST = "smtp_host"; + private static final String SMTP_PORT = "smtp_port"; + private static final String SMTP_USER = "smtp_user"; + private static final String SMTP_PASS = "smtp_pass"; private final String name; private final String email; public static final String TABLE_NAME = "Lists"; @@ -33,6 +37,14 @@ public class MailingList { .append(" (") .append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ") .append(NAME).append(" ").append(VARCHAR) + .append(IMAP_HOST).append(" ").append(VARCHAR) + .append(IMAP_PORT).append(" ").append(INT) + .append(IMAP_USER).append(" ").append(VARCHAR) + .append(IMAP_PASS).append(" ").append(VARCHAR) + .append(SMTP_HOST).append(" ").append(VARCHAR) + .append(SMTP_PORT).append(" ").append(INT) + .append(SMTP_USER).append(" ").append(VARCHAR) + .append(SMTP_PASS).append(" ").append(VARCHAR) .append(");"); Database.open().query(sql.toString()).run(); } diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index e84bc8f..4f496ec 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -44,6 +44,12 @@ public class Web extends HttpServlet { if (error != null) resp.sendError(400,error); } + @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 handleGet(HttpServletRequest req, HttpServletResponse resp) { var path = req.getPathInfo(); path = path == null ? INDEX : path.substring(1); @@ -81,33 +87,38 @@ public class Web extends HttpServlet { return redirectTo(LOGIN,resp); } - private void loadTemplates() { - var templateDir = String.join(File.separator,baseDir,"static","templates"); - templates = new STRawGroupDir(templateDir,'«','»'); - } - private String loadTemplate(String path, Map data, HttpServletResponse resp) { - var template = templates.getInstanceOf(path); - if (template != null){ + private String handleLogin(HttpServletRequest req, HttpServletResponse resp) { + var email = req.getParameter("email"); + var pass = req.getParameter("pass"); + if (email == null || pass == null) return loadTemplate("login", Map.of("error",t("Missing username or password!")), 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 { - template.add("data",data); - resp.getWriter().println(template.render()); - return null; - } catch (IOException e) { - return t("Failed to load template '{}'",path); + LOG.warn("Static.handleLogin failed:",e); + Thread.sleep(10000); + } finally { + return loadTemplate("login", Map.of("error",t("Invalid username/password")), resp); } } - return t("No template for path {}!",path); + return null; } - - private String redirectTo(String page, HttpServletResponse resp) { - try { - resp.sendRedirect(String.join("/",WEB_ROOT,page)); - return null; - } catch (IOException e) { - return t("Was not able to redirect to {} page: {}", page, e.getMessage()); + private String handlePost(HttpServletRequest req, HttpServletResponse resp) { + var path = req.getPathInfo(); + path = path == null ? INDEX : path.substring(1); + switch (path){ + case LOGIN: + return handleLogin(req,resp); + case REGISTER: + return registerUser(req,resp); } + + return t("No handler for path {}!",path); } private String loadFile(String filename, HttpServletResponse resp) { @@ -124,25 +135,36 @@ public class Web extends HttpServlet { 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 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 String handlePost(HttpServletRequest req, HttpServletResponse resp) { - var path = req.getPathInfo(); - path = path == null ? INDEX : path.substring(1); - switch (path){ - case LOGIN: - return handleLogin(req,resp); - case REGISTER: - return registerUser(req,resp); - } + private void loadTemplates() { + var templateDir = String.join(File.separator,baseDir,"static","templates"); + templates = new STRawGroupDir(templateDir,'«','»'); + } - return t("No handler for path {}!",path); + private String redirectTo(String page, HttpServletResponse resp) { + try { + resp.sendRedirect(String.join("/",WEB_ROOT,page)); + return null; + } catch (IOException e) { + return t("Was not able to redirect to {} page: {}", page, e.getMessage()); + } } + + private String registerUser(HttpServletRequest req, HttpServletResponse resp) { var email = req.getParameter("email"); @@ -177,24 +199,5 @@ public class Web extends HttpServlet { } } - private String handleLogin(HttpServletRequest req, HttpServletResponse resp) { - var email = req.getParameter("email"); - var pass = req.getParameter("pass"); - if (email == null || pass == null) return loadTemplate("login", Map.of("error",t("Missing username or password!")), 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; - } } diff --git a/static/templates/add_list.st b/static/templates/add_list.st new file mode 100644 index 0000000..be19384 --- /dev/null +++ b/static/templates/add_list.st @@ -0,0 +1,69 @@ + + + + + + + + + + «userinfo()» +

Widerhall List Creation

+ «messages()» +
+
+ List configuration +
+ Basic data + + +
+
+ IMAP protocol + + + + +
+
+ SMTP protocol + + + + +
+ +
+
+ + \ No newline at end of file diff --git a/static/templates/listlist.st b/static/templates/listlist.st index 530910f..ad8a09d 100644 --- a/static/templates/listlist.st +++ b/static/templates/listlist.st @@ -7,4 +7,8 @@ Details + Add new mailing list + \ No newline at end of file