From 262def9aa5c91f4a850ac22ff2d4f8231bbbd950 Mon Sep 17 00:00:00 2001 From: Andrey Chervonyuk Date: Sun, 3 Jan 2016 19:35:10 +0200 Subject: [PATCH] Homework 9 --- src/com/geekhub/hw8/CreateFileServlet.java | 20 ++++++- src/com/geekhub/hw8/RemoveFileServlet.java | 45 ++++++++++++++- src/com/geekhub/hw8/ViewDirectoryServlet.java | 53 +++++++++++++---- src/com/geekhub/hw8/ViewFileServlet.java | 30 +++++++++- src/com/geekhub/hw8/file/FileInformation.java | 32 +++++++++++ src/com/geekhub/hw8/file/Type.java | 5 ++ web/WEB-INF/pages/fileContent.jsp | 16 ++++++ web/WEB-INF/pages/fileList.jsp | 57 +++++++++++++++++++ 8 files changed, 245 insertions(+), 13 deletions(-) create mode 100644 src/com/geekhub/hw8/file/FileInformation.java create mode 100644 src/com/geekhub/hw8/file/Type.java create mode 100644 web/WEB-INF/pages/fileContent.jsp create mode 100644 web/WEB-INF/pages/fileList.jsp diff --git a/src/com/geekhub/hw8/CreateFileServlet.java b/src/com/geekhub/hw8/CreateFileServlet.java index 940501f..6e3cd0d 100644 --- a/src/com/geekhub/hw8/CreateFileServlet.java +++ b/src/com/geekhub/hw8/CreateFileServlet.java @@ -6,12 +6,30 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; @WebServlet("/file/create") public class CreateFileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.doGet(req, resp); + String filePath = req.getParameter("path") + "\\" + req.getParameter("fileName"); + String type = req.getParameter("type"); + + try { + switch (type) { + case "file" : + Files.createFile(Paths.get(filePath)); + break; + case "directory" : + Files.createDirectories(Paths.get(filePath)); + break; + } + } catch (Exception e) { + req.setAttribute("message", "Error create new file " + e.getClass().getSimpleName()); + } + + req.getRequestDispatcher("/dir/view?path=" + req.getParameter("path")).forward(req, resp); } } diff --git a/src/com/geekhub/hw8/RemoveFileServlet.java b/src/com/geekhub/hw8/RemoveFileServlet.java index a6322e6..6991a89 100644 --- a/src/com/geekhub/hw8/RemoveFileServlet.java +++ b/src/com/geekhub/hw8/RemoveFileServlet.java @@ -6,12 +6,55 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; @WebServlet("/file/remove") public class RemoveFileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.doGet(req, resp); + Path path = null; + String message; + + try { + path = Paths.get(req.getParameter("path")); + if(Files.isDirectory(path)) { + deleteDirectory(path); + } else { + Files.delete(path); + } + message = path.toString() + " deleted"; + } catch (FileSystemException e) { + message = "Error delete file"; + } catch (NullPointerException e) { + message = "Error delete file"; + path = Paths.get("/"); + } + + req.setAttribute("message", message.replace("\\", "/")); + req.getRequestDispatcher("/dir/view?path=" + path.getParent()).forward(req, resp); + } + + private void deleteDirectory(Path path) throws IOException { + Files.walkFileTree(path, new SimpleFileVisitor() { + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + if(exc == null) { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } else { + throw exc; + } + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + }); + } } diff --git a/src/com/geekhub/hw8/ViewDirectoryServlet.java b/src/com/geekhub/hw8/ViewDirectoryServlet.java index d915e71..63072c8 100644 --- a/src/com/geekhub/hw8/ViewDirectoryServlet.java +++ b/src/com/geekhub/hw8/ViewDirectoryServlet.java @@ -1,5 +1,7 @@ package com.geekhub.hw8; +import com.geekhub.hw8.file.FileInformation; + import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; @@ -9,9 +11,9 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.*; +import java.util.ArrayList; +import java.util.List; @WebServlet(value = "/dir/view", initParams = { @WebInitParam(name = "root", value = "D:\\") @@ -27,16 +29,47 @@ public void init(ServletConfig config) throws ServletException { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - StringBuilder sb = new StringBuilder(); - sb.append(""); + Path currentPath; + req.setCharacterEncoding(StandardCharsets.UTF_8.name()); + String path = req.getParameter("path"); + + if (path == null || path.equals("null")) { + currentPath = ROOT_PATH; + } else { + currentPath = Paths.get(path); + } - //TODO: Implement directory listing here + if (Files.exists(currentPath)) { + req.setAttribute("fileList", createFileList(currentPath)); + req.setAttribute("currentPath", currentPath); + req.getRequestDispatcher("/WEB-INF/pages/fileList.jsp").forward(req, resp); + } else { + req.setAttribute("message", "File: " + currentPath.toString().replace("\\", "/") + " not found"); + req.getRequestDispatcher("/dir/view?path=" + getLastExistDirectory(currentPath)).forward(req, resp); + } + } - sb.append(""); - resp.getWriter().write(sb.toString()); + private List createFileList(Path path) { + List fileList = new ArrayList<>(); + try (DirectoryStream stream = Files.newDirectoryStream(path)) { + for (Path filePath : stream) { + fileList.add(new FileInformation(filePath)); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return fileList; } - private void appendLink(StringBuilder sb, String text, Path path) { - //TODO: Implement link rendering based on path type (directory or file) + private Path getLastExistDirectory(Path path) { + while(path != null) { + path = path.getParent(); + if (Files.exists(path) & Files.isDirectory(path)) { + break; + } + } + return path; } + + } diff --git a/src/com/geekhub/hw8/ViewFileServlet.java b/src/com/geekhub/hw8/ViewFileServlet.java index 263194b..385201c 100644 --- a/src/com/geekhub/hw8/ViewFileServlet.java +++ b/src/com/geekhub/hw8/ViewFileServlet.java @@ -5,14 +5,42 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; @WebServlet("/file/view") public class ViewFileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.doGet(req, resp); + Path path = null; + + try { + path = Paths.get(req.getParameter("path")); + req.setAttribute("backLink", path.getParent()); + req.setAttribute("content", readFile(path)); + req.getRequestDispatcher("/WEB-INF/pages/fileContent.jsp").forward(req, resp); + } catch (IOException e) { + req.setAttribute("message", "Error read file "); + req.getRequestDispatcher("/dir/view?path=" + path.getParent()).forward(req, resp); + } catch (NullPointerException e) { + req.setAttribute("message", "Not selected file"); + req.getRequestDispatcher("/dir/view").forward(req, resp); + } + } + + private static String readFile(Path path) throws IOException { + StringBuilder content = new StringBuilder(); + try(BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset())) { + String line; + while ((line = reader.readLine()) != null) { + content.append(line).append(System.lineSeparator()); + } + } + return content.toString(); } } diff --git a/src/com/geekhub/hw8/file/FileInformation.java b/src/com/geekhub/hw8/file/FileInformation.java new file mode 100644 index 0000000..3482fd6 --- /dev/null +++ b/src/com/geekhub/hw8/file/FileInformation.java @@ -0,0 +1,32 @@ +package com.geekhub.hw8.file; + +import java.nio.file.Files; +import java.nio.file.Path; + +public class FileInformation { + private String name; + private String path; + private Type type; + + public FileInformation(Path path) { + this.name = path.getFileName().toString(); + this.path = path.toString(); + if (Files.isDirectory(path)) { + this.type = Type.DIRECTORY; + } else { + this.type = Type.FILE; + } + } + + public String getName() { + return name; + } + + public String getPath() { + return path; + } + + public Type getType() { + return type; + } +} diff --git a/src/com/geekhub/hw8/file/Type.java b/src/com/geekhub/hw8/file/Type.java new file mode 100644 index 0000000..232abb6 --- /dev/null +++ b/src/com/geekhub/hw8/file/Type.java @@ -0,0 +1,5 @@ +package com.geekhub.hw8.file; + +public enum Type { + FILE, DIRECTORY +} diff --git a/web/WEB-INF/pages/fileContent.jsp b/web/WEB-INF/pages/fileContent.jsp new file mode 100644 index 0000000..9f1d3a0 --- /dev/null +++ b/web/WEB-INF/pages/fileContent.jsp @@ -0,0 +1,16 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + + +
+ + Back to: ${backLink} + + + diff --git a/web/WEB-INF/pages/fileList.jsp b/web/WEB-INF/pages/fileList.jsp new file mode 100644 index 0000000..cfffbb2 --- /dev/null +++ b/web/WEB-INF/pages/fileList.jsp @@ -0,0 +1,57 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + + + + +

Current path: ${currentPath}

+ + +

+ Back to: ${currentPath.parent} +

+
+ +
+ + +
+ +
+ +
+ +
+ + + + + + + + + + + + + + + +
${file.name} ${file.name} Delete
+ + + + + + +