diff --git a/.gitignore b/.gitignore index ff424ad..9f758d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea +archive target config/config.json *.sqlite3 diff --git a/pom.xml b/pom.xml index 3b62480..c00c192 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.2.1 + 0.2.2 diff --git a/src/main/java/de/srsoftware/widerhall/Configuration.java b/src/main/java/de/srsoftware/widerhall/Configuration.java index dd234e7..0acb2fa 100644 --- a/src/main/java/de/srsoftware/widerhall/Configuration.java +++ b/src/main/java/de/srsoftware/widerhall/Configuration.java @@ -59,6 +59,7 @@ public class Configuration { dbFile(); baseUrl(); serverPort(); + archiveDir(); return this; } @@ -92,6 +93,13 @@ public class Configuration { return this; } + public File archiveDir() { + var locations = locations(); + if (!locations.containsKey(ARCHIVE)) locations.put(ARCHIVE, String.join(File.separator,baseDir(),"archive")); + return new File((String) locations.get(ARCHIVE)); + } + + public int serverPort() { if (!data.containsKey(PORT)) data.put(PORT,80L); var o = data.get(PORT); @@ -107,4 +115,5 @@ public class Configuration { public File file() { return file; } + } diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java index b84620f..fc3ca19 100644 --- a/src/main/java/de/srsoftware/widerhall/Constants.java +++ b/src/main/java/de/srsoftware/widerhall/Constants.java @@ -1,6 +1,7 @@ package de.srsoftware.widerhall; public class Constants { + public static final String ARCHIVE = "archive"; public static final String BASE = "base"; public static final String BASE_URL = "base_url"; public static final String CONFIG = "configuration"; diff --git a/src/main/java/de/srsoftware/widerhall/data/Database.java b/src/main/java/de/srsoftware/widerhall/data/Database.java index 1f85370..31e143e 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Database.java +++ b/src/main/java/de/srsoftware/widerhall/data/Database.java @@ -93,6 +93,7 @@ public class Database { private final StringBuilder sql; // buffer the sql to be built private final HashMap> where = new HashMap<>(); // buffer condition statements for select private final HashMap values = new HashMap<>(); // buffer values for insert/update statements + private final List sortFields = new ArrayList<>(); /** * Start to create a new request with the initial SQL @@ -126,6 +127,10 @@ public class Database { } } + private void applySorting(){ + if (!sortFields.isEmpty()) sql.append(" ORDER BY ").append(String.join(", ",sortFields)); + } + @Override protected Request clone() { Request clone = new Request(sql); @@ -142,6 +147,7 @@ public class Database { var args = new ArrayList<>(); applyValues(args); applyConditions(args); + applySorting(); return new CompiledRequest(sql.toString(),args); } @@ -245,6 +251,11 @@ public class Database { list.add(value); return this; } + + public Request sort(String field) { + sortFields.add(field); + return this; + } } public Database(Connection connection) { diff --git a/src/main/java/de/srsoftware/widerhall/data/Post.java b/src/main/java/de/srsoftware/widerhall/data/Post.java index 75eeeda..adf8a3e 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Post.java +++ b/src/main/java/de/srsoftware/widerhall/data/Post.java @@ -1,5 +1,6 @@ package de.srsoftware.widerhall.data; +import de.srsoftware.widerhall.Configuration; import de.srsoftware.widerhall.Util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,8 +12,10 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -59,7 +62,9 @@ public class Post { var time = message.getSentDate().getTime(); Post post = new Post(id,list,fromEmail,fromName,subject,time); - Files.writeString(post.file().toPath(),text, StandardCharsets.UTF_8); + var file = post.file(); + file.getParentFile().mkdirs(); + Files.writeString(file.toPath(),text, StandardCharsets.UTF_8); return post.save(); } catch (MessagingException | IOException | SQLException e) { LOG.warn("Failed to create post from {}",message); @@ -93,10 +98,10 @@ public class Post { return new File(filename); } - public static HashSet find(MailingList list) throws SQLException { - var rs = Database.open().select(TABLE_NAME).where(LIST,list.email()).compile().exec(); + public static ArrayList find(MailingList list) throws SQLException { + var rs = Database.open().select(TABLE_NAME).where(LIST,list.email()).sort(DATE).compile().exec(); try { - var result = new HashSet(); + var result = new ArrayList(); while (rs.next()) result.add(Post.from(rs)); return result; } finally { @@ -111,6 +116,7 @@ public class Post { if (post == null) { var list = MailingList.load(rs.getString(LIST)); post = new Post(id, list, rs.getString(FROM_ADDR), rs.getString(FROM_NAME), rs.getString(SUBJECT), rs.getLong(DATE)); + post.filename = rs.getString(FILE); cache.put(id,post); } return post; @@ -122,7 +128,7 @@ public class Post { private String generateFilename() { - return "/tmp/"+id+".json"; + return Configuration.instance().archiveDir()+File.separator+id+".txt"; } public String id() { diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index 8c8a87d..f1ff180 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -9,19 +9,15 @@ import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.mail.MessagingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.Serializable; -import java.io.UnsupportedEncodingException; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import static de.srsoftware.widerhall.Constants.*; import static de.srsoftware.widerhall.Util.t; @@ -148,17 +144,17 @@ public class Rest extends HttpServlet { } } - private Map archive(HttpServletRequest req) { + private List archive(HttpServletRequest req) { var list = Util.getMailingList(req); if (list != null){ try { - return Post.find(list).stream().collect(Collectors.toMap(Post::timestamp,Post::safeMap)); + return Post.find(list).stream().map(Post::safeMap).toList(); } catch (SQLException e) { e.printStackTrace(); } } LOG.debug("list: {}",list.email()); - return Map.of(); + return List.of(); } public String handlePost(HttpServletRequest req, HttpServletResponse resp){ diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 698d81d..9b01702 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -28,7 +28,6 @@ public class Web extends TemplateServlet { private static final String CONFIRM = "confirm"; private static final Logger LOG = LoggerFactory.getLogger(Web.class); private static final String ADMIN = "admin"; - private static final String ARCHIVE = "archive"; private static final String INSPECT = "inspect"; private static final String LOGIN = "login"; private static final String LOGOUT = "logout";