Browse Source

working on list creation

drop_old_mail
Stephan Richter 3 years ago
parent
commit
b9f1ac0516
  1. 1
      src/main/java/de/srsoftware/widerhall/Constants.java
  2. 24
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  3. 109
      src/main/java/de/srsoftware/widerhall/web/Web.java
  4. 69
      static/templates/add_list.st
  5. 4
      static/templates/listlist.st

1
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 IMAPS = "imaps";
public static final String INBOX = "inbox"; public static final String INBOX = "inbox";
public static final String INDEX = "index"; public static final String INDEX = "index";
public static final String INT = "INT";
public static final String NAME = "name"; public static final String NAME = "name";
public static final String NOTES = "notes"; public static final String NOTES = "notes";
public static final String PASSWORD = "password"; public static final String PASSWORD = "password";

24
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.Logger;
import org.slf4j.LoggerFactory; 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.sql.SQLException;
import java.util.Map; import java.util.Map;
import static de.srsoftware.widerhall.Constants.*;
public class MailingList { public class MailingList {
private static final Logger LOG = LoggerFactory.getLogger(MailingList.class); 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 name;
private final String email; private final String email;
public static final String TABLE_NAME = "Lists"; public static final String TABLE_NAME = "Lists";
@ -33,6 +37,14 @@ public class MailingList {
.append(" (") .append(" (")
.append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ") .append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ")
.append(NAME).append(" ").append(VARCHAR) .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(");"); .append(");");
Database.open().query(sql.toString()).run(); Database.open().query(sql.toString()).run();
} }

109
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); 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) { private String handleGet(HttpServletRequest req, HttpServletResponse resp) {
var path = req.getPathInfo(); var path = req.getPathInfo();
path = path == null ? INDEX : path.substring(1); path = path == null ? INDEX : path.substring(1);
@ -81,33 +87,38 @@ public class Web extends HttpServlet {
return redirectTo(LOGIN,resp); 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<String, ? extends Object> data, HttpServletResponse resp) { private String handleLogin(HttpServletRequest req, HttpServletResponse resp) {
var template = templates.getInstanceOf(path); var email = req.getParameter("email");
if (template != null){ 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 { try {
template.add("data",data); LOG.warn("Static.handleLogin failed:",e);
resp.getWriter().println(template.render()); Thread.sleep(10000);
return null; } finally {
} catch (IOException e) { return loadTemplate("login", Map.of("error",t("Invalid username/password")), resp);
return t("Failed to load template '{}'",path);
} }
} }
return t("No template for path {}!",path); return null;
} }
private String handlePost(HttpServletRequest req, HttpServletResponse resp) {
private String redirectTo(String page, HttpServletResponse resp) { var path = req.getPathInfo();
try { path = path == null ? INDEX : path.substring(1);
resp.sendRedirect(String.join("/",WEB_ROOT,page)); switch (path){
return null; case LOGIN:
} catch (IOException e) { return handleLogin(req,resp);
return t("Was not able to redirect to {} page: {}", page, e.getMessage()); case REGISTER:
return registerUser(req,resp);
} }
return t("No handler for path {}!",path);
} }
private String loadFile(String filename, HttpServletResponse resp) { private String loadFile(String filename, HttpServletResponse resp) {
@ -124,25 +135,36 @@ public class Web extends HttpServlet {
return null; return null;
} }
@Override private String loadTemplate(String path, Map<String, ? extends Object> data, HttpServletResponse resp) {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { var template = templates.getInstanceOf(path);
String error = handlePost(req, resp); if (template != null){
if (error != null) resp.sendError(400,error); 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) { private void loadTemplates() {
var path = req.getPathInfo(); var templateDir = String.join(File.separator,baseDir,"static","templates");
path = path == null ? INDEX : path.substring(1); templates = new STRawGroupDir(templateDir,'«','»');
switch (path){ }
case LOGIN:
return handleLogin(req,resp);
case REGISTER:
return registerUser(req,resp);
}
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) { private String registerUser(HttpServletRequest req, HttpServletResponse resp) {
var email = req.getParameter("email"); 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;
}
} }

69
static/templates/add_list.st

@ -0,0 +1,69 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="jquery"></script>
<script src="js"></script>
<link rel="stylesheet" href="css" />
</head>
<body id="login">
«userinfo()»
<h1>Widerhall List Creation</h1>
«messages()»
<form method="POST" action="add_list">
<fieldset>
<legend>List configuration</legend>
<fieldset>
<legend>Basic data</legend>
<label>
<input type="text" name="name" value="«data.name»" id="name" />
List name
</label>
<label>
<input type="text" name="email" value="«data.email»" id="email" />
List E-Mail Address
</label>
</fieldset>
<fieldset>
<legend>IMAP protocol</legend>
<label>
<input type="text" name="imap_host" value="«data.imap.host»" id="imap_host" />
Hostname
</label>
<label>
<input type="number" name="imap_port" value="«data.imap.port»" id="imap_port" />
Port
</label>
<label>
<input type="text" name="imap_user" value="«data.imap.user»" id="imap_user" />
Username
</label>
<label>
<input type="password" name="imap_pass" value="«data.imap.pass»" id="imap_pass" />
Password
</label>
</fieldset>
<fieldset>
<legend>SMTP protocol</legend>
<label>
<input type="text" name="smtp_host" value="«data.smtp.host»" id="smtp_host" />
Hostname
</label>
<label>
<input type="number" name="smtp_port" value="«data.smtp.port»" id="smtp_port" />
Port
</label>
<label>
<input type="text" name="smtp_user" value="«data.smtp.user»" id="smtp_user" />
Username
</label>
<label>
<input type="password" name="smtp_pass" value="«data.smtp.pass»" id="smtp_pass" />
Password
</label>
</fieldset>
<button type="submit">Save new mailing list</button>
</fieldset>
</form>
</body>
</html>

4
static/templates/listlist.st

@ -7,4 +7,8 @@
<th>Details</th> <th>Details</th>
</tr> </tr>
</table> </table>
<a href="add_list">Add new mailing list</a>
<script type="text/javascript">
loadListList();
</script>
</fieldset> </fieldset>
Loading…
Cancel
Save