From fbcd9e913f8ac11f3a4cd503008e7d7d11fc754d Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 3 May 2019 08:44:52 +0200 Subject: [PATCH 1/3] + Anfang Dateien Verwaltung Signed-off-by: Johannes Theiner #SPM-7 add work development 1h --- pom.xml | 13 +++++ .../java/de/hsel/spm/baudas/web/Data.java | 39 +++++++++++++++ .../java/de/hsel/spm/baudas/web/Files.java | 29 +++++++++++ .../java/de/hsel/spm/baudas/web/Upload.java | 49 +++++++++++++++++++ src/main/webapp/version.jsp | 7 +++ 5 files changed, 137 insertions(+) create mode 100644 src/main/java/de/hsel/spm/baudas/web/Data.java create mode 100644 src/main/java/de/hsel/spm/baudas/web/Files.java create mode 100644 src/main/java/de/hsel/spm/baudas/web/Upload.java create mode 100644 src/main/webapp/version.jsp diff --git a/pom.xml b/pom.xml index 1c9e45e..b7c613d 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,13 @@ TomcatServer /bauDas + + + javax.servlet + javax.servlet-api + 4.0.1 + + org.apache.maven.plugins @@ -258,6 +265,12 @@ 3.8.3 + + com.google.code.gson + gson + 2.8.2 + + diff --git a/src/main/java/de/hsel/spm/baudas/web/Data.java b/src/main/java/de/hsel/spm/baudas/web/Data.java new file mode 100644 index 0000000..ed3581e --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/Data.java @@ -0,0 +1,39 @@ +package de.hsel.spm.baudas.web; + +import lombok.Getter; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ +public class Data { + + @Getter + private static ConcurrentMap files = new ConcurrentHashMap<>(); + + + public static Path add(String name) { + Path path; + if(files.size() >= 5) { + path = null; + files.put(UUID.randomUUID(), name); + }else { + UUID uuid = UUID.randomUUID(); + files.put(uuid, name); + path = Paths.get("target/" + uuid + ".csv"); + } + return path; + } + + public static File get(int index) { + return new File("target/" + index + ".csv"); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Files.java b/src/main/java/de/hsel/spm/baudas/web/Files.java new file mode 100644 index 0000000..56ff232 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/Files.java @@ -0,0 +1,29 @@ +package de.hsel.spm.baudas.web; + +import com.google.gson.Gson; +import org.jetbrains.annotations.NotNull; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ + +@WebServlet("/files") +public class Files extends HttpServlet { + + @Override + protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException { + resp.setContentType("application/json"); + PrintWriter out = resp.getWriter(); + Gson gson = new Gson(); + out.println(gson.toJson(Data.getFiles())); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Upload.java b/src/main/java/de/hsel/spm/baudas/web/Upload.java new file mode 100644 index 0000000..5da00e2 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -0,0 +1,49 @@ +package de.hsel.spm.baudas.web; + +import org.jetbrains.annotations.NotNull; + +import javax.servlet.ServletException; +import javax.servlet.annotation.MultipartConfig; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.Part; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ + +@WebServlet("/upload") +@MultipartConfig +public class Upload extends HttpServlet { + + @Override + protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding(StandardCharsets.UTF_8.name()); + + Part filePart = req.getPart("file"); + String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); + InputStream inputStream = filePart.getInputStream(); + + + Path path = Data.add(fileName); + System.out.println(path); + if(!Files.exists(path)) { + Files.createFile(path); + } + + Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING); + + resp.sendRedirect("files"); + } +} \ No newline at end of file diff --git a/src/main/webapp/version.jsp b/src/main/webapp/version.jsp new file mode 100644 index 0000000..d47f7ea --- /dev/null +++ b/src/main/webapp/version.jsp @@ -0,0 +1,7 @@ +<%@ page import="weka.core.Version" %> +Server info: <%= application.getServerInfo() %>
+Servlet version: <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %>
+JSP version: <%= JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion() %>
+Java version: <%= System.getProperty("java.version") %>
+ +Weka version: <%= Version.VERSION %> \ No newline at end of file From b50a490cd4d9fb3140fd74b548d3a025953aa478 Mon Sep 17 00:00:00 2001 From: joethei Date: Fri, 3 May 2019 13:06:21 +0200 Subject: [PATCH 2/3] Upload saves files in correct order. #SPM-7: add work 1h development --- .../java/de/hsel/spm/baudas/HelloWorld.java | 45 ------------------- .../java/de/hsel/spm/baudas/web/Data.java | 37 ++++++++++----- .../java/de/hsel/spm/baudas/web/Files.java | 4 ++ .../de/hsel/spm/baudas/web/SavedFile.java | 14 ++++++ .../java/de/hsel/spm/baudas/web/Upload.java | 2 + src/main/webapp/upload-test.html | 11 +++++ 6 files changed, 57 insertions(+), 56 deletions(-) delete mode 100644 src/main/java/de/hsel/spm/baudas/HelloWorld.java create mode 100644 src/main/java/de/hsel/spm/baudas/web/SavedFile.java create mode 100644 src/main/webapp/upload-test.html diff --git a/src/main/java/de/hsel/spm/baudas/HelloWorld.java b/src/main/java/de/hsel/spm/baudas/HelloWorld.java deleted file mode 100644 index 1cca1c8..0000000 --- a/src/main/java/de/hsel/spm/baudas/HelloWorld.java +++ /dev/null @@ -1,45 +0,0 @@ -package de.hsel.spm.baudas; - -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; - -/** - * Hello World. - * - * @author Johannes Theiner - * @version 0.1 - * @since 0.1 - */ - -@WebServlet("/") -public class HelloWorld extends HttpServlet { - - private static final long serialVersionUID = 156790693L; - - /** - * doGet. - * - * @param req Request - * @param resp Response - * @throws IOException failed - */ - @Override - protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) - throws IOException { - - resp.setContentType("text/html"); - final PrintWriter out = resp.getWriter(); - out.println(""); - out.println(""); - out.println("Hallo Welt!"); - out.println(""); - out.println(""); - out.println("

