From 20a3743ec219ab38337c8e8f99189cfa0b441cb8 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Sat, 25 May 2019 17:10:25 +0200 Subject: [PATCH] working version Signed-off-by: Johannes Theiner #SPM-26: add work #3h development --- .../spm/baudas/analysis/ShoppingCart.java | 10 +-- .../spm/baudas/web/AuthenticationFilter.java | 26 +++--- .../java/de/hsel/spm/baudas/web/Data.java | 79 +++++++++++-------- .../java/de/hsel/spm/baudas/web/Files.java | 2 +- .../de/hsel/spm/baudas/web/LoginServlet.java | 7 +- .../de/hsel/spm/baudas/web/LogoutServlet.java | 11 +-- .../spm/baudas/web/TopFlopArticleDiagram.java | 10 ++- .../java/de/hsel/spm/baudas/web/Upload.java | 2 +- src/main/webapp/header.jsp | 4 +- 9 files changed, 92 insertions(+), 59 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java index 3f01c76..7b858a4 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java @@ -27,7 +27,7 @@ public class ShoppingCart implements Analysis, List>> { @Override public Map, List> getResult() { - if(result != null) { + if (result != null) { return result; } @@ -67,19 +67,19 @@ public class ShoppingCart implements Analysis, List>> { e.printStackTrace(); } - for(int i = 0; i < model.getAssociationRules().getRules().size();i++){ + for (int i = 0; i < model.getAssociationRules().getRules().size(); i++) { AssociationRule rule = model.getAssociationRules().getRules().get(i); List l1 = new ArrayList<>(); - for(Item item : rule.getPremise()){ + for (Item item : rule.getPremise()) { l1.add(item.getAttribute().name()); } List l2 = new ArrayList<>(); - for(Item item : rule.getConsequence()){ + for (Item item : rule.getConsequence()) { l2.add(item.getAttribute().name()); } - result.put(l1,l2); + result.put(l1, l2); } diff --git a/src/main/java/de/hsel/spm/baudas/web/AuthenticationFilter.java b/src/main/java/de/hsel/spm/baudas/web/AuthenticationFilter.java index d262553..df5f74f 100644 --- a/src/main/java/de/hsel/spm/baudas/web/AuthenticationFilter.java +++ b/src/main/java/de/hsel/spm/baudas/web/AuthenticationFilter.java @@ -1,6 +1,12 @@ package de.hsel.spm.baudas.web; -import javax.servlet.*; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -14,27 +20,29 @@ import java.io.IOException; */ @WebFilter(urlPatterns = {"/*"}) -public class AuthenticationFilter implements Filter{ +public class AuthenticationFilter implements Filter { private ServletContext context; /** * Called by the web container to indicate to a filter that it is being placed into service. This filter manages the authentication. - * @param fConfig This parameter provides access to everything the code needs to work. + * + * @param filterConfig This parameter provides access to everything the code needs to work. * @throws ServletException Defines a general exception a servlet can throw when it encounters difficulty. */ - public void init(FilterConfig fConfig) throws ServletException { - this.context = fConfig.getServletContext(); + public void init(FilterConfig filterConfig) throws ServletException { + this.context = filterConfig.getServletContext(); this.context.log("AuthenticationFilter initialized"); } /** * The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. - * @param request This parameter provides access to everything the code needs to work. + * + * @param request This parameter provides access to everything the code needs to work. * @param response This parameter provides access to everything the code needs to issue a response. - * @param chain This parameter allows passing request along the chain of potential handlers until one of them handles the request. + * @param chain This parameter allows passing request along the chain of potential handlers until one of them handles the request. * @throws ServletException Defines a general exception a servlet can throw when it encounters difficulty. - * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. + * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { @@ -42,7 +50,7 @@ public class AuthenticationFilter implements Filter{ HttpServletResponse res = (HttpServletResponse) response; HttpSession session = req.getSession(false); String url = req.getRequestURI(); - if(url.contains("login") || url.contains("logo") || url.contains("js/")){ + if (url.contains("login") || url.contains("logo") || url.contains("js/")) { chain.doFilter(request, response); } else if (session == null || !((boolean) session.getAttribute("authentication"))) { //checking whether the session exists and if authentication succeed this.context.log("Unauthorized access request"); 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 6c8d159..88c3d32 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Data.java +++ b/src/main/java/de/hsel/spm/baudas/web/Data.java @@ -10,8 +10,12 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; import java.util.UUID; -import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; /** * manages data about .csv files. @@ -23,7 +27,7 @@ import java.util.concurrent.ConcurrentLinkedQueue; public class Data { @Getter - private static ConcurrentLinkedQueue files = new ConcurrentLinkedQueue<>(); + private static ConcurrentMap> files = new ConcurrentHashMap<>(); /** @@ -33,42 +37,55 @@ public class Data { * @return path to save file to */ @NotNull - static Path add(@NotNull String name) { - //cleanup old files - if (files.isEmpty()) { - try { - Files.list(Paths.get("")).forEach(path -> { - if (path.toFile().getName().endsWith(".csv")) { + static Path add(@NotNull String name, @NotNull String session) { + UUID uuid = UUID.randomUUID(); + + if (!files.containsKey(session)) + files.put(session, new ArrayList<>()); + + if (files.get(session).size() >= 5) { + SavedFile file = files.get(session).iterator().next(); + if (!get(file.getUuid()).delete()) { + System.err.println("failed to delete file..." + file); + } + files.get(session).remove(file); + } + files.get(session).add(new SavedFile(uuid, name, LocalDateTime.now())); + return Paths.get(getFileName(uuid)); + } + + /** + * delete all files for session. + * + * @param session session id + */ + static void delete(String session) { + for (SavedFile file : files.get(session)) { + if (!get(file.getUuid()).delete()) + System.err.println("failed to delete file..." + file); + } + files.remove(session); + + //deleting all files older than x days. + int days = 30; + + try { + Files.list(Paths.get("")).forEach(path -> { + if (path.toFile().getName().endsWith("*.csv")) { + long diff = new Date().getTime() - path.toFile().lastModified(); + + if (diff > (long) days * 24 * 60 * 60 * 1000) { try { Files.delete(path); } catch (IOException e) { e.printStackTrace(); } } - }); - } catch (IOException ex) { - ex.printStackTrace(); - } + } + }); + } catch (IOException ex) { + ex.printStackTrace(); } - - Path path; - UUID uuid = UUID.randomUUID(); - - if (files.size() >= 5) { - //remove last and add new one - SavedFile file = files.poll(); - if (!get(file.getUuid()).delete()) { - System.out.println("failed to delete file..."); - } - path = Paths.get(getFileName(uuid)); - files.offer(new SavedFile(uuid, name, LocalDateTime.now())); - - - } else { - files.add(new SavedFile(uuid, name, LocalDateTime.now())); - path = Paths.get(getFileName(uuid)); - } - return path; } /** 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 9688f07..362ef89 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Files.java +++ b/src/main/java/de/hsel/spm/baudas/web/Files.java @@ -31,6 +31,6 @@ public class Files extends HttpServlet { resp.setCharacterEncoding(StandardCharsets.UTF_8.name()); PrintWriter out = resp.getWriter(); Gson gson = new Gson(); - out.print(gson.toJson(Data.getFiles())); + out.print(gson.toJson(Data.getFiles().get(req.getSession().getId()))); } } \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java b/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java index ce33667..e6571e0 100644 --- a/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java +++ b/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java @@ -23,10 +23,11 @@ public class LoginServlet extends HttpServlet { /** * This is a Servlet that manages the Login and creates Sessions. - * @param request This parameter provides access to everything the code needs to work. + * + * @param request This parameter provides access to everything the code needs to work. * @param response This parameter provides access to everything the code needs to issue a response. * @throws ServletException Defines a general exception a servlet can throw when it encounters difficulty. - * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. + * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -34,7 +35,7 @@ public class LoginServlet extends HttpServlet { if (this.password.equals(password)) { HttpSession newSession = request.getSession(true); newSession.setAttribute("authentication", true); - newSession.setMaxInactiveInterval(5*60*60); //setting session to expiry in 5 hours + newSession.setMaxInactiveInterval(5 * 60 * 60); //setting session to expiry in 5 hours response.sendRedirect("index.jsp"); } else { RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html"); diff --git a/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java b/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java index 165f851..cf04180 100644 --- a/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java +++ b/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java @@ -1,12 +1,11 @@ package de.hsel.spm.baudas.web; -import java.io.IOException; - import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import java.io.IOException; /** * Servlet implementation class LogoutServlet. @@ -19,14 +18,16 @@ public class LogoutServlet extends HttpServlet { /** * This is a Servlet that manages the Logout and deletes Sessions. - * @param request This parameter provides access to everything the code needs to work. + * + * @param request This parameter provides access to everything the code needs to work. * @param response This parameter provides access to everything the code needs to issue a response. * @throws IOException Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { - HttpSession session = request.getSession(false); - if(session != null){ + HttpSession session = request.getSession(); + if (session != null) { + Data.delete(session.getId()); session.invalidate(); } response.sendRedirect(request.getContextPath() + "/"); diff --git a/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java b/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java index 2d6692f..0e0abe3 100644 --- a/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/TopFlopArticleDiagram.java @@ -12,9 +12,15 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; /** + * top/flop articles list diagram. + * * @author Johannes Theiner * @version 0.1 * @since 0.1 @@ -45,7 +51,7 @@ public class TopFlopArticleDiagram extends HttpServlet { List labels = new ArrayList<>(); List data = new ArrayList<>(); - for(Map.Entry entry : map.entrySet()) { + for (Map.Entry entry : map.entrySet()) { labels.add(entry.getKey()); data.add(entry.getValue().toString()); } 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 28f7c9d..2bb2046 100644 --- a/src/main/java/de/hsel/spm/baudas/web/Upload.java +++ b/src/main/java/de/hsel/spm/baudas/web/Upload.java @@ -42,7 +42,7 @@ public class Upload extends HttpServlet { InputStream inputStream = filePart.getInputStream(); - Path path = Data.add(fileName); + Path path = Data.add(fileName, req.getSession().getId()); if (!Files.exists(path)) { Files.createFile(path); } diff --git a/src/main/webapp/header.jsp b/src/main/webapp/header.jsp index 5cd9784..59fa480 100644 --- a/src/main/webapp/header.jsp +++ b/src/main/webapp/header.jsp @@ -38,12 +38,12 @@