Browse Source

added fromtpage

drop_old_mail
Stephan Richter 3 years ago
parent
commit
791b28aec6
  1. 2
      pom.xml
  2. 2
      src/main/java/de/srsoftware/widerhall/Application.java
  3. 19
      src/main/java/de/srsoftware/widerhall/web/Front.java
  4. 57
      src/main/java/de/srsoftware/widerhall/web/TemplateServlet.java
  5. 48
      src/main/java/de/srsoftware/widerhall/web/Web.java
  6. 40
      static/templates/frontpage.st

2
pom.xml

@ -6,7 +6,7 @@
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>Widerhall</artifactId> <artifactId>Widerhall</artifactId>
<version>0.0.8</version> <version>0.0.9</version>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

2
src/main/java/de/srsoftware/widerhall/Application.java

@ -1,5 +1,6 @@
package de.srsoftware.widerhall; package de.srsoftware.widerhall;
import de.srsoftware.widerhall.web.Front;
import de.srsoftware.widerhall.web.Rest; import de.srsoftware.widerhall.web.Rest;
import de.srsoftware.widerhall.web.Web; import de.srsoftware.widerhall.web.Web;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
@ -34,6 +35,7 @@ public class Application {
ServletContextHandler context = new ServletContextHandler(server, "/",sh,null,null,null); ServletContextHandler context = new ServletContextHandler(server, "/",sh,null,null,null);
context.addServlet(Rest.class,"/api/*"); context.addServlet(Rest.class,"/api/*");
context.addServlet(Web.class,"/web/*"); context.addServlet(Web.class,"/web/*");
context.addServlet(Front.class,"/*");
server.start(); server.start();
} }

19
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);
}
}

57
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<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);
}
protected void loadTemplates() {
var templateDir = String.join(File.separator,baseDir,"static","templates");
templates = new STRawGroupDir(templateDir,'«','»');
}
}

48
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.Constants.*;
import static de.srsoftware.widerhall.Util.t; 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 ADD_LIST = "add_list";
private static final String CONFIRM = "confirm"; private static final String CONFIRM = "confirm";
private static final Logger LOG = LoggerFactory.getLogger(Web.class); 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_USER = "smtp_user";
private static final String SMTP_PASS = "smtp_pass"; private static final String SMTP_PASS = "smtp_pass";
private static final int PRIMARY_KEY_CONSTRAINT = 19; 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) { private String addList(HttpServletRequest req, HttpServletResponse resp) {
@ -154,7 +146,6 @@ public class Web extends HttpServlet {
} }
} }
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String error = handleGet(req, resp); String error = handleGet(req, resp);
@ -181,12 +172,13 @@ public class Web extends HttpServlet {
} }
private String handleGet(HttpServletRequest req, HttpServletResponse resp) { private String handleGet(HttpServletRequest req, HttpServletResponse resp) {
var path = req.getPathInfo();
if (path == null) return redirectTo(INDEX,resp);
var o = req.getSession().getAttribute("user"); var o = req.getSession().getAttribute("user");
User user = o instanceof User ? (User) o : null; User user = o instanceof User ? (User) o : null;
var data = new HashMap<String,Object>(); var data = new HashMap<String,Object>();
if (user != null) data.put(USER,user.safeMap()); if (user != null) data.put(USER,user.safeMap());
var path = req.getPathInfo(); path = path.equals("/") ? INDEX : path.substring(1);
path = (path == null || path.equals("/")) ? INDEX : path.substring(1);
String notes = null; String notes = null;
var listEmail = req.getParameter(LIST); var listEmail = req.getParameter(LIST);
var list = MailingList.load(listEmail); var list = MailingList.load(listEmail);
@ -276,37 +268,9 @@ public class Web extends HttpServlet {
return t("No handler for path {}!",path); 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<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 void loadTemplates() {
var templateDir = String.join(File.separator,baseDir,"static","templates");
templates = new STRawGroupDir(templateDir,'«','»');
}
private String redirectTo(String page, HttpServletResponse resp) { private String redirectTo(String page, HttpServletResponse resp) {
try { try {

40
static/templates/frontpage.st

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css" />
</head>
<body id="login">
<h1>Widerhall front page</h1>
If you are looking for you mailing lists, <a href="web/index">Go to the /web page</a>...
<fieldset>
<legend>What is <em>Widerhall</em>?</legend>
<p>
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.
</p>
<p>
<em>Widerhall</em> is very lightweight, as does not include a full email server stack.
</p>
</fieldset>
<fieldset>
<legend>Why should I use <em>Widerhall</em>?</legend>
<p>
Compared to other mailing list systems, widerhall is very lightweight:
<p>
It contains not mailserver stack. It does not even require you to set up a mailserver!
</p>
</fieldset>
<fieldset>
<legend>How does it work?</legend>
<p>
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.
</p>
<p>
That's it.
</p>
</fieldset>
</body>
</html>
Loading…
Cancel
Save