Hallo Ostfriesland!

"); - out.println(""); - out.println(""); - } -} diff --git a/src/main/java/de/hsel/spm/baudas/web/Data.java b/src/main/java/de/hsel/spm/baudas/web/Data.java index ed3581e..346e3ac 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Data.java +++ b/src/main/java/de/hsel/spm/baudas/web/Data.java @@ -1,15 +1,18 @@ package de.hsel.spm.baudas.web; import lombok.Getter; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentLinkedQueue; /** + * + * * @author Johannes Theiner * @version 0.1 * @since 0.1 @@ -17,23 +20,35 @@ import java.util.concurrent.ConcurrentMap; public class Data { @Getter - private static ConcurrentMap files = new ConcurrentHashMap<>(); + private static ConcurrentLinkedQueue files = new ConcurrentLinkedQueue<>(); - public static Path add(String name) { + @NotNull + static Path add(@NotNull String name) { Path path; + UUID uuid = UUID.randomUUID(); if(files.size() >= 5) { - path = null; - files.put(UUID.randomUUID(), name); + SavedFile file = files.poll(); + if(!get(file.getUuid()).delete()) { + System.out.println("failed"); + } + path = Paths.get(getFileName(uuid)); + files.offer(new SavedFile(uuid, name)); }else { - UUID uuid = UUID.randomUUID(); - files.put(uuid, name); - path = Paths.get("target/" + uuid + ".csv"); + files.add(new SavedFile(uuid, name)); + path = Paths.get(getFileName(uuid)); } return path; } - public static File get(int index) { - return new File("target/" + index + ".csv"); + @Contract + @NotNull public static File get(@NotNull UUID uuid) { + return new File(getFileName(uuid)); + } + + @Contract(pure = true) + @NotNull + private static String getFileName(@NotNull UUID uuid) { + return "target/" + uuid + ".csv"; } } \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Files.java b/src/main/java/de/hsel/spm/baudas/web/Files.java index 56ff232..e586c08 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Files.java +++ b/src/main/java/de/hsel/spm/baudas/web/Files.java @@ -9,8 +9,11 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; /** + * list all currently available files. + * * @author Johannes Theiner * @version 0.1 * @since 0.1 @@ -22,6 +25,7 @@ public class Files extends HttpServlet { @Override protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException { resp.setContentType("application/json"); + resp.setCharacterEncoding(StandardCharsets.UTF_8.name()); PrintWriter out = resp.getWriter(); Gson gson = new Gson(); out.println(gson.toJson(Data.getFiles())); diff --git a/src/main/java/de/hsel/spm/baudas/web/SavedFile.java b/src/main/java/de/hsel/spm/baudas/web/SavedFile.java new file mode 100644 index 0000000..3ed9901 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/SavedFile.java @@ -0,0 +1,14 @@ +package de.hsel.spm.baudas.web; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.UUID; + +@Getter +@AllArgsConstructor +public class SavedFile { + + private UUID uuid; + private String name; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/Upload.java b/src/main/java/de/hsel/spm/baudas/web/Upload.java index 5da00e2..92c992b 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Upload.java +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -18,6 +18,8 @@ import java.nio.file.Paths; import java.nio.file.StandardCopyOption; /** + * saves uploaded files. + * * @author Johannes Theiner * @version 0.1 * @since 0.1 diff --git a/src/main/webapp/upload-test.html b/src/main/webapp/upload-test.html new file mode 100644 index 0000000..b57a823 --- /dev/null +++ b/src/main/webapp/upload-test.html @@ -0,0 +1,11 @@ + + + +
+ + +
+ + \ No newline at end of file From d5b4888d5d81d24409a943aee51acbb15f262d09 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 3 May 2019 16:52:20 +0200 Subject: [PATCH 3/3] + Documentation Signed-off-by: Johannes Theiner #SPM-7: add work documentation 20m --- .../java/de/hsel/spm/baudas/web/Data.java | 54 ++++++++++++++++--- .../java/de/hsel/spm/baudas/web/Files.java | 13 ++++- .../de/hsel/spm/baudas/web/SavedFile.java | 9 ++++ .../java/de/hsel/spm/baudas/web/Upload.java | 18 +++++-- 4 files changed, 82 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/web/Data.java b/src/main/java/de/hsel/spm/baudas/web/Data.java index 346e3ac..d5f47e2 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Data.java +++ b/src/main/java/de/hsel/spm/baudas/web/Data.java @@ -5,13 +5,15 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.UUID; import java.util.concurrent.ConcurrentLinkedQueue; /** - * + * manages data about .csv files. * * @author Johannes Theiner * @version 0.1 @@ -23,29 +25,69 @@ public class Data { private static ConcurrentLinkedQueue files = new ConcurrentLinkedQueue<>(); + /** + * adds file to list and generates a new filename. + * + * @param name File name + * @return path to save file to + */ @NotNull static Path add(@NotNull String name) { + //cleanup old files + if (files.isEmpty()) { + try { + Files.list(Paths.get("target/")).forEach(path -> { + if (path.toFile().getName().endsWith(".csv")) { + try { + Files.delete(path); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + Path path; UUID uuid = UUID.randomUUID(); - if(files.size() >= 5) { + + if (files.size() >= 5) { + //remove last and add new one SavedFile file = files.poll(); - if(!get(file.getUuid()).delete()) { - System.out.println("failed"); + if (!get(file.getUuid()).delete()) { + System.out.println("failed to delete file..."); } path = Paths.get(getFileName(uuid)); files.offer(new SavedFile(uuid, name)); - }else { + + + } else { files.add(new SavedFile(uuid, name)); path = Paths.get(getFileName(uuid)); } return path; } + /** + * generates File from uuid. + * + * @param uuid uuid + * @return file + */ @Contract - @NotNull public static File get(@NotNull UUID uuid) { + @NotNull + public static File get(@NotNull UUID uuid) { return new File(getFileName(uuid)); } + /** + * generates file name from uuid. + * + * @param uuid uuid + * @return file name + */ @Contract(pure = true) @NotNull private static String getFileName(@NotNull UUID uuid) { diff --git a/src/main/java/de/hsel/spm/baudas/web/Files.java b/src/main/java/de/hsel/spm/baudas/web/Files.java index e586c08..defc588 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Files.java +++ b/src/main/java/de/hsel/spm/baudas/web/Files.java @@ -22,12 +22,21 @@ import java.nio.charset.StandardCharsets; @WebServlet("/files") public class Files extends HttpServlet { + private static final long serialVersionUID = 148451844848L; + + /** + * do get. + * @param req request + * @param resp response + * @throws IOException writer could not be retrieved + */ @Override - protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException { + protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) + throws IOException { resp.setContentType("application/json"); resp.setCharacterEncoding(StandardCharsets.UTF_8.name()); PrintWriter out = resp.getWriter(); Gson gson = new Gson(); - out.println(gson.toJson(Data.getFiles())); + out.print(gson.toJson(Data.getFiles())); } } \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/SavedFile.java b/src/main/java/de/hsel/spm/baudas/web/SavedFile.java index 3ed9901..a292925 100644 --- a/src/main/java/de/hsel/spm/baudas/web/SavedFile.java +++ b/src/main/java/de/hsel/spm/baudas/web/SavedFile.java @@ -2,11 +2,20 @@ package de.hsel.spm.baudas.web; import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.ToString; import java.util.UUID; +/** + * meta data about file. + * + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ @Getter @AllArgsConstructor +@ToString public class SavedFile { private UUID uuid; diff --git a/src/main/java/de/hsel/spm/baudas/web/Upload.java b/src/main/java/de/hsel/spm/baudas/web/Upload.java index 92c992b..f90f673 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Upload.java +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -29,8 +29,19 @@ import java.nio.file.StandardCopyOption; @MultipartConfig public class Upload extends HttpServlet { + private static final long serialVersionUID = 14144111845151L; + + /** + * doPost. + * + * @param req request + * @param resp response + * @throws ServletException could not get file part + * @throws IOException writer could not be retrieved + */ @Override - protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + protected void doPost(@NotNull HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { req.setCharacterEncoding(StandardCharsets.UTF_8.name()); Part filePart = req.getPart("file"); @@ -39,13 +50,12 @@ public class Upload extends HttpServlet { Path path = Data.add(fileName); - System.out.println(path); - if(!Files.exists(path)) { + if (!Files.exists(path)) { Files.createFile(path); } Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING); - resp.sendRedirect("files"); + resp.sendRedirect("index.jsf"); } } \ No newline at end of